#!/bin/bash
#
# $Id: playWxAlertBg 28 2008-12-14 06:10:02Z kovacs $
#
# Play weather alerts Announcements
# Normally placed in background from getWxAlert.
#     by W0ANM
#

CUSTOM="/scripts/wx"

# Load config file

if [ -f ${CUSTOM}/wx_scripts.conf ] ; then
    source ${CUSTOM}/wx_scripts.conf
else
    echo "Missing ${CUSTOM}/wx_scripts.conf file, aborting..."
    exit 1
fi

# multiple zone support.
# if ZONE  not an argument, then use what is defined in configure file.
if [ "$1" = "" ] ; then
    ZONE_ALERT=$ZONE
else
    ZONE_ALERT=$1
fi

# if argument is not a directory, abort and do nothing. send error
if [ ! -d /audio/wx/alert/$ZONE_ALERT ] ; then
    # msg to log file.
    log "ZONE directory $ZONE_ALERT is not present, aborting..." 
    exit 1
fi


WXALERT_TEXT=/audio/wx/alert/$ZONE_ALERT/alert.txt

##########

PlayAlert() {
   # Set if ACTIVE_PLAY is FALSE and the node is NOT active, then OK to play
   if  [ $ACTIVE_PLAY = "FALSE" ] ; then
       if [ -f ${LOCAL}/active ] ; then  # if active skip
           echo "Node is busy...."
       else
           touch ${LOCAL}/active
           if [ -f ${CUSTOM}/custom_wavplaydddd ] ; then
               ${CUSTOM}/custom_wavplay custom/wx/alert/$ZONE_ALERT/ALERT &> /dev/null 2>&1
           else
                sudo /usr/sbin/asterisk -rx "rpt localplay 2090 /audio/wx/alert/$ZONE_ALERT/ALERT"
           fi
           # clear active file
           rm ${LOCAL}/active
        fi
   else
       # ACTIVE_PLAY=TRUE
       if [ -f ${LOCAL}/active ] ; then
            # checking for PTT
            while ! $BIN/pttstate ; do
                # Wait for PTT to drop
                sleep 1
            done
            # All clear to send alert.
            # kill wrapper
            killall sfswrapper

            if [ -f ${CUSTOM}/custom_wavplayss ] ; then
                ${CUSTOM}/custom_wavplayddd custom/wx/alert/$ZONE_ALERT/ALERT &> /dev/null 2>&1
            else
                sudo /usr/sbin/asterisk -rx "rpt localplay 2090 /audio/wx/alert/$ZONE_ALERT/ALERT"
            fi
            # start wrapper back up
            ${SCRIPT}/sfswrapper
       else
           touch ${LOCAL}/active
           if [ -f ${CUSTOM}/custom_wavplay ] ; then
               ${CUSTOM}/custom_wavplay custom/wx/alert/$ZONE_ALERT/ALERT &> /dev/null 2>&1
           else
                sudo /usr/sbin/asterisk -rx "rpt localplay 2090 /audio/wx/alert/$ZONE_ALERT/ALERT"
           fi
           rm ${LOCAL}/active
       fi
    fi
}


#######################################################################
#  Main Program

# Run's in background, called by getWxAlert

while [  -f $WXALERT_TEXT ] ; do

   # initial broadcast
   log "Playing Wx Alert (initial)"
   PlayAlert

   # play time is initial, 10m, 15m, 15m, 30m, 30m, then every 60m
   for SLEEPTIME in 600 900 900 1800 1800 ; do
      # sleep 15 mins (900)
      sleep $SLEEPTIME
      if  [ ! -f $WXALERT_TEXT ] ; then
         break
      fi
      log "Playing Wx Alert (${SLEEPTIME}s)"
      PlayAlert
   done

   while [ -f $WXALERT_TEXT ] ; do
      #every 60m
      sleep 3600
      if  [ ! -f  $WXALERT_TEXT ] ; then
         break
      fi
      log "Playing Wx Alert (${SLEEPTIME}s)"
      PlayAlert
   done

done
log "Alert over"

exit 0

