#!/bin/bash
#
# Script to rotate Allstar Voice IDs, by K0KN 9/2012, last updated 3-8-2013
#
# Usage - Place numbered ID files in the IDBASE folder in sequential 
#         order. For example, id1.ulaw, id2.ulaw, id3.ulaw and so forth
#         up to the IDMAX number specified. If 8 files are used, for
#         example, set IDMAX to 8.
#
#         ID is the file specified in RPT.conf for your voice ID. 
#

# 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

# Read current ID number from file
IDTOUSE=$(sed -n ""$RAN_ID"p" "$CUSTOM"/beacon_no)
IDMAX=$(ls ${WXALERT_SPOOLDIR}/${COUNTY}/conv_beacon* | wc -l)

while [ $IDMAX != $IDTOUSE ]
do
CUSTOM=${WXALERT_SPOOLDIR}/${COUNTY}
IDBASE=${WXALERT_SPOOLDIR}/${COUNTY}/conv_beacon              # base file names
#IDMAX=$(ls ${WXALERT_SPOOLDIR}/${COUNTY}/conv_beacon* | wc -l)                                 # max ID number

if [ ! -f $CUSTOM/beacon_no ] ; then
        echo "1" >> $CUSTOM/beacon_no
else
echo 
fi

# Read current ID number from file
IDTOUSE=$(sed -n ""$RAN_ID"p" "$CUSTOM"/beacon_no)

# Increment Variable
IDTOUSE=$(( $IDTOUSE + 1 ))

echo $IDTOUSE > "${WXALERT_SPOOLDIR}/${COUNTY}"/beacon_no
/usr/local/bin/texttransmit KD8TUZ_B -file ${WXALERT_SPOOLDIR}/${COUNTY}/conv_beacon${IDTOUSE}
sleep 25

done

   IDTOUSE=0
   echo $IDTOUSE > "${WXALERT_SPOOLDIR}/${COUNTY}"/beacon_no

exit 0

