#!/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
# RWS discontinued jan 2020, so extract the synopsis section of AFD

#curl -L -o "$ORIGINAL" -s --compressed -m 300 --retry-max-time 1000 --retry 10 'http://www.weather.gov/data/BOX/AFDBOX'
# curl stopped working, see [curl] note on metar.sh
link='https://tgftp.nws.noaa.gov/data/raw/fx/fxus61.kbox.afd.box.txt'
wget -O "$ORIGINAL" -q --timeout=300 --tries=10 "$link"

PROCESSED=`mktemp`
perl -x /afs/sipb.mit.edu/user/kenta/zweather/lessunicode.pl "$ORIGINAL" | perl /afs/sipb.mit.edu/user/kenta/zweather/process-afd.pl > "$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 afdbox < "$PROCESSED"
else echo FAILED $0
fi
if [ -s "$ORIGINAL" ]
then cp "$ORIGINAL" /mit/kenta/Public/weather/afdbox
fi
rm "$ORIGINAL" "$PROCESSED"
