#!/bin/sh
# GhostCrawl self-host launcher — Linux / macOS (POSIX sh).
# Requires ONLY Docker. No Python, Node, WSL, or shell-specific setup.
# Usage: ./ghostcrawl.sh {start|stop|restart|status|logs|update|uninstall}
set -eu

IMAGE="get.ghostcrawl.io/ghostcrawl/ghostcrawl:2.3.7"
NAME="ghostcrawl"
VOLUME="gc-data"
PORT="${GC_PORT:-47700}"
BIND="${GC_BIND:-127.0.0.1}"   # set GC_BIND=0.0.0.0 for deliberate LAN access

need_docker() {
  command -v docker >/dev/null 2>&1 || { echo "Docker is required. Install Docker Engine (Linux) or Docker Desktop (macOS)." >&2; exit 1; }
  docker info >/dev/null 2>&1 || { echo "Docker daemon is not running. Start Docker and try again." >&2; exit 1; }
}

exists() { docker container inspect "$NAME" >/dev/null 2>&1; }

get_key() {
  if [ -n "${GHOSTCRAWL_API_KEY:-}" ]; then echo "$GHOSTCRAWL_API_KEY"; return; fi
  printf 'GhostCrawl API key (gck_live_…): '
  stty -echo 2>/dev/null || true; read -r k; stty echo 2>/dev/null || true; echo
  echo "$k"
}

start() {
  need_docker
  if exists; then echo "GhostCrawl is already running. Use '$0 restart' or '$0 update'."; exit 0; fi
  KEY="$(get_key)"
  case "$KEY" in gck_live_*) ;; *) echo "That does not look like a gck_live_ API key." >&2; exit 1;; esac
  echo "Pulling $IMAGE …"
  docker pull "$IMAGE"
  docker volume create "$VOLUME" >/dev/null
  echo "Starting GhostCrawl …"
  docker run -d --name "$NAME" --restart unless-stopped \
    -p "$BIND:$PORT:47700" \
    --tmpfs /run/gc:rw,exec,size=2g,mode=1777 \
    -v "$VOLUME:/data" \
    -e GHOSTCRAWL_EULA_ACCEPTED=1 \
    -e GHOSTCRAWL_API_KEY="$KEY" \
    -e GHOSTCRAWL_LOCAL_PORT=47700 \
    "$IMAGE"
  echo "Waiting for the API to become healthy …"
  i=0
  while [ "$i" -lt 90 ]; do
    if curl -fsS "http://$BIND:$PORT/healthz" >/dev/null 2>&1; then break; fi
    if [ "$(docker inspect -f '{{.State.Status}}' "$NAME" 2>/dev/null)" = "exited" ]; then
      echo "GhostCrawl exited during startup. Recent logs:" >&2
      docker logs --tail 40 "$NAME" >&2
      exit 1
    fi
    i=$((i+1)); sleep 2
  done
  if [ "$i" -ge 90 ]; then echo "Timed out waiting for health. Run '$0 logs'." >&2; exit 1; fi
  echo
  echo "GhostCrawl is ready."
  echo "  Dashboard: http://$BIND:$PORT/dashboard"
  echo "  API:       http://$BIND:$PORT/v1"
  echo "  MCP:       http://$BIND:$PORT/mcp/"
  echo "  Logs:      $0 logs"
}

stop() { need_docker; exists && docker stop "$NAME" || echo "Not running."; }
restart() { need_docker; exists && docker restart "$NAME" || echo "Not running."; }
status() { need_docker; if exists; then docker ps -a -f "name=^/$NAME$" --format '{{.Status}}'; else echo "Not installed."; fi; }
logs() { need_docker; docker logs -f --tail 200 "$NAME"; }
update() {
  need_docker
  echo "Pulling latest image …"; docker pull "$IMAGE"
  if exists; then docker stop "$NAME" >/dev/null; docker rm "$NAME" >/dev/null; fi
  echo "Updated. Run '$0 start' (your API key will be requested again)."
}
uninstall() {
  need_docker
  if exists; then docker stop "$NAME" >/dev/null 2>&1 || true; docker rm "$NAME" >/dev/null; fi
  printf 'Delete persistent data volume %s? [y/N] ' "$VOLUME"; read -r ans
  case "$ans" in y|Y) docker volume rm "$VOLUME" >/dev/null 2>&1 || true; echo "Data volume removed.";; *) echo "Data volume kept.";; esac
}

case "${1:-}" in
  start) start;; stop) stop;; restart) restart;; status) status;;
  logs) logs;; update) update;; uninstall) uninstall;;
  *) echo "Usage: $0 {start|stop|restart|status|logs|update|uninstall}" >&2; exit 2;;
esac
