#!/bin/sh
. /etc/init.d/functions.sh
# ---------------------------------------------------------------------------------------
kill_process() {
	if ps | grep $1 | grep -v grep >/dev/null ; then 
		if [ -z "$2" ] ; then
			echo "$1 is alive. Will kill $1 ..."
			killall $1 
		else
			echo "$1 is alive. Will kill $2 $1 ..."
			killall $2 $1 >/dev/null 2>&1
		fi
	fi 

}
# ---------------------------------------------------------------------------------------
check_resolv(){
	result=`wc -l /etc/resolv.conf | awk -F" " '{ print $1 }'`
	echo $result
	while [ $result -le 7 ]
	do
		echo "" >> /etc/resolv.conf
		result=$(( $result + 1 ))
	done
	result=`wc -l /etc/resolv.conf | awk -F" " '{ print $1 }'`
	echo $result
}
# ---------------------------------------------------------------------------------------
error_msg() {
	echo -e "${INFOPOS}${BAD}!!!${NORMAL} $*"
}
# ---------------------------------------------------------------------------------------
startme() {
	check_resolv
  mkdir /mnt > /dev/null 2>&1
	mkdir /mnt/1 > /dev/null 2>&1
	mkdir /mnt/3 > /dev/null 2>&1
	mkdir /var > /dev/null 2>&1
	mkdir /var/local > /dev/null 2>&1
	mkdir /var/local/updates > /dev/null 2>&1
  /usr/local/bin/iptodb.sh > /dev/null 2>&1
	/usr/local/bin/watchdog > /dev/null 2>&1 &
	if ! ps | grep $main_app | grep -v grep >/dev/null ; then 
		error_msg "$main_app - Could not be started"
		end 1
	fi
}
# ---------------------------------------------------------------------------------------
stopme() {
	# Start by killing watchdog
	killall $main_app >/dev/null 2>&1
	#killall statusled >/dev/null 2>&1
	if [ "$update_memstick" != "1" ]; then
		killall -9  updateclock >/dev/null 2>&1
		killall -9  clock_update.sh >/dev/null 2>&1
	fi
	killall rdate >/dev/null 2>&1
	killall wget >/dev/null 2>&1
	
	# Give it some time to stop (up to 60 seconds)
	i=0
	LIMIT=60
	while [ "$i" -lt "$LIMIT" ] ; do
		i=$(($i+1))
		if ps | grep $main_app | grep -v grep >/dev/null ; then 
			killall $main_app >/dev/null 2>&1
			sleep 1
		else
			i=$LIMIT
		fi
	done
	echo "Watchdog has stopped"
	# specifically kill processes that are not managed by watchdog)
	killall -9 wget >/dev/null 2>&1
	killall -9 rdate >/dev/null 2>&1
	sleep 5
	# clockos is special becuase it may have _started_ the script that is now running, eg usb-restore.
	killall -9 clockos >/dev/null 2>&1
	# Check if there are any clock processes still running
	# Give it some time to stop (up to 60 seconds)
	i=0
	LIMIT=60
	while [ "$i" -lt "$LIMIT" ] ; do
		i=$(($i+1))
		if ps | grep -e watchdog -e clockos -e pcserver -e replicate -e host -e processor | grep -v grep ; then 
			kill_process watchdog
			kill_process clockos
			kill_process pcserver
			kill_process replicate
			kill_process processor
			kill_process host
			sleep 1
		else
			i=$LIMIT
			echo "All processes stopped"
		fi
	done	
}	
# ---------------------------------------------------------------------------------------
rebootme() {
# now reboot device	 
sync
sleep 2
sync
sleep 2
echo reboot preparation sequence complete - will reboot now
sleep 2
}
# ---------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------
main_app="watchdog"

case "$1" in
	start)
		begin "Starting Clock Applications"
		/usr/local/bin/sendchild -t 5000 "Starting Clock" "Starting Readers" "" ""
		startme
		end 0
		;;
	stop)
		begin "Stopping Clock Applications Initiated"
		stopme
		echo "Clock Stop complete"
		end 0
		;;
	reboot)
		begin "Reboot Initiated"
		shutdownme
		rebootme
		/usr/local/bin/sendchild "Rebooting now" "Please be Patient" "" ""
		reboot		
		end 0
		;;		
	restart)
		begin "Restarting Clock Applications"
		stopme
		startme
		end 0
		;;
	shutdown)	
		begin "Shutdown Clock"
		stopme	
		/usr/local/bin/sendchild "Clock Programmes" "Stopped" "" ""		
		shutdown -h now
		end 0
		;;		
	*)
		error "Usage: $0 start|stop|restart|shutdown|reboot"
		;;
esac
exit 0
	
# ---------------------------------------------------------------------------------------
