#!/bin/bash
#######################################################################
# filename:	play_q5
# description:	Play any one of a number different Q5 audio files
# arguments:	episode number, 2 digits  (09-45)
#
# output:	none, only audio! (we hope)
#
#
# history:
# 20200607 v1.0 vk2yld  original release
#
#
#
#
#############################################################################
###                           Configuration                               ###
#############################################################################

Q5="/home/news_archive/Q5"        # Where are the news files stored?
IRLPV3="YES"                      # Do you have an IRLP Ver3 Board and fan?
  FANAUX=3                        # If so, which aux line is the fan on?
  COOLTIME=60                     # How long to cool after transmission (sec)
RESET_REQ="NO"                    # Is reset of PTT required? (into repeater)

#############################################################################
###                        Function definitions                           ###
#############################################################################
#
# This function logs Date etc then message..
#
writelog() {
  MESSAGE="`date '+%b %d %Y %T %z'` "$@
  if [ -n "$LOGFILE" ]; then
    echo $MESSAGE >> $LOGFILE
  fi
}
#
### Key...
# Key the PTT. If the PTT manager program is running then use it
# otherwise, just mash it
#
my_key() {
   if ps -C ptt -o pid= >&/dev/null
   then
      echo "+" > $LOCAL/ptt_fifo
   else
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/forcekey
   fi
}
### Unkey
# Unkey the PTT. If the PTT manager program is running, then use it
# otherwise, just bury it
#
my_unkey() {
   if ps -C ptt -o pid= >&/dev/null
   then
      echo "-" > $LOCAL/ptt_fifo
   else
      $BIN/forceunkey
   fi
}

### Restart CWID
# Restarts the CWID sequence when it has been stopped
#
#
restartcwid() {
   rm -f /home/irlp/local/cwtimer    # remove lockout flag file
   /home/irlp/custom/cwtimer_mon     # force restart the loop
}
#
### PlayMP3
# Key the PTT, play MP3 file passed in command line, then unkey
#  Filename should be specified without the extension.  If second
#  parameter is 'txon' the routine will not shut down tx on completion
#
playmp3() {
  my_key
  sleep 0.5
  echo "Playing $1.mp3"
  /usr/bin/mpg123 $1.mp3 > /dev/null 2>&1
  sleep 0.5
  my_unkey
}

#
#############################################################################
###                                                                       ###
###                 MAIN PROGRAM EXECUTION STARTS HERE                    ###
###                                                                       ###
#############################################################################
#
# Make sure we are user repeater!!!
   if [ "`/usr/bin/whoami`" != "repeater" ] ; then
     writelog "Play_Q5: ERROR - Incorrect user"
     echo "This program must be run as user REPEATER!"
     exit 1
   fi
# Make sure we have sourced the environment file
   if [ "$RUN_ENV" != "TRUE" ] ; then
      . /home/irlp/custom/environment
   fi
#
if [ ! -f $LOCAL/enable ] ; then
   echo "Node disabled..  Aborting.."
   writelog "Play_Q5: ERROR - Node is Disabled"
   exit 1
fi

if [ "$#" = "0" ] ; then                     # If no arguments
   echo "No program number specified."
   writelog "Play_Q5: ERROR - No parameters."
   exit 99
fi

##############################################################################
#
#
case $1 in
09|[1-3][0-9]|4[0-5])  writelog "Play_Q5: Episode $1 selected.."
                       AUDIOFILE=$Q5"/program"$1"DC" ;;
q)                     ERRORFILE=$NEWS/error-q
                       COOLDOWN="NO" ;;
*)                     writelog "Invalid Program Number specified. ($1)"
                       echo "Invalid Program Number specified. ($1)"; exit 1 ;;
                              # Error.. screwed something up..
esac

if [ -f $LOCAL/timeout ] ; then
   TIMEOUT="YES"                                  # Set timeout restore flag
   TOVALUE="`cat $LOCAL/timeout`"            	# Save TIMEOUT VALUE
   rm -f $LOCAL/timeout &>/dev/null          	# Disable activity timer
 else
   TIMEOUT="NO"	                             	# Set timeout OFF flag
fi

#
##################################
# Rip node out of IRLP Mode      #
##################################
#
if [ -f $LOCAL/active ] ; then               	# If node is connected
   writelog "Play_Q5: Killing current connection before broadcast."
   $SCRIPT/end                               	# Kill connection
   sleep 3                                   	# Beauty sleep.. need some...
fi
$SCRIPT/disable                              	# Disable node
#
#
if [ "$IRLPV3" == "YES" ] ; then
   touch $LOCAL/fan_lock                     	# lock fan ON - FANCTRL script
                                             	# will see flag and start fan
fi
touch /home/irlp/local/cwtimer               	# Stop CWID tx
#
##################################
# Play the requested audio file  #
##################################
#
writelog "Play_Q5: Playing file "$AUDIOFILE
playmp3 $AUDIOFILE                                # play MP3 file
#
#

writelog "Play_Q5: Broadcast ended normally."

if [ "$TIMEOUT" == "YES" ] ; then
   touch $LOCAL/timeout
   if [ "$TOVALUE" != "" ] ; then
      echo $TOVALUE > $LOCAL/timeout
   fi
fi
sleep 2                                           # Another nap?
#
##################################
# Cleanup and exit time..        #
##################################
#
if [ "RESET_REQ" == "YES" ] ; then
   killall ptt_reset                            # stop ptt reset timer
   writelog "Play_Q5: PTT Reset timer stopped."
fi
$SCRIPT/enable                                  # re-enable node
restartcwid					# Re-start CWID sequence.....
#
   if [ "$IRLPV3" == "YES" ] ; then
      echo -n $COOLTIME" second radio cooldown cycle in progress..."
      writelog "Play_Q5: Cooling cycle, "$COOLTIME"sec Timer Start..."
      sleep $COOLTIME				# Let radio chill out a while
      rm -f $LOCAL/fan_lock			# allow auto fan ctrl again
      echo "Completed."
      writelog "Play_Q5: Radio cooling cycle complete."
   fi
exit 0

#  All done.....
