#!/bin/sh

report="$HOME/.local/share/weatherreport"
timed=$(date | awk '{print $6}')
timet=$(date | awk '{print $5}' | awk -F ":" '{print $1}')

getforecast() { curl -sf "wttr.in?format="%C:+%t"" > "${XDG_DATA_HOME:-$HOME/.local/share}/weatherreport" || exit 1 ;}

[ -z "${XDG_DATA_HOME:-$HOME/.local/share}/weatherreport" ] & rm "${XDG_DATA_HOME:-$HOME/.local/share}/weatherreport"

[ "$(stat -c %y "${XDG_DATA_HOME:-$HOME/.local/share}/weatherreport" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] || getforecast

condition=$(cat $report | awk -F ":" '{print $1}' | sed 's/ //g')
temp=$(cat $report | awk  -F ":" '{print $2}' | sed 's/ //g;s/+//g')

case "$condition" in
    Sunny|Clear)
    if [ "$timed" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Partlycloudy)
    if [ "$time" = "AM" ]
    then
        icon="杖"
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon="杖"
        fi ;;
    Cloudy)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Verycloudy)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Fog)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Lightshowers)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Heavyshowers)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Lightsleet)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Lightsleetshowers)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Lightrain|Lightrainshower)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Heavyrain)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Thunderyshowers|Thunderyheavyrain)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Lightsnow|Lightsnowshowers|Heavysnow|Heavysnowshowers)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
   Thunderysnowshowers)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
        fi ;;
    Thunderstorm)
    if [ "$time" = "AM" ]
    then
        icon=""
    elif [ "$timed" = "PM" ] && [ "$timet" -ge "07" ]
    then
        icon=""
    else
        icon=""
    fi ;;
esac

printf "%s \n" "$icon $temp"
