#!/bin/bash
#
# v2.2
# v2.3 -             - Added UTC support
# v2.4 - 15 May 2003 - Complete rewrite to clean up audio playback routine
# v2.5 -             - 
# v2.6 -             -
# v2.7 -             - Cleanup of prompt packaging
# v2.8 - 02 Nov 2003 - Added support for Australian prompts
# v2.9 -             - 
# v3.0 - 21 Feb 2004 - Added ability to extend PTT keyup delay with
#                      the -longptt flag
# v3.1 - 04 Jul 2008 - Re-hash for CentOS and some cosmetic surgery..
#                      added logging function to script via my-functions
# v3.2 - 30 Jan 2009 - Added line to trigger CWIDent after timecall and
#                      rehashed code to reduce number of audio files
#                      required.
# v4.0 - 24 Apr 2009 - Rehash to speak time in 'to'/'past' fashion.
#                      This version is for cron calls as beacon - use
#                      old version for custom-decode calls. File name
#                      changed to 'time_cron' to separate files.
# v4.1 - 25 Apr 2009 - Added lines to trigger playing of welcome message
#                      on the 1/2 hour.  Sprooks node details and full
#                      system ident or whatever the text file contains.
#                      Spoken using 'swift' tts engine.
# v4.2 - 25 Apr 2009 - Reinstalled support for custom_decode calls.
# v4.3 - 21 Dec 2009 - Option to spit weather data after time call if a
#                      flag is present - /home/irlp/weathertime
# v5.0 - 25 Apr 2021   Rehash for Debian-10 and move temp report from
#                      bom_cron script to here and from BOM to
#                      weatherzone as BoM no longer likes automation.
# v5.1 - 03 Nov 2021   Weathezone site rehashed so temp download no longer
#                      works from there either..  Abandoned routine...
# v6.0 - 01 Jan 2022   Well looky there.. BoM has free pages on FTP..
#
#
#
# NOTE:- Temperature routine needs to be converted to FTP if BoM not
#        happy with automation and chucks the poops.!
#        have to use IDN60058.txt for obs and alter fields
#
#
###################################################################
#
#  Script Variables
#
summary_file="IDN60058.txt"
BOM="ftp://ftp.bom.gov.au/anon/gen/fwo"
Webpage7="www.bom.gov.au/nsw/observations/nswall.shtml"
ftppage="IDN60058.txt"
#
###################################################################
#
# Make sure we are user repeater!!!
   if [ "`/usr/bin/whoami`" != "repeater" ] ; then
     echo "This program must be run as user REPEATER!"
     exit 1
   fi
# Make sure we have sourced the environment file
   if [ "$RUN_ENV" != "TRUE" ] ; then
      . /home/irlp/custom/environment
   fi

#
#############################################################################
#
### Logging ..
#
#
writelog() {
  MESSAGE="`date '+%b %d %Y %T'` "$@
  if [ -n "$LOGFILE" ]; then
    echo $MESSAGE >> $LOGFILE
  fi
}
#
temperature() {
  rm -f $LOCAL/temp* > /dev/null 2>&1
  lynx -dump -dont_wrap_pre $Webpage7 > $LOCAL/temp_dump
  Location="Cessnock Airport"
  line=`grep "$Location" $LOCAL/temp_dump`
  echo $line > /home/irlp/weathertime
  temp=`echo $line | awk '{print $4}'`
  updated=`echo $line | awk '{print $3}'`
  HOUR=`echo ${updated:3:2}`
  MIN=`echo ${updated:6:2}`
  process_time
  echo ;echo " BOM Check Data"
  echo "----------------------------"
  echo "Hour             - "$HOUR
  echo "Minute           - "$MIN
  echo "Temp             - "$temp     # TEST Diagnostic Print
  echo "----------------------------"
  echo
  if [ "$MINFILE" = "00" ] ; then
     text="At "$HOUR" O'Clock, the temperature was $temp degrees."
  else
     if [ "$MINFILE" = "half" ] ; then
        text="At half past "$HOUR", the temperature was $temp degrees."
     else
        text="At "$MIN" "$HAND" "$HOUR", the temperature was $temp degrees."
     fi
  fi
  echo $text
  writelog $text
  if [ ! -f $LOCAL/active ] ; then
    $BIN/key
    /usr/local/bin/swift -m text "$text"
    $BIN/unkey
  fi
  rm -f $LOCAL/temp*

}

process_time() {
  # Convert 24h time back to 12h time
  if [ "$HOUR" -le "11" ] ; then
    AMPM=am
  else
    AMPM=pm
  fi
  #
  if [ "$MIN" -le "30" ] ; then
    HAND="past"
  else
    if [ "$MIN" -gt "30" ] ; then
      HAND="to"
      HOUR=`expr $HOUR + 1`
      MIN=`expr 60 - $MIN`
    fi
  fi
  #
  case $HOUR in
  0)              HOUR=12 ;;
  00)             HOUR=12 ;;
  0[1-9])         HOUR=`echo $HOUR | tr -d "0"` ;;
  1[3-9]|2[0-3])  HOUR=`expr $HOUR - 12` ;;
  24)             HOUR=12 ;;
  esac

  case $MIN in
  0)               MINFILE="00" ;;          # says o'clock
  00)              MINFILE="00" ;;
  15)              MINFILE="quarter" ;;  # Quarter to/past
  30)              MINFILE="half" ;;     # Yup - half past
  [1-9])           MINFILE="$MIN" ;;
  0[1-9])          MINFILE="`echo $MIN|cut -c2-2`" ;;
  1[0-9]|20)       MINFILE="$MIN" ;;
  2[1-9])          MINFILE="20 `echo $MIN|cut -c2-2`" ;;
  40|50)           MINFILE="$MIN" ;;
  3[1-9])          MINFILE="30 `echo $MIN|cut -c2-2`" ;;
  4[1-9])          MINFILE="40 `echo $MIN|cut -c2-2`" ;;
  5[1-9])          MINFILE="50 `echo $MIN|cut -c2-2`" ;;
  esac
}


#############################################################################
#
#

  #################################
  #  Process command line options
  #  ***Removed command line options in this version***
  #  locked to 12hr, local time, no ampm, no date
  #
  DATENOW=`/bin/date '+%H %M %S %p'`
  HOUR=`echo $DATENOW | awk '{ print $1 }'`
  MIN=`echo $DATENOW | awk '{ print $2 }'`

  process_time

  echo ;echo "  Check Data"
  echo "----------------------------"
  echo "Hour             - "$HOUR
  echo "Minute           - "$MIN
  echo "Hand             - "$HAND     # TEST Diagnostic Print
  echo "MinFile          - "$MINFILE  # when run from the cmd line
  echo "AmPm             - "$AMPM
  echo "----------------------------"
  echo

  if [ -f $LOCAL/enable ] && [ ! -f $LOCAL/active ] ; then  # enabled and free

    if [ "$MINFILE" = "00" ] ; then
       echo "The time is "$HOUR" O'Clock"
       $CUSTOM/wavplay_nice_custom the_time_is $HOUR $MINFILE
    else
       echo "The time is "$MIN" "$HAND" "$HOUR"."
       $CUSTOM/wavplay_nice_custom the_time_is $MINFILE $HAND $HOUR
    fi

    if [ "$#" = "0" ] ; then            # No command line params = time only
       writelog "Time beacon transmitted."
     else
       writelog "Requested timecall completed."
       exit 0                           # Not cron so exit here
    fi
    temperature
    touch $LOCAL/id_trigger             # send CW Ident
    sleep 7                             # wait for CW-ID to go out

  fi
  echo " "
  exit 0
