#!/bin/sh

. /etc/init.d/functions.sh


# ---------------------------------------------------------------------------------------
kill_process() {
	if ps | grep $1 | grep -v grep >/dev/null ; then 
		if [ -z "$2" ] ; then
			information "$1 is alive. Will kill $1 ..."
		else
			information "$1 is alive. Will kill $2 $1 ..."
		fi
	fi 
	killall $2 $1 >/dev/null 2>&1
}


# ---------------------------------------------------------------------------------------
error_msg() {
	echo -e "${INFOPOS}${BAD}!!!${NORMAL} $*"
}


# ---------------------------------------------------------------------------------------
startme() {
	/usr/local/bin/watchdog > /dev/null 2>&1 &
	statusled flash 2 &
	/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
	usleep 50
	killall rdate >/dev/null 2>&1
	
	# Give it a little time to stop (up to 10 seconds)
	i=0
	LIMIT=40
	while [ "$i" -lt "$LIMIT" ] ; do
		i=$(($i+1))
		if ps | grep $main_app | grep -v grep >/dev/null ; then 
			killall rdate >/dev/null 2>&1
			usleep 250
		else
			i=$LIMIT
		fi
	done
	
	# specifically kill processes that can't close that quickly
	killall wget >/dev/null 2>&1
	killall rdate >/dev/null 2>&1
	killall clockos >/dev/null 2>&1

	killall -9 wget >/dev/null 2>&1
	
	# Give it a little time to stop (up to 3 seconds)
	i=0
	LIMIT=12
	while [ "$i" -lt "$LIMIT" ] ; do
		i=$(($i+1))
		if ps | grep -e watchdog -e rdate -e clockos -e pcserver -e replicate  -e bundy -e processor | grep -v grep >/dev/null ; then 
			killall rdate >/dev/null 2>&1
			usleep 250
		else
			i=$LIMIT
		fi
	done
	
	killall wget >/dev/null 2>&1
	# Check if there are any clock processes still running
	#if ps | grep -e watchdog -e rdate -e clockos -e pcserver -e replicate -e finger -e bundy -e processor | grep -v grep >/dev/null ; then 
	if ps | grep -e watchdog -e rdate -e clockos -e pcserver -e replicate -e finger  -e bundy -e processor | grep -v grep ; then 
		information "Some Clock processes are still alive..."
		usleep 250
		kill_process watchdog
		kill_process rdate
		kill_process clockos
		kill_process pcserver
		kill_process replicate
		kill_process processor
		kill_process bundy
		kill_process finger
		usleep 250
		if ps | grep -e watchdog -e rdate -e clockos -e pcserver -e replicate -e finger kill_process replicate | grep -v grep >/dev/null ; then 
			information "Some Clock processes are still alive... Will try harder to kill them"
			usleep 250
			kill_process watchdog -9
			kill_process rdate -9
			kill_process clockos -9
			kill_process pcserver -9
			kill_process replicate -9
			kill_process processor -9
			kill_process bundy -9
			kill_process finger -9
			sleep 1
			if ps | grep -e watchdog -e rdate -e clockos -e pcserver -e replicate -e finger -e bundy -e processor | grep -v grep >/dev/null ; then 
				ps
				error_msg "Some Clock processes are still alive."
				end 1
			else
				information "All Clock processes are closed...."
			fi
		else
			information "All Clock processes are closed..."
		fi
	fi
	end 0
}


# ---------------------------------------------------------------------------------------


# ---------------------------------------------------------------------------------------
main_app="watchdog"

case "$1" in
	start)
		begin "Starting Clock Applications"
		startme
		end 0
		;;
	stop)
		begin "Stopping Clock Applications"
		stopme
		end 0
		;;
	restart)
		begin "Restarting Clock Applications"
		stopme
		startme
		end 0
		;;
	*)
		error "Usage: $0 start|stop|restart"
		;;
esac
# ---------------------------------------------------------------------------------------
