#!/bin/sh
#h morn v 0.1 by duus
echo "morn"
echo " "
__thisdate=`date +_%h_%d_%y`
#
#
_SCRIPT_NAME=morn
_SCRIPT_LOCATION=~/bin/
_mornf=~/.morn/
if [ "$1" = "version" ]
	then
	echo "$_SCRIPT_NAME by duus"
	echo " the morning script "
	exit 1;
fi
# --- normal script here ---
#
#h default = no arguments
#h
#
################################################     begin admin
if [ "$1" = "admin" ]
	then
#h -- admin -- <morn admin>
#h  morn admin with no arguments shows the current settings.
	shift
	if [ "$1" != "" ]
		then
		echo "morn admin: set $*"
	fi
	case "$1" in
		'meditate')
#h   X = meditate  (run mornmeditate)
		shift
		mornmeditate $*
			;;
	   'codeoff')
#h   X = codeoff  ::  run ~/bin/morningcode
	   	rm ${_mornf}coderun
	   		;;
	   'codeon')
#h   X = codeon  
	   	echo "on `date`" > ${_mornf}coderun
	   		;;
	   'printoff')
#h   X = printoff  ::  lpr -p ~/Documents/GTD/morningmessage/morningMessage.txt
	   	rm ${_mornf}print
	   		;;
	   'printon')
#h   X = printon
	   	echo "on `date`" > ${_mornf}print
	   		;;
	   'airport')
#h   X = airport off/on/none  (none means that there is no airport change)
	   if [ "$2" = "on" -o "$2" = "off" ]
			then 
			echo "set airport $2 in morning."
			echo -n $2 > ${_mornf}airport
		elif [ "$2" = "none" ]
			then
			echo "no airport setting"
			rm ${_mornf}airport
		else
			echo error $*
		fi
	   		;;
	esac
	echo ; echo "current settings:"
	cd ${_mornf}
	for _thisfile in  `ls` 
		do
		echo "${_thisfile}: `cat ${_thisfile}`"
	done
elif [ "$1" = "undo" ]
	then
#h  morn undo  unflags morn.
	echo "do you really want to undo morn?  type (y) to undo, anything else to cancel."
	read _yesno
	if [ "$_yesno" = "y" ]
		then
		echo -n "removing ... "
		rm -v ~/Documents/_SCRATCH/archive/scratch$__thisdate.txt
		echo "it is now unflagged.  other changes may have still occurred."
		echo "you're playing with fire.  now you've done it!"
		bong 3
		morn help
		else
		echo "cancelling..."
	fi
	exit 1;
elif [ "$1" = "h" -o "$1" = "help" ]
	then
#h  morn help does this
	grep "^#h" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
elif [ "$1" != "" ]
	then
	echo "sorry, `getname`, morn $* not understood."
	morn help
	exit 1;
################################################       end admin
#
	else # go to this if. XX
if [ -e ~/Documents/_SCRATCH/archive/scratch$__thisdate.txt  ]
	then
	echo "Honestly, `getname`, you already ran morn today!  quitting!"
	bong 3 &
	open ~/Documents/GTD/morningmessage/PJMorn.tmproj 
	else
	echo 
	echo "good morning, `getname`!"
	whenlast s morn &
	#cd ~/bin
	itunes pause &
	runlater s 2 bong 2 &
	echo "mornarchive"
	~/bin/mornarchive
	if [ -d /Volumes/duus\ ipod ]
		then
		ipodfilebkp &
		syncipod &
		runlater count 300 60 itunes eject &
	fi
	open ~/Documents/GTD/morningmessage/PJMorn.tmproj &
	open ~/IN/OpenBin/* &
	whenlast w morn
	# openhide Skype &
	runlater s 5 open ~/Documents/GTD/morningmessage/morningMessage.txt\
	if [ -f ${_mornf}coderun ]
		then
		echo "run morningcode..."
		~/bin/morningcode
	fi
	if [ -f ${_mornf}print ]
		then
		echo "print new morning message!"
		lpr -p ~/Documents/GTD/morningmessage/morningMessage.txt
	fi
	if [ -f ${_mornf}airport ]
		then
		_airport=`cat ${_mornf}airport`
		echo "turn airport ${_airport}"
		airport ${_airport} &
	fi
	if [ -f ${_mornf}meditate ]
		then
		echo "starting meditation `cat ${_mornf}meditate` when you press return"
		echo "(Note, `getname`: I'm starting meditation `cat ${_mornf}meditate` when you press return in Terminal)" >>  ~/Documents/GTD/morningmessage/morningMessage.txt
		runlater s 11 bong 2 &
		runalter s 12 bong 2 &
		echo "press return to continue:"
		read -e proceed &
		meditate `cat ${_mornf}meditate` &
	fi
fi
fi #end if XX
