#!/bin/bash
######################################################################################################
#                                                                                                    #
#  Animated Flip Clock with Weather forecast on basis of openweathermap api. Shows 4 day forecast.   #
#  See https://openweathermap.org/faq for API key.                                                   #
#  Depends on curl.                                                                                  #
#                                                                                                    #
#  Scripted by Koentje   (remon@cobrasoft.nl)                                                        #                                                                                                                               #
#                                                                                      version 1.2   #
######################################################################################################

source "./settings.ini"

# Check if JQ is installed
if [ "$(which jq)" = "" ]; then
  echo -e "\n\${voffset 220}\${font :bold:size=12}  $(basename $0): \${color ff4400} JQ not installed !\${color}\n\n  Run 'sudo apt install jq'"
  exit
fi
# Check if curl is installed
if [ "$(which curl)" = "" ]; then
  echo -e "\n\${voffset 220}\${font :bold:size=12}  $(basename $0): \${color ff4400} CURL not installed !\${color}\n\n  Run 'sudo apt install curl'"
  exit
fi


# Get json files
  lang=${loc%%_*}
  curl -s "api.openweathermap.org/data/2.5/forecast?APPID=$apikey&id=$cityid&cnt=5&units=$units&lang=$lang" -o ./json/forecast.json
  curl -s "api.openweathermap.org/data/2.5/weather?APPID=$apikey&id=$cityid&cnt=5&units=$units&lang=$lang" -o ./json/weather.json


# Check JSON files for correct API key
  akf=$(cat ./json/forecast.json | awk -F'\"message\": \"' '{print $2}' | awk -F'.' '{print $1}')
  if [ "$akf" = "Invalid API key" ]; then
    echo -e "\${voffset 250}\${font Ubuntu:bold:size=12}\${goto 20}$akf\n\n\${goto 20}Please see https://openweathermap.org/faq"
    exit
  fi
  akw=$(cat ./json/weather.json | awk -F'\"message\": \"' '{print $2}' | awk -F'.' '{print $1}')
  if [ "$akw" = "Invalid API key" ]; then
    echo -e "\${voffset 250}\${font Ubuntu:bold:size=12}\${goto 20}$akw\n\n\${goto 20}Please see https://openweathermap.org/faq"
    exit
  fi


# Show date, city name and weather description
  cityname=$(jq -r ".name" ./json/weather.json)
  if [ "$cityupper" = "yes" ]; then
    cityname=${cityname^^}
  fi
  if [ "$dupper" = "yes" ]; then
    dformat=$(echo "$dformat" | sed s'/%/%^/'g)
    echo "$dformat"
  fi
  mydate=$(eval "date '+$dformat'")
  descript=$(jq -r .weather[0].description ./json/weather.json | sed 's|\<.|\U&|g')
  if [ "$desupper" = "yes" ]; then
    descript=${descript^^}
  fi
  maintemp=$(jq -r .main.temp ./json/weather.json | awk '{print int($1+0.5)}') #'
  echo "\${goto 24}\${voffset $dvoffset}\${font $dfontname:size=$dfontsize}\${color $dcolor}$mydate"
  echo "\${goto 24}\${voffset $cvoffset}\${font $cfontname:size=$cfontsize}\${color $ccolor}$cityname"
  echo "\${goto 24}\${voffset $desvoffset}\${font $desfontname:size=$desfontsize}\${color $descolor}$descript\${font}"


# Main pressure, humidity, windspeed
  pressure=$(jq -r .main.pressure ./json/weather.json | awk '{print int($1+0.5)}') #'
  humidity=$(jq -r .main.humidity ./json/weather.json | awk '{print int($1+0.5)}') #'
  windspeed=$(jq -r .wind.speed ./json/weather.json | awk '{print int($1+0.5)}')   #'
  if [ "$units" = "default" ]; then
    spd="m/s"
  elif [ "$units" = "metric" ]; then
    spd="m/s"
  elif [ "$units" = "imperial" ]; then
    spd="mi/h"
  fi
# echo "\${goto 22}\${voffset 10}\${font :size=8}$pressure hPa,  $windspeed $spd,  $humidity %"


# Show main, min and max temps
  mainmin=$(jq ".list[0].main.temp_min" ./json/forecast.json | awk '{print int($1+0.5)}') #'
  mainmax=$(jq ".list[0].main.temp_max" ./json/forecast.json | awk '{print int($1+0.5)}') #'
  echo "\${alignr 30}\${voffset $ttvoffset}\${font $ttfontname:size=$ttfontsize}\${color $ttfontcol}$maintemp°"
  echo "\${alignr 30}\${voffset $mmvoffset}\${font $mmfontname:size=$mmfontsize}\${color $mmfontcol}$mainmax° / $mainmin°"


# Todays weather icon
  todaysicon=$(jq .weather[0].icon ./json/weather.json)
  todaysicon=${todaysicon:1:-1}
  cp -f ./weatherimages/$todaysicon.png ./weatherimages/weather-1.png
# Moved the below line to the flipclock.lua because it needs to be on top of the flipclock!
# echo "\${lua conky_image ./weatherimages/weather-1.png 70 70 250 150}"


# Show 4 day names
  if [ "$wdformat" = "long" ]; then
    wdf="A"
  else 
    wdf="a"
  fi
  if [ "$wdupper" = "yes" ]; then
    wdu="^"
  else 
    wdu=""
  fi
  day1=$(LANG=$loc LC_TIME=$loc date -d +1day +%$wdu$wdf)
  day2=$(LANG=$loc LC_TIME=$loc date -d +2day +%$wdu$wdf)
  day3=$(LANG=$loc LC_TIME=$loc date -d +3day +%$wdu$wdf)
  day4=$(LANG=$loc LC_TIME=$loc date -d +4day +%$wdu$wdf)
  echo "\${voffset $wdvoffset}\${font $wdfontname:size=$wdfontsize}\${color $wdfontcol}"
  echo "\${goto 32}$day1 \${goto 157}$day2 \${goto 284}$day3 \${goto 409}$day4"


# Show 4 min/max temps
  max1=$(jq .list[1].main.temp_max ./json/forecast.json | awk '{print int($1+0.5)}') #'
  min1=$(jq .list[1].main.temp_min ./json/forecast.json | awk '{print int($1+0.5)}') #'
  max2=$(jq .list[2].main.temp_max ./json/forecast.json | awk '{print int($1+0.5)}') #'
  min2=$(jq .list[2].main.temp_min ./json/forecast.json | awk '{print int($1+0.5)}') #'
  max3=$(jq .list[3].main.temp_max ./json/forecast.json | awk '{print int($1+0.5)}') #'
  min3=$(jq .list[3].main.temp_min ./json/forecast.json | awk '{print int($1+0.5)}') #'
  max4=$(jq .list[4].main.temp_max ./json/forecast.json | awk '{print int($1+0.5)}') #'
  min4=$(jq .list[4].main.temp_min ./json/forecast.json | awk '{print int($1+0.5)}') #'
  echo "\${voffset $wdmmvoffset}\${font $wdmmfontname:size=$wdmmfontsize}\${color $wdmmfontcol}"
  echo "\${goto 31}"$max1"°C  / "$min1"°C \${goto 158}"$max2"°C  / "$min2"°C \${goto 284}"$max3"°C  / "$min3"°C \${goto 409}"$max4"°C  / "$min4"°C"


# Show 4 day forecast icons
  cp -f ./weatherimages/$(jq .list[1].weather[0].id ./json/forecast.json).png ./weatherimages/forecast-1.png
  cp -f ./weatherimages/$(jq .list[2].weather[0].id ./json/forecast.json).png ./weatherimages/forecast-2.png
  cp -f ./weatherimages/$(jq .list[3].weather[0].id ./json/forecast.json).png ./weatherimages/forecast-3.png
  cp -f ./weatherimages/$(jq .list[4].weather[0].id ./json/forecast.json).png ./weatherimages/forecast-4.png
  echo "\${lua conky_image ./weatherimages/forecast-1.png 12 $wdfvoffset 90 48}"
  echo "\${lua conky_image ./weatherimages/forecast-2.png 110 $wdfvoffset 80 48}"
  echo "\${lua conky_image ./weatherimages/forecast-3.png 206 $wdfvoffset 80 48}"
  echo "\${lua conky_image ./weatherimages/forecast-4.png 300 $wdfvoffset 80 48}"
  echo "\${voffset -150}"
