#!/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 

}
# ---------------------------------------------------------------------------------------
error_msg() {
	echo -e "${INFOPOS}${BAD}!!!${NORMAL} $*"
}
# ---------------------------------------------------------------------------------------
startme() {
	/usr/local/bin/watchdog > /dev/null 2>&1 &
	#statusled flash 2 & # not compatible with debian
	#/usr/local/bin/incboot > /dev/null 2>&1 &
	#if ! ps | grep incboot | grep -v grep >/dev/null ; then 
	#	error_msg "incboot - Could not be started"
	#	end 1
	#fi
	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() {
  stopme	
# unmount internal usb drive
	i=0
	LIMIT=60
	while [ "$i" -lt "$LIMIT" ] ; do
		i=$(($i+1))
		if mount | grep -i "mnt/3" ; then 
		  echo unmounting /mnt/3
			sync
			umount /mnt/3
			sleep 1
		else
			i=$LIMIT
		fi
	done	

# now reboot device	 
sync
sleep 2
sync
sleep 2
echo reboot preparation sequence complete - will reboot now
sleep 2
reboot
}
# ---------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------
main_app="watchdog"

case "$1" in
	start)
		begin "Starting Clock Applications"
		startme
		end 0
		;;
	stop)
		begin "Stopping Clock Applications Initiated"
		stopme
		echo "Clock Stop complete"
		end 0
		;;
	reboot)
		begin "Reboot Initiated"
		rebootme
		echo "Reboot complete"
		end 0
		;;		
	restart)
		begin "Restarting Clock Applications"
		stopme
		startme
		end 0
		;;
	*)
		error "Usage: $0 start|stop|restart"
		;;
esac
exit 0
	
# ---------------------------------------------------------------------------------------
