#!/bin/sh
set -u
HERE="$(cd "$(dirname "$0")" && pwd)"
RES="$(cd "$HERE/../Resources" && pwd)"
APP="$RES/app"
GAME="$APP/game/voidbreakers-2d(6).html"
DATA="${VB_DATA_DIR:-$HOME/Library/Application Support/Voidbreakers}"
PROFILE="$DATA/chrome-profile"
SERVER_DATA="$DATA/server-data"
LOGS="$DATA/logs"
LOCKDIR="$DATA/app.lockdir"
RANK_PORT="${VB_RANKING_PORT:-8788}"
ACCOUNT_PORT="${VB_ACCOUNT_PORT:-8765}"
RANK_PID=""
ACC_PID=""
OWNER=0

mkdir -p "$PROFILE" "$SERVER_DATA" "$LOGS"

cleanup() {
  if [ -n "$RANK_PID" ]; then kill "$RANK_PID" 2>/dev/null || true; fi
  if [ -n "$ACC_PID" ]; then kill "$ACC_PID" 2>/dev/null || true; fi
  if [ "$OWNER" = 1 ]; then rm -rf "$LOCKDIR"; fi
}
trap cleanup EXIT INT TERM HUP

if mkdir "$LOCKDIR" 2>/dev/null; then
  OWNER=1
  printf '%s\n' "$$" > "$LOCKDIR/pid"
else
  OLD_PID="$(cat "$LOCKDIR/pid" 2>/dev/null || true)"
  if [ -z "$OLD_PID" ] || ! kill -0 "$OLD_PID" 2>/dev/null; then
    rm -rf "$LOCKDIR"
    if mkdir "$LOCKDIR" 2>/dev/null; then
      OWNER=1
      printf '%s\n' "$$" > "$LOCKDIR/pid"
    fi
  fi
fi

CHROME=""
for C in "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \
         "$HOME/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \
         "/Applications/Chromium.app/Contents/MacOS/Chromium" \
         "$HOME/Applications/Chromium.app/Contents/MacOS/Chromium" \
         "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" \
         "$HOME/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"; do
  if [ -x "$C" ]; then CHROME="$C"; break; fi
done
if [ -n "${VB_BROWSER:-}" ]; then CHROME="$VB_BROWSER"; fi
if [ -z "$CHROME" ]; then
  /usr/bin/osascript -e 'display dialog "Voidbreakers needs Google Chrome, Chromium, or Microsoft Edge. Install one and open the app again." buttons {"OK"} default button "OK" with title "Voidbreakers"' >/dev/null 2>&1 || true
  exit 1
fi

PY="${VB_PYTHON:-}"
if [ -z "$PY" ]; then
  for C in /opt/homebrew/bin/python3 /usr/local/bin/python3 /Library/Developer/CommandLineTools/usr/bin/python3; do
    if [ -x "$C" ]; then PY="$C"; break; fi
  done
fi
if [ -z "$PY" ]; then
  FOUND="$(command -v python3 2>/dev/null || true)"
  if [ -n "$FOUND" ] && [ "$FOUND" != "/usr/bin/python3" ]; then PY="$FOUND"; fi
fi

port_open() {
  /usr/bin/nc -z 127.0.0.1 "$1" >/dev/null 2>&1
}

if [ "$OWNER" = 1 ] && [ -n "$PY" ]; then
  if [ ! -f "$SERVER_DATA/ranking.json" ]; then
    cp "$APP/servers/ranking.json" "$SERVER_DATA/ranking.json" 2>/dev/null || true
  fi
  if ! port_open "$RANK_PORT"; then
    "$PY" "$APP/servers/ranking_server.py" --host 0.0.0.0 --port "$RANK_PORT" \
      --data "$SERVER_DATA/ranking.json" >> "$LOGS/ranking.log" 2>&1 &
    RANK_PID=$!
  fi
  if "$PY" -c 'import websockets' >/dev/null 2>&1 && ! port_open "$ACCOUNT_PORT"; then
    "$PY" "$APP/servers/server.py" --host 127.0.0.1 --port "$ACCOUNT_PORT" \
      --db "$SERVER_DATA/voidbreakers.db" >> "$LOGS/account.log" 2>&1 &
    ACC_PID=$!
  fi
fi

GAME_URL="$(printf '%s' "$GAME" | sed 's/ /%20/g')"
"$CHROME" --app="file://$GAME_URL" --user-data-dir="$PROFILE" \
  --no-first-run --no-default-browser-check --disable-session-crashed-bubble \
  --disable-background-mode --disable-features=Translate,MediaRouter \
  --password-store=basic --window-size=1280,760
RC=$?
cleanup
trap - EXIT INT TERM HUP
exit "$RC"
