#!/usr/bin/env bash
# cmux agy wrapper - installs cmux hooks and captures launch metadata.

find_real_agy() {
    local self_dir
    self_dir="$(cd "$(dirname "$0")" && pwd)"
    local IFS=:
    for d in $PATH; do
        [[ "$d" == "$self_dir" ]] && continue
        local candidate="$d/agy"
        [[ -x "$candidate" ]] || continue
        if [[ "$candidate" -ef "$0" ]]; then
            continue
        fi
        printf '%s' "$candidate"
        return 0
    done
    return 1
}

resolve_cmux_bin() {
    local bundled_cli self_dir
    bundled_cli="${CMUX_BUNDLED_CLI_PATH:-}"
    if [[ -n "$bundled_cli" && -x "$bundled_cli" ]]; then
        printf '%s' "$bundled_cli"
        return 0
    fi

    self_dir="$(cd "$(dirname "$0")" && pwd)"
    bundled_cli="$self_dir/cmux"
    if [[ -x "$bundled_cli" ]]; then
        printf '%s' "$bundled_cli"
        return 0
    fi

    bundled_cli="$(command -v cmux || true)"
    if [[ -n "$bundled_cli" ]]; then
        printf '%s' "$bundled_cli"
        return 0
    fi

    return 1
}

cmux_socket_available() {
    local socket="${CMUX_SOCKET_PATH:-}"
    [[ -n "$socket" && -S "$socket" ]] || return 1

    local cmux_bin
    cmux_bin="$(resolve_cmux_bin)" || return 1
    CMUXTERM_CLI_RESPONSE_TIMEOUT_SEC=0.75 \
        "$cmux_bin" --socket "$socket" ping >/dev/null 2>&1
}

encode_launch_argv() {
    {
        printf '%s\0' "$REAL_AGY"
        if (( $# > 0 )); then
            printf '%s\0' "$@"
        fi
    } | base64 | tr -d '\n'
}

agy_option_consumes_value() {
    case "$1" in
        --add-dir|--agent|--conversation|--effort|--input-format|--json-schema|\
        --log-file|--mode|--model|--output-format|--print|--print-timeout|\
        --prompt|--prompt-interactive|--project|--sandbox|-i|-p)
            return 0
            ;;
    esac
    return 1
}

agy_passthrough_option_flag() {
    case "$1" in
        --help|-h|--version|-v)
            return 0
            ;;
    esac
    return 1
}

agy_builtin_command_name() {
    case "$1" in
        agent|agents|changelog|help|install|mcp|models|plugin|plugins|\
        mic-serve|remote-control|update)
            return 0
            ;;
    esac
    return 1
}

# Install hooks for interactive and print-mode sessions, including explicit
# conversation resumes. Management commands must pass through untouched.
should_install_agy_hooks() {
    (( $# == 0 )) && return 0

    local arg
    local skip_next=false
    for arg in "$@"; do
        if [[ "$skip_next" == true ]]; then
            skip_next=false
            continue
        fi
        case "$arg" in
            --)
                return 0
                ;;
            -*)
                if agy_passthrough_option_flag "$arg"; then
                    return 1
                fi
                if [[ "$arg" != *=* ]] && agy_option_consumes_value "$arg"; then
                    skip_next=true
                fi
                continue
                ;;
            *)
                if agy_builtin_command_name "$arg"; then
                    return 1
                fi
                return 0
                ;;
        esac
    done

    return 0
}

REAL_AGY="$(find_real_agy)" || { echo "Error: agy not found in PATH" >&2; exit 127; }

IN_CMUX=0
if [[ -n "${CMUX_SURFACE_ID:-}" ]]; then
    IN_CMUX=1
fi

if [[ "$IN_CMUX" == "1" && "${CMUX_ANTIGRAVITY_HOOKS_DISABLED:-}" != "1" ]] \
    && should_install_agy_hooks "$@" \
    && cmux_socket_available; then
    CMUX_BIN="$(resolve_cmux_bin || true)"
    if [[ -n "$CMUX_BIN" ]]; then
        CMUXTERM_CLI_RESPONSE_TIMEOUT_SEC=2 \
            "$CMUX_BIN" --socket "$CMUX_SOCKET_PATH" hooks agy install --yes >/dev/null 2>&1 || true
    fi
    export CMUX_AGENT_LAUNCH_KIND="antigravity"
    export CMUX_AGENT_LAUNCH_EXECUTABLE="$REAL_AGY"
    export CMUX_AGENT_LAUNCH_ARGV_B64="$(encode_launch_argv "$@")"
    export CMUX_AGENT_LAUNCH_CWD="$PWD"
fi

exec "$REAL_AGY" "$@"
