#!/bin/sh
# Resistro installer — https://resistro.org
# Usage: curl -fsSL https://resistro.org/install.sh | sh
set -e

REPO_URL="https://resistro.org/releases"

# --- Detect OS and Architecture ---
detect_platform() {
    OS=$(uname -s | tr '[:upper:]' '[:lower:]')
    ARCH=$(uname -m)

    case "$OS" in
        linux)  OS="linux" ;;
        darwin) OS="darwin" ;;
        *)      echo "Error: Unsupported OS: $OS"; exit 1 ;;
    esac

    case "$ARCH" in
        x86_64|amd64)   ARCH="amd64" ;;
        aarch64|arm64)  ARCH="arm64" ;;
        armv7l|armhf)   ARCH="arm_armv7" ;;
        *)              echo "Error: Unsupported architecture: $ARCH"; exit 1 ;;
    esac
}

# --- Determine install directory ---
detect_install_dir() {
    if [ "$(id -u)" -eq 0 ]; then
        INSTALL_DIR="/usr/local/bin"
    else
        INSTALL_DIR="$HOME/.local/bin"
        mkdir -p "$INSTALL_DIR"
    fi
}

# --- Download helpers ---
download() {
    if command -v curl >/dev/null 2>&1; then
        curl -fsSL "$1"
    elif command -v wget >/dev/null 2>&1; then
        wget -qO- "$1"
    else
        echo "Error: curl or wget required"
        exit 1
    fi
}

download_file() {
    if command -v curl >/dev/null 2>&1; then
        curl -fsSL -o "$2" "$1"
    elif command -v wget >/dev/null 2>&1; then
        wget -qO "$2" "$1"
    fi
}

# --- Main ---
main() {
    echo "Resistro Installer"
    echo "=================="
    echo

    detect_platform
    detect_install_dir

    BINARY="resistro_${OS}_${ARCH}"
    echo "  Platform:  ${OS}/${ARCH}"
    echo "  Install:   ${INSTALL_DIR}/resistro"
    echo

    # Get latest version
    VERSION=$(download "${REPO_URL}/latest/version.txt" 2>/dev/null) || true
    if [ -z "$VERSION" ]; then
        echo "Error: Could not determine latest version"
        exit 1
    fi
    echo "  Version:   v${VERSION}"
    echo

    # Download binary
    echo "Downloading ${BINARY}..."
    TMPDIR=$(mktemp -d)
    trap 'rm -rf "$TMPDIR"' EXIT

    download_file "${REPO_URL}/latest/${BINARY}" "${TMPDIR}/resistro"

    if [ ! -s "${TMPDIR}/resistro" ]; then
        echo "Error: Download failed"
        exit 1
    fi

    # Verify checksum if available
    CHECKSUMS=$(download "${REPO_URL}/latest/checksums.txt" 2>/dev/null) || true
    if [ -n "$CHECKSUMS" ]; then
        EXPECTED=$(echo "$CHECKSUMS" | grep "$BINARY" | awk '{print $1}')
        if [ -n "$EXPECTED" ]; then
            ACTUAL=$(sha256sum "${TMPDIR}/resistro" 2>/dev/null | awk '{print $1}' || shasum -a 256 "${TMPDIR}/resistro" | awk '{print $1}')
            if [ "$EXPECTED" != "$ACTUAL" ]; then
                echo "Error: Checksum mismatch!"
                echo "  Expected: ${EXPECTED}"
                echo "  Got:      ${ACTUAL}"
                exit 1
            fi
            echo "  Checksum:  verified"
        fi
    fi

    # Install
    chmod +x "${TMPDIR}/resistro"
    mv "${TMPDIR}/resistro" "${INSTALL_DIR}/resistro"

    echo
    echo "Installed resistro v${VERSION} to ${INSTALL_DIR}/resistro"

    # Check if install dir is in PATH
    case ":$PATH:" in
        *":${INSTALL_DIR}:"*) ;;
        *)
            echo
            echo "Note: ${INSTALL_DIR} is not in your PATH."
            echo "Add it with:  export PATH=\"${INSTALL_DIR}:\$PATH\""
            ;;
    esac

    echo
    "${INSTALL_DIR}/resistro" version 2>/dev/null | head -3 || true
    echo
    echo "Run 'resistro --help' to get started."
}

main
