#!/usr/bin/env bash
# Korvyx FiveM Platform Agent — Linux installer.
# Run via: curl -sSL https://downloads.korvyx.dev/install.sh | \
#           KORVYX_SERVER_ID=... KORVYX_TOKEN=... bash
#
# Installs to /opt/korvyx, registers a systemd service, starts it.

set -euo pipefail

if [ -z "${KORVYX_SERVER_ID:-}" ] || [ -z "${KORVYX_TOKEN:-}" ]; then
  echo "ERROR: KORVYX_SERVER_ID and KORVYX_TOKEN must be set." >&2
  echo "       Copy the full one-liner from your korvyx.dev dashboard." >&2
  exit 1
fi

if [ "$(id -u)" != "0" ]; then
  echo "ERROR: run with sudo (need to write /etc/systemd/system)." >&2
  exit 1
fi

PLATFORM_URL="wss://api.korvyx.dev/agent/ws"
INSTALL_DIR="/opt/korvyx"
BINARY_URL="https://downloads.korvyx.dev/korvyx-agent-linux"
SERVICE_NAME="korvyx-agent"

echo
echo "  Korvyx Agent installer"
echo "  ----------------------"
echo

# ---- Detect FiveM server-data directory ----
FIVEM_PATH=""
for p in \
    /opt/fivem/server-data \
    /home/fivem/server-data \
    /var/lib/fivem/server-data \
    /opt/FXServer/server-data \
    "${SUDO_USER:+/home/$SUDO_USER/fivem/server-data}"; do
  if [ -n "$p" ] && [ -d "$p" ]; then FIVEM_PATH="$p"; break; fi
done
if [ -n "$FIVEM_PATH" ]; then
  echo "  Detected FiveM server:  $FIVEM_PATH"
else
  echo "  Could not auto-detect your FiveM server folder."
  while [ -z "$FIVEM_PATH" ] || [ ! -d "$FIVEM_PATH" ]; do
    read -p "  Full path to your FiveM server-data directory: " FIVEM_PATH
    if [ ! -d "$FIVEM_PATH" ]; then
      echo "  Not a real folder. Try again."
      FIVEM_PATH=""
    fi
  done
fi

# ---- Stop any previous install ----
systemctl stop "$SERVICE_NAME" 2>/dev/null || true
systemctl disable "$SERVICE_NAME" 2>/dev/null || true

# ---- Download binary ----
mkdir -p "$INSTALL_DIR"
echo "  Downloading agent (~55 MB)..."
curl -fsSL -o "$INSTALL_DIR/korvyx-agent" "$BINARY_URL"
chmod +x "$INSTALL_DIR/korvyx-agent"

# ---- Write config ----
cat > "$INSTALL_DIR/agent.config.json" <<EOF
{
  "serverId": "$KORVYX_SERVER_ID",
  "agentToken": "$KORVYX_TOKEN",
  "platformUrl": "$PLATFORM_URL",
  "fiveMServerPath": "$FIVEM_PATH",
  "rconHost": "127.0.0.1",
  "rconPort": 30120
}
EOF
chmod 600 "$INSTALL_DIR/agent.config.json"

# ---- Register systemd service ----
echo "  Registering systemd service..."
cat > "/etc/systemd/system/$SERVICE_NAME.service" <<EOF
[Unit]
Description=Korvyx FiveM Platform Agent
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=$INSTALL_DIR/korvyx-agent
WorkingDirectory=$INSTALL_DIR
Environment=AGENT_CONFIG_PATH=$INSTALL_DIR/agent.config.json
Restart=always
RestartSec=10
User=root
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable "$SERVICE_NAME" >/dev/null
systemctl start "$SERVICE_NAME"

sleep 3

echo
echo "  Installed."
echo "    Status:   $(systemctl is-active $SERVICE_NAME)"
echo "    Path:     $INSTALL_DIR"
echo
echo "  Open your korvyx.dev dashboard — agent appears online in a few seconds."
echo
echo "  Manage later:"
echo "    Logs:       journalctl -u $SERVICE_NAME -f"
echo "    Restart:    systemctl restart $SERVICE_NAME"
echo "    Uninstall:  systemctl disable --now $SERVICE_NAME; rm -rf $INSTALL_DIR /etc/systemd/system/$SERVICE_NAME.service"
echo
