#!/bin/bash

ZEIT=$(TZ=Europe/Vienna date '+%d.%m. · %H:%M')
FONT="DejaVu-Sans"
# Wetterdaten einmal für alle vier Bilder abrufen.
WETTER=$(curl -fsS --connect-timeout 10 --max-time 20 "https://schoeckl.info/misc/getvalue.php") || exit 1
# Werte aus dem JSON lesen.
TEMP_TAL=$(jq -er '."temp-tal"' <<< "$WETTER") || exit 1
TEMP_BERG=$(jq -er '."temp-berg"' <<< "$WETTER") || exit 1
WIND=$(jq -er '."windgeschwindigkeit-berg"' <<< "$WETTER") || exit 1
WIND_BOE=$(jq -er '."windgeschwindigkeit-boe-berg"' <<< "$WETTER") || exit 1
# Aus "221° (SW)" nur "SW" übernehmen.
WINDRICHTUNG=$(jq -er '."windrichtung-berg" | capture("\\((?<richtung>[^)]+)\\)").richtung' <<< "$WETTER") || exit 1
# Schreibweise der Einheiten vereinheitlichen.
WIND="${WIND//Km\/H/km\/h}"
WIND_BOE="${WIND_BOE//Km\/H/km\/h}"

# Datum in österreichischer Ortszeit.
HEUTE=$(TZ=Europe/Vienna date '+%F')
SONNE=$(curl -fsS --connect-timeout 10 --max-time 20 "https://api.sunrise-sunset.org/json?lat=47.198332&lng=15.459760&date=${HEUTE}&formatted=0") || exit 1
AUFGANG_UTC=$(jq -er 'select(.status == "OK") | .results.sunrise' <<< "$SONNE") || exit 1
UNTERGANG_UTC=$(jq -er 'select(.status == "OK") | .results.sunset' <<< "$SONNE") || exit 1
# UTC in österreichische Ortszeit umrechnen.
SONNENAUFGANG=$(TZ=Europe/Vienna date -d "$AUFGANG_UTC" '+%H:%M') || exit 1
SONNENUNTERGANG=$(TZ=Europe/Vienna date -d "$UNTERGANG_UTC" '+%H:%M') || exit 1

erstelle_bild() {
    local nummer="$1"
    local position="$2"
    local wert="$3"
    local beschreibung="$4"
    local icon

    case "$nummer" in
        1)
            # Thermometer
            icon="
                path 'M 88,961 L 88,906
                      C 88,890 112,890 112,906
                      L 112,961
                      C 132,980 119,1007 100,1007
                      C 81,1007 68,980 88,961 Z'
                line 100,920 100,975
                circle 100,986 100,992
            "
            ;;
        2)
            # Berge
            icon="
                polyline 58,993 88,930 108,967 124,942 153,993
                line 58,993 153,993
                polyline 78,952 88,958 97,947
            "
            ;;
        3)
            # Wind
            icon="
                path 'M 60,939 L 119,939
                      C 149,939 149,909 129,909
                      C 118,909 114,917 118,923'
                path 'M 60,960 L 137,960
                      C 160,960 160,990 139,990'
                path 'M 60,981 L 105,981'
            "
            ;;
        4)
            # Sonne am Horizont
            icon="
                line 58,981 153,981
                path 'M 78,974 A 28,28 0 0,1 134,974'
                line 106,923 106,912
                line 72,938 64,930
                line 140,938 148,930
                line 64,961 54,961
                line 148,961 158,961
                line 82,995 130,995
            "
            ;;
    esac



convert bigpano.jpg \
    -crop "2701x2701+${position}+0" +repage \
    -resize 1080x1080 \
    -gravity northwest \
    -fill 'rgba(12,23,32,0.76)' \
    -stroke none \
    -draw "roundrectangle 30,866 1050,1050 24,24" \
    -fill none \
    -stroke white \
    -strokewidth 4 \
    -draw "$icon" \
    -stroke 'rgba(255,255,255,0.25)' \
    -strokewidth 1 \
    -draw "line 180,898 180,1018" \
    -stroke none \
    -font "$FONT" \
    -fill white \
    -pointsize 56 \
    -annotate +212+894 "$wert" \
    -fill '#DCE5EB' \
    -pointsize 27 \
    -annotate +214+977 "$beschreibung" \
    -quality 92 \
    "cpano-1-${nummer}.jpg"

}

erstelle_bild 1 1600 "$TEMP_TAL" "Temperatur im Tal"

erstelle_bild 2 4301 "$TEMP_BERG" "Temperatur am Schöckl"

erstelle_bild 3 6748 "$WIND" "Wind ${WINDRICHTUNG} · Böen ${WIND_BOE}"

erstelle_bild 4 9449 "${SONNENAUFGANG} / ${SONNENUNTERGANG}" "Sonnenaufgang / Sonnenuntergang"