#!/bin/bash
# @desc Bash client for Gismeteo weather forecast service
URL_CACHE="$HOME/.cache/gismeteo"
if [ "$#" -eq 0 ] && [ -r "$URL_CACHE" ]
then
    xargs curl -Ls < "$URL_CACHE" |xml sel -t -m '/document/GetHHForecastResult' -v '@cityName' -o ', ' -v '@country_name' -n -t -m '//HHForecast|//HHWeather' -v 'time' -o '      ' -v 't' -o '°C ' -v 'fullDesc' -n
    exit;
fi
ENDPOINT="http://c31b3236.services.gismeteo.ru/inform-service"
HASH=$(curl -sL "$ENDPOINT/register/?id=13371" |xml sel -t -v '/document/data/hash')
URL="$ENDPOINT/$HASH"
CITY="${1:-moscow}"
CITY_ID=$(curl -Ls "$URL/cities/?startsWith=$CITY&searchAll=0&lang=en" |xml sel -t -v '/document/data/LocationInfoFull[1]/id')
DATA_URL="$URL/forecast/?city=$CITY_ID&lang=en"
echo -n "$DATA_URL" > "$URL_CACHE"
curl -Ls "$DATA_URL"|xml sel -t -m '/document/GetHHForecastResult' -v '@cityName' -o ', ' -v '@country_name' -n -t -m '//HHForecast|//HHWeather' -v 'time' -o '      ' -v 't' -o '°C ' -v 'fullDesc' -n
