#!/bin/bash
#  

#Usage: firetext.sh [lat min.] [lat max.] [long min.] [long max.] [mobile no. 1]
#
# This script downloads the incident alert XML document from
# emergency.vic.gov.au, checks the location of each incident, and
# if it is within the area specified in latitude and longitude,
# sends relevent information about the incident to the mobile
# phone numbers specified (if any), as SMS messages. Also prints
# new alerts to stdout.
#
# Version: 1.0
# Author: The Free Thinker, 2020 ( gopher://aussies.space/1/%7efreet/ )
#
#TODO:
#-Multiple Phone Numbers

#Mobile Modem Device:
TTYDEV='/dev/ttyUSB1'

#Incidents XML Data URL:
URL='http://data.emergency.vic.gov.au/Show?pageId=getIncidentXML'

#Output to terminal:
messagedisplay ()
{
 echo "---  ---  ---  ---  ---  ---  ---  ---  ---

$last_updated_dt_str
$category1 , $incident_type , $category2
$incident_status
$name, $incident_location
Longitude: $LONG
Latitude: $LAT
Ref: $UTMREF

Status: $origin_status
Size: $incident_size
Resources: $RESOURCES
Incident No. $INCIDENT_NO
Event Code: $event_code

---  ---  ---  ---  ---  ---  ---  ---  ---
"
#Bell:
echo -e "\a"
}

#Output to SMS:
#Manual terminal command:
# picocom -b 9600 -f s -r /dev/ttyUSB1
messageoutput ()
{
#SMS Commands
stty -F "$TTYDEV" 9600 raw
#echo "\
chat -e \
'' AT \
OK\\r \
AT+CMGF=1 \
OK\\r \
AT+CMGS=\""$PHONENO1"\" \
'> ' \
\
"$category1 , $incident_type , $category2"\\n\
"$origin_status"\\n\
"$name, $incident_location"\\n\
"Ref: $UTMREF"\\n\"^z \
OK\\r \
 < "$TTYDEV" > "$TTYDEV"
#" > smschat.temp

#chat -t 15 -f smschat.temp -T "$PHONENO1" < "$TTYDEV" > "$TTYDEV"

}

#Check for incidents not yet processed
newincident ()
{
  local IFS='>'
  if read -d '<' TAG VALUE
  then
#   echo "TAG: $TAG"
#   echo "VALUE: $VALUE"
#The following commands use two regular expressions to extract the
#field with its data, then extract only the data from that string.
#The first, its command nested in the seccond, skips all data
#before the tag name, then ends with the final ".
#The second skips all data that isn't a number until it finds a
#sequence of numbers, beginning with a number, then outputs all
#sequential numbers after that.
#Latter part copied from sed example here:
#https://unix.stackexchange.com/questions/31476/extracting-a-regex-matched-with-sed-without-printing-the-surrounding-character/31479#31479

#Below line works on its own but not in a script for some reason...
#   UPDATETIME="`expr '\`expr "$TAG" : '.*\(last-updated-dt=\"[0-9]*\"\)'\`' : '.*[^0-9]\([0-9][0-9]*\).*'`"

   UPDATETIMETEMP=`expr "$TAG" : '.*\(last-updated-dt=\"[0-9]*\"\)'`
#Variable will be empty if not on an incident tag
   if [ -n '$UPDATETIMETEMP' ]
   then
    UPDATETIME=`expr "$UPDATETIMETEMP" : '.*[^0-9]\([0-9][0-9]*\).*'`
#   echo "UPDATETIME is: $UPDATETIME"
    if (( UPDATETIME > OLDLATESTTIME )) && (( GETINCIDENT == 0 ))
    then
#     LONG="`expr \`expr "$TAG" : '.*\(longitude=\"[.0-9]*\"\)'\` : '.*[^0-9]\([0-9]*[.][0-9]*\).*'`"
     LONG=`expr "$TAG" : '.*\(longitude=\"[.0-9]*\"\)'`
     LONG=`expr "$LONG" : '.*[^0-9]\([0-9]*[.][0-9]*\).*'`
#Remove "-" from latitude regex for northern hemisphere
#     LAT="`expr \`expr "$TAG" : '.*\(latitude=\"-[.0-9]*\"\)'\` : '.*[^0-9]\(-[0-9][.0-9]*\).*'`"
     LAT=`expr "$TAG" : '.*\(latitude=\"-[.0-9]*\"\)'`
     LAT=`expr "$LAT" : '.*[^0-9]\(-[0-9][.0-9]*\).*'`
#     RESOURCES="`expr \`expr "$TAG" : '.*\(resource-count=\"[0-9]*\"\)'\` : '.*[^0-9]\([0-9][0-9]*\).*'`"
     RESOURCES=`expr "$TAG" : '.*\(resource-count=\"[0-9]*\"\)'`
     RESOURCES=`expr "$RESOURCES" : '.*[^0-9]\([0-9][0-9]*\).*'`
#     INCIDENT_NO="`expr \`expr "$TAG" : '.*\(incident-no=\"[0-9]*\"\)'\` : '.*[^0-9]\([0-9][0-9]*\).*'`"
     INCIDENT_NO=`expr "$TAG" : '.*\(incident-no=\"[0-9]*\"\)'`
     INCIDENT_NO=`expr "$INCIDENT_NO" : '.*[^0-9]\([0-9][0-9]*\).*'`

#Check LAT and LONG here and set GETINCIDENT accordingly
     #Results are all negative when within range:
     LATUPPER=`echo "$LAT - $LATMAX" | bc`
     LATLOWER=`echo "$LATMIN - $LAT" | bc`
     LONGUPPER=`echo "$LONG - $LONGMAX" | bc`
     LONGLOWER=`echo "$LONGMIN - $LONG" | bc`
     
     if [ "${LATUPPER:0:1}" != "-" ] || \
        [ "${LATLOWER:0:1}" != "-" ] || \
        [ "${LONGUPPER:0:1}" != "-" ] || \
        [ "${LONGLOWER:0:1}" != "-" ] || \
        grep -xFq "$INCIDENT_NO" oldincidents.lst
     then
      GETINCIDENT=0
     else
      GETINCIDENT=1

#Fetch UTM/WGS84/GDA94 map ref:
      NCATRES="`wget -q --no-check-certificate -O - \"https://www.ngs.noaa.gov/api/ncat/llh?lat=$LAT&lon=$LONG&a=6378160.0&invf=298.25\"`"
      UTME=`expr "$NCATRES" : '.*"utmEasting": "\([0-9,.]*\)"' | tr -d ,`
      UTMN=`expr "$NCATRES" : '.*"utmNorthing": "\([0-9,.]*\)"' | tr -d ,`
      UTMREF="${UTME:1:3} ${UTMN:2:3}"
     fi

# Track the latest incident read. assume they're not in order:
     LATESTTIME=$LATESTTIME
     if (( LATESTTIME < UPDATETIME ))
     then
      LATESTTIME=$UPDATETIME
     fi
    fi
   fi
  else
   if (( GETINCIDENT == 1 ))
   then
    echo "ERROR: Unexpected End of XML File While Reading Incident Information"
    return 1
   else
#   Save timestamp of latest incident read:
    echo "$LATESTTIME" > "latesttime"
    return 10
   fi
  fi
 return 0
}


#Read incident info and format it in text
readincident ()
{
 
  case $TAG in

   "last-updated-dt-str")
     last_updated_dt_str=$VALUE
     ;;

   "origin-date-time-str")
     origin_date_time_str=$VALUE
     ;;

   "incident-status")
     incident_status=$VALUE
     ;;

   "incident-type")
     incident_type=$VALUE
     ;;

   "event-code")
     event_code=$VALUE
     ;;

   "incident-location")
     incident_location=$VALUE
     ;;

   "origin-status")
     origin_status=$VALUE
     ;;

   "incident-size")
     incident_size=$VALUE
     ;;

   "category2")
     category2=$VALUE
     ;;

   "category1")
     category1=$VALUE
     ;;

   "name")
     name=$VALUE
     messagedisplay
     [ "$PHONENO1" ] && messageoutput
     oldincidents="`tail -n 5 oldincidents.lst`"
     echo -e -n "$oldincidents\n$INCIDENT_NO" > oldincidents.lst
#early exit:
#     exit 0
     GETINCIDENT=0
     ;;
  esac
return 0
}

#MAIN

#Check at least four command line parameters for lat/long range
if [ $# -lt 4 ]
then
 echo "Invalid parameters - Need at least four parameters for Lat/Long range.
Usage: firetext.sh  LAT_MIN  LAT_MAX  LONG_MIN  LONG_MAX  [MOBILE_NUMBER]

Messages will be sent to MOBILE_NUMBER by SMS if number is supplied."
 exit 1
fi

#Check that XML data has changed (can only check the size - other headers aren't useful):
XMLSIZE="`wget --no-check-certificate --spider \"$URL\" 2>&1 | grep -o 'Length: [0-9]*'`"
if [ -f lastsize ] && [ "`cat lastsize`" == "$XMLSIZE" ]
then
# echo "no change"
 exit 0
else
 echo -n "$XMLSIZE" > lastsize
fi

#Command-Line Parameters:
LATMIN=$1
LATMAX=$2
LONGMIN=$3
LONGMAX=$4
PHONENO1=$5

GETINCIDENT=0
# Start latesttime at zero if it doesn't exist:
if [ ! -f latesttime ]
then
 echo "0" > "latesttime"
fi

LATESTTIME=`cat latesttime`
OLDLATESTTIME=$LATESTTIME

#Download the latest Incident XML file:
if ! wget -q --no-check-certificate -O "incident.xml" "$URL"
then
 echo -n "ERROR: Download of XML File Failed on: "
 date +"%Y-%m-%d %H:%M:%S"
 exit 1
fi

cat incident.xml | while newincident
do
 if (( GETINCIDENT > 0 ))
 then
  readincident
 fi
done

if (( GETINCIDENT == 0 ))
then
 exit 0
else
 echo "ERROR: Terminating before output of last message."
 exit 1
fi
