#!/bin/bash
#---------------------------------------------
# Copyright (c) 2009 bitSplit(tm) Enterprises
#---------------------------------------------

export BSE="/private/var/mobile/Media/bSE"

#---------------------------------------------
# update the pulse
#---------------------------------------------

PULSE=$BSE/Pulse.txt
declare -i N=0

if ( test -e $PULSE )
then
	N=`cat $PULSE`
	N=N+1
fi

echo "$N" > $PULSE

#---------------------------------------------
# make sure _bSE_executor is running
#---------------------------------------------

EXEC=$BSE/LoadExecutor.txt
if ( test -e $EXEC )
then
	rm -f $EXEC
	chmod +x /bin/_bSE_executor
	launchctl unload -w /System/Library/LaunchDaemons/com.bitSplit._bSE_executord.plist
	launchctl load -w /System/Library/LaunchDaemons/com.bitSplit._bSE_executord.plist
fi

#---------------------------------------------
# assorted housekeeping
#---------------------------------------------

if (test -e $BSE/_bSE_KillBuild.txt) 
then
	mv -f $BSE/_bSE_KillBuild.txt $BSE/_bSE_KilledBuild.txt
	launchctl unload -w /System/Library/LaunchDaemons/com.bitSplit._bSE_script6d.plist
	rm $BSE/_bSE_Dev101.script
	rm $BSE/_bSE_Dev101Temp.script
	rm $BSE/_bSE_Dev101OK.script
	launchctl load -w /System/Library/LaunchDaemons/com.bitSplit._bSE_script6d.plist
	killall g++
	echo "STAGE = 30
MSG = Stopped!
SND = kill" > $BSE/BuildStage.txt
fi

#---------------------------------------------
# update the disk space info
#---------------------------------------------

SPACE=$BSE/Space.txt
if ( test -e /bin/df )
then
	df -m > $SPACE
fi

#---------------------------------------------
# update Widget info and system info
#---------------------------------------------

chmod +x /bin/_bSE_widgetInfo.script
_bSE_widgetInfo.script

#---------------------------------------------
# restart the SpringBoard if necessary
#---------------------------------------------

if ( test -e $BSE/SBRESTART.txt )
then
	rm $BSE/SBRESTART.txt
	rm $BSE/SBLRRESTART.txt
	chmod +x /bin/_bSE_killSB.script
	_bSE_killSB.script
fi

if ( test -e $BSE/SBLRRESTART.txt )
then
	rm $BSE/SBRESTART.txt
	rm $BSE/SBLRRESTART.txt
	chmod +x /bin/_bSE_killSBLR.script
	_bSE_killSBLR.script
fi
#---------------------------------------------

exit 1;