#!/usr/bin/env bash
set -euo pipefail

# init.sh — Set up the demo skill's headless Chrome container
#
# Prerequisites:
#   - go (for installing showboat & rodney)
#   - docker
#
# This script:
#   1. Installs showboat and rodney if missing
#   2. Downloads Chromium for the host (macOS, via rodney)
#   3. Downloads Chromium for the Docker image (Linux_x64, from GCS)
#   4. Copies Linux binary into chrome/ (the Docker build context)
#   5. Builds the demo-chrome image

SKILL_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CHROME_DIR="$SKILL_DIR/chrome"
IMAGE_NAME="${1:-demo-chrome}"

echo "==> Checking prerequisites..."

# ------------------------------------------------------------------
# 1. Install CLI tools
# ------------------------------------------------------------------
GO_BIN_DIR="$(go env GOBIN 2>/dev/null || echo "$HOME/go/bin")"
export PATH="$GO_BIN_DIR:$PATH"

if ! command -v showboat &>/dev/null; then
  echo "==> Installing showboat..."
  go install github.com/simonw/showboat@latest
else
  echo "    showboat: ok"
fi

if ! command -v rodney &>/dev/null; then
  echo "==> Installing rodney..."
  go install github.com/simonw/rodney@latest
else
  echo "    rodney: ok"
fi

# ------------------------------------------------------------------
# 2. Download macOS Chromium for local rodney
# ------------------------------------------------------------------
echo "==> Downloading macOS Chromium via rodney..."
rodney start 2>/dev/null || true
rodney stop  2>/dev/null || true

ROD_CACHE="$HOME/.cache/rod/browser"
if [ -d "$ROD_CACHE" ]; then
  CHROMIUM_DIR=$(ls -d "$ROD_CACHE"/chromium-* 2>/dev/null | head -1)
else
  echo "ERROR: rod cache not found at $ROD_CACHE"
  exit 1
fi

if [ -z "$CHROMIUM_DIR" ]; then
  echo "ERROR: No chromium-* directory found in $ROD_CACHE"
  exit 1
fi

echo "    macOS Chromium at: $CHROMIUM_DIR"

# Extract revision number from directory name (e.g. chromium-1321438)
REVISION=$(basename "$CHROMIUM_DIR" | sed 's/chromium-//')
echo "    Revision: $REVISION"

# ------------------------------------------------------------------
# 3. Download Linux Chromium for the Docker image
# ------------------------------------------------------------------
LINUX_ZIP="/tmp/chrome-linux-$REVISION.zip"
LINUX_EXTRACT="/tmp/chrome-linux-extract"

echo "==> Downloading Linux_x64 Chromium (revision $REVISION)..."

if [ ! -f "$LINUX_ZIP" ]; then
  curl -fSL \
    "https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/$REVISION/chrome-linux.zip" \
    -o "$LINUX_ZIP"
  echo "    Downloaded: $LINUX_ZIP"
else
  echo "    Already cached: $LINUX_ZIP"
fi

# ------------------------------------------------------------------
# 4. Extract and copy into build context
# ------------------------------------------------------------------
echo "==> Extracting Linux Chromium into $CHROME_DIR ..."
rm -rf "$LINUX_EXTRACT" "$CHROME_DIR"
mkdir -p "$LINUX_EXTRACT"
unzip -q "$LINUX_ZIP" -d "$LINUX_EXTRACT"

# chrome-linux.zip contains a single chrome-linux/ directory
mv "$LINUX_EXTRACT/chrome-linux" "$CHROME_DIR"

# IMPORTANT: set suid on the sandbox binary so Chrome can sandbox child processes
if [ -f "$CHROME_DIR/chrome_sandbox" ]; then
  chmod 4755 "$CHROME_DIR/chrome_sandbox"
fi

echo "==> Files in $CHROME_DIR:"
ls -lh "$CHROME_DIR/" | head -15

# Clean up extraction temp
rm -rf "$LINUX_EXTRACT"

# ------------------------------------------------------------------
# 5. Build Docker image
# ------------------------------------------------------------------
echo "==> Building Docker image '$IMAGE_NAME' ..."
docker build --tag "$IMAGE_NAME" "$SKILL_DIR"

# ------------------------------------------------------------------
# 6. Verify
# ------------------------------------------------------------------
echo ""
echo "==> Success!"
echo "    Image:  $IMAGE_NAME"
echo ""
echo "    Run the container:"
echo "      docker run -d --name demo-chrome \\"
echo "        --cap-add=SYS_ADMIN --cap-drop=ALL \\"
echo "        --security-opt=no-new-privileges:false \\"
echo "        -p 9222:9222 \\"
echo "        $IMAGE_NAME"
echo ""
echo "    Connect rodney to it:"
echo "      rodney connect localhost:9222"
echo ""
echo "    Stop the container:"
echo "      docker stop demo-chrome && docker rm demo-chrome"