#!/bin/bash
########################################################################
#
#
# description:	This script will play the weather alert audio files
#		that get created by the getWxAlert script.
#
# history:
# Original script written from kc6hur, modified by w0anm
#
# $Id: playWxAlert 84 2012-07-20 02:35:35Z  $
########################################################################

# 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=$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} '`
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 playWxAlert <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 [ $ACTIVE_PLAY = "FALSE" ] ; then
    if [ -f ${LOCAL}/active ] ; then
       echo "${0##*/}: ERROR - Node is busy, must be idle to run."
       exit 1
    fi
fi

# Set if ACTIVE_PLAY is false, 
if  [ $ACTIVE_PLAY = "FALSE" ] ; then
     touch ${LOCAL}/active
fi

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

# if full summary selected, then play the complete summary, else
# play the truncated alert.
if [ "$FULL_ALERT" = "Y" ] ; then
   if [ -f ${CUSTOM}/custom_wavplay ] ; then
       ${CUSTOM}/custom_wavplay custom/wx/alert/$COUNTY/FULLCOMPLETE_ALERT &> /dev/null 2>&1
   else
       /usr/sbin/asterisk -rx "rpt localplay /custom/wx/alert/$COUNTY/FULLCOMPLETE_ALERT" &> /dev/null 2>&1
   fi
else
   if [ -f ${CUSTOM}/custom_wavplay ] ; then
       ${CUSTOM}/custom_wavplay custom/wx/alert/$COUNTY/COMPLETE_ALERT &> /dev/null 2>&1
   else
      # /usr/sbin/asterisk -rx "rpt localplay 27539 /custom/wx/alert/$COUNTY/COMPLETE_ALERT" &> /dev/null 2>&1
        voicetransmit KD8TUZ_B /custom/wx/alert/$COUNTY/ALERT.dvtool &> /dev/null 2>&1
   fi
fi

if [ $ACTIVE_PLAY = "FALSE" ] ; then
    rm ${LOCAL}/active
fi

exit 0

