#!/usr/bin/env bash
cd "$(dirname "$0")"
base="https://wusel2d.priebe.world/wusel/downloads/godot"
latest=$(curl -fsL "$base/version.txt" 2>/dev/null | tr -d "[:space:]")
if [ -z "$latest" ]; then
  base="https://home23-demo.priebe.world/wusel/downloads/godot"  # fallback host
  latest=$(curl -fsL "$base/version.txt" 2>/dev/null | tr -d "[:space:]")
fi
have=$(cat .version 2>/dev/null || true)
if [ -n "$latest" ] && [ "$latest" != "$have" ]; then
  echo "Aktualisiere Wusel2D (Godot) ($have -> $latest) ..."
  if curl -fL -o wusel2d-godot-linux.zip "$base/wusel2d-godot-linux.zip" && unzip -o wusel2d-godot-linux.zip; then
    printf "%s\n" "$latest" > .version
  else
    echo "Update fehlgeschlagen - starte die vorhandene Version."
  fi
elif [ -z "$latest" ]; then
  echo "Server nicht erreichbar - starte die vorhandene Version."
else
  echo "Wusel2D (Godot) ist aktuell ($have)."
fi
chmod +x ./wusel2d-godot-linux.x86_64
exec ./wusel2d-godot-linux.x86_64 "$@"
