#!/bin/sh
#
#  Created by duus on 2007-03-27.
# 
#####
# Defaults
_SecsToOff=3000
_ipod=/Volumes/duus\ ipod
_evereport=~/Documents/GTD/evereports/evereport.txt
_eveascript=~/Documents/Software/unixscripts/eve/ascript/
_evefolder=~/Documents/Software/unixscripts/eve/
_normscript=eve_applescript.app
_shutdownscript=eve_shutdown_applescript.app
_nosleepscript=eve_nosleep_applescript.app
#
#
#### you may now proceed with everything else
#
_SCRIPT_NAME=eve
_SCRIPT_LOCATION=~/bin/
if [ "$1" = "version" -o "$1" = "help" ]
	then
	echo "$_SCRIPT_NAME by duus"
	echo " the evening script...backs up ipod, docs folder, runs cleanup scripts, stuff like that. "
	echo "  initialcommands are eve, eve shutdown, and eve nosleep, or eve oneterm."
	echo "  secondary (internal) commands can be accessed through the source"
	exit 1;
fi
#
# assign argument
_arg=${1}
#
#################################################################################################### eve one terminal mode
if [ "${_arg}" = "one" -o "${_arg}" = "oneterm" ]
	then
	if [ "$2" = "" -o "$2" = "nosleep" -o "$2" = "shutdown" -o "$2" = "sleep "]
		then
		echo "you have chosen to run eve in one terminal window."
		echo "  I haven't really tested oneterm very much."
		eve noscript
		sudo eve logins
		shift
		eve main $*
		else
		echo "error, eve oneterm $2 not understood.  ($*)"
		eve help
	fi
	exit 1;
#################################################################################################### eve main
elif [ "${_arg}" = "m" -o "${_arg}" = "main" ]
	then
	#standard eve running goes here.
	bong 3 &
	whenlast s eve
	#
	#################### kgtd
	echo ". syncing kgtd..."
	runlater s 30 openhide OmniOutliner\ Professional &
	runlater s 150 synckgtd &
	#
	################### podcasts
	echo ". updating podcasts..."
	itunes pod &
	#
	################### quit extra applications
	echo ". quiet itunes and boodler in 2 minutes..."
	runlater 120 boodler kill &
	runlater 120 itunes stop &
	#
	################### quit extra applications
	echo ". quitting firefox and Preview..."
	quit Firefox &
	quit Preview &
	# openhide Skype &
	#
	################### cleaning temp files
	echo ". cleaning temp files...."
	trash ~/tmp/*
	#
	#
	#################### ipod stuff
	eve ipod
	#
	################### take a picture
	_olddir=`pwd`
	cd ${_evefolder}
	archthis evephoto.jpg 
	rm -v evephoto.jpg 
	runlater s 5 echo "!!!!!!!!!!!!!!!!!!!!!!!!      say cheese!         !!!!!!!!!!!!!!!!!!!!!!" &
	runlater count 5 1 cam file evephoto.jpg &
	runlater s 150 open evephoto.jpg &
	cd ${_olddir}
	#
	#
	if [ "$2" != "nosleep" ]
		then
	#################### turn off  airport
	_airportoffsecs=`expr ${_SecsToOff} - 137`
	echo "turning airport off in ${_airportoffsecs} seconds"
	runlater s ${_airportoffsecs} echo "turning airport off...." &
	runlater s ${_airportoffsecs} airport off &
	####################
	fi  #end "if nosleep"
	#
	#################### shutdown or sleep
	#
	_mins=`expr ${_SecsToOff} / 60`
	if [ "$2" = "shutdown" ]
		then
		echo ". "
		echo ". now i'll shutdown in ${_mins} minutes."
		echo ". "
		sudo runlater s 10 runlater count ${_SecsToOff} 60 shutdown -h now &
		echo ". "
	elif [ "$2" = "nosleep" ]
		then
		echo "."
		echo "(nosleep)"
	elif [ "$2" = "" -o "$2" = "sleep" -o "$2" = "default" ] 
		then
		echo ". "
		echo ". now i'll go to sleep in ${_mins} minutes."
		echo ". "
		runlater s 10 runlater count ${_SecsToOff} 60 sleepmachine &
		echo ". "
	else
		echo "eve $* not understood.  You will now die."
		runlater s 5 thunder &
		exit 1;
	fi
	#####################
	#
	#####################  FINISH HIM! (aka "generate logfile")
	echo " "
	echo " _evereport generated_ "
	echo "    (file loc: ${_evereport})"
	diagnostic >> ${_evereport}
	echo $__proceed >>  ${_evereport}
	tail ${_evereport}
	#echo "... "
	echo ".. "
	echo ". "
	echo ". eve complete "
	echo ". eve timestamp: `date`"
	_delay=`expr ${_SecsToOff} / 2`
	runlater s ${_delay} whenlast w eve & # delayed just for kicks, really.
	#####################
#################################################################################################### eve ipod (internal)
elif [ "${_arg}" = "ipod" ]  
	then
	#
	#################### ipod stuff, second pass. ()
	# sync ipod
	# sync kgtd2go
	# run ipodbkp
	if [ ! -d "${_ipod}" ]
		then
		echo ".  please connect your ipod if you would like it to be updated."
		echo ". press return to proceed or (c) to check..."
		read -e __proceed ##### so that password can be entered before anything else happens.
		if [ "${__proceed}" = "c" ]
			then 
				while [ "${__proceed}" = "c" ]
				do
					if [ ! -d "${_ipod}" ]
						then
						echo "the ipod has not been connected.  see ls of /Volumes for evidence:"
						ls /Volumes/
						echo "press (c) to check again, return to proceed."
						read -e __proceed ##### so that password can be entered before anything else happens.
						else
						echo "the ipod has been FOUND!  see ls of /Volumes for evidence:"
						ls /Volumes/
						echo "therefore I will enter an affirmative statement into the proceed entry for you:"
						echo " .. "
						echo " done."
						echo " ."
						echo " ."
						__proceed="yes, please proceed, `getname`"
					fi
				done
				echo "proceed now equals <${__proceed}>.  Therefore, I am continuing.  Thank you for your patience."
				sleep 2
				echo ". "
				sleep 1
				echo ". "
			fi
	fi
	#
	if [ -d "${_ipod}" ] # only do this if the ipod is attached
		then
		echo ". "
		echo ". ipod stuff: syncipod, kgtd2go, ipodbkp (including mail)..."
		echo ". "
		syncipod &
		echo "open Mail.app, because, if mail is closed, iPDA chokes."
		runlater s 200 openhide s Mail &
		runlater 360 kgtd2go &  # delayed so kgtd sync will be finished.
		runlater 420 ipodbkp &  # delayed so will not interfere with kgtd2go
		sleep 2
	fi
#################################################################################################### eve script, default behavior
elif [ "${_arg}" = "script" -o "${_arg}" = "s" -o "${_arg}" = "" -o "$_arg" = "shutdown" -o "$_arg" = "nosleep"  -o "$_arg" = "noscript" ]
	then
	#### the universal eve header
	echo "eve"
	cat ~/.evemessage
	echo " "
	# the opening  applescript...
	#
	if [ "${_arg}" = "script" -o "${_arg}" = "s" ]
		then
		shift
	fi
	echo "."
	echo ". eve says: goodnight duus, `getname`..."
	echo "."
	echo ". I will run sudo periodic daily, and then"
	echo ". i will run afsup, so you'll have to put in your password"
	echo ". but then you can leave."
	echo ". "
	if [ ! -e "${_ipod}" ]
		then
		echo ".  don't forget to plug in your ipod!"
		echo ". "
	fi
	if [ "$1" = "shutdown" ]
		then
		open ${_eveascript}/${_shutdownscript} &
	elif [ "$1" = "nosleep" ]
		then
		open ${_eveascript}/${_nosleepscript} &
	elif [ "$1" = "noscript" ]
		then
		echo "no script running"
	else
		open ${_eveascript}/${_normscript} &
	fi
#################################################################################################### eve logins (internal)	
elif [ "${_arg}" = "l" -o "${_arg}" = "logins" ]
   then
   #loginstuff here.
   echo "SECONDARY WINDOW (logins)"
   #################### afsup
   ~/bin/afsup  
   #
   ################### run daily maintenance scripts
   # listed here: http://www.thexlab.com/faqs/maintscripts.html
   # flag added in weekly review...
   # or, every seven days
   _tmp=`date +%d`
   if [ `expr $_tmp % 7` -eq 1 ]
   	then
   	_tmp="full"
   	else
   	_tmp=""
   fi
   sudo runlater s 30 myperiodic run ${_tmp} &
   #
   ### runlater so that sudo periodic daily is already underway
   echo " .... quit itunes in 1500 secs, run software update in 1600 seconds"
   echo "       (to avoid itunes/softwareupdate conflicts that have arisen in the past)"
   runlater count 1500 100 quit itunes &
   sudo runlater count 1600 100 softwareupdate -i -r &
   #	
   echo "check processes in 300 seconds...."
   runlater s 298 echo " ... " &
   runlater s 297 echo " .. " &
   runlater s 299 echo " ........ processes ....... " &
   runlater count 300 100 ps -a &
   runlater s 301 echo " .......................... " &
###########
else
	thunder &
	sleep 1
	echo "sorry, `getname`, <eve $1> not understood.  ($*)"
	echo 
	eve help
	exit 1;
fi  # end opening arg script....
#####################################################################################################################


