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

START_DELAY=.2

MNAME=`basename $PROC`
PIDFILE=/opt/wlan/var/run/${MNAME}.pid
LOG=/opt/wlan/var/log/${MNAME}.log

#SNMP_START_OPTION=0
SNMP_CONF_FILE=/opt/meru/etc/snmp.conf  

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

status() {

    vrfy `cat $PIDFILE 2>/dev/null` && {
        return 0
    }
    return 1
}

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

#   if [ $SNMP_START_OPTION -eq 0 ]; then
#	return;
#    fi

    # if snmp conf file does not exist, then do not start snmp process (default behaviour)        
    if [ ! -f $SNMP_CONF_FILE ]; then                                                             
         # create snmp conf file with default config
         echo "SNMP_START_OPTION=0" > $SNMP_CONF_FILE
    fi                                                                                            
                                                                                                  
    # read SNMP_START_OPTION value                                                                
    line=`cat $SNMP_CONF_FILE | sed -n 1,1p`                                                      
    snmp_option=`echo $line | cut -d= -f2`                                                            
                                                                                                  
    # if SNMP start option is 0 then do not start snmp                                            
    if [ $snmp_option -eq 0 ]; then                                                                   
         return;                                                                                  
    fi

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

    $PROC >> $LOG 2>&1 &
    sleep $START_DELAY
    write_pid
    vrfy `cat $PIDFILE 2>/dev/null` || warning "$PROC did not start. Will try again."
}

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
		;;
	status)
		status
		ret=$?
		if [ $ret -eq 0 ]; then
		echo "running"
		else
		echo "stopped"
		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)
                # if snmp conf file does not exist, then do not start snmp process (default behaviour)        
                if [ ! -f $SNMP_CONF_FILE ]; then                                                             
                     stop_with_monit $MNAME
                     exit;
                fi                                                                                            
                                                                                                  
                # read SNMP_START_OPTION value                                                                
                line=`cat $SNMP_CONF_FILE | sed -n 1,1p`                                                      
                snmp_option=`echo $line | cut -d= -f2`                                                            
                                                                                                  
                # if SNMP start option is 0 then do not start snmp                                            
                if [ $snmp_option -eq 0 ]; then                                                                   
                     stop_with_monit $MNAME
                     exit;
                fi

		start
		;;
	mstop)
		stop
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|status}"
		exit 1
esac

exit 0
