#!/bin/bash
#
# $Id: getWxAlert 85 2012-07-21 23:13:23Z  $
#
# Change the method of updating alerts.  Check if summary is different instead
# of the entire url download.  This should help prevent updating alert beacons
# that are unnecessary.
#
# Added a check to see if the download failed, if so abort and log. Also
# changed the wget options for retry=10.
#
# Spool Directory should contain the alert.info, pre/post converted files, 
# no_alert, and alert summary
#
DEBUG="0"                               # debug levels 0 = no debug
                                        # level 1 = save alerts
                                        # level 2 = detailed info


# 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

# Check Arguments...
# has only one argument, county info
if [ "$#" = "1" ] ; then
   COUNTY=$1
   COUNTY_NAME=""
fi

# has two arguments, county info and county name
if [ "$#" = "2" ] ; then
   COUNTY=$1
   COUNTY_NAME=$2
fi

# if no arguments, then use what is defined in configure file.
if [ "$1" = "" ] ; then
   # extract County Information and County  Name
   COUNTY_NAME=`echo $COUNTY | awk 'BEGIN { FS="," } { print $2} '`
   COUNTY=`echo $COUNTY | awk 'BEGIN { FS="," } { print $1} '`
   # if name is empty, then use COUNTY
   if [ "$COUNTY_NAME" = "" ] ; then
       COUNTY_NAME=$COUNTY
   fi
fi

if [ "$COUNTY" = "NOTSET" ] || [ "$COUNTY" = "" ] ; then
    echo "ERROR - The COUNTY variable in ${CUSTOM}/wx_scripts.conf is not set"
    echo "or missing, aborting..."
    exit 1
fi

if [ "$COUNTY" = "help" ] ; then                        # If no arguments
    echo "Usage getWxAlert <county>"
    echo "    where <county>=State County, for example: WIC002"
    echo "    See http://alerts.weather.gov/ for more informaton"
    echo 
    echo "    Or you can edit the wx_scripts.conf file to include the COUNTY variable."
    echo
    exit 1
fi

if [ -z $TEXT2SPEECH ] ; then
    echo "TEXT2SPEECH value not defined. Please edit wx_scripts_conf file."
    exit 1
fi

if [ ! -d /tmp/wx/$COUNTY ] ; then
    mkdir -p /tmp/wx/$COUNTY
fi

####### Functions ######
# Send message to LOGFILE
function debug2 () {
  MESSAGE="${0##*/} DEBUG: $@"
  echo "$MESSAGE"
  if [ -n "$LOGFILE" ]; then
    echo "`date '+%b %d %Y %T %z'` $MESSAGE" >> $LOGFILE
  fi
}

function send_email () {
for EADDR in `echo $EMAIL | sed 's/,/ /g'` ; do

    # summary version
    # $MAIL -s "Weather Alert for $COUNTY_NAME" $EADDR < /tmp/wx/$COUNTY/pre_conv_alert_summary.txt

    # full alert info
    cp /tmp/wx/$COUNTY/pre_alert.txt /tmp/wx/$COUNTY/email_alert.txt
    # append the alert to the header
    cat /tmp/wx/$COUNTY/pre_full_alert_info.txt >> /tmp/wx/$COUNTY/email_alert.txt
    # mail it
    $MAIL -s "Weather Alert for $COUNTY_NAME" $EADDR < /tmp/wx/$COUNTY/email_alert.txt

done
}

# Convert URL to file
#function geturldata () {
#    # supply URL
#    # Alternative method of getting url information:
#   ##/usr/bin/lynx -dump $URL$COUNTY > /tmp/wx/$COUNTY/alert.info
# 
#   $WGET -t 10 -w 10 --cache=off --random-wait -q $URL$COUNTY -O - > /tmp/wx/$COUNTY/alert.info
#                                   # -t 10 retries, -w 10 sec between
#                                   # turn cache off at server
#}

###### End of Functions ######
PLIST="$CUSTOM/wx_alert_product.txt"     # Product list file and directory
# URL="http://www.weather.gov/alerts-beta/wwaatmget.php?x=" # URL to RSS data
URL="http://alerts.weather.gov/cap/wwaatmget.php?x=" # URL to RSS data
DATE=`date +%Y.%m.%d.\%H\%M`
NO_WXALERT_MSG=$LOCAL/no_wxalert_msg

MAILDIR="/tmp/wx/$COUNTY"                 # Mail log file
MAILLOG="$MAILDIR/spotter_mail.log"     # Mail log file
#MAIL=/bin/mail                          # mail binary
MAIL="$CUSTOM/SendMail/SendMail.pl"     # Perl SendMail script

CRIT_TYPES=`cat $CUSTOM/wx_critical_alerts.txt`
NORM_TYPES=`cat $CUSTOM/wx_normal_alerts.txt`

#----------------------------- Main program ----------------------

if [ ! -d $WXALERT_SPOOLDIR/$COUNTY ] ; then
    mkdir -p $WXALERT_SPOOLDIR/$COUNTY
fi

if [ ! -d /tmp/wx/$COUNTY ] ; then
    mkdir -p /tmp/wx/$COUNTY
fi

cd $WXALERT_SPOOLDIR/$COUNTY

ALERT_DL=""
CHKCNT=0
# loop until get proper response, if after 5 attempts notify
while [ -z "$ALERT_DL" ] ; do
 
     if [ $CHKCNT -gt 5 ] ; then
         log "ERROR: $URL is not responding for getWxAlert - ${COUNTY}"
         log "        Possible issue with nws.gov server or internet connection"
         ls -l /tmp/wx/$COUNTY/alert.info
         exit 1
     fi
 
     # Alternative method of getting url information:
     ##/usr/bin/lynx -dump $URL$COUNTY > /tmp/wx/$COUNTY/alert.info
 
     $WGET -t 10 -w 10 --cache=off --random-wait -q $URL$COUNTY -O - > /tmp/wx/$COUNTY/alert.info
                                   # -t 10 retries, -w 10 sec between
                                   # turn cache off at server
 
     # place  data in variable to see if contains data
     ALERT_DL="`head -1  /tmp/wx/$COUNTY/alert.info`"
     let CHKCNT=$CHKCNT+1
     # delay before retrying
     sleep 4
done

# process the downloaded information
cat /tmp/wx/$COUNTY/alert.info | \
  grep "title>" |sed '/<title>/,/<\/title/!d' |  \
  sed  's/<title>//' | sed 's/<\/title>//' | \
  egrep -v "Current Watches" > wx_new.txt

# get full summary URL Link
FULL_SUMMARY_URL=`cat /tmp/wx/$COUNTY/alert.info | egrep "<id>|</id>" | sed  's/<id>//' | sed 's/<\/id>//'`

# Extract Alert from url information:
ALERT=`cat /tmp/wx/$COUNTY/alert.info | egrep "<title>|</title>" | sed  's/<title>//' | sed 's/<\/title>//'| sed 's/\(by NWS*\).*/\1/' | sed 's/by NWS//'`

# Extract Summary from url information:
SUMMARY=`cat /tmp/wx/$COUNTY/alert.info | egrep "<summary>|</summary>" | sed  's/<summary>//' | sed 's/<\/summary>//'`

# Debug stuff, print values for $ALERT and SUMMARY
if [ "$DEBUG" = "2" ] ; then
    echo
    debug2 "************START**************************"
    debug2 "NWS_URL=$URL"
    debug2 "FULL_SUMMARY_URL=$FULL_SUMMARY_URL"
    debug2 "CHKCNT=$CHKCNT"
    debug2 "ALERT=$ALERT"
    debug2 "SUMMARY=$SUMMARY"
    debug2 "COUNTY=$COUNTY"
    debug2 "COUNTY_NAME=$COUNTY_NAME"
fi

# Full_alert
echo "$ALERT ... $SUMMARY" > alert_summary.txt

#if the download failed, alert_summary.txt has only " ... ",  then log and exit.
if [ "$DEBUG" = "2" ] ; then
    DWNCHK=`cat alert_summary.txt`
    echo
    debug2 "dwnload chk:$DWNCHK"
fi
if [ "`cat alert_summary.txt`" == " ... " ] ; then
    log "ERROR: Failed to download alert correct data from ${COUNTY}"
    log "        Verify $COUNTY is valid or URL has not changed."
    exit 1
fi

# check if no active watches, if so clean up any alerts and create
# no watches message for summary.
if ( grep "no active watches" wx_new.txt &> /dev/null ) ; then

    # skip all of this if no_alert file is present indicating audio generated
    if [ ! -f no_alerts.txt ] ; then    
        rm -f *.ul                        # Remove all alerts, audio files
        rm -f alert.txt
	rm -f *.dvtool
	rm -f conv_*
	/usr/local/bin/remotecontrold kd8tuz_b unlink

        # alert_summary.txt and alert_pre_summary.txt  must match, or will
        # generate an alert
##
        cp alert_summary.txt alert_prev_summary.txt
        cp wx_new.txt  no_alerts.txt      # After clean up, create 
                                          # "no active warnings file

        # setup the no warnings message, append file with county info, the 
        # create audio
##
        echo "for $COUNTY_NAME, $COUNTY" >> no_alerts.txt
    
        if [ $TEXT2SPEECH = "festival" ] ; then
            $TEXT2WAVE -F 8000 -scale 2.0 -otype ulaw  no_alerts.txt -o COMPLETE_ALERT.ul  -eval "(voice_ked_diphone)"
        else
            $TEXT2WAVE -m text -f no_alerts.txt -p audio/encoding=ulaw,audio/sampling-rate=8000,audio/volume=$SWIFT_VOL -o COMPLETE_ALERT.ul
        fi

        ##############################
        # save a history of alerts
        # mv over to saved directory
        # Save alert Information

        if [ "$DEBUG" = "1" ] ; then
            if [ ! -f alert.txt ] && [ -f /tmp/wx/$COUNTY/pre_conv_alert_summary.txt ] ; then
                # make sure directory is created
                if [ !  -d saved/$DATE ] ; then
                    mkdir -p saved/$DATE
                fi
                # now save files
                cp /tmp/wx/$COUNTY/*.txt  saved/$DATE/.
            fi 
        fi
    fi   # < if [ -f no_alerts.txt ] 
    exit 0   # Done, now exit.
fi

log "Checking for Wx Alert updates for $COUNTY"
# Skip if files are the same, alerts are active, but nothing has changed.
if ! diff -q alert_summary.txt alert_prev_summary.txt &> /dev/null; then

    # New Alerts are found.
    log "New Wx Alerts for $COUNTY"
    cp alert_summary.txt alert_prev_summary.txt # make the files the same now
    rm -f alert.txt *.raw no_alerts.txt        # and remove old alerts
    # remove email alert file
    if [ -f /tmp/wx/${COUNTY}/email_alert.txt ] ; then
        rm -f /tmp/wx/${COUNTY}/email_alert.txt
    fi
    # remove the old plist.txt file.
    if [ -f plist.txt ] ; then
        cp plist.txt /tmp/.
        rm plist.txt
    fi

    # create the alert file
    echo "$ALERT" > alert.txt

    # Create the product list which is tested against 
    #   if product not found, will leave a null file. This is tested against 
    #   at end of script, see ref#1
    while read PRODUCT; do                     # Check for alert products
        # appending the 1st matching, ignore case
        grep -i -m 1 "^$PRODUCT" wx_new.txt
    done < $PLIST >> plist.txt        # line to alert.txt file

    #################################################################
    #   Pre Conversions file creation.
    #################################################################
  
    #############################
    # create pre_alert.txt file (input to  post conversion)
    #
    #set up header of the alert file
    echo ". . Weather Alert. for $COUNTY_NAME" > /tmp/wx/$COUNTY/a_tmp.txt

    # check the alert types, critical/normal
    if [ "$DEBUG" = "2" ] ; then
        debug2 "********* WXALERT_BEACON=[$WXALERT_BEACON] **********"
    fi
    # Setup TYPES of alerts to trigger on depending upon WXALERT_BEACON Variable
    if [ "$WXALERT_BEACON" = "C" ] ; then
        TYPES=$CRIT_TYPES
    else
        TYPES=$NORM_TYPES
    fi

    # Set flag
    CRIT_BEACON_FLAG="N"
    SAVEIFS=$IFS
    IFS=$(echo -en "\n\b")
    if [ "$DEBUG" = "2" ] ; then
        debug2 "TYPES= $TYPES"
    fi

    # check for matching TYPES in alert
    for TYPE in $TYPES
        do
        ## added space to avoid the first line being seen as an alert
        ## this may need to be changed as follows:
        #  egrep -v "Current Watches" alert.txt > alert1.txt
        #  mv alert1.txt alert
        ## if grep -i  "$TYPE" alert.txt >> /tmp/wx/$COUNTY/a_tmp.txt; then      # Extract matching alerts

        if grep -i  "$TYPE " alert.txt >> /tmp/wx/$COUNTY/a_tmp.txt; then      # Extract matching alerts
            echo ".." >> /tmp/wx/$COUNTY/a_tmp.txt
            log "Get Wx Alert INFO for $COUNTY: $TYPE"
            # set critical beacon flag, if a match
            CRIT_BEACON_FLAG="Y"
        fi
    done
    IFS=$SAVEIFS

    # rename to pre_alert.txt
    mv /tmp/wx/$COUNTY/a_tmp.txt /tmp/wx/$COUNTY/pre_alert.txt

    #############################
    # create pre_conv_alert_summmary.txt (input to  post conversion)
    # insert weather header/trailer to full alert txt
    echo ". . Weather Alert Summary for county $COUNTY_NAME." > /tmp/wx/$COUNTY/a_tmp.txt
    cat alert_summary.txt >> /tmp/wx/$COUNTY/a_tmp.txt

    # copy tmp.txt over for post conversion
    cp /tmp/wx/$COUNTY/a_tmp.txt /tmp/wx/$COUNTY/pre_conv_alert_summary.txt

# Remove since we want to have the email text with the full alert
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
##    if [ "$FULL_ALERT" = "Y" ] ; then

      ALERT_DL=""
      CHKCNT=0
      # loop until get proper response, if after 5 attempts notify
      while [ -z "$ALERT_DL" ] ; do
 
        if [ $CHKCNT -gt 5 ] ; then
           log "ERROR: $URL is not responding for getWxAlert - ${COUNTY}"
           log "        Possible issue with nws.gov server or internet connection"
           ls -l /tmp/wx/$COUNTY/alert.info
           exit 1
        fi
 

        ##############################
        # Create the raw full Summary File
        $WGET -t 10 -w 10 --cache=off --random-wait -q $FULL_SUMMARY_URL -O - > /tmp/wx/$COUNTY/full_alert_info.txt
                                   # -t 10 retries, -w 10 sec between
                                   # turn cache off at server

        # place  data in variable to see if contains data
        ALERT_DL="`head -1  /tmp/wx/$COUNTY/alert.info`"
        let CHKCNT=$CHKCNT+1
        # delay before retrying
        sleep 4
      done

      # Create full summary from URL file
      # setup pre_converted full alert message 
      # cp  /tmp/wx/$COUNTY/pre_conv_alert_summary.txt  /tmp/wx/$COUNTY/pre_full_alert_info.txt
      cat /tmp/wx/$COUNTY/full_alert_info.txt |  sed '/<description>/,/<\/description/!d' | \
       sed 's/<description>//' |  \
       sed 's/<\/description>//' > /tmp/wx/$COUNTY/pre_full_alert_info.txt

#     fi
# end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    # additional debug info
    if [ "$DEBUG" = "2" ] ; then
        debug2 "**************************************************************"
        debug2 "**alert's don't match**"
        debug2 "=============================================================="
        debug2 "alert_summary file:"
        debug2 "   /home/irlp/audio/custom/wx/alert/${COUNTY}/alert_summary.txt"
        debug2 "=============================================================="
        debug2 "alert_prev_summary.txt file:"
        debug2 "    /home/irlp/audio/custom/wx/alert/${COUNTY}/alert_prev_summary.txt"
        debug2 "=============================================================="
        debug2 "pre_full_alert_info.txt file:"
        debug2 "    /tmp/wx/${COUNTY}/pre_full_alert_info.txt"
        debug2 "**************************************************************"
        debug2
    fi

    #################################################################
    #   Post Conversions
    # You must edit wxtext_conv.sed file to perform the post conversion.
    #################################################################

    #############################
    # pre_alert txt file to conv_alert.txt
    sed -f ${CUSTOM}/wxtext_conv.sed /tmp/wx/$COUNTY/pre_alert.txt > conv_alert.txt
    # conv_alert summary txt file to conv_alert_summary.txt 
    sed -f ${CUSTOM}/wxtext_conv.sed /tmp/wx/$COUNTY/pre_conv_alert_summary.txt > conv_alert_summary.txt
    /home/irlp/custom/dstartext

    # full alert text
    if [ "$FULL_ALERT" = "Y" ] ; then
      #############################
      # pre_full_alert_info file to conv 
      sed -f ${CUSTOM}/wxtext_conv.sed /tmp/wx/$COUNTY/pre_full_alert_info.txt > conv_full_alert_info.txt
    fi

    #################################################################
    # create the audio files
    #################################################################

    if [ $TEXT2SPEECH = "festival" ] ; then
        $TEXT2WAVE -F 8000 -scale 2.0 -otype wav conv_alert.txt -o conv_alert.wav
       #  $TEXT2WAVE -F 8000 -scale 2.0 -otype ulaw conv_alert_summary.txt -o COMPLETE_ALERT.ul   -eval "(voice_ked_diphone)"
#	  cd $WXALERT_SPOOLDIR/$COUNTY
	 # /usr/bin/espeak conv_alert.txt ALERT
	ffmpeg -i conv_alert.wav -y -ar 48000 ALERT.wav
	/usr/local/bin/wav2dvtool ALERT.wav KD8TUZ/ALERT KD8TUZ_B KD8TUZ_G "Weather Alert" ALERT.dvtool
	# full alert text
        if [ "$FULL_ALERT" = "Y" ] ; then
          $TEXT2WAVE -F 8000 -scale 1 -otype ulaw  conv_full_alert_info.txt -o FULLCOMPLETE_ALERT.ul   -eval "(voice_ked_diphone)"
        fi
    else
        $TEXT2WAVE -m text -f conv_alert.txt -p audio/encoding=ulaw,audio/sampling-rate=8000,audio/volume=$SWIFT_VOL -o ALERT.ul
        $TEXT2WAVE -m text -f conv_alert_summary.txt -p audio/encoding=ulaw,audio/sampling-rate=8000,audio/volume=$SWIFT_VOL -o COMPLETE_ALERT.ul
        # full alert text
        if [ "$FULL_ALERT" = "Y" ] ; then
          $TEXT2WAVE -m text -f conv_full_alert_info.txt -p audio/encoding=ulaw,audio/sampling-rate=8000,audio/volume=$SWIFT_VOL -o FULLCOMPLETE_ALERT.ul
        fi
    fi

   #clean up
   rm -f /tmp/wx/$COUNTY/a_tmp.txt

   # check if file exists and size gt zero, if not email/play weather (ref#1)
   if [ -s plist.txt ]; then
       # Email Alerts if $EMAIL defined.
       if [ ! -z "$EMAIL" ] ; then
           send_email
           echo -e "\nMail sent on $DATE" >> $MAILLOG
       fi
       # if WXALERT_BEACON is set to Yes, then place playWxAlertBg into 
       # background
       #
       # Beacon needs to be Y -or- (beacon C -and- Crit_Beacon flag yes)
       if [ "$DEBUG" = "2" ] ; then
           echo "WXALERT_BEACON = $WXALERT_BEACON, CRIT_BEACON_FLAG=$CRIT_BEACON_FLAG"
       fi
       if [ $WXALERT_BEACON = "Y" ] || ([ $WXALERT_BEACON = "C" ] && [ $CRIT_BEACON_FLAG = "Y" ]) ; then
           # first kill any left over process
           # get PID and kill it
           if [ -f $WXALERT_SPOOLDIR/$COUNTY/playWxAlertBg.pid ] ; then
               kill `cat < playWxAlertBg.pid`
           fi
           # if $LOCAL/no_wxalert_msg file is present, remove it to start
           # new weather alert msg
           if [ -f $NO_WXALERT_MSG ] ; then
               rm $NO_WXALERT_MSG
           fi
           # call playWxAlert and place in background
           log "Launching playWxAlertBg..."
           /usr/local/bin/remotecontrold kd8tuz_b link never ref039_C
	   sleep 10
	   $CUSTOM/playWxAlertBg $COUNTY  &
       fi
    fi # -s plist.txt
fi

exit 0

