#!/bin/bash

jweather=$( curl --silent wttr.in/?format=j1 )
#jweather=$( cat ~/weather.json )

current_f=$( jq -r '.current_condition.[].FeelsLikeF' <<< ${jweather} )
high_f=$( jq -r '.weather.[0].maxtempF' <<< ${jweather} )
low_f=$( jq -r '.weather.[0].mintempF' <<< ${jweather} )
weather_desc=$( jq -r '.current_condition.[].weatherDesc.[].value' <<< ${jweather} )
hourly_forecast=""
daily_forecast=""

current_hour=$( date +'%H' )
echo $current_hour

for h in {0..7};do
  check_hour=$( date -d $( jq -r --argjson h ${h} '.weather.[0].hourly.[$h].time' <<< ${jweather} ) +'%H' )
  if [[ ${check_hour#0} -ge ${current_hour#0} ]];then
    echo "future"
    hourly_forecast="${hourly_forecast}\n${check_hour}00: "$( jq -r --argjson h ${h} '.weather.[0].hourly.[$h].weatherDesc.[].value' <<< ${jweather} )", "$( jq -r --argjson h ${h} '.weather.[0].hourly.[$h].FeelsLikeF' <<< ${jweather} )"°F"
  fi
done

for d in {1..2};do
  echo $d
  daily_forecast="${daily_forecast}\n"$( date -d $( jq -r --argjson d ${d} '.weather.[$d].date' <<< ${jweather} ) +'%a' )": "$( jq -r --argjson d ${d} '.weather.[$d].hourly.[4].weatherDesc.[].value' <<< ${jweather} )" ("$( jq -r --argjson d ${d} '.weather.[$d].maxtempF' <<< ${jweather} )"°F/"$( jq -r --argjson d ${d} '.weather.[$d].mintempF' <<< ${jweather} )"°F)"
done

hourly_forecast=$( echo -e $hourly_forecast | sed '/^[[:space:]]*$/d' )
daily_forecast=$( echo -e $daily_forecast ) # | sed '/^[[:space:]]*$/d' )

echo $daily_forecast

notify-send "${weather_desc}, ${current_f}°F (${high_f}°F/${low_f}°F)
${hourly_forecast}
${daily_forecast}"
