#!/bin/sh
PATH=$PATH:/home/arvint/nvram/
source network.sh
source service.sh

sigusr1(){
event=`ram_get what_event`
process=`ram_get which_process`
waitus=`ram_get need_wait`

    case "$event" in
    	START)
		state='START'
	;;
	STOP)
	        state='STOP'
	;;	
    	IDLE)
	        state='IDLE'
	;;
    	RESTART)
		state='RESTART'
    	;;
	MODECHANGE)
		state='MODECHANGE'
	;;

	*)
		echo "In valid value in what_event"
	;;	
    esac
}

sigusr2(){
state='START'
}

SIGHUP=1
SIGINT=2
SIGKILL=9
SIGUSR1=10
SIGUSR2=12
loop='yes'
state='START'

trap 'echo "Hit SIGUSR1";sigusr1' SIGUSR1
trap 'echo "Hit SIGUSR2";sigusr2' SIGUSR2

while [ "$loop" = 'yes' ]; do
    case "$state" in
    	START)
	    	start_lan
		start_lan_app
		start_wan  
		start_other_app 			
		state='IDLE'
		if [ "$waitus" = 'yes' -a "$process" != "" ];then
    		    killall -$SIGUSR1 "$process"
		fi    
	;;
	STOP)
	        stop_lan_app
		stop_wan_app
		stop_wan
	        stop_lan
	        state='IDLE'
		if [ "$waitus" = 'yes' -a "$process" != "" ];then
    		    killall -$SIGUSR1 "$process"
		fi    
	;;	
    	IDLE)
	    	sleep 1
		if [ "$waitus" = 'yes' -a "$process" != "" ];then
    		    killall -$SIGUSR1 "$process"
		fi    
	;;
    	RESTART)
		stop_wan_app
		stop_lan_app
		stop_wan
		stop_lan
		start_lan
		start_lan_app
		start_wan
		state='IDLE'
		if [ "$waitus" = 'yes' -a "$process" != "" ];then
    		    killall -$SIGUSR1 "$process"
		fi    
    	;;
	MODECHANGE)
		stop_wan_app
		stop_wan
		start_wan
		start_other_app
		state='IDLE'
		if [ "$waitus" = 'yes' -a "$process" != "" ];then
    		    killall -$SIGUSR1 "$process"
		fi    
	;;	
    esac
done
