#!/bin/bash

tempfile="/tmp/weather.$$"
# shellcheck disable=SC2064
trap "rm -f $tempfile" $(seq 0 15)

function process_url
{
    url="$1"

    curl -s "$url" \
        | from-table --file /dev/stdin --strip-data --quiet \
        | tr -d '\015' \
        | grep -v '^$' \
        | (printf '|Day|Decription|Temperature|Chance of precipitation|Wind speed|Humidity\n'; cat) \
        | sed 's/^|//' \
        | tr -d '[\200-\377]' \
        | to-table2 -d > "$tempfile"

    links2 -dump -width 120 "$tempfile" \
        | grep -v -- '----------' \
        | sed -e 's/^[ |]*//' -e 's/[ |]*$//' -e 's/^/    /'

}

# echo "Long Beach:"
# process_url 'https://weather.com/weather/tenday/l/9755a6fa3ce6d1812c8f3184eb57e79132d813351fe1cbec5644baf15fc28c51'

echo "Laguna Niguel:"
process_url 'https://weather.com/weather/tenday/l/USCA0575:1:US'

# echo "Aliso Viejo:"
# process_url 'https://weather.com/weather/tenday/l/5c81899e49f3e33289d3e85e638c2c4b4dfddcb2b3f67b3e6beeec6818e66937'