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

# agent-loader.sh — Ubuntu agent sandbox setup

AGENT_USER="agent"
AGENT_HOME="/home/${AGENT_USER}"

log() { echo "[agent-loader] $*"; }

# ── 1. Base system setup ──────────────────────────────────────────────────────
log "Step 1: Running ubuntu-base setup..."
curl -sSL https://go.poorna.net/go/ubuntu-base.sh | bash

# ── 2. Docker setup ───────────────────────────────────────────────────────────
log "Step 2: Running ubuntu-docker setup..."
curl -sSL https://go.poorna.net/go/ubuntu-docker.sh | bash

# ── 3. Create 'agent' user with sudo (no password) and docker group ───────────
log "Step 3: Creating '${AGENT_USER}' user..."
if ! id "${AGENT_USER}" &>/dev/null; then
    useradd -m -s /bin/bash "${AGENT_USER}"
fi
usermod -aG sudo "${AGENT_USER}"
usermod -aG docker "${AGENT_USER}"
# Grant passwordless sudo
if ! grep -q "^${AGENT_USER} " /etc/sudoers.d/"${AGENT_USER}" 2>/dev/null; then
    echo "${AGENT_USER} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/"${AGENT_USER}"
    chmod 440 /etc/sudoers.d/"${AGENT_USER}"
fi

# ── 4. Install Claude Code ────────────────────────────────────────────────────
log "Step 4: Installing Claude Code..."
curl -sSL https://go.poorna.net/go/claude-code-install.sh -o /usr/local/bin/claude-code-install.sh
chmod +x /usr/local/bin/claude-code-install.sh
bash /usr/local/bin/claude-code-install.sh

# ── 5. Install Codex ─────────────────────────────────────────────────────────
log "Step 5: Installing Codex..."
curl -sSL https://go.poorna.net/go/codex-install.sh -o /usr/local/bin/codex-install.sh
chmod +x /usr/local/bin/codex-install.sh
bash /usr/local/bin/codex-install.sh

# ── 6. Install UV ─────────────────────────────────────────────────────────────
log "Step 6: Installing UV..."
curl -sSL https://go.poorna.net/go/uv-install.sh | bash

# ── 7. Install Node.js / NPM ──────────────────────────────────────────────────
log "Step 7: Installing Node.js/NPM..."
curl -sSL https://go.poorna.net/go/node-install.sh | bash

# ── 8. Install cron, at, screen, tmux ────────────────────────────────────────
log "Step 8: Installing cron, at, screen, tmux..."
apt-get install -y cron at screen tmux
systemctl enable cron
systemctl start cron

# ── 9. Download tmux and screen configs for root and agent user ───────────────
log "Step 9: Downloading tmux and screen configs..."

for dest in /root "${AGENT_HOME}"; do
    curl -sSL https://go.poorna.net/go/.screenrc  -o "${dest}/.screenrc"
    curl -sSL https://go.poorna.net/go/.tmux.conf -o "${dest}/.tmux.conf"
done
chown "${AGENT_USER}:${AGENT_USER}" "${AGENT_HOME}/.screenrc" "${AGENT_HOME}/.tmux.conf"

# ── 10. Install Gitea CLI (tea) ───────────────────────────────────────────────
log "Step 10: Installing Gitea CLI (tea)..."
curl -sSL https://gitea.com/gitea/tea/releases/download/v0.13.0/tea-0.13.0-linux-amd64 \
    -o /usr/local/bin/tea
chmod +x /usr/local/bin/tea
chown root:root /usr/local/bin/tea

# ── 11. Install PHP CLI ───────────────────────────────────────────────────────
log "Step 11: Installing PHP CLI..."
_php_tar="$(mktemp -t php-XXXXXX.tar.gz)"
curl -sSL https://dl.static-php.dev/static-php-cli/bulk/php-8.5.5-cli-linux-x86_64.tar.gz \
    -o "${_php_tar}"
tar -xzf "${_php_tar}" -C /usr/local/bin php
chmod +x /usr/local/bin/php
chown root:root /usr/local/bin/php
rm -f "${_php_tar}"

# ── 12. Install Composer ──────────────────────────────────────────────────────
log "Step 12: Installing Composer..."
curl -sSL https://github.com/composer/composer/releases/download/2.9.5/composer.phar \
    -o /usr/local/bin/composer
chmod +x /usr/local/bin/composer
chown root:root /usr/local/bin/composer

log "Done. Agent sandbox setup complete."
