#!/bin/bash
set -eu

# It seems that in conky the execi command will start before curl has completely
# written the file. For some reason adding a sleep before calling curl fixes it.
sleep 2

forecast=~/".cache/jelly-conky/forecast.json"
weather=~/".cache/jelly-conky/weather.json"

mkdir -p ~/".cache/jelly-conky"

api_prefix="api.openweathermap.org/data/2.5/"

appid="APPID=$1"
id="&id=$2"
units="&units=$3"
lang="${4%%_*}"
lang="&lang=$lang"

curl -s "${api_prefix}forecast?${appid}${id}${units}${lang}" -o "$forecast"
curl -s "${api_prefix}weather?${appid}${id}${units}${lang}" -o "$weather"
