#! /bin/bash

# SPXX0082 - Valencia
# SPXX0021 - Castellón
# SPXX0230 - Teruel
# SPXX0118 - Reus
# SPXX0117 - Requena
# SPXX0061 - Mallorca

if [[ -e /tmp/tiempo ]]; then
	rm /tmp/tiempo
fi
while [ 1 ]; do
	if [[ $1 == "" ]]; then
		touch /tmp/tiempo
		for i in SPXX0082 SPXX0021 SPXX0230 SPXX0118 SPXX0117 SPXX0061; do
			curl --silent "http://weather.yahooapis.com/forecastrss?p=$i&u=c" | awk -F '- '  '
				/<title>/ { sub("</title>", "", $2) && l=$2 }
				/<b>Forecast/ { getline; gsub("<.*", "", $2); printf("%s: %s\n", l, $2); exit }' |
				sed 's/Castellon/Castellón/' >> /tmp/tiempo
		done
		exit
	else
		curl --silent "http://weather.yahooapis.com/forecastrss?p=$1&u=c" | awk -F '- '  '
		/<title>/ { sub("</title>", "", $2) && l=$2 }
		/<b>Forecast/ { getline; gsub("<.*", "", $2); printf("%s: %s\n", l, $2); exit }' > /tmp/tiempo
	fi
	# sleep 3600 # cada hora.
done
