#!/bin/sh
#
#  uptime.sh v0.1 - written by Robert Pectol
#  http://rob.pectol.com/irlp
#
#  This script calculates the node's uptime and then
#  feeds the result to the festival text to speech
#  engine for vocal announcement of system uptime.
#
#  Because it requires festival, which is not
#  installed by default on IRLP nodes, it will
#  need to be installed before this will function
#  properly.  Festival can easily be installed by
#  doing the following at the root prompt:
#  "yum install festival" (without quotes).
#####################################################

###  ---> user adjustable settings <---  ###
#
# for AUX line ptt support
aux_line_ptt="yes"
aux_line="1"
#
## ---> end user adjustable settings <--- ##

########################################
#  NO NEED TO MODIFY BELOW THIS LINE!  #
########################################

# ensure script is run as repeater user
if [ `/usr/bin/whoami` != "repeater" ] ; then
	exit 1
fi

# ensure the environment file has been sourced
if [ "$RUN_ENV" != "TRUE" ] ; then
	. /home/irlp/custom/environment
fi

# exit silently if there is a QSO in progress
if ! $BIN/pttstate; then
	exit 0
fi
if ! $BIN/cosstate; then
	exit 0
fi

# configure AUX line PTT if enabled above
if [ "$aux_line_ptt" == "yes" ]; then
	tx_key=`echo "$BIN/aux$aux_line""on"`
	tx_unkey=`echo "$BIN/aux$aux_line""off"`
fi

# decide if it's morning, afternoon, or evening
curhour=`date +%k`
if [ "$curhour" -lt "12" ]; then
	speak_greeting="morning"
elif [ "$curhour" -gt "17" ]; then
	speak_greeting="evening"
else
	speak_greeting="afternoon"
fi

# Calculate the system uptime
totalseconds=`cat /proc/uptime`
totalseconds=${totalseconds%%.*}
let days=$((${totalseconds}/86400))
let hours=$((${totalseconds}/3600%24))
let minutes=$((${totalseconds}/60%60))
let seconds=$((${totalseconds}%60))

# formatting, etc.
if [ "$days" -eq "1" ]; then
	day_sp="day"
else
	day_sp="days"
fi
if [ "$hours" -eq "1" ]; then
	hour_sp="hour"
else
	hour_sp="hours"
fi
if [ "$minutes" -eq "1" ]; then
	minute_sp="minute"
else
	minute_sp="minutes"
fi
if [ "$seconds" -eq "1" ]; then
	second_sp="second"
else
	second_sp="seconds"
fi
announce_uptime="Good $speak_greeting.  The $CALLSIGN node \
	has been up for $days $day_sp, $hours $hour_sp, $minutes \
	$minute_sp, and $seconds $second_sp."

# key-up the node
if [ "$aux_line_ptt" == "yes" ]; then
	$tx_key
else
	$BIN/key
fi
sleep .5

# announce the node's uptime
echo "$announce_uptime" | festival --tts

# unkey the node
if [ "$aux_line_ptt" == "yes" ]; then
	$tx_unkey
else
	$BIN/unkey
fi
exit 0


