#!/bin/sh
#
# Copyright 2003 Meru Networks
# All rights reserved.
#

NP1ADV=np1adv
NP1ADV_PROC=/opt/meru/bin/${NP1ADV}
PROC=${NP1ADV_PROC}

START_DELAY=.2

MNAME=${NP1ADV}
PIDFILE=/opt/wlan/var/run/${MNAME}.pid
LOG=/opt/wlan/var/log/${MNAME}.log

if [ -f /etc/init.d/meru-functions ]; then
	. /etc/init.d/meru-functions
else
	echo "/etc/init.d/meru-functions must exist to use $0"; exit 1
fi

start() {

    # Check if np1main service is running
    if [ ! "$(/sbin/chkconfig --list | /bin/grep np1main)" ]; then
        echo "NP1 service is not set"
        return

    else

        vrfy_combo `cat $PIDFILE 2>/dev/null` && {
            return
        }

        echo "============ $0 start =================" >> $LOG
        cd /opt/wlan/etc

        ${NP1ADV_PROC} > /dev/null 2>&1 < /dev/null >> $LOG 2>&1 &
        sleep $START_DELAY
        writePO_pid
        vrfy `cat $PIDFILE 2>/dev/null` || warning "$NP1ADV did not start. Will try again."
    fi

}

stop() {

    monit_stop_process
}

case "$1" in
	start)
		if vrfy monit; then
			start_with_monit $MNAME
		else
			start
		fi
		;;
	stop)
		if vrfy monit; then
                        stop_with_monit $MNAME
		else
			stop
		fi
		;;
	restart)
		if vrfy monit; then
                        restart_with_monit $MNAME
		else
			stop
			start
		fi
		;;
	mstart)
		start
		;;
	mstop)
		stop
		;;
	*)
		echo $"Usage: $prog {start|stop|restart}"
		exit 1
esac

exit 0
