#!/bin/dash
# Copyright (C) James Budiono, 2012
# License: GNU GPL Version 3 or later
#
APPTITLE="Rox Clock"
FLAG_DIR=/tmp/rox-clock
FLAG=$(readlink -f $0 | sed 's_/_-_g; s/^-//; s/-AppRun$//').pid
APPDIR=${0%/*}

clock_is_up() {
	test -e $FLAG_DIR/$FLAG && 
	kill -0 $(cat $FLAG_DIR/$FLAG) 2>/dev/null # SFR suggested this
}

start_clock() {
	mkdir -p $FLAG_DIR 2> /dev/null
	$APPDIR/clock.sh &
	echo $! > $FLAG_DIR/$FLAG
}

stop_clock() {
	kill $(cat $FLAG_DIR/$FLAG)
	rm -f $FLAG_DIR/$FLAG 
}

### main ###
case $1 in
	start|"") if ! clock_is_up; then # default is to start clock
				start_clock
			fi
			;;
			
	stop)	stop_clock
			;;
			
	status) if clock_is_up; then
				Xdialog --title "$APPTITLE" --infobox "Clock is running" 0 0 10000
			else
				Xdialog --title "$APPTITLE" --infobox "Clock is stopped" 0 0 10000
			fi 
			;;
esac
