#!/bin/bash
# Installeert de snoezel-zender op een Mac en laat hem bij het inloggen meedraaien.
# Start: curl -kfsSL "https://dmx.kloek-advies.nl/download.php?bestand=installeer-mac.command" | bash
set -u

BASE="https://dmx.kloek-advies.nl"
DIR="$HOME/Library/Application Support/KloekSnoezelen"
LABEL="nl.kloek-advies.snoezelen"
PLIST="$HOME/Library/LaunchAgents/${LABEL}.plist"
LOG="$HOME/Library/Logs/kloek-snoezelen.log"
PY_PKG_URL="https://www.python.org/ftp/python/3.12.10/python-3.12.10-macos11.pkg"
PY_FRAMEWORK="/Library/Frameworks/Python.framework/Versions/3.12/bin/python3"

echo "Snoezelen wordt geïnstalleerd op deze Mac."
echo "De zender stuurt naar Art-Net-node 10.0.1.219, universum 0."
echo

mkdir -p "$DIR" "$HOME/Library/LaunchAgents" "$HOME/Library/Logs"

fetch() {
  local url="$1" dest="$2"
  curl -fsSL --retry 3 "$url" -o "$dest" && return 0
  curl -kfsSL --retry 3 "$url" -o "$dest"
}

echo "Bestanden ophalen…"
fetch "$BASE/download.php?bestand=artnet_snoezelen.py" "$DIR/artnet_snoezelen.py" || { echo "Download van het script mislukt."; exit 1; }
fetch "$BASE/download.php?bestand=config.json" "$DIR/config.json" || { echo "Download van config.json mislukt."; exit 1; }

python_ok() {
  [ -x "$1" ] && "$1" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 9) else 1)' >/dev/null 2>&1
}

PY=""
for candidate in \
  "$PY_FRAMEWORK" \
  "/opt/homebrew/bin/python3" \
  "/usr/local/bin/python3" \
  "/usr/bin/python3"
do
  if python_ok "$candidate"; then
    PY="$candidate"
    break
  fi
done

if [ -z "$PY" ] && command -v python3 >/dev/null 2>&1; then
  found="$(command -v python3)"
  if python_ok "$found"; then
    PY="$found"
  fi
fi

if [ -z "$PY" ]; then
  echo "Python 3 staat er nog niet op. Het installatieprogramma wordt gedownload."
  echo "macOS vraagt zo om je wachtwoord. Dat is één keer nodig."
  pkg="/tmp/python-3.12.10-macos11.pkg"
  curl -fL --retry 3 "$PY_PKG_URL" -o "$pkg" || { echo "Python downloaden mislukt."; exit 1; }
  sudo installer -pkg "$pkg" -target / || { echo "Python installeren mislukt."; exit 1; }
  rm -f "$pkg"
  PY="$PY_FRAMEWORK"
  if ! python_ok "$PY"; then
    echo "Python is na de installatie niet gevonden."
    exit 1
  fi
fi

echo "Python: $PY"

uid_num="$(id -u)"
launchctl bootout "gui/${uid_num}/${LABEL}" >/dev/null 2>&1 || true

cat > "$PLIST" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>${LABEL}</string>
  <key>ProgramArguments</key>
  <array>
    <string>${PY}</string>
    <string>${DIR}/artnet_snoezelen.py</string>
  </array>
  <key>WorkingDirectory</key>
  <string>${DIR}</string>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>StandardOutPath</key>
  <string>${LOG}</string>
  <key>StandardErrorPath</key>
  <string>${LOG}</string>
</dict>
</plist>
EOF

if ! launchctl bootstrap "gui/${uid_num}" "$PLIST"; then
  echo "Automatisch starten lukte niet. Start hem zelf met:"
  echo "\"$PY\" \"$DIR/artnet_snoezelen.py\""
  exit 1
fi
launchctl enable "gui/${uid_num}/${LABEL}" >/dev/null 2>&1 || true
launchctl kickstart -k "gui/${uid_num}/${LABEL}"

echo
echo "Klaar. De zender draait en start opnieuw wanneer je inlogt."
echo "Node 10.0.1.219, subnet 255.255.252.0, universum 0."
echo "Deze Mac moet in dat netwerk zitten."
echo "Logbestand: $LOG"
echo "Zet de reeks aan op ${BASE}/snoezelen.php of met de Flic-knop."
echo
if [ -t 0 ]; then
  read -r -p "Druk op Return om dit venster te sluiten."
fi
