#!/bin/sh
#
# Copyright 2003 Meru Networks
# All rights reserved.
#
# Meru configuration
#
# chkconfig: 45 90 10
# description: Meru sub-system
LC_ALL=C
export LC_ALL
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/meru/bin
export PATH

LOG=/opt/meru/var/log/meru_init.log

. /etc/init.d/meru-functions

start() {
        echo "meru:start" >> $LOG 
        echo ">> list of ifcfg-eth* files at /etc/sysconfig/network-scripts/" >>$LOG
        ls /etc/sysconfig/network-scripts/ifcfg-eth* >> $LOG
        echo ">> ifconfig output" >> $LOG
        ifconfig >> $LOG
        logger -p local7.info "Start WLAN Services ..."

	ifconfig | sed '/127.0.0.1/d' | grep 'inet addr:' >/dev/null 2>&1 || {
		logger -p local7.info -s "Could not identify an IP address." | tee -a $LOG
		logger -p local7.info -s "The Meru sub-system must have a valid IP address to start." | tee -a $LOG
		logger -p local7.info -s "No IP address? Could not start Meru services without an IP address."
		echo "Could not identify an IP address" >> $LOG
		echo "The Meru sub-system must have a valid IP address to start" >> $LOG
		echo "No IP address. Could not start Meru services without an IP address." >> $LOG
	}
	dmesg -n1

	# kcomm loaded and coordinator running assume system is already started.
	lsmod | grep kcomm >/dev/null 2>&1 && {
		pgrep coordinator >/dev/null 2>&1 && {
			logger -p local7.info -s "Subsystem is already up."
            logger -p local7.info "WLAN Services already started"
			exit 0
		}
	}

	# Delete any old state from prior system operation.
        # In case if this is a slave controller, don't remove the slave process pid
	if [ -f /opt/meru/var/run/np1slave.pid ] ; then
	    mv /opt/meru/var/run/np1slave.pid /tmp/ 
	    rm -rf /opt/meru/var/run/*
	    mv /tmp/np1slave.pid /opt/meru/var/run/
	else
	    rm -rf /opt/meru/var/run/*
	fi 

	rm -f /var/run/ipsec_setup.*
	rm -f /var/run/pluto.*
	rm -f /var/run/heartbeat.*

	touch /var/lock/subsys/meru

	# Setup ntp
	/opt/meru/etc/cli/ntpserver.pl -i

	# Turn on logrotate watchdog.
	echo "on" > /opt/meru/var/log/lrwd

	# move to inittab for INIT respawns monit 
	#/etc/init.d/monit start >/dev/null 2>&1
        
	#check if monit started then restart
	if vrfy monit; then
	    echo "monit is already running. No need to restart" >> $LOG
	else
	    # Start monit
	    /opt/meru/bin/monit -c /opt/meru/etc/monit.conf -l /opt/meru/var/log/monit.log -d 5 & > /dev/null 2 1
	fi
	
	# give it time to initialize
	sleep 2

	# Start CEFLOG
	/etc/init.d/ceflog start
	/etc/init.d/mdebugd start
	/etc/init.d/meru-wnc start
        logger -p local7.info "Controller Up on `date`"

        logger -p local7.info "WLAN Services started"
        echo "meru:start - finished" >> $LOG
}

stop() {
        logger -p local7.info "Stop WLAN Services ..."
        echo "meru:stop" >> $LOG 

        # Suspending monit before stopping our services
        #/etc/init.d/monit stop >/dev/null 2>&1
        # Kannan:2006-01-16:Do not attempt to suspend monit.
        #        Otherwise monit will not be able to stop the
        #        processes we are asking it to stop!
        # pkill -19 monit

	# cli sessions aren't started by this script but they do need to be
	# stopped by this script.
	pkill -x cli
	/etc/init.d/meru-wnc stop
	/etc/init.d/mdebugd stop
	/etc/init.d/apache stop
	/etc/init.d/ceflog stop

	rm -f $LOG
	# Make sure everything is down.
	sleep 2
	ps -ef | egrep /opt/meru/bin/[a-ln-tv-z] >/dev/null 2>&1 && {
		rmmod littledog >/dev/null 2>&1
		rmmod i810-tco >/dev/null 2>&1
		pkill -f '/opt/meru/bin/[a-ln-tv-z]' >/dev/null 2>&1
		pkill -f mdebugd >/dev/null 2>&1
		pkill -f /opt/meru/sip  >/dev/null 2>&1
		MMODS="meru_util meru_debug ip_tables kcomm"; export MMODS
		/etc/init.d/mmods stop >/dev/null 2>&1
	}

	rm -f /var/lock/subsys/meru
       logger -p local7.info "WLAN Services stopped"
       echo "meru:stop - finished" >> $LOG
}

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

exit 0
