#!/bin/bash
# Entpackt und startet Rinkplanner auf einem Mac.
#
# Doppelklick im Finder (die Datei liegt neben rinkplanner.zip) oder:
#   ./start_rinkplanner.command
#
# Entfernt das Quarantäne-Attribut (nötig, weil das Bundle nur ad-hoc
# signiert und nicht notarisiert ist) und öffnet die App.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ZIP="$SCRIPT_DIR/rinkplanner.zip"
APP="$SCRIPT_DIR/Rinkplanner.app"

if [[ ! -f "$ZIP" ]]; then
  echo "Fehler: rinkplanner.zip nicht gefunden in $SCRIPT_DIR" >&2
  echo "Lege dieses Script neben die ZIP-Datei und starte es erneut." >&2
  exit 1
fi

# Ein vorhandenes Bundle ersetzen (Nutzerdaten liegen nicht im Bundle,
# sondern unter ~/Library/Application Support/Godot/app_userdata/Rinkplanner/).
if [[ -e "$APP" ]]; then
  echo "Ersetze vorhandenes Rinkplanner.app ..."
  rm -rf "$APP"
fi

echo "Entpacke $(basename "$ZIP") ..."
unzip -q -o "$ZIP" -d "$SCRIPT_DIR"

if [[ ! -d "$APP" ]]; then
  echo "Fehler: Rinkplanner.app wurde nicht entpackt." >&2
  exit 1
fi

echo "Entferne Quarantäne-Attribut ..."
xattr -cr "$APP"

echo "Starte Rinkplanner ..."
open "$APP"
