#!/bin/bash
#
# garudai — control helper for the GarudAI surveillance stack (macOS).
#
set -euo pipefail

INSTALL_DIR="$HOME/GarudAI"
COMPOSE_FILE="docker-compose.production.yml"
DASHBOARD_URL="http://localhost:3010"

if [ ! -f "$INSTALL_DIR/$COMPOSE_FILE" ]; then
  echo "GarudAI is not installed at $INSTALL_DIR" >&2
  exit 1
fi

dc() { (cd "$INSTALL_DIR" && docker compose -f "$COMPOSE_FILE" --env-file .env "$@"); }

case "${1:-}" in
  start)     dc up -d ;;
  stop)      dc down ;;
  restart)   dc restart "${2:-}" ;;
  status|ps) dc ps ;;
  logs)      dc logs -f "${2:-}" ;;
  update)
    TAR="${2:-$INSTALL_DIR/garudai-images.tar.gz}"
    [ -f "$TAR" ] || curl -fSL --retry 3 "https://licensestore.garudai.com/downloads/garudai-images.tar.gz" -o "$TAR"
    docker load -i "$TAR"
    dc up -d
    ;;
  open)      open "$DASHBOARD_URL" 2>/dev/null || echo "Open $DASHBOARD_URL" ;;
  *)
    cat <<EOF
GarudAI control helper

Usage: garudai <command>

  start            Start all GarudAI services
  stop             Stop all services
  restart [svc]    Restart everything (or one service)
  status           Show service status
  logs [svc]       Follow logs (all, or one service)
  update [tar]     Load new images and recreate containers
  open             Open the dashboard ($DASHBOARD_URL)
EOF
    ;;
esac
