#!/bin/bash
# Aiden launcher — Bash / WSL entry point
# Invokes the Windows-side Aiden.exe via WSL interop when running inside WSL,
# or emits a helpful error if a native Linux binary is not available.

AIDEN_WIN_PATH="${AIDEN_WIN_PATH:-/mnt/c/Users/$USER/AppData/Local/Programs/Aiden/Aiden.exe}"
SUBCMD="${1:-}"

if [[ -n "$WSL_DISTRO_NAME" ]]; then
  case "$SUBCMD" in
    pc)
      # Launch Aiden desktop (Electron GUI)
      if [[ -f "$AIDEN_WIN_PATH" ]]; then
        "$AIDEN_WIN_PATH" &
      else
        echo "Aiden not found at $AIDEN_WIN_PATH"
        echo "Set AIDEN_WIN_PATH to your Aiden.exe install location."
        exit 1
      fi
      ;;
    "")
      echo ""
      echo "  Aiden | Local-first Windows AI OS"
      echo ""
      echo "  Usage:"
      echo "    aiden pc       Launch the desktop app (Electron GUI)"
      echo "    aiden          Show this help message"
      echo ""
      echo "  Terminal (TUI) interface:"
      echo "    To start the CLI, run the API server first, then launch the TUI:"
      echo "      npm run serve          # starts API on port 4200"
      echo "      npm run cli            # starts the TUI"
      echo "    Or from a packaged install, use the Aiden desktop app."
      echo ""
      ;;
    *)
      # Unknown sub-command — pass through to Aiden.exe
      if [[ -f "$AIDEN_WIN_PATH" ]]; then
        "$AIDEN_WIN_PATH" "$@"
      else
        echo "Aiden not found at $AIDEN_WIN_PATH"
        echo "Set AIDEN_WIN_PATH to your Aiden.exe install location, e.g.:"
        echo "  export AIDEN_WIN_PATH=\"/mnt/c/Users/$USER/AppData/Local/Programs/Aiden/Aiden.exe\""
        exit 1
      fi
      ;;
  esac
else
  echo "Native Linux Aiden is not yet available."
  echo "Install Aiden on Windows and run it from WSL, or check https://aiden.taracod.com for updates."
  exit 1
fi
