#/bin/bash
#

# load up configuration variables
if [ -f ${CUSTOM}/wx_scripts.conf ] ; then
    source /wx_scripts.conf
else
    echo "Missing ${CUSTOM}/wx_scripts.conf file, aborting..."
    exit 1
fi

# if Weather Underground _STN not an argument, then use what is defined in confi gure file.
if [ "$1" = "" ] ; then
    WX_UNDERGRND_STN=${WX_UNDERGRND_STN}
else
    WX_UNDERGRND_STN=$1
fi

# Various files and directories

# report/working dirs
UG_WX_DIR=/custom/wx/report_ug/${WX_UNDERGRND_STN}
UG_WX_PAR_DIR=/custom/WxRpt_ug
UG_WX_WRKING_DIR=/custom/WxRpt_ug/${WX_UNDERGRND_STN}

# make sure custom audio wx directory is created
if [ ! -d  $UG_WX_DIR ] ; then
    mkdir -p $UG_WX_DIR
fi

# make sure custom working wx directory is created
if [ ! -d  $UG_WX_WRKING_DIR ] ; then
    mkdir -p $UG_WX_WRKING_DIR
fi

# parsing_list.txt file lists weather items to capture
P_LIST=$UG_WX_PAR_DIR/parsing_list.txt

# weather underground web information.  ID value is important
# ID set from wx_scripts.conf
URL="http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=${WX_UNDERGRND_STN}"

# get date info for last update information
DATE=`date +%Y.%m.%d.\%H\%M`

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

# clean up first
rm -f $UG_WX_WRKING_DIR/var.src $UG_WX_DIR/pre_current_wx.txt $UG_WX_DIR/conv_current_wx.txt $UG_WX_DIR/working_current_wx.ul

# Get weather information
$WGET -q $URL -O - > ${UG_WX_DIR}/${WX_UNDERGRND_STN}.xml

# parse the list and extract the variables
for ITEM in `cat "$P_LIST"  | awk '{ print $1 }'`
  do
    VARIABLE=`grep -m 1 $ITEM ${UG_WX_DIR}/${WX_UNDERGRND_STN}.xml |  awk 'BEGIN { FS = "<" } { print $2 } ' | sed 's/>/ /g' | awk '{ print $1}'` 

    VALUE=`grep -m 1 $ITEM ${UG_WX_DIR}/${WX_UNDERGRND_STN}.xml | awk ' BEGIN { FS = "/" } { print $1 }'|  awk 'BEGIN { FS = ">" } { print $2 } ' | awk ' BEGIN { FS = "<" } { print $1 }'`

    echo "${VARIABLE}=\"${VALUE}\"" >> $UG_WX_WRKING_DIR/var.src

done 

# initialize the variables (import them)
source $UG_WX_WRKING_DIR/var.src

# Save current pressure and get current pressure trend for pressure info
cur_pressure_in=$pressure_in

if [ -f $UG_WX_WRKING_DIR/old_pressure.var ] ; then
    # get old "saved pressure_in" value, for pressure trends
    # this is created by crontab every 4 hours.
    source $UG_WX_WRKING_DIR/old_pressure.var

    # convert to integer (may change to *100)
    cur_pressure=`echo "${cur_pressure_in}*100" | bc`
    pressure=`echo "${pressure_in}*100" | bc`

    # convert to integer
    cur_pressure=${cur_pressure/\.*}
    pressure=${pressure/\.*}

    # see if pressure is rising or falling...
    if [ $cur_pressure -gt $pressure ]; then
        pressure_trend="rising"
    fi
    if [ $cur_pressure -lt $pressure ]; then
        pressure_trend="falling"
    fi
    if [ $cur_pressure -eq  $pressure ]; then
        pressure_trend="steady"
    fi
fi


# Build the wx report...
# Intro info and time recorded
echo ". . This is the ${CALL_SPACED} node weather report for $neighborhood ,  $city . " >> $UG_WX_DIR/pre_current_wx.txt
echo "$observation_time," >> $UG_WX_DIR/pre_current_wx.txt 

# Wind Info
if [ "$wind_string" = "Calm" ] ; then
    echo "the wind was calm." >> $UG_WX_DIR/pre_current_wx.txt
    ##echo "wind_string=$wind_string, wind_mph=$wind_mph, wind_gust_mph=$wind_gust_mph"
else
    case $wind_dir in
        NNE) wind_dir="north north east" ;;
        NE)  wind_dir="north east" ;;
        ENE) wind_dir="east north east" ;;

        ESE) wind_dir="east south east" ;;
        SE) wind_dir="south east" ;;
        SSE) wind_dir="south south east" ;;

        SSW) wind_dir="south south west" ;;
        SW)  wind_dir="south west" ;;
        WSW) wind_dir="west south west" ;;

        WNW) wind_dir="west north west" ;;
        NW)  wind_dir="north west" ;;
        NNW) wind_dir="north north west" ;;
    esac

    echo "the wind was blowing at a speed of ${wind_mph} miles per hour from ${wind_dir} with wind gusts up to ${wind_gust_mph} miles per hour.  " >> $UG_WX_DIR/pre_current_wx.txt

fi

# Temp and Dewpoint Info
echo "The temperature was ${temp_f} degrees with a dewpoint of ${dewpoint_f} degrees." >> $UG_WX_DIR/pre_current_wx.txt

# Windchill
if [ ${windchill_f} ] ; then
    echo "the wind chill was at  ${windchill_f} degrees." >> $UG_WX_DIR/pre_current_wx.txt
fi

# Pressure and pressure trend
echo "The pressure was ${pressure_trend} at ${cur_pressure_in} inches of mercury. " >> $UG_WX_DIR/pre_current_wx.txt

# Humidity info
echo "The relative humidity was $relative_humidity percent." >> $UG_WX_DIR/pre_current_wx.txt

# Finally, Precipatation Data
if [ "$precip_today_in" != "0.00" ] ; then
    echo "Total precipatation today was ${precip_today_in} inches, precipatation in the last hour was ${precip_1hr_in} inches." >> $UG_WX_DIR/pre_current_wx.txt
fi

# store current pressure

echo "# Last Updated: $DATE" > $UG_WX_WRKING_DIR/saved_pressure.var
echo "pressure_in=$cur_pressure_in" >> $UG_WX_WRKING_DIR/saved_pressure.var

# end of message
echo "end of message. ." >> $UG_WX_DIR/pre_current_wx.txt

#############################
#   Post Conversions
# You must edit wxtext_conv.sed file to perform the post conversion.
##############################
# alert txt file to conv_alert.txt
sed -f ${CUSTOM}/wxtext_conv.sed $UG_WX_DIR/pre_current_wx.txt > $UG_WX_DIR/conv_current_wx.txt

##############################
# create the audio file
if [ $TEXT2SPEECH = "festival" ] ; then
    $TEXT2WAVE -F 8000 -scale 0.5 -otype ulaw  $UG_WX_DIR/conv_current_wx.txt -o $UG_WX_DIR/working_current_wx.ul   -eval "(voice_ked_diphone)"
else
    $TEXT2WAVE -m text -f $UG_WX_DIR/conv_current_wx.txt -p audio/encoding=ulaw,audio/sampling-rate=8000,audio/volume=$SWIFT_VOL -o $UG_WX_DIR/working_current_wx.ul
fi

# Since it takes time to create the working_current_wx.ul, copy it over when finished.
 
cp $UG_WX_DIR/working_current_wx.ul $UG_WX_DIR/current_wx.ul

rm $UG_WX_DIR/working_current_wx.ul

# Notes:
# create a file that has the items that you want to capture and used
# for example:
##  value	equivalent in speech
#    temp_f  "temperature is"
#
# create a text file based upon the above file and store.  Use a for/loop
# to run through the text file.

