#!/bin/sh
# 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

# http://www.weather.gov/data/METAR/KBOS.1.txt is the website as linked from
# the XML current conditions http://w1.weather.gov/xml/current_obs/KBOS.xml
# although it currently automatically redirects to w1

# no longer works, see [curl] note below.
# curl -L -o "$ORIGINAL" -s --compressed -m 300 --retry-max-time 1000 --retry 10 'http://www.weather.gov/data/METAR/KBOS.1.txt'
# -L = follow redirects
# -s = --silent
# -m = --max-time <seconds>  (time of a single request)

# wget auto follows, compression is experimental.
wget -O "$ORIGINAL" -q --timeout=300 --tries=10 'https://tgftp.nws.noaa.gov/data/observations/metar/decoded/KBOS.TXT'

PROCESSED=`mktemp`
# processed originally made lowercase of raw metar
#lowercase is easier to read
# 2026-01-10 null bytes at the end
perl -plwe 'if(s/\x00//g){print STDERR "WARNING: Null characters removed"}' "$ORIGINAL" > "$PROCESSED"

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 metar < "$PROCESSED"
else echo FAILED $0
fi
if [ -s "$ORIGINAL" ]
then cp "$ORIGINAL" /mit/kenta/Public/weather/metar-kbos
fi
rm "$PROCESSED" "$ORIGINAL"

# [curl]
# as of approximately 11 AM 2023-06-16, https://www.weather.gov fails with
# curl -L --compressed -m 300 --retry-max-time 1000 --retry 10 -v http://www.weather.gov/data/BOX/ZFPBOX
# ... after redirect to https
# * NSS error -12286 (SSL_ERROR_NO_CYPHER_OVERLAP)
# * Cannot communicate securely with peer: no common encryption algorithm(s)."

# This is because, according to `sslscan www.weather.gov`
#  Supported Server Cipher(s):
# Preferred TLSv1.3  256 bits  TLS_AES_256_GCM_SHA384        Curve 25519 DHE 253
# Accepted  TLSv1.3  256 bits  TLS_CHACHA20_POLY1305_SHA256  Curve 25519 DHE 253
# Accepted  TLSv1.3  128 bits  TLS_AES_128_GCM_SHA256        Curve 25519 DHE 253
# Accepted  TLSv1.3  128 bits  TLS_AES_128_CCM_8_SHA256      Curve 25519 DHE 253
# Accepted  TLSv1.3  128 bits  TLS_AES_128_CCM_SHA256        Curve 25519 DHE 253
# Accepted  TLSv1.3  128 bits  TLS_AES_128_CCM_8_SHA256      Curve 25519 DHE 253
# Preferred TLSv1.2  256 bits  ECDHE-RSA-AES256-GCM-SHA384   Curve P-256 DHE 256
# Accepted  TLSv1.2  128 bits  ECDHE-RSA-AES128-GCM-SHA256   Curve P-256 DHE 256
# Accepted  TLSv1.2  256 bits  ECDHE-RSA-CHACHA20-POLY1305   Curve P-256 DHE 256
# Accepted  TLSv1.2  256 bits  ECDHE-RSA-AES256-SHA384       Curve P-256 DHE 256
# Accepted  TLSv1.2  128 bits  ECDHE-RSA-AES128-SHA256       Curve P-256 DHE 256
# Accepted  TLSv1.2  256 bits  ECDHE-RSA-AES256-SHA          Curve P-256 DHE 256
# Accepted  TLSv1.2  128 bits  ECDHE-RSA-AES128-SHA          Curve P-256 DHE 256

# That is, all elliptic curves all the time.

# The old version of nss that curl relies on on Fedora 20, NSS/3.18 according to curl -V or 3.19 according to rpm -q -a, is too old to support any of these.  I was unable to figure out what ciphers NSS/3.18 or 3.19 supports.

# However, wget on Fedora 20 which uses openssl not nss, does work.
