#!/bin/sh
#
#####################################################################
# filename:     getWxFor
#
# description:  Uses festival text-to-speech synthesiser to convert 
#               weather data to audio and save as a ul file.
#
# usage:        getWxFor
#
# 2006/06/20    w0anm  Original Release
#
# $Id: getWxFor 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 ZONE  not an argument, then use what is defined in configure file.
if [ "$1" = "" ] ; then
    ZONE_FOR=$ZONE
else
    ZONE_FOR=$1
fi
if [ ! -d $WXFOR_SPOOLDIR/$ZONE_FOR ] ; then
    mkdir -p $WXFOR_SPOOLDIR/$ZONE_FOR
fi

# Local noaa forecast text link
# setup ZONE_FOR variable to convert to lower case and extract state
LC_ZONE=`echo $ZONE_FOR | tr 'A-Z' 'a-z'`
STATE=`echo $LC_ZONE | awk 'BEGIN { FS="z" } { print $1} '`

# set URL for wget.  This is the url where the forcasts originate.
URL="http://weather.noaa.gov/pub/data/forecasts/zone/${STATE}/${LC_ZONE}.txt"

WX_TXT="wx_forecast.txt"
WX_TXT_TMP="wx_forecast.tmp"

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

cd $WXFOR_SPOOLDIR/$ZONE_FOR
$WGET -q $URL -O - > $WX_TXT_TMP

# delete the first 10 lines before converting
TOTAL_LINE=`wc $WX_TXT_TMP | awk '{print $1}'`
LINE_DISP=`expr $TOTAL_LINE - 10`

# sed -f ${CUSTOM}/wxtext_conv.sed $WX_TXT_TMP > $WX_TXT

tail -${LINE_DISP} $WX_TXT_TMP | sed "s/\./\. /g ; s/0S/0's/g ; s/\$\$\$/. end of message . . . /g"  | sed -f ${CUSTOM}/wxtext_conv.sed >  $WX_TXT


# Do standard conversion..
# first convert to lower case
cat $WX_TXT | tr 'A-Z' 'a-z' > $WX_TXT_TMP 

sed -f ${CUSTOM}/wxtext_conv.sed $WX_TXT_TMP > $WX_TXT

# remove temp
rm $WX_TXT_TMP

# convert

if [ $TEXT2SPEECH = "festival" ] ; then
   $TEXT2WAVE -F 8000 -scale 3.5 -otype ulaw  $WX_TXT -o forecast.ul
fi

if [ $TEXT2SPEECH = "cepstral" ] ; then
   $TEXT2WAVE -p audio/encoding=ulaw  -m text -f $WX_TXT -o forecast.ul
fi

exit 0

# EOF
