#!/bin/sh
#
# get_skywarn - Used to parse hazardous weather outlook web page for
# Skywarn activation.  Designed to be run from crond once an hour.
#
#     (c) 2006 Timothy Miller WB0RXX -- Released under GPL v2
#
# Modified by W0ANM
# $Id: getWxSkywarn 79 2011-04-22 18:19:55Z  $
######################################################################
# 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

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

# Check Arguments...
# has only one argument, county info
if [ "$#" = "1" ] ; then
   COUNTY=$1
   COUNTY_NAME=$1
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

#---------------------------- Command variables ------------------------
CP="/bin/cp"                                    # Location of binaries
DATE="/bin/date"                                #
DIFF="/usr/bin/diff"                            #
GREP="/bin/grep"                                #
HEAD="/usr/bin/head"                            #
LYNX="/usr/bin/lynx"                            #
# MAIL="/bin/mail"                              #
MAIL="/home/irlp/custom/SendMail/SendMail.pl"            # SendMail script
SED="/bin/sed"                                  #
TAIL="/usr/bin/tail"                            #
TR="/usr/bin/tr"                                #

#---------------------------- User variables ----------------------------
MAILDIR="/tmp/$ZONE"	                # Mail log file
TMPDIR="/home/irlp/audio/custom/wx/skywarn/$ZONE"	# Temp directory
WXSKY_SPOOLDIR=$TMPDIR				# Skywarn spooldir
COUNTY="MNC053"					# NWS county 

#--------------------------- Static variables ---------------------------

NOTNEEDED="WILL NOT BE NEEDED"			# Spotters not needed string #1
NOTANTICIPATED="IS NOT ANTICIPATED"		# Spotters not needed string #2
HWO_TXT="$TMPDIR/hwo.txt"			# Hazardous wx outlook outfile
SPOTTER=".SPOTTER INFORMATION"			# Spotter info string
SPOTTER_OLD="$WXSKY_SPOOLDIR/spotter.old"		# Old spotter outfile
SPOTTER_TXT="$WXSKY_SPOOLDIR/spotter.txt"		# Spotter outfile
URL="http://www.crh.noaa.gov/showsigwx.php?warnzone=$ZONE&warncounty=$COUNTY&product1=Hazardous+Weather+Outlook"

#--------------------------- Functions       ---------------------------
function send_email () {
for EADDR in `echo $EMAIL | sed 's/,/ /g'` ; do
     $MAIL -s "SkyWarn for $COUNTY_NAME" $EADDR < $SPOTTER_TXT
done
}

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

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

# check if directories are present, if not create.
if [ ! -d $WXSKY_SPOOLDIR ] ; then
    mkdir -p $WXSKY_SPOOLDIR
fi
if [ ! -d $MAILDIR ] ; then
    mkdir -p $MAILDIR
fi

$LYNX -dump -nolist $URL > $HWO_TXT		# Fetch NWS data

$GREP -i -A 6 "^$SPOTTER" $HWO_TXT | \
$TR '\n' ' ' | $SED 's/\.\.\./*/; s/\$\$/\*\*/g' | \
$TR '*' '\n' | $HEAD -2 | $TAIL -1 > $SPOTTER_TXT

if ! $DIFF -q $SPOTTER_OLD $SPOTTER_TXT &> /dev/null; then
  $CP $SPOTTER_TXT $SPOTTER_OLD
  if ( [ -s $SPOTTER_TXT ] && \
     $GREP -v "$NOTNEEDED" $SPOTTER_TXT &>/dev/null && \
     $GREP -v "$NOTANTICIPATED" $SPOTTER_TXT &>/dev/null ); then
        if [ ! -z "$EMAIL" ] ; then
            send_email
	    log "Spotter mail sent for $ZONE, $COUNTY"
        fi
	# create the audio file
        if [ $TEXT2SPEECH = "festival" ] ; then
            $TEXT2WAVE -F 8000 -scale 3.5 -otype ulaw $SPOTTER_TXT -o $TMPDIR/spotter.ul  
       else
            $TEXT2WAVE -f $SPOTTER_TXT -p audio/encoding=ulaw,audio/sampling-rate=8000,audio/volume=$SWIFT_VOL -o $TMPDIR/spotter.ul
       fi

  else
       if [ $TEXT2SPEECH = "festival" ] ; then
            $TEXT2WAVE -F 8000 -scale 3.5 -otype ulaw $SPOTTER_TXT -o $TMPDIR/spotter.ul
       else
            $TEXT2WAVE -m text "Spotter Activation is not needed at this time.." -p audio/encoding=ulaw,audio/sampling-rate=8000,audio/volume=$SWIFT_VOL -o $TMPDIR/spotter.ul
       fi
  fi
fi
