#!/usr/bin/env sh
# mcpsheriff release installer (SHA-256 verified standalone binary with uv/pipx fallback)
# Usage: curl -fsSL https://normahubcc.github.io/mcpsheriff/install.sh | sh
#        curl -fsSL https://normahubcc.github.io/mcpsheriff/install.sh | sh -s -- --version 2.0.2
set -eu

DEFAULT_VERSION="2.0.2"
VERSION=""
METHOD="auto"
INSTALL_DIR="${MCPSHERIFF_INSTALL_DIR:-$HOME/.local/bin}"

while [ "$#" -gt 0 ]; do
  case "$1" in
    --version)
      if [ "$#" -lt 2 ]; then
        printf 'error: --version requires an argument\n' >&2
        exit 2
      fi
      VERSION="$2"
      shift 2
      ;;
    --method)
      if [ "$#" -lt 2 ]; then
        printf 'error: --method requires auto or python\n' >&2
        exit 2
      fi
      METHOD="$2"
      shift 2
      ;;
    --dir)
      if [ "$#" -lt 2 ]; then
        printf 'error: --dir requires a directory path\n' >&2
        exit 2
      fi
      INSTALL_DIR="$2"
      shift 2
      ;;
    -h|--help)
      cat <<'EOF'
mcpsheriff installer

Usage:
  curl -fsSL https://normahubcc.github.io/mcpsheriff/install.sh | sh
  curl -fsSL https://normahubcc.github.io/mcpsheriff/install.sh | sh -s -- --version 2.0.2

Options:
  --version <ver>   Install a specific release version (default: 2.0.2)
  --method <mode>   auto (binary + SHA256SUMS, else uv/pipx) or python
  --dir <path>      Destination directory for standalone binary (default: ~/.local/bin)
EOF
      exit 0
      ;;
    *)
      printf 'error: unknown option: %s\n' "$1" >&2
      exit 2
      ;;
  esac
done

TARGET_VER="${VERSION:-$DEFAULT_VERSION}"
TARGET_VER="${TARGET_VER#v}"
case "$TARGET_VER" in
  *[!A-Za-z0-9._-]*|"")
    printf 'error: invalid version format: %s\n' "$TARGET_VER" >&2
    exit 2
    ;;
esac

install_python_pkg() {
  pkg="mcpsheriff"
  if [ -n "$VERSION" ]; then
    pkg="mcpsheriff==${TARGET_VER}"
  fi
  if command -v uv >/dev/null 2>&1; then
    uv tool install --upgrade "$pkg"
    printf 'Installed %s via uv tool. Run: mcpsheriff scan\n' "$pkg"
    exit 0
  fi
  if command -v pipx >/dev/null 2>&1; then
    pipx install --force "$pkg"
    printf 'Installed %s via pipx. Run: mcpsheriff scan\n' "$pkg"
    exit 0
  fi
  printf 'error: binary unavailable for this platform and neither uv nor pipx is installed.\n' >&2
  printf 'Install uv first (https://docs.astral.sh/uv/getting-started/installation/) or use Homebrew/Docker.\n' >&2
  exit 1
}

if [ "$METHOD" = "python" ]; then
  install_python_pkg
elif [ "$METHOD" != "auto" ]; then
  printf 'error: unsupported --method: %s\n' "$METHOD" >&2
  exit 2
fi

OS="$(uname -s)"
ARCH="$(uname -m)"
ASSET=""
case "${OS}/${ARCH}" in
  Linux/x86_64|Linux/amd64)
    ASSET="mcpsheriff-linux-x64"
    ;;
  Linux/aarch64|Linux/arm64)
    ASSET="mcpsheriff-linux-arm64"
    ;;
  Darwin/arm64|Darwin/aarch64)
    ASSET="mcpsheriff-macos-arm64"
    ;;
  *)
    printf 'No standalone binary for %s/%s; falling back to uv/pipx...\n' "$OS" "$ARCH"
    install_python_pkg
    ;;
esac

if ! command -v curl >/dev/null 2>&1; then
  printf 'curl not found; falling back to uv/pipx...\n'
  install_python_pkg
fi

BASE_URL="https://github.com/normahubcc/mcpsheriff/releases/download/v${TARGET_VER}"
TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT INT TERM

printf 'Downloading %s v%s and verifying SHA256SUMS...\n' "$ASSET" "$TARGET_VER"
if ! curl -fsSL --retry 3 "${BASE_URL}/${ASSET}" -o "${TMPDIR}/${ASSET}" \
   || ! curl -fsSL --retry 3 "${BASE_URL}/SHA256SUMS" -o "${TMPDIR}/SHA256SUMS"; then
  printf 'Release asset download failed; falling back to uv/pipx...\n' >&2
  install_python_pkg
fi

EXPECTED_HASH="$(awk -v f="$ASSET" '$2 == f { print $1 }' "${TMPDIR}/SHA256SUMS")"
if [ -z "$EXPECTED_HASH" ]; then
  printf 'error: %s missing from SHA256SUMS\n' "$ASSET" >&2
  exit 1
fi

if command -v sha256sum >/dev/null 2>&1; then
  ACTUAL_HASH="$(sha256sum "${TMPDIR}/${ASSET}" | awk '{ print $1 }')"
elif command -v shasum >/dev/null 2>&1; then
  ACTUAL_HASH="$(shasum -a 256 "${TMPDIR}/${ASSET}" | awk '{ print $1 }')"
else
  printf 'error: neither sha256sum nor shasum found to verify release checksum\n' >&2
  exit 1
fi

if [ "$EXPECTED_HASH" != "$ACTUAL_HASH" ]; then
  printf 'error: SHA-256 checksum mismatch for %s (expected %s, got %s)\n' \
    "$ASSET" "$EXPECTED_HASH" "$ACTUAL_HASH" >&2
  exit 1
fi

mkdir -p "$INSTALL_DIR"
chmod 0755 "${TMPDIR}/${ASSET}"
mv -f "${TMPDIR}/${ASSET}" "${INSTALL_DIR}/mcpsheriff"
printf 'Verified SHA-256 (%s) and installed mcpsheriff v%s to %s/mcpsheriff\n' \
  "$EXPECTED_HASH" "$TARGET_VER" "$INSTALL_DIR"
case ":${PATH}:" in
  *":${INSTALL_DIR}:"*) ;;
  *) printf 'Note: add %s to PATH to run mcpsheriff directly.\n' "$INSTALL_DIR" ;;
esac
