#!/bin/bash
function osxtimer() {
	# dependency check
	#termdown --version >/dev/null 2>&1 || { echo osxtimer requires 'termdown';  exit 1; } 
	#lolcat --version >/dev/null 2>&1 || lolcatError='`lolcat` is not installed'; 
	#figlet --version >/dev/null 2>&1 || figletError='`figlet` is not installed';

	# set defaults
	local msg;
	local timestring='10s';
	local voice;
	local critical;
	local dontsaycountdown;
	local dontsaymessage;
	local dowait;
	local dontdisplaymessage;
	local displayHelp;

	unset OPTIND;
	while getopts "MVTYhwm:t:c:v:" opt; do
		case $opt in
			m) msg=$OPTARG          ;; # optional message
			M) dontdisplaymessage=t ;; # don't display message (but maybe say it)
			t) timestring=$OPTARG   ;; # termdown countdown string
			c) critical=$OPTARG     ;; # optional critical seconds remaining
			v) voice=$OPTARG        ;; # optional voice for critical seconds remaining
			V) voice=$(voicerandom) ;; # select random voice
			T) dontsaycountdown=t;  ;; # don't readout countdown
			V) dontsaymessage=t;    ;; # don't readout message after countdown
			w) dowait=t;          ;; # don't wait for user input after the timer
			h) displayHelp=t; ;; # display help message
			:) echo option -$OPTARG requires an argument; displayHelp=t;
				;;
		esac
	done
	unset OPTIND;

	# exit on fail
	if [ $displayHelp ]; then
		printf "
Correct usage:
 [-t]\t countdown amount as a timestring used in 'termdown'
 [-m]\t display message after countdown is finished
 [-c]\t critical seconds param for 'termdown'
 [-v]\t 'say' voice used for reading the message and countdown
 [-V]\t select random voice for message and timer readout
 [-T]\t used with -v or -V :: don't readout the countdown
 [-Y]\t used with -v or -V :: don't readout the message
 [-w]\t wait for user input after the timer
\n";
		return 1;
	fi;

	# verify that the voice exists
	say -v ? | grep $voice >/dev/null 2>&1 || unset voice;

	local termdownargs="$timestring ";
	[[ $critical ]] && termdownargs="$termdownargs -c $critical";
	[[ $voice ]] && [ -z $dontsaycountdown ] && termdownargs="$termdownargs -v $voice";
	termdown $termdownargs;

	[[ $msg ]] && [ -z $dontdisplaymessage ] && figlet "$msg" | lolcat;
	[[ $voice ]] && [ -z $dontsaymessage ] && say -v "$voice" "$msg";

	[[ $dowait ]] && read -n 1; # wait for user input
}

function rdexercise() {
	local message='Plank time!';
	local delay='1h30m';
	local count;

	# flagged arguments
	unset OPTIND;
	while getopts "m:t:c:" opt; do
		case $opt in
			m) message=$OPTARG ;; # optional message
			t) timeout=$OPTARG ;; # termdown countdown string
			c) count=$OPTARG   ;; # length of exercise as a timestring
		esac
	done
	unset OPTIND;

	# operation loop
	while true; do
		rdstart >/dev/null 2>&1;
		clear; osxtimer -VT -m "$message" -t "$delay";
		rdplank >/dev/null 2>&1;

		read -n 1; clear;
		[[ $count ]] && osxtimer -c 10 -m "Done" -v "$(voicerandomnormal)" -t "$count";
	done
}

alias planktimer='rdexercise -c';
alias pushuptimer='rdexercise -m "Push-up time!" -t "1h" -c';