#!/bin/bash
# Hermes Agent — macOS Installer
# Double-click this file to run
# Uses osascript for GUI dialogs — no terminal knowledge needed

# ===== GUI via osascript =====

# Welcome
osascript -e '
    set result to button returned of (display dialog "Hermes Agent — macOS Installer

This will install Hermes Agent to your system.

What it does:
  • Installs Python, Git, and dependencies (via Homebrew if needed)
  • Downloads Hermes Agent from Nous Research
  • Sets up ~/.hermes directory and configuration
  • Creates an Applications folder shortcut

No root password needed for most steps.

Continue?" buttons {"Install", "Cancel"} default button 1 with icon note with title "Hermes Agent")

    if result is "Cancel" then error number -128
'

if [ $? -ne 0 ]; then exit 0; fi

# ===== CHECK PREREQUISITES =====
check_and_install_deps() {
    needs_brew=false
    needs_git=false

    if ! command -v brew &>/dev/null; then
        needs_brew=true
    fi

    if ! command -v git &>/dev/null; then
        needs_git=true
    fi

    if [ "$needs_brew" = true ]; then
        osascript -e '
            display dialog "Homebrew (package manager) is needed.

This installer will install Homebrew first.
It may ask for your password — this is normal.

Click OK to continue, or Cancel to install Homebrew manually from brew.sh" buttons {"OK", "Cancel"} default button 1 with icon note with title "Install Homebrew"
        '
        if [ $? -ne 0 ]; then exit 0; fi

        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || {
            osascript -e 'display dialog "Homebrew installation failed.

Please install it manually:
1. Open Terminal
2. Run: /bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"
3. Then run this installer again." buttons {"OK"} with icon stop with title "Installation Failed"'
            exit 1
        }
    fi

    if [ "$needs_git" = true ]; then
        if ! xcode-select --version &>/dev/null; then
            osascript -e 'display dialog "Command Line Tools (Git) are needed.

A popup will appear asking to install.
Click Install and wait for it to finish." buttons {"OK"} default button 1 with icon note'
            xcode-select --install || {
                osascript -e 'display dialog "Command Line Tools installation failed.

Please install manually:
1. Open Terminal
2. Run: xcode-select --install
3. Then run this installer again." buttons {"OK"} with icon stop with title "Installation Failed"'
                exit 1
            }
        fi
    fi
}

# ===== PROGRESS VIA NOTIFICATIONS =====
notify() {
    osascript -e "display notification \"$2\" with title \"Hermes Agent\" subtitle \"$1\"" 2>/dev/null
}

# ===== MAIN INSTALL =====
install_hermes() {
    notify "Step 1 of 5" "Installing dependencies..."
    check_and_install_deps

    notify "Step 2 of 5" "Setting up directories..."
    mkdir -p "$HOME/.hermes"

    notify "Step 3 of 5" "Downloading Hermes Agent..."
    if [ -d "$HOME/.hermes/hermes-agent/.git" ]; then
        cd "$HOME/.hermes/hermes-agent"
        git pull origin main
    else
        cd "$HOME/.hermes"
        git clone --branch main https://github.com/NousResearch/hermes-agent.git
    fi

    notify "Step 4 of 5" "Installing Python dependencies..."
    cd "$HOME/.hermes/hermes-agent"

    # Install uv
    if ! command -v uv &>/dev/null; then
        curl -LsSf https://astral.sh/uv/install.sh | sh 2>/dev/null
        export PATH="$HOME/.local/bin:$PATH"
    fi

    # Create venv and install
    uv venv venv --python 3.11 2>/dev/null || true
    VIRTUAL_ENV="$HOME/.hermes/hermes-agent/venv" uv pip install -e '.[all]' 2>/dev/null || \
    VIRTUAL_ENV="$HOME/.hermes/hermes-agent/venv" uv pip install -e '.'

    notify "Step 5 of 5" "Creating shortcuts..."

    # Create hermes command symlink
    mkdir -p "$HOME/.local/bin"
    ln -sf "$HOME/.hermes/hermes-agent/venv/bin/hermes" "$HOME/.local/bin/hermes"
    chmod +x "$HOME/.local/bin/hermes"

    # Create config directories
    mkdir -p "$HOME/.hermes"/{cron,sessions,logs,pairing,memories,skills,whatsapp/session}

    # Copy env template
    if [ -f "$HOME/.hermes/hermes-agent/.env.example" ] && [ ! -f "$HOME/.hermes/.env" ]; then
        cp "$HOME/.hermes/hermes-agent/.env.example" "$HOME/.hermes/.env"
    fi

    # Add to PATH in shell configs
    for rc in "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.bash_profile" "$HOME/.profile"; do
        if [ -f "$rc" ] || [ "$rc" = "$HOME/.zshrc" ]; then
            touch "$rc"
            if ! grep -q '\.local/bin' "$rc" 2>/dev/null; then
                echo '' >> "$rc"
                echo '# Hermes Agent' >> "$rc"
                echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$rc"
            fi
        fi
    done

    # Create Applications shortcut
    cat > "/Applications/Hermes Agent.command" <<'APPEOF'
#!/bin/bash
cd "$HOME"
export PATH="$HOME/.local/bin:$PATH"
source ~/.zshrc 2>/dev/null || source ~/.bashrc 2>/dev/null
exec "$HOME/.local/bin/hermes"
APPEOF
    chmod +x "/Applications/Hermes Agent.command"
}

# ===== RUN =====
install_hermes

# ===== SUCCESS =====
osascript -e '
    display dialog "Hermes Agent has been installed successfully!

Next steps:

1. Open a new Terminal window
   (or run: source ~/.zshrc)

2. Run the setup wizard:
   hermes setup

3. Start chatting:
   hermes

4. Or double-click \"Hermes Agent\"
   in your Applications folder

Need help?
• Docs: hermes-agent.nousresearch.com/docs
• Discord: catfish_011230" buttons {"Got it!"} default button 1 with icon note with title "Installation Complete!"
'
