#!/bin/sh
#
# saydate.sh - by tekatch@idirect.com
# adapted from saytime.sh - shell version of saytime, by david@eng.sun.com
set -e

# set the language so that locale time files can be found
LC_ALL=us_EN
LANG=$LC_ALL


version="0.3.0"


# Get the directory name that the sound files are stored in
SDIR=/usr/share/saydate


# Clear the say files buffer
SAYFILES=


# Declare the routine that fills the SAYFILE buffer with valid filenames
say () {
	for arg in "$@"
	do
		file=$SDIR/$arg.raw
		if [ ! -f $file ] ; then
			echo "`basename $0`: cannot find $file"
			exit 1
		fi
		SAYFILES="$SAYFILES $file"
	done
}


# Declare the routine that decodes numbers up to 9999 to be spoken
saynum () {
	number=$1
	if [ $number -ge 10000 ] ; then
	    say days and days and
	    return
	fi

	if [ $number -ge 1000 ] ; then 
		say $(($number/1000)) 1000
		number=$(($number%1000))
		if [ $number -eq 0 ] ; then
		    return
		fi
	fi

	if [ $number -ge 100 ] ; then 
		say $(($number/100)) hundred
		number=$(($number%100))
		if [ $number -eq 0 ] ; then
		    return
		fi
	fi

	case $number in
		0[0-9])		say $((1$number-100))		;;
		[0-9])		say $number			;;
		1[0-3])		say $number			;;
		14)		say 4 teen			;;
		15)		say fif teen			;;
		1[6-9])		say $(($number-10)) teen	;;
		20)		say 20 				;;
		2[1-9])		say 20 $(($number-20))		;;
		30)		say 30 				;;
		3[1-9])		say 30 $(($number-30))		;;
		40)		say 4 ty			;;
		4[1-9])		say 4 ty $(($number-40))	;;
		50)		say fif ty			;;
		5[1-9])		say fif ty $(($number-50))	;;
		60)		say 6 ty			;;
		6[1-9])		say 6 ty $(($number-60))	;;
		70)		say 7 ty			;;
		7[1-9])		say 7 ty $(($number-70))	;;
		80)		say 8 ty			;;
		8[1-9])		say 8 ty $(($number-80))	;;
		90)		say 9 ty			;;
		9[1-9])		say 9 ty $(($number-90))	;;
	esac
}



# Check for user supplied options or default=all options
if [ $1 ] ; then
	options=$@
else
		options="-the_date_is -dow -month -the -day -year -and -the -uptime"
fi

# Get all pieces of the date and put into variables
set -$- `/bin/date '+%A %B %d %Y'`
dow=$1 month=$2 date=$3 year=$4


# Execute all options in the command line
for option in $options
do
	case $option in 
	--version)# Print the version number
		echo "saydate version "$version
		;;

	--help)	#Display the help screen
		echo 
		echo "saydate [options]"
		echo " -dow		weekday name (Sunday..Saturday)"
		echo " -month		month name (January..December)"
		echo " -day		day of month (the 1st..31st)"
		echo " -year		year (1970...)"
		echo " -uptime	system uptime"
		echo " -the_date_is	say the phrase the date is "
		echo " -the		say the word the"
		echo " -and		say the word and"
		echo " --help		this help screen"
		echo " --version	print the version number of this program"
		echo 
		;;
		
	-uptime) #system uptime
		set -$- `uptime`
#		set -$- `cat ~anthony/temp/uptime.5`
		say uptime

		case $4 in
		    day,|days,)	updays=$3
				saynum $updays
				if [ $updays -eq 1 ] ; then
				    say day
				else
				    say days
				fi
				shift 2;;
		esac

		case $4 in
		    min,)	upminutes=$3;;
		    *)		save_ifs=$IFS
				IFS=:,
				set $3
				IFS=$save_ifs
				uphours=$1
				saynum $uphours 
				if [ $uphours -eq 1 ] ; then
				    say hour
				else
				    say hours
				fi
				upminutes=$2;;
		esac

		saynum $upminutes
		if [ $upminutes -eq 1 ] ; then		
		    say minute
		else
		    say minutes
		fi
		
		;;


	-the_date_is)
		 say the_date_is;;

	-dow) # Day of week
		say $dow;;

	-month) # Month name
		say $month;;

	-the)
		say the;;

	-and)
		say and;;

	-day) # Say the day of the month
		case $date in
			01)		say 1st			;;
			02)		say 2nd			;;
			03)		say 3rd			;;
			04)		say 4 th		;;
			05)		say fif th		;;
			06)		say 6 th		;;
			07)		say 7 th		;;
			08)		say 8 th		;;
			09)		say 9 th		;;
			10)		say 10 th		;;
			11)		say 11 th		;;
			12)		say 12 th		;;
			13)		say 13 th		;;
			14)		say 4 teen th		;;
			15)		say fif teen th		;;
			16)		say 6 teen th		;;
			17)		say 7 teen th		;;
			18)		say 8 teen th		;;
			19)		say 9 teen th		;;
			20)		say 20 th		;;
			21)		say 20 1st		;;
			22)		say 20 2nd		;;
			23)		say 20 3rd		;;
			24)		say 20 4 th		;;
			25)		say 20 fif th		;;
			2[6-9])		say 20 $(($date-20)) th	;;
			30)		say 30 th		;;
			31)		say 30 1st		;;
		esac;;


	-year)
		case $year in
			1999)		say 9 teen 9 ty 9		;;
			2000)		say 2 1000			;;
			200[1-9])	say 2 1000 $(($year-2000))	;;
			201[0-3])	say 20 $(($year-2000))		;;
			2014)		say 20 4 teen			;;
			2015)		say 20 fif teen			;;
			201[6-9])	say 20 $(($year-2010)) teen	;;
		esac;;
	esac
done


#If there is something to say then say it
if [ ${#SAYFILES} -gt 0 ] ; then
	cat $SAYFILES | sox -t.ul - -t ossdsp /dev/dsp
fi
