#!/usr/bin/env bash
# clawd installer
# Run: curl -fsSL https://git.cutos.ai/claw-daemon/clawd/raw/branch/main/install.sh | sudo bash
# Requires root and Node.js >= 18

set -e

RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'

info()  { echo -e "${GREEN}[clawd]${NC} $*"; }
warn()  { echo -e "${YELLOW}[clawd]${NC} $*"; }
error() { echo -e "${RED}[clawd]${NC} $*"; exit 1; }

# Check root
if [ "$EUID" -ne 0 ]; then
  error "Please run as root: sudo bash install.sh"
fi

# Check Node.js
if ! command -v node &>/dev/null; then
  error "Node.js not found. Please install Node.js >= 18"
fi

NODE_VER=$(node -e "process.stdout.write(process.versions.node)")
MAJOR=$(echo "$NODE_VER" | cut -d. -f1)
if [ "$MAJOR" -lt 18 ]; then
  error "Node.js version $NODE_VER is too old. Requires >= 18"
fi
info "Node.js $NODE_VER OK"

# Install dnsmasq (required for WiFi captive portal)
if ! command -v dnsmasq &>/dev/null; then
  info "Installing dnsmasq for WiFi captive portal..."
  if command -v apt-get &>/dev/null; then
    apt-get install -y -qq dnsmasq >/dev/null 2>&1
  elif command -v yum &>/dev/null; then
    yum install -y -q dnsmasq >/dev/null 2>&1
  elif command -v apk &>/dev/null; then
    apk add --quiet dnsmasq >/dev/null 2>&1
  else
    warn "Cannot install dnsmasq. WiFi captive portal may not work."
  fi
  # Disable system dnsmasq; clawd manages it directly
  systemctl disable dnsmasq 2>/dev/null || true
  systemctl stop dnsmasq 2>/dev/null || true
fi
if command -v dnsmasq &>/dev/null; then
  info "dnsmasq OK"
fi

# Configure NetworkManager for WiFi
if command -v nmcli &>/dev/null; then
  if ! systemctl is-active --quiet NetworkManager 2>/dev/null; then
    info "Starting NetworkManager..."
    systemctl enable --now NetworkManager 2>/dev/null || true
  fi
  info "NetworkManager OK"

  # Write captive-portal DNS config
  NM_DNSMASQ_DIR="/etc/NetworkManager/dnsmasq-shared.d"
  mkdir -p "$NM_DNSMASQ_DIR"
  cat > "$NM_DNSMASQ_DIR/clawd-captive.conf" << 'DNSCONF'
# clawd captive portal DNS hijack
# All DNS queries resolve to gateway to trigger captive portal
address=/#/10.42.0.1
DNSCONF
  info "DNS captive config written to $NM_DNSMASQ_DIR"
fi

# Unblock WiFi via rfkill
for rf in /sys/class/rfkill/rfkill*; do
  if [ -f "$rf/type" ] && [ "$(cat "$rf/type")" = "wlan" ]; then
    if [ "$(cat "$rf/soft")" = "1" ]; then
      info "Unblocking WiFi ($(basename "$rf"))..."
      echo 0 > "$rf/soft"
    fi
  fi
done

# Install rfkill unblock script + systemd unit for persistence
RFKILL_SCRIPT="/usr/local/bin/clawd-unblock-wifi.sh"
cat > "$RFKILL_SCRIPT" << 'SCRIPT'
#!/bin/sh
for rf in /sys/class/rfkill/rfkill*; do
  [ -f "$rf/type" ] || continue
  if [ "$(cat "$rf/type")" = "wlan" ] && [ "$(cat "$rf/soft")" = "1" ]; then
    echo 0 > "$rf/soft"
    echo "clawd-rfkill: unblocked $(basename "$rf")"
  fi
done
SCRIPT
chmod +x "$RFKILL_SCRIPT"

RFKILL_SERVICE="/etc/systemd/system/clawd-rfkill.service"
cat > "$RFKILL_SERVICE" << 'UNIT'
[Unit]
Description=Unblock WiFi for clawd
Before=NetworkManager.service clawd.service
After=sys-subsystem-net-devices-wlan0.device

[Service]
Type=oneshot
ExecStart=/usr/local/bin/clawd-unblock-wifi.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable clawd-rfkill
info "WiFi rfkill service installed"

# Install ttyd (Web terminal)
info "Installing ttyd..."
if apt-get install -y ttyd >/dev/null 2>&1; then
  info "ttyd installed OK"
else
  warn "ttyd install failed. Web terminal will not be available."
fi

# Clone / update clawd
INSTALL_DIR="/opt/clawd"
CONFIG_DIR="/etc/clawd"
ENV_FILE="$CONFIG_DIR/env"
info "Setting up $INSTALL_DIR ..."

mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"

# Use git if available, fall back to tarball
CUTOS_REPO="https://git.cutos.ai/claw-daemon/clawd.git"
if command -v git &>/dev/null && [ -d ".git" ]; then
  CURRENT_REMOTE=$(git remote get-url origin 2>/dev/null || echo "")
  if echo "$CURRENT_REMOTE" | grep -q "github.com"; then
    info "Migrating git remote to git.cutos.ai ..."
    git remote set-url origin "$CUTOS_REPO"
  fi
  info "Pulling latest code..."
  git fetch origin
  git reset --hard origin/main
  git clean -fd
elif [ -f "package.json" ]; then
  info "Files already present, skipping git clone"
elif command -v git &>/dev/null; then
  git clone --depth=1 "$CUTOS_REPO" .
else
  TARBALL_URL="https://git.cutos.ai/claw-daemon/clawd/archive/main.tar.gz"
  curl -fsSL "$TARBALL_URL" | tar -xz --strip-components=1
fi

# Install npm dependencies
info "Running npm install..."
npm install --omit=dev --silent

# Create symlink
ln -sf "$INSTALL_DIR/bin/clawd.js" /usr/local/bin/clawd
chmod +x "$INSTALL_DIR/bin/clawd.js"

info "clawd symlinked to /usr/local/bin/clawd"

# Install RK3588S LVGL demo
DEVICE_MODEL="$(tr -d '\0' </proc/device-tree/model 2>/dev/null || true)"
if echo "$DEVICE_MODEL" | grep -qi 'RK3588S'; then
  DEMO_SRC="$INSTALL_DIR/lib/resource/3588s/demo"
  DEMO_DST="/usr/bin/demo"
  if [ -f "$DEMO_SRC" ]; then
    info "RK3588S detected, installing LVGL demo to $DEMO_DST"
    if [ -f "$DEMO_DST" ] && [ ! -f "${DEMO_DST}.clawd-bak" ]; then
      cp "$DEMO_DST" "${DEMO_DST}.clawd-bak"
      info "Backup created: ${DEMO_DST}.clawd-bak"
    fi
    install -m 0755 "$DEMO_SRC" "$DEMO_DST"
  else
    warn "RK3588S demo binary not found: $DEMO_SRC"
  fi
fi

# Write default config files
mkdir -p "$CONFIG_DIR"

if [ ! -f "$CONFIG_DIR/config.json" ]; then
  cat > "$CONFIG_DIR/config.json" <<EOF
{
  "server": "wss://claw.cutos.ai/ws",
  "claw_id": null,
  "token": null,
  "heartbeat_interval": 30
}
EOF
  info "Default config written to $CONFIG_DIR/config.json"
fi

if [ ! -f "$ENV_FILE" ]; then
  cat > "$ENV_FILE" <<EOF
# clawd environment (loaded by systemd EnvironmentFile)
# Log level: debug / info / warn / error
CLAWD_LOG_LEVEL=info
# Log to file (0 = journald only)
CLAWD_LOG_FILE=1
# Override server URL (default from config.json)
# CLAWD_SERVER=wss://claw.cutos.ai/ws
# Agent runtime: openclaw (default) or cutos-agent
# AGENT_TYPE=openclaw
# Enable Bluetooth monitor (bluetoothctl); disabled by default
# CLAWD_ENABLE_BT=1
# OpenVFD sysfs path (default: /sys/class/leds/openvfd)
# CLAWD_OPENVFD_PATH=/sys/class/leds/openvfd
# vfdservice pipe path (default: /tmp/openvfd_service)
# CLAWD_VFD_PIPE=/tmp/openvfd_service
# Wired LAN interface for carrier detection
# CLAWD_ETH_IFACE=end0
EOF
  info "Default env file written to $ENV_FILE"
fi

# Create log directory
mkdir -p "$CONFIG_DIR/logs"
info "Log directory: $CONFIG_DIR/logs"

# Write systemd service file
NODE_BIN=$(command -v node)
SERVICE_FILE="/etc/systemd/system/clawd.service"

cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=Claw Box Daemon
Documentation=https://git.cutos.ai/claw-daemon/clawd
After=NetworkManager.service
Wants=NetworkManager.service

[Service]
Type=simple
# NotifyAccess=all required for systemd-notify with WatchdogSec
NotifyAccess=all
EnvironmentFile=$ENV_FILE
ExecStart=$NODE_BIN $INSTALL_DIR/bin/clawd.js
WorkingDirectory=$INSTALL_DIR

# Restart policy
Restart=always
RestartSec=5
StartLimitInterval=300
StartLimitBurst=10

# Allow 10s for graceful shutdown before SIGKILL
TimeoutStopSec=10
KillMode=mixed
KillSignal=SIGTERM

# Resource limits
MemoryMax=256M
CPUQuota=50%
TasksMax=64

# Sandbox disabled: clawd needs to write system/config files on some devices

# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=clawd

# systemd Watchdog: restart if no heartbeat within 60s
WatchdogSec=60

[Install]
WantedBy=multi-user.target
EOF

info "systemd service file written"

# Configure journald retention
JOURNAL_CONF="/etc/systemd/journald.conf.d/clawd.conf"
if [ ! -f "$JOURNAL_CONF" ]; then
  mkdir -p /etc/systemd/journald.conf.d
  cat > "$JOURNAL_CONF" <<EOF
# clawd journald limits
[Journal]
SystemMaxUse=100M
MaxFileSec=7day
EOF
  systemctl restart systemd-journald 2>/dev/null || true
  info "journald config written"
fi

# Enable and start clawd
systemctl daemon-reload
systemctl enable clawd
systemctl restart clawd

sleep 2
if systemctl is-active --quiet clawd; then
  info "clawd is running"
  echo ""
  echo "  Logs:    journalctl -u clawd -f"
  echo "  Status:  systemctl status clawd"
  echo "  Stop:    systemctl stop clawd"
  echo "  Config:  $CONFIG_DIR/config.json"
  echo "  Env:     $ENV_FILE"
  echo "  Log dir: $CONFIG_DIR/logs/clawd.log"
  echo ""
else
  warn "clawd failed to start. Check logs:"
  echo "  journalctl -u clawd -n 50 --no-pager"
fi
