#!/bin/sh
#
#

# PROVIDE: netams4
# REQUIRE: SERVERS
# BEFORE: DAEMON
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable n4agent:
# n4agent_enable (bool):        Set to "NO" by default.
#                       Set it to "YES" to enable n4agent.
# n4agent_flags (string):       Set to "-p" by default.
#                       Extra flags passed to start command.
#
#

. /etc/rc.subr

name="netams4"
rcvar=`set_rcvar netams4`

# read settings, set default values
load_rc_config ${name}

: ${netams4_enable="NO"}

command="/usr/local/netams4/startup.sh"
required_dirs=/usr/local/netams4
required_files=/usr/local/netams4/netams4.properties

do_start() {
       if ! checkyesno ${rcvar}; then
           echo "Cannot start $name. Set ${rcvar} to YES in /etc/rc.conf"
           return 0
        fi

        getrun=`ps ax | grep java | grep netams | wc -l`
        if [ "|$getrun|" != "|       0|" ]; then
           echo "$name seems to be already running."
           return 0;
        fi

        echo "Starting NETAMS4"
        exec $command &
}

do_stop() {
        echo "Stopping NETAMS4"
        ps ax | grep java | grep netams | awk '{ system("kill " $1) }'
}

do_restart() {
    do_stop
    sleep 1
    do_start
}

case "$1" in
faststart)      do_start   ;;
start)          do_start   ;;
faststop)       do_stop    ;;
stop)           do_stop    ;;
restart)        do_restart ;;
*)
    echo "Usage: `basename $0` {start|stop|restart}" >&2
    exit 64
    ;;
esac

exit 0
