#!/bin/bash
# call randomsleep.sh beforehand to avoid overloading the server,
# but do not call it from this script itself to avoid editing this script while it is running.
ORIGINAL=`mktemp`
# coral CDN cannot be used because it defaults to a 12 hour expiry
# was 'http://www.weather.gov/data/BOX/RWSBOX' until July 2012
# http://www.srh.noaa.gov/data/BOX/RWSBOX stopped updating on Sept 2 2014
# weather.gov 3xx redirects to http://w1.weather.gov/data/box/rwsbox
curl -L -o "$ORIGINAL" -s --compressed -m 300 --retry-max-time 1000 --retry 10 'http://www.weather.gov/data/BOX/RWSBOX'
PROCESSED=`mktemp`
perl -nlwe 'last if /^\$\$/;print ' "$ORIGINAL" > "$PROCESSED"
#as of 2016, no longer need to convert all caps to lowercase
if [ -s "$PROCESSED" ] && perl /afs/sipb.mit.edu/user/kenta/zweather/sanity-check.pl "$PROCESSED"
then zwrite -c message -q -n -d -l -O AUTO -s $0 -i weather < "$PROCESSED"
    zwrite -c weather -q -n -d -l -O AUTO -s $0 -i rwsbox < "$PROCESSED"
else echo FAILED $0
fi
if [ -s "$ORIGINAL" ]
then cp "$ORIGINAL" /mit/kenta/Public/weather/rwsbox
fi
rm "$ORIGINAL" "$PROCESSED"
