#!/bin/bash
set -euo pipefail

# ------------------------------------------------------------
# Color 64 BBS launcher for macOS
# Launches tcpser and VICE in separate Terminal.app windows
# ------------------------------------------------------------

# ---- Resolve script directory ----
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo "Launching Color 64 BBS for macOS..."
echo

# ---- Verify required launcher scripts exist ----
need_file() {
  local f="$1"

  if [[ ! -f "$f" ]]; then
    echo "ERROR: Missing required file: $f"
    exit 1
  fi
}

need_file "mybbs-tcpser.sh"
need_file "mybbs-vice.sh"

# ---- Ensure scripts are executable ----
chmod +x mybbs-tcpser.sh
chmod +x mybbs-vice.sh

# ---- Launch tcpser in Terminal.app ----
osascript <<EOF
tell application "Terminal"
    do script "cd \"$SCRIPT_DIR\" && ./mybbs-tcpser.sh"
end tell
EOF

sleep 2

# ---- Launch VICE in another Terminal.app window ----
osascript <<EOF
tell application "Terminal"
    do script "cd \"$SCRIPT_DIR\" && ./mybbs-vice.sh"
end tell
EOF

echo
echo "Color 64 launch initiated."
echo
echo "If VICE or tcpser fail to launch,"
echo "review the Terminal.app windows for diagnostics."

exit 0