#!/bin/bash
# Bootstrap script for Universe Optimize VMs.
# Executed by the orchestrator via SSH on a fresh Ubuntu 24.04 VM.
#
# Placeholders replaced by the orchestrator before uploading:
#   __REPO_NAME__              - e.g. "flask"
#   __GITHUB_PAT__             - GitHub PAT for cloning/pushing
#   __CODEFLASH_API_KEY__      - Codeflash API key
#   __AWS_BEARER_TOKEN_BEDROCK__ - AWS Bedrock bearer token for Claude
set -euo pipefail

echo "=== [bootstrap] Starting VM bootstrap ==="

# ── System dependencies ──────────────────────────────────────────────
sudo apt-get update -qq
sudo apt-get install -y -qq git curl build-essential python3-dev python3-venv jq

# ── Python: install uv ──────────────────────────────────────────────
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc

# ── Node.js (needed for Claude Code CLI) ────────────────────────────
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y -qq nodejs

# ── Install Claude Code CLI ─────────────────────────────────────────
npm install -g @anthropic-ai/claude-code

# ── Install GitHub CLI ──────────────────────────────────────────────
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt-get update -qq && sudo apt-get install -y -qq gh

# ── Environment variables (Bedrock auth for Claude Code) ────────────
# Write to a dedicated file, then source from both .bashrc and .profile
# so it works in both interactive and non-interactive SSH sessions.
cat > ~/.env_universe << 'ENVEOF'
export CLAUDE_CODE_USE_BEDROCK=1
export AWS_REGION=us-east-1
export AWS_BEARER_TOKEN_BEDROCK="__AWS_BEARER_TOKEN_BEDROCK__"
export CODEFLASH_API_KEY="__CODEFLASH_API_KEY__"
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
ENVEOF
echo 'source ~/.env_universe' >> ~/.bashrc
echo 'source ~/.env_universe' >> ~/.profile
source ~/.env_universe

# ── GitHub auth ─────────────────────────────────────────────────────
git config --global credential.helper store
echo "https://codeflash-ai:__GITHUB_PAT__@github.com" > ~/.git-credentials
git config --global user.name "codeflash-ai"
git config --global user.email "bot@codeflash.ai"

# Authenticate gh CLI
echo "__GITHUB_PAT__" | gh auth login --with-token 2>/dev/null || true

# ── Create results directory ────────────────────────────────────────
mkdir -p ~/results

# ── Clone the forked repo ───────────────────────────────────────────
git clone "https://github.com/codeflash-ai/__REPO_NAME__.git" ~/project

# ── Copy optimization.md as CLAUDE.md ───────────────────────────────
# (orchestrator SCPs optimization.md to ~/optimization.md before running this)
cp ~/optimization.md ~/project/CLAUDE.md

# ── codeflash-agent plugin ──────────────────────────────────────────
# (orchestrator SCPs the dist/ directory to ~/codeflash-agent/dist before running this)

echo "=== [bootstrap] Bootstrap complete ==="
echo "  Project cloned to ~/project"
echo "  CLAUDE.md injected"
echo "  Ready to launch Claude Code"
