#!/bin/bash
# deploy-to-agents.sh — Deploy Live Status v4 to OpenClaw workspace
#
# Deploys the gateway:startup hook and installs npm dependencies.
# Does NOT inject anything into AGENTS.md.
# The watcher daemon auto-starts on next gateway restart.
#
# Usage: bash deploy-to-agents.sh [--workspace DIR]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORKSPACE="${WORKSPACE:-/home/node/.openclaw/workspace}"

# Parse flags
while [[ "$#" -gt 0 ]]; do
  case "$1" in
    --workspace) WORKSPACE="$2"; shift 2 ;;
    *) shift ;;
  esac
done

echo "==================================="
echo "  Live Status v4 Deploy"
echo "==================================="
echo "Workspace: $WORKSPACE"
echo ""

# 1. npm install
echo "[1/2] Installing npm dependencies..."
cd "$SCRIPT_DIR"
npm install --production
echo "      Done."

# 2. Deploy hook
echo "[2/2] Deploying gateway:startup hook..."
HOOKS_DIR="$WORKSPACE/hooks"
mkdir -p "$HOOKS_DIR/status-watcher-hook"
cp -r "$SCRIPT_DIR/hooks/status-watcher-hook/." "$HOOKS_DIR/status-watcher-hook/"
echo "      Hook deployed to: $HOOKS_DIR/status-watcher-hook/"

echo ""
echo "==================================="
echo "  Deployment Complete"
echo "==================================="
echo ""
echo "The watcher will auto-start on next gateway startup."
echo ""
echo "To activate immediately, ensure these env vars are set, then run:"
echo "  node $SCRIPT_DIR/src/watcher-manager.js start"
echo ""
echo "Required env vars: MM_TOKEN, MM_URL, TRANSCRIPT_DIR, SESSIONS_JSON"
echo "See install.sh for full config reference."
