#!/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 46 2009-04-10 04:27:25Z  $
######################################################################

# 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://tgftp.nws.noaa.gov/data/forecasts/zone/${STATE}/${LC_ZONE}.txt"

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

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

echo Downloading....
cd $WXFOR_SPOOLDIR/$ZONE_FOR
wget  $URL 
echo Done....
mv ohz070.txt $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_TMP2

# add heater and cat file
echo "Weather forecast for $ZONE..." > $WX_TXT_TMP
cat  $WX_TXT_TMP  $WX_TXT_TMP2 > $WX_TXT

# clean up temp files
rm $WX_TXT_TMP $WX_TXT_TMP2

# convert

if [ $TEXT2SPEECH = "festival" ] ; then
  # $TEXT2WAVE -F 8000 -scale 0.5 -otype ulaw  $WX_TXT -o forecast.ul   -eval "(voice_ked_diphone)"
   $TEXT2WAVE  -otype wav -o forecast.wav $WXTXT
         ffmpeg -i forecast.wav -y -ar 48000 forecast1.wav
        /usr/local/bin/wav2dvtool forecast1.wav KD8TUZ_REPORT KD8TUZ_B KD8TUZ_G "Hamilton Forecast Report" forecast.dvtool
fi

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

exit 0

# EOF
