#!/bin/bash
#####################################################################
# filename:	getWxRpt
#
# description:	Uses festival text-to-speech synthesiser to convert 
#		weather data to audio and save as a ul file.
#
# usage:	getWxRpt
#
# history:
# 2004/08/18	kc6hur	Original Release
#
# Modified by w0anm
# $Id: getWxRpt 22 2008-11-26 06:23:19Z 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 ICAO_STN not an argument, then use what is defined in configure file.
if [ "$1" = "" ] ; then
    ICAO_LIST=$ICAO_STN
else
    ICAO_LIST=$1
fi

WXTXT="$WXDIR/report/$ICAO.txt"
WXTXT_TMP="/tmp/$ICAO.txt"
WXAUD="$WXDIR/report/$ICAO.ul"
WXURL="http://weather.noaa.gov/pub/data/observations/metar/stations/KRFD.TXT"

if [ ! -d $WXDIR/report ] ; then
    mkdir -p $WXDIR/report
fi


#-----------------------------------------------------------------------
# Get the METAR weather string and convert it to a text file.

for ICAO in $ICAO_LIST ; do
    log "Get METAR for $ICAO"
    WXTXT="$WXDIR/report/$ICAO.txt"
    WXTXT_TMP="/tmp/$ICAO.txt"
    WXAUD="$WXDIR/report/$ICAO.ul"
    WXURL="http://weather.noaa.gov/pub/data/observations/metar/stations/KRFD.TXT"

   ## rm -f $WXTXT &>/dev/null

   /usr/bin/wget -q -O - $WXURL > /tmp/wx_metar
   /usr/bin/wget -q -O - $WXURL | /scripts/wx/metar2text > $WXTXT 2>> /scripts/wx/LOGFILE
cp $WXTXT /tmp/dontest

   # Fix any formating issues:
   sed -f  ${CUSTOM}/wxtext_conv.sed $WXTXT >  $WXTXT_TMP
   echo  "end of report. . . " >> $WXTXT_TMP
   cp $WXTXT_TMP $WXTXT

   # Convert text output into a ul file.
   #
   if [ -f $WXTXT ] ; then
      if [ $TEXT2SPEECH = "festival" ] ; then
          $TEXT2WAVE -scale 3.0 -eval /home/irlp/.festivalrc -otype ulaw -o $WXAUD.new $WXTXT
      fi
      if [ $TEXT2SPEECH = "cepstral" ] ; then
          $TEXT2WAVE -p audio/encoding=ulaw -m text -f $WXTXT -o $WXAUD.new 
      fi
      mv $WXAUD.new $WXAUD
      log "$WXAUD has been saved"
      ## rm -f $WXTXT &>/dev/null
   fi
done
exit 0

