#!/bin/sh
# Generated by pmat installer
# This is a proof-of-concept static shell script
set -euf

# Constants
readonly REPO_URL="https://github.com/paiml/paiml-mcp-agent-toolkit"
readonly DEFAULT_INSTALL_DIR="${HOME}/.local/bin"

# Error handling
trap 'echo "Installation failed" >&2; exit 1' ERR

# Cleanup function to remove test artifacts
cleanup_artifacts() {
    # Remove any tar.gz files that might be test artifacts in current directory
    # Only remove files with suspicious names that indicate test failures
    for file in *-platform.tar.gz nonexistent-platform.tar.gz; do
        if [ -f "$file" ]; then
            rm -f "$file" 2>/dev/null || true
        fi
    done
}

main() {
    # Parse arguments
    install_dir="${1:-$DEFAULT_INSTALL_DIR}"
    version="${2:-latest}"
    
    # Platform detection
    os=$(uname -s)
    arch=$(uname -m)
    
    case "${os}_${arch}" in
        Linux_x86_64)
            platform="x86_64-unknown-linux-gnu"
            ;;
        Linux_aarch64)
            platform="aarch64-unknown-linux-gnu"
            ;;
        Darwin_x86_64)
            platform="x86_64-apple-darwin"
            ;;
        Darwin_arm64|Darwin_aarch64)
            platform="aarch64-apple-darwin"
            ;;
        *)
            echo "Unsupported platform: ${os}_${arch}" >&2
            exit 1
            ;;
    esac
    
    # Create temp directory with cleanup trap
    temp_dir=$(mktemp -d)
    trap 'rm -rf "$temp_dir"; cleanup_artifacts' EXIT
    
    # Construct URLs
    if [ "$version" = "latest" ]; then
        # Get latest version from GitHub API
        version=$(curl -sSfL "${REPO_URL}/releases/latest" | grep -o 'tag/v[0-9.]*' | head -1 | cut -d'v' -f2)
    fi
    
    base_url="${REPO_URL}/releases/download"
    binary_url="${base_url}/v${version}/paiml-mcp-agent-toolkit-${platform}.tar.gz"
    checksum_url="${binary_url}.sha256"
    
    echo "Installing pmat v${version} for ${platform}..."
    
    # Download binary
    echo "Downloading from ${binary_url}..."
    curl -sSfL --max-time 300 --retry 3 -o "${temp_dir}/archive.tar.gz" "$binary_url"
    
    # Download and verify checksum
    echo "Verifying checksum..."
    expected_checksum=$(curl -sSfL "$checksum_url" | cut -d' ' -f1)
    actual_checksum=$(sha256sum "${temp_dir}/archive.tar.gz" | cut -d' ' -f1)
    
    if [ "$expected_checksum" != "$actual_checksum" ]; then
        echo "Checksum verification failed!" >&2
        echo "Expected: $expected_checksum" >&2
        echo "Actual: $actual_checksum" >&2
        exit 1
    fi
    
    # Extract archive
    echo "Extracting..."
    tar -xzf "${temp_dir}/archive.tar.gz" -C "$temp_dir"
    
    # Create install directory if needed
    [ -d "$install_dir" ] || mkdir -p "$install_dir"
    
    # Install atomically
    echo "Installing to ${install_dir}/pmat..."
    mv -f "${temp_dir}/pmat" "${install_dir}/pmat"
    chmod 755 "${install_dir}/pmat"
    
    echo "Installation complete!"
    echo ""
    echo "To use pmat, add ${install_dir} to your PATH:"
    echo "  export PATH=\"${install_dir}:\$PATH\""
}

# Run main with all arguments
main "$@"
