#!/usr/bin/env bash
# mclovin public install script.
# Served at https://mclovin.org/install.sh.
#
# Downloads the latest pre-release binary from the public mclovin-release
# repo, verifies its sha256, and installs it.
#
# Usage:
#   curl -fsSL https://mclovin.org/install.sh | bash
#   curl -fsSL https://mclovin.org/install.sh | bash -s v0.1.0-alpha.1   # pin tag
#
# Supported: Linux x86_64, macOS arm64.

set -euo pipefail

REPO="guilhermeyo/mclovin-release"
VERSION="${1:-latest}"
BINARY_NAME="mclovin"

require_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    echo "error: '$1' is required but not installed" >&2
    exit 1
  fi
}

require_cmd curl

# ---- platform detection ----------------------------------------------

case "$(uname -s)" in
  Linux*)
    OS=linux
    require_cmd sha256sum
    # `sha256sum -c file.sha256` verifies the matching file by name.
    SHA_CMD="sha256sum -c"
    DEFAULT_INSTALL_DIR="$HOME/.local/bin"
    ;;
  Darwin*)
    OS=macos
    # macOS ships `shasum -a 256` by default. `sha256sum` only if the
    # user installed coreutils, which we don't want to depend on.
    require_cmd shasum
    SHA_CMD="shasum -a 256 -c"
    # `~/.cargo/bin` is the convention on macOS — it's the path the
    # .app's Swift launcher hardcodes (compiled by `mclovin setup`).
    DEFAULT_INSTALL_DIR="$HOME/.cargo/bin"
    ;;
  *)
    echo "error: unsupported OS (uname=$(uname -s))" >&2
    exit 1
    ;;
esac

INSTALL_DIR="${MCLOVIN_INSTALL_DIR:-$DEFAULT_INSTALL_DIR}"

case "$(uname -m)" in
  x86_64|amd64)
    ARCH="x86_64"
    ;;
  arm64|aarch64)
    if [ "$OS" = "macos" ]; then
      ARCH="arm64"
    else
      echo "error: Linux arm64 not yet shipped (uname -m=$(uname -m))" >&2
      exit 1
    fi
    ;;
  *)
    echo "error: unsupported architecture (uname -m=$(uname -m))" >&2
    exit 1
    ;;
esac

ASSET="mclovin-${OS}-${ARCH}"
SHA_ASSET="${ASSET}.sha256"

# ---- resolve version ------------------------------------------------

# Resolve "latest" to the most recent tag (including pre-releases).
# Uses the public Atom feed instead of the REST API — no auth, no
# rate limit, and the entries are already sorted newest-first.
if [ "$VERSION" = "latest" ]; then
  VERSION=$(curl -fsSL "https://github.com/${REPO}/releases.atom" \
    | grep -oE 'releases/tag/v[^"]+' \
    | head -n1 \
    | sed 's|releases/tag/||')
fi

if [ -z "$VERSION" ]; then
  echo "error: could not resolve a release version from GitHub" >&2
  echo "  Try pinning a tag: curl -fsSL https://mclovin.org/install.sh | bash -s v0.1.0-alpha.X" >&2
  exit 1
fi

DL_BASE="https://github.com/${REPO}/releases/download/${VERSION}"

echo "==> Installing mclovin ${VERSION}"
echo "    To: ${INSTALL_DIR}/${BINARY_NAME}"

mkdir -p "$INSTALL_DIR"
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT

curl -fsSL -o "${TMP}/${ASSET}"     "${DL_BASE}/${ASSET}"
curl -fsSL -o "${TMP}/${SHA_ASSET}" "${DL_BASE}/${SHA_ASSET}"

(cd "$TMP" && $SHA_CMD "$SHA_ASSET" >/dev/null)

install -m 755 "${TMP}/${ASSET}" "${INSTALL_DIR}/${BINARY_NAME}"

# Strip Gatekeeper quarantine xattr macOS attaches to curl-downloaded
# binaries. Without this, the first execution of mclovin pops a
# "downloaded from the internet" prompt and the .app's headless
# Swift launcher just fails when LSOpens deliver URLs to it.
# `xattr -d` returns non-zero when the attribute isn't set; expected.
if [ "$OS" = "macos" ]; then
  xattr -d com.apple.quarantine "${INSTALL_DIR}/${BINARY_NAME}" 2>/dev/null || true
fi

echo "==> Installed: ${INSTALL_DIR}/${BINARY_NAME}"

# ---- post-install ---------------------------------------------------

# Register as default browser. `mclovin setup` is idempotent — running
# it again on an already-set install is a no-op, so re-running this
# script is safe. On macOS, setup opens System Settings → Desktop &
# Dock for the user to pick mclovin as the default web browser (macOS
# 26 blocks programmatic setDefaultApplication for unsigned apps).
echo
if "${INSTALL_DIR}/${BINARY_NAME}" setup >/dev/null 2>&1; then
  echo "==> Registered as default browser"
else
  echo "!!  Auto-setup did not complete. Run \`${BINARY_NAME} setup\` manually," >&2
  echo "    or \`${BINARY_NAME} doctor\` to see what's missing." >&2
fi

# PATH hint when the install dir isn't on the user's PATH.
case ":$PATH:" in
  *":$INSTALL_DIR:"*) ;;
  *)
    echo
    echo "    NOTE: $INSTALL_DIR is not on your PATH. Add to your shell rc:"
    echo "      export PATH=\"$INSTALL_DIR:\$PATH\""
    ;;
esac

echo
if [ "$OS" = "macos" ]; then
  echo "Ready. Open mclovin from Spotlight or the Dock, or run: ${BINARY_NAME} settings"
else
  echo "Ready. Open mclovin from your app launcher (Walker, rofi, GNOME/KDE menu)"
  echo "or run: ${BINARY_NAME} settings"
fi
