#!/bin/sh
#
# fetch weather and temperature using openweathermap API

CITY=${1:-aix-en-provence}
FORECAST=${FORECAST:-5}
UNITS=${UNITS:-metric}

URL="http://api.openweathermap.org/data/2.5/forecast/daily/?q=${CITY}&cnt=${FORECAST}&mode=xml&units=${UNITS}"

curl --silent "${URL}" | sed -e 's/.*time day="\([^" ]*\).*/\1,/p' \
  -e 's/^.*symbol.*name="\([^"]*\).*$/\1,/p' \
  -e 's/^.*temperature.*day="\([^"]*\).*$/\1°C/p' \
  -e 'd' \
  | paste -d "" - - - \
  | sed 's/^\(.*\),\(.*\),\(.*\)/\1,\3,\2/' \
  | column -t -s, -o" - "
