#!/bin/sh
# alioth smoke tests — exits 1 if any check fails
FAIL=0

ok() { printf "  PASS  %s\n" "$1"; }
fail() { printf "  FAIL  %s\n" "$1"; FAIL=1; }

check() {
    name="$1"; url="$2"; want="${3:-200}"
    code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "$url")
    if [ "$code" = "$want" ] || { [ "$want" = "2xx" ] && [ "${code%?}" = "20" ]; }; then
        ok "$name ($code)"
    else
        fail "$name — got $code, want $want ($url)"
    fi
}

echo "=== dashboard ==="
check "GET /"           http://127.0.0.1:9090/
check "GET /style.css"  http://127.0.0.1:9090/style.css
check "GET /app.js"     http://127.0.0.1:9090/app.js
check "GET /api/stats"  http://127.0.0.1:9090/api/stats

echo "=== iptv-dl ==="
check "GET /"           http://127.0.0.1:8787/
check "GET /iptv.css"   http://127.0.0.1:8787/iptv.css
check "GET /downloads"  http://127.0.0.1:8787/downloads
check "GET /api/downloads" http://127.0.0.1:8787/api/downloads
check "GET /movies"     http://127.0.0.1:8787/movies
check "GET /series"     http://127.0.0.1:8787/series

echo "=== arr stack ==="
check "jellyfin"  http://127.0.0.1:8096/jellyfin/health
check "sonarr"    http://127.0.0.1:8989/sonarr/ping
check "radarr"    http://127.0.0.1:7878/radarr/ping
check "prowlarr"  http://127.0.0.1:9696/prowlarr/ping
check "qbit"      http://127.0.0.1:8080/

echo "=== nginx proxy ==="
check "nginx /"         http://127.0.0.1/
check "nginx /style.css" http://127.0.0.1/style.css
check "nginx /app.js"   http://127.0.0.1/app.js
check "nginx /iptv/"    http://127.0.0.1/iptv/
check "nginx /iptv/iptv.css" http://127.0.0.1/iptv/iptv.css

echo ""
if [ "$FAIL" -eq 0 ]; then
    echo "ALL PASS"
else
    echo "SOME FAILED"
fi
exit $FAIL
