#!/bin/bash
# bootstrap.sh — First-time setup for VC Node
#
# Usage:
#   curl -sL <repo-url>/bootstrap.sh | sudo bash
#   or:
#   sudo bash bootstrap.sh
#
# This script:
# 1. Installs PHP CLI and git
# 2. Clones the vcnodesetup repository
# 3. Runs setup.php in interactive mode

set -e

REPO_URL="https://vcnd:7d3a413d444b944374113cc60aff89ae@git.ultra-x.su/vcnodesetup.git"
CLONE_DIR="/opt/vcnodesetup"

echo "=== VC Node Bootstrap ==="
echo ""

# Check root
if [ "$(id -u)" -ne 0 ]; then
    echo "ERROR: This script must be run as root."
    exit 1
fi

# Install PHP CLI and git
echo "[1/3] Installing php-cli and git..."
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y -qq php-cli git

# Clone repository
echo "[2/3] Cloning vcnodesetup repository..."
if [ -d "$CLONE_DIR" ]; then
    echo "  Directory $CLONE_DIR already exists, updating..."
    cd "$CLONE_DIR"
    git -c http.sslVerify=false pull || true
else
    git -c http.sslVerify=false clone "$REPO_URL" "$CLONE_DIR"
    cd "$CLONE_DIR"
fi

# Show version
if [ -f "$CLONE_DIR/VERSION" ]; then
    echo "  Version: $(cat "$CLONE_DIR/VERSION" | tr -d '[:space:]')"
fi

# Run setup
echo "[3/3] Starting interactive setup..."
echo ""
php "$CLONE_DIR/setup.php" --interactive "$@"
