#!/bin/sh
#
# Copyright 2003 Meru Networks
# All rights reserved.
#
PROC=/opt/meru/bin/wncagent

#option for the gigabase page pool size(2560 * 8192 = 20 MB)
POOLSIZE="-poolsize 2560"

#File to enable Debug option in WncAgent
WNCAGENT_DEBUG_FILE=/opt/meru/var/run/wncagent.debug

START_DELAY=.2
STOP_DELAY=10
OEM=meru

MNAME=`basename $PROC`
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() {

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

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

    # In HA failover(slave->master) "wncagent mstart" was hanging and beacuse of that 
    # even if monit starts another instance of "wncagent mstart" in the following condition it 
    # exits so commenting the code.

    #ps -elf |grep "/etc/init.d/wncagent mstart" | grep -v grep >/tmp/out.$$
    #COUNT=`cat /tmp/out.$$ | wc -l`
    #rm -f /tmp/out.$$
    #if [ $COUNT -gt 1 ]; then
    #    echo "mstart is already running..exiting" >> $LOG
    #    exit 1
    #fi

    # vrfy_running_db
    
    if [ -f $WNCAGENT_DEBUG_FILE ]; then
        #Start WncAgent with Debug enabled
        $PROC $DB_DIR -d $POOLSIZE >> $LOG 2>&1 &
    else
        $PROC $DB_DIR $POOLSIZE >> $LOG 2>&1 &
    fi

    sleep $START_DELAY
    write_pid
    vrfy `cat $PIDFILE 2>/dev/null` || warning "$PROC did not start. Will try again."
	
	[ "echo licensing-management" = "true" ] &&
	{
		sleep 1
		echo "no licensing-management"; /opt/meru/bin/licdisable
	}&

	echo "meru" >	/opt/meru/var/run/brand
}

stop() {

	monit_stop_process $STOP_DELAY
}

case "$1" in
	start)
		set_params
		if vrfy monit; then
			start_with_monit $MNAME
		else
			start
		fi
		;;
	stop)
                [ -f $WNCAGENT_DEBUG_FILE ] && rm -f $WNCAGENT_DEBUG_FILE
		if vrfy monit; then
                        stop_with_monit $MNAME
		else
			stop
		fi
		;;
	restart)
		# check if nplus1 runs with passive slave role
		check_np1_role

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

exit 0
