#!/bin/bash
# bootstrap.sh — Fresh AIOS bootstrap for a new machine
# Usage: curl -sL https://192.168.5.10/install/bootstrap.sh | bash
#
# Supports: Ubuntu/Debian (apt), macOS (brew)
# Architecture: x86_64, arm64

set -e

AIOS_DIR="$HOME/AIOS"
PRODUCT_DIR="$HOME/Projects/AIOS"

echo "========================================="
echo "  AIOS Bootstrap"
echo "  $(date)"
echo "  Host: $(hostname)"
echo "  OS: $(uname -s) $(uname -m)"
echo "========================================="
echo ""

# ── OS detection ──
detect_pkg_mgr() {
    if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        command -v apt-get &>/dev/null && echo "apt" || echo "unknown"
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        echo "brew"
    else
        echo "unknown"
    fi
}

# ── Step 1: Basic packages ──
echo "━━━ Step 1: Basic packages ━━━"
PKG_MGR=$(detect_pkg_mgr)
echo "→ Package manager: $PKG_MGR"

case $PKG_MGR in
    apt)
        sudo apt-get update -qq
        sudo apt-get install -y -qq git curl wget rsync jq
        ;;
    brew)
        brew install git curl wget rsync jq
        ;;
    *)
        echo "ERROR: Unsupported package manager."
        exit 1
        ;;
esac
echo "✓ Packages installed"
echo ""

# ── Step 2: AIOS folder structure ──
echo "━━━ Step 2: AIOS folder structure ━━━"
mkdir -p "$AIOS_DIR"/{Atlas,Daylog,Projects,x,maps,tools,wiki}
mkdir -p "$AIOS_DIR/community/inbox/ragnar"
mkdir -p "$AIOS_DIR/community/inbox/$(hostname)"
mkdir -p "$AIOS_DIR/community/updates"
mkdir -p "$AIOS_DIR/community/knowledge"
mkdir -p "$PRODUCT_DIR"
echo "✓ AIOS directories created"
echo ""

# ── Step 3: Hermes Agent ──
echo "━━━ Step 3: Hermes Agent ━━━"
HERMES_DIR="$HOME/git/hermes-agent"

if [ ! -d "$HERMES_DIR" ]; then
    echo "→ Cloning Hermes Agent..."
    mkdir -p "$HOME/git"
    git clone https://github.com/nousresearch/hermes-agent.git "$HERMES_DIR"
fi

echo "→ Setting up venv..."
cd "$HERMES_DIR"
[ ! -d ".venv" ] && python3 -m venv .venv
source .venv/bin/activate
pip install -e . -q 2>/dev/null
echo "✓ Hermes Agent installed"
echo ""

# ── Step 4: Deploy product to runtime ──
echo "━━━ Step 4: Deploy product ━━━"
if [ -d "$PRODUCT_DIR/.git" ]; then
    echo "→ Product repo exists, pulling..."
    cd "$PRODUCT_DIR" && git pull 2>/dev/null || true
fi

if [ -f "$PRODUCT_DIR/scripts/deploy-to-runtime.sh" ]; then
    bash "$PRODUCT_DIR/scripts/deploy-to-runtime.sh" 2>/dev/null || true
    echo "✓ Product deployed"
else
    echo "→ Copying product files..."
    [ -d "$PRODUCT_DIR/maps" ] && rsync -a --exclude='.git/' "$PRODUCT_DIR/maps/" "$AIOS_DIR/maps/"
    [ -d "$PRODUCT_DIR/tools" ] && cp -r "$PRODUCT_DIR/tools" "$AIOS_DIR/"
    echo "✓ Product files copied"
fi
echo ""

# ── Step 5: Community setup ──
echo "━━━ Step 5: Community ━━━"
echo "→ Agent ID: $(hostname)"
echo "✓ Community folders ready"
echo ""

# ── Step 6: C-Ragnar first-run ──
echo "━━━ Step 6: C-Ragnar first-run ━━━"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  C-Ragnar Startar"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "  C-Ragnar kommer nu att guida dig genom"
echo "  första installationen."
echo ""
echo "  Tryck Enter för att starta..."
read

cd "$HERMES_DIR"
source .venv/bin/activate
hermes chat --skills aios-first-run

echo ""
echo "========================================="
echo "  Bootstrap Complete"
echo "========================================="
echo ""
echo "  AIOS_DIR:     $AIOS_DIR"
echo "  PRODUCT_DIR:  $PRODUCT_DIR"
echo "  HERMES_DIR:   $HERMES_DIR"
echo "  Agent ID:     $(hostname)"
echo ""
echo "  Community:    $AIOS_DIR/community/inbox/$(hostname)/"
echo "========================================="
