#!/bin/bash
set -eo pipefail

export TMPDIR="${TMPDIR:-/tmp}"

# Install build tools — try multiple package managers
if command -v apt-get &>/dev/null; then
    apt-get update && apt-get install -y curl build-essential git
elif command -v apk &>/dev/null; then
    apk add --no-cache curl bash build-base git python3 py3-pip
elif command -v yum &>/dev/null; then
    yum install -y curl git gcc make
elif command -v dnf &>/dev/null; then
    dnf install -y curl git gcc make
else
    echo "Warning: No known package manager found, assuming build tools are available" >&2
fi

# Install uv
curl -LsSf https://astral.sh/uv/0.7.13/install.sh | sh

# Ensure $HOME/.local/bin is in PATH via .bashrc
if ! grep -q 'export PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then
    echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc"
fi

# Source the uv environment
source "$HOME/.local/bin/env"

# Install mini-swe-agent from PyPI
{% if version %}
uv tool install mini-swe-agent=={{ version }}
{% else %}
uv tool install mini-swe-agent
{% endif %}

mini-swe-agent --help
echo "INSTALL_SUCCESS"
