#!/bin/bash
#Mark Feineigle 2014 03 14
#Wait x time, then play sound/note & prompt for input. On input "0" close, on input "-ge 1" loop 

END_SOUND="/home/egg/.sounds/river_done.wav"
#FUNCTIONS
timer () {
	while [ $TIME -ge 1 ]  2>/dev/null #loop while TIME >0
	do
		if [ $1 == "-m" ]; then
			let TIME=$TIME*60 #multiply for minutes
			TITLE="Minutes";TEXT="minutes" #set labels for use in zenity
		else
			TITLE="Seconds";TEXT="seconds" #set labels for use in zenity
		fi
		sleep $TIME #sleep for TIME
		aplay $END_SOUND > /dev/null 2>&1 & #play sound, suppress output, run in background
		if [ $NOTE != "" ]  2>/dev/null; then #if there is a NOTE, display it
			zenity --title="Reminder" --info --text="$NOTE"
		fi
		export TIME=`zenity --title="$TITLE" --entry --text="More $TEXT?"` #input TIME from GUI
		if [ $TIME == "0" -o $TIME == "" ]  2>/dev/null; then #if TIME = 0 of null
			exit 0
		fi
		NOTE="" #clear old note
		if [ $TIME -ge 11 ] 2>/dev/null; then #if TIME is longer than x
			export NOTE=`zenity --title="Reminder" --entry --text="Note?"` # input NOTE from GUI
		fi
	done
	exit 0
}
#START OF MAIN
if [ "$1" == "-s" -a "$2" -ge 1 ] 2>/dev/null; then  #if the 1st arg is "-s" and the 2nd arg is >=1
		TIME="$2";NOTE="$3" #set 2nd/3rd args as TIME/NOTE
		timer $1 #pass $1 (-s) to timer function
elif [ "$1" == "-s" -a "$2" == "" ]; then #if no 2nd arg
		export TIME=`zenity --title="Seconds" --entry --text="How many seconds?"` #read SECONDS 
		if [ $TIME == "0" -o $TIME == "" ] 2>/dev/null; then #if 0 or null
			exit 0
		fi
		if [ $TIME -ge 3 ] 2>/dev/null; then #if TIME longer than x
			export NOTE=`zenity --title="Reminder" --entry --text="Note?"` # add NOTE from GUI
		fi
		timer $1 #$TIME $NOTE   #call timer function, exported TIME/NOTE = no passing needed?
elif [ "$1" == "-m" -a  "$2" -ge 1 ] 2>/dev/null; then #if 1st arg is "-m" and 2nd arg is >0
		TIME="$2";NOTE="$3" #set 2nd/3rd args as TIME/NOTE
		timer $1 #pass $1 (-m) to timer function
elif [ "$1" == "-m" -a "$2" == "" ]; then #GUI prompt for integer
		export TIME=`zenity --title="Minutes" --entry --text="How many minutes?"` #return MINUTES from GUI
		if [ $TIME == "0" -o $TIME == "" ] 2>/dev/null; then #if 0 of null
			exit 0
		fi
		if [ $TIME -ge 9 ] 2>/dev/null; then #if TIME longer than x
			export NOTE=`zenity --title="Reminder" --entry --text="Note?"` # add NOTE from GUI		
		fi
		timer $1 #$TIME $NOTE   #call timer function, exported TIME/NOTE = no passing needed?
elif [ "$1" -ge 1 ]  2>/dev/null; then #if 1st arg is >0
	TIME="$1";NOTE="" #set TINE as 1st arg, NOTE as null
	timer -m #call timer function with $1 = "=m"
else
	printf "minutes usage = $0 [MINUTES]\n"
	printf "              = $0 -m [MINUTES]\n\n" #display help on any non-defined useage
	printf "seconds usage = $0 -s [SECONDS]\n\n"
	printf "this help     = $0 -h\n"
	printf "	      = $0 --help\n\n"
fi
exit 0
