#!/usr/bin/env bash
# InterLoop Thin Shell Quick Installer
set -e

SERVER="https://aiproxy.interloop.live"
TOKEN=""

while [[ "$#" -gt 0 ]]; do
    case $1 in
        --server) SERVER="$2"; shift ;;
        --token) TOKEN="$2"; shift ;;
        *) echo "Unknown parameter: $1"; exit 1 ;;
    esac
    shift
done

INSTALL_DIR="$HOME/.interloop"
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"

echo "📥 Downloading InterLoop Thin Shell..."
curl -fsSL "${SERVER}/shell/interloop-shell.py" -o "$INSTALL_DIR/interloop-shell.py"
chmod +x "$INSTALL_DIR/interloop-shell.py"

cat << EOF > "$INSTALL_DIR/interloop-shell.json"
{
  "server_url": "${SERVER}",
  "token": "${TOKEN}"
}
EOF

echo "✅ InterLoop Thin Shell installed to $INSTALL_DIR/interloop-shell.py"
echo "🚀 Starting Thin Shell background updater..."
nohup python3 "$INSTALL_DIR/interloop-shell.py" --config "$INSTALL_DIR/interloop-shell.json" > "$INSTALL_DIR/shell.log" 2>&1 &
echo "🎉 Thin Shell is now running in the background! (PID: $!)"
echo "   Logs: tail -f $INSTALL_DIR/shell.log"
