#!/bin/sh
### BEGIN INIT INFO
# Provides:						clock
# Required-Start:			$local_fs $network $named $time muxcontrol pppcontrol detectbigflash
# Required-Stop:			$local_fs $network $named muxcontrol
# Default-Start:			2 3 4 5
# Default-Stop:				0 1 6
# Short-Description:	Start and stop the clock
### END INIT INFO

. /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
	RESDEF=/etc/resolv.conf.def
	if [ ! -e $RESDEF ]
		then
		cp /etc/resolv.conf $RESDEF
	fi

	ALT=$(pidof /usr/local/bin/db_alter)
	while [ -n "$ALT" ]
	do
		sleep 1
		echo "$ALT"
		ALT=$(pidof /usr/local/bin/db_alter)
	done

	/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"
		/usr/local/bin/csapi.sh 151 clock &
		end 1
	fi
	NOMODEM=$(/usr/local/bin/sqlite3shell /var/local/data/capeclock.db 'select modem from thisdev where id = 1;')
	if [ "$NOMODEM" -ne 2 ]
		then
#		/usr/local/bin/sendchild -t 25000 "Starting Clock" "Getting GSM info" "if available" ""
		/usr/local/bin/gsm > /dev/null 2>&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 csrdate >/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
	killall -9 csrdate >/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 mcr -e processor -e sms | grep -v grep ; then
			kill_process watchdog
			kill_process clockos
			kill_process pcserver
			kill_process replicate
			kill_process mcr
			kill_process processor
			kill_process sms
			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)
		RUNNING=$(ps -ef | grep watchdog | grep -v grep | wc -l)
		if [ $RUNNING -gt 0 ]
			then
			echo "Already running"
		else
			begin "Starting Clock Applications"
#TIMECLOCK 			/usr/local/bin/sendchild -t 5000 "Starting Clock" "Starting Readers" "" ""
#JOBCLOCK 			/usr/local/bin/sendchild -t 5000 -l 16 "Starting Clock" "Please Wait" "" ""
			/usr/local/bin/sendchild -t 5000 "Starting Clock" "Starting Readers" "" ""
			startme
			/usr/local/bin/csapi.sh 152 clock &
			end 0
		fi
		;;
	stop)
		begin "Stopping Clock Applications Initiated"
		stopme
		echo "Clock Stop complete"
		end 0
		;;
	reboot)
		begin "Reboot Initiated"
		stopme
#TIMECLOCK 		/usr/local/bin/sendchild "Rebooting Now" "Please be Patient" "" ""
#JOBCLOCK 		/usr/local/bin/sendchild -l 16 "Rebooting Now" "Please Wait" "" ""
		/usr/local/bin/sendchild "Rebooting Now" "Please be Patient" "" ""
		rm /etc/udev/rules.d/70-persistent-net.rules
		/usr/local/bin/csapi.sh 100 clock-reboot &
		rebootme
		reboot
		end 0
		;;
	restart)
		begin "Restarting Clock Applications"
		stopme
#TIMECLOCK 		/usr/local/bin/sendchild "Restarting Now" "Please be Patient" "" ""
#JOBCLOCK 		/usr/local/bin/sendchild -l 16 "Restarting Now" "Please Wait" "" ""
		/usr/local/bin/sendchild "Restarting Now" "Please be Patient" "" ""
		startme
		end 0
		;;
	shutdown)
		begin "Shutdown Clock"
		stopme
#TIMECLOCK 		/usr/local/bin/sendchild -s "Clock Programmes" "Stopped" "" ""
#JOBCLOCK 		/usr/local/bin/sendchild -l 16 "Clock Programmes" "Stopped" "" ""
		/usr/local/bin/sendchild -s "Clock Programmes" "Stopped" "" ""
		rm /etc/udev/rules.d/70-persistent-net.rules
		/usr/local/bin/csapi.sh 120 clock-shutdown &
		sleep 1
		shutdown -h now
		end 0
		;;
	*)
		error "Usage: $0 start|stop|restart|shutdown|reboot"
		;;
esac
exit 0

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