#!/bin/bash
# ==========================================
# AEA SERVER MODULE: CORE AGENT
# VERSION: 1.01 (Bitmask Status Engine + Full Protocols)
# ==========================================
AEA_VERSION="1.01"
MODULE_DATE="2026-03-29"

# Строгий "API Контракт" с Android-приложением (AeaScriptManager.kt).
# ИНДЕКС в этом массиве ДОЛЖЕН совпадать с позицией бита в Kotlin и манифестом!
MODULES=(
    "aea_core"             # 0: Core Agent
    "aea_get_metrics"      # 1: Standalone Agent (executable)
    "aea_notifier"         # 2: Notifier Commander
    "aea_cleaner"          # 3: System Cleaner
    "aea_firewall"         # 4: Firewall Guard
    "aea_auto_updater"     # 5: Auto Updater
    "aea_outline"          # 6: Outline VPN
    "aea_amnezia_wg"       # 7: AmneziaWG
    "aea_vless_3xui"       # 8: VLESS (3x-ui)
    "aea_hysteria2"        # 9: Hysteria 2
    "aea_matrix_conduit"   # 10: Matrix Conduit
)

BIN_DIR="/usr/local/bin"

# ... (весь остальной код без изменений) ...
# 1. Version check for Android app
if [ "$1" == "--version" ] || [ "$1" == "-v" ]; then
    echo "$AEA_VERSION"
    exit 0
fi

# 2. STATUS MODE (Returns bitmask for Android UI)
if [ "$1" == "--status" ]; then
    MASK=0
    
    for i in "${!MODULES[@]}"; do
        MODULE_PATH="$BIN_DIR/${MODULES[$i]}"
        if [ -x "$MODULE_PATH" ]; then
            MASK=$((MASK | (1 << i)))
        fi
    done
    
    echo "$MASK"
    exit 0
fi

# 3. INSTALLATION MODE
echo "[AEA] Installing Core Agent environment (v$AEA_VERSION)..."
CORE_PATH="$BIN_DIR/aea_core"

cat "$0" > "$CORE_PATH"
chmod +x "$CORE_PATH"

echo "[AEA] Core Agent successfully installed! Available command: 'aea_core --status'"
exit 0
