#!/bin/bash
#
# $Id: playWxAlertBg 74 2011-04-20 19:55:19Z  $
#
# Play weather alerts Announcements
# Normally placed in background from getWxAlert.
#     by W0ANM
#

# My process id
MYPROC=$$

# 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 COUNTY  not an argument, then use what is defined in configure file.
if [ "$1" = "" ] ; then
    COUNTY=$COUNTY
else
    COUNTY=$1
fi

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

WXALERT_TEXT=${WXALERT_SPOOLDIR}/$COUNTY/alert.txt
NO_WXALERT_MSG=$LOCAL/no_wxalert_msg

# store the PID
echo "$$" > $WXALERT_SPOOLDIR/$COUNTY/playWxAlertBg.pid

##########
PlayMsg () {
#       /usr/sbin/asterisk -rx "rpt localplay 27539 /custom/wx/alert/$COUNTY/ALERT" &> /dev/null 2>&1
#       /usr/local/bin/texttransmit N8MFN__B -f /custom/wx/alert/OHC017/wx_new.txt
#        /home/irlp/custom/dstartx
	#/usr/local/bin/voicetransmit N8MFN _B /custom/wx/alert/$COUNTY/ALERT.dvtool
#        /root/voice-ann/ambestream -t "Current Wx Alert" -v -4 -my N8MFN -d 141.148.63.71 -bi 7500 -bl 1000 -p 40000 XLX265 B /custom/wx/alert/OHC017/ALERT.dvtool > /dev/null 2>&1
        /root/voice-ann/ambestream -t "Current Wx Alert" -v -4 -my N8MFN -d 10.147.17.8 -bi 7500 -bl 1000 -p 40000 N8MFN B /custom/wx/alert/OHC017/ALERT.dvtool > /dev/null 2>&1
#        curl -X POST https://home.kb8pmy.net/api/webhook/play_weather_alert
#        curl -X POST -H 'Content-type: application/json' --data '{"text":"WRJI518, Testing intergration!"}' https://hooks.slack.com/services/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
#        curl -X POST --silent --data-urlencode "payload={\"text\": \"$(cat /custom/wx/alert/$COUNTY/alert.txt | sed "s/\"/'/g")\"}" "$2" https://hooks.slack.com/services/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

} # end PlayMsg 

##########
PlayAlert() {

           # Play Alert Message
	   log "$Msg"
           PlayMsg
}


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

# Run's in background, called by getWxAlert

while [  -f $WXALERT_TEXT ] ; do
   # initial broadcast
   # check if lock file is present. If so, wait for file to be 
   # removed, this prevents alert announcements from simultaneioulsy
   # being sent.
   # if NO_WXALERT file is present, skip playing msg
   if [ ! -f $NO_WXALERT_MSG ] ; then
       while [ -f /tmp/playWxAlertBg-lock ] ; do
           sleep 30
       done

       # set lock file
       echo $MYPROC > /tmp/playWxAlertBg-lock
            
       Msg="Playing Wx Alert (initial) for $COUNTY"
       PlayAlert

       # clear lock file
       rm /tmp/playWxAlertBg-lock
   else
       log "Skipping Wx Alert (initial) for $COUNTY, $NO_WXALERT_MSG present"
   fi

   # play time is initial, 15m, 15m, 15m, 30m, 30m, then every 60m
    for SLEEPTIME in 900 900 900 1800 1800 ; do
   # for SLEEPTIME in 86400 ; do
      # sleep 15 mins (900)
      sleep $SLEEPTIME
      if  [ ! -f $WXALERT_TEXT ] ; then
         break
      fi
      if [ ! -f $NO_WXALERT_MSG ] ; then
          # check if lock file is present. If so, wait for file to be 
          # removed, this prevents alert announcements from simultaneioulsy
          # being sent.
          while [ -f /tmp/playWxAlertBg-lock ] ; do
              sleep 30
          done
          # set lock file
          echo $MYPROC > /tmp/playWxAlertBg-lock
 
          Msg="Playing Wx Alert (${SLEEPTIME}s) for $COUNTY"
          PlayAlert

          # clear lock file
          rm /tmp/playWxAlertBg-lock
      else
          log "Skipping Wx Alert (${SLEEPTIME}s) for $COUNTY, $NO_WXALERT_MSG present"
      fi
   done

   while [ -f $WXALERT_TEXT ] ; do
      #every 60m
      sleep 3600 
      if  [ ! -f  $WXALERT_TEXT ] ; then
         break
      fi
      if [ ! -f $NO_WXALERT_MSG ] ; then
          # check if lock file is present. 
          while [ -f /tmp/playWxAlertBg-lock ] ; do
              sleep 30
          done
          # set lock file
          echo $MYPROC > /tmp/playWxAlertBg-lock
            
          Msg="Playing Wx Alert (${SLEEPTIME}s) for $COUNTY"
          PlayAlert

          # clear lock file
          rm /tmp/playWxAlertBg-lock
      else
          log "Skipping Wx Alert (${SLEEPTIME}s) for $COUNTY, $NO_WXALERT_MSG present"
      fi
   done   
done

# remove PID file
rm $WXALERT_SPOOLDIR/$COUNTY/playWxAlertBg.pid

# remove NO_WXALERT_MSG file
if [ -f $NO_WXALERT_MSG ] ; then
    rm -r $NO_WXALERT_MSG
fi

log "Alert over for $COUNTY"

exit 0

