#!/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 18 2008-04-02 21:17:04Z kovacs $
########################################################################
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

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

# 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 $WXALERT_SPOOLDIR/$ZONE_ALERT ] ; then
    # msg to log file.
    log "ZONE directory $ZONE_ALERT is not present, aborting..." 
    exit 1
fi

if [ -f ${CUSTOM}/custom_wavplayddd ] ; then
    ${CUSTOM}/custom_wavplay custom/wx/alert/$ZONE_ALERT/COMPLETE_ALERT &> /dev/null 2>&1
else
     sudo /usr/sbin/asterisk -rx "rpt localplay 2090 /audio/wx/alert/$ZONE_ALERT/COMPLETE_ALERT" 
fi

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

exit 0

