#!/bin/sh
# finctl installer — downloads the latest release binary for your platform,
# verifies its SHA256 checksum, and installs it to your PATH.
#
#   curl -fsSL https://install.finctl.io | sh
#
# Overrides (env vars):
#   FINCTL_VERSION  Install this version instead of latest (e.g. 0.2.0)
#   FINCTL_BIN_DIR  Install directory (default: /usr/local/bin, fallback ~/.local/bin)

set -eu

BASE_URL="https://install.finctl.io"

err() {
  printf 'finctl install: %s\n' "$1" >&2
  exit 1
}

# --- Detect OS ----------------------------------------------------------------
os="$(uname -s)"
case "$os" in
  Darwin) os="darwin" ;;
  Linux)  os="linux" ;;
  *)      err "unsupported OS '$os'. Supported: macOS, Linux. Windows: use install.ps1." ;;
esac

# --- Detect architecture ------------------------------------------------------
arch="$(uname -m)"
case "$arch" in
  x86_64|amd64)  arch="x64" ;;
  arm64|aarch64) arch="arm64" ;;
  *)             err "unsupported architecture '$arch'. Supported: x86_64, arm64." ;;
esac

asset="finctl-${os}-${arch}"

# --- Pick a downloader --------------------------------------------------------
if command -v curl >/dev/null 2>&1; then
  download()      { curl -fsSL "$1" -o "$2"; }
  download_text() { curl -fsSL "$1"; }
elif command -v wget >/dev/null 2>&1; then
  download()      { wget -qO "$2" "$1"; }
  download_text() { wget -qO- "$1"; }
else
  err "need curl or wget to download the binary."
fi

# --- Pick a checksum verifier -------------------------------------------------
if command -v sha256sum >/dev/null 2>&1; then
  checksum_of() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
  checksum_of() { shasum -a 256 "$1" | cut -d' ' -f1; }
else
  err "need sha256sum or shasum to verify the download."
fi

# --- Resolve version ----------------------------------------------------------
if [ -n "${FINCTL_VERSION:-}" ]; then
  version="$FINCTL_VERSION"
else
  version="$(download_text "${BASE_URL}/latest.json" | grep -o '"version":"[^"]*"' | cut -d'"' -f4)"
  [ -n "$version" ] || err "could not resolve latest version from ${BASE_URL}/latest.json"
fi

version_url="${BASE_URL}/v${version}"

# --- Download to temp dir -----------------------------------------------------
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

printf 'Downloading finctl %s (%s-%s)...\n' "$version" "$os" "$arch"
download "${version_url}/${asset}" "${tmp}/${asset}" \
  || err "download failed for ${version_url}/${asset}"

# --- Verify checksum ----------------------------------------------------------
printf 'Verifying checksum...\n'
download "${version_url}/checksums.txt" "${tmp}/checksums.txt" \
  || err "could not download checksums.txt from ${version_url}/checksums.txt"

expected="$(grep "  ${asset}$" "${tmp}/checksums.txt" | head -1 | cut -d' ' -f1)"
[ -n "$expected" ] || err "no checksum entry for '${asset}' in checksums.txt"

actual="$(checksum_of "${tmp}/${asset}")"
[ "$actual" = "$expected" ] \
  || err "checksum mismatch — refusing to install. Expected: ${expected}  Got: ${actual}"

# --- Choose install directory -------------------------------------------------
if [ -n "${FINCTL_BIN_DIR:-}" ]; then
  bindir="$FINCTL_BIN_DIR"
elif [ -w /usr/local/bin ]; then
  bindir="/usr/local/bin"
else
  bindir="${HOME}/.local/bin"
fi

mkdir -p "$bindir" || err "cannot create install directory '$bindir'."
chmod +x "${tmp}/${asset}"
mv "${tmp}/${asset}" "${bindir}/finctl" \
  || err "failed to install to '${bindir}/finctl'."

# macOS: remove quarantine so Gatekeeper does not block first run
if [ "$os" = "darwin" ]; then
  xattr -d com.apple.quarantine "${bindir}/finctl" 2>/dev/null || true
fi

# --- Report success -----------------------------------------------------------
printf 'Installed finctl to %s\n' "${bindir}/finctl"
installed_version="$("${bindir}/finctl" --version 2>/dev/null || printf 'unknown')"
printf 'Version: %s\n' "$installed_version"

case ":${PATH}:" in
  *":${bindir}:"*) ;;
  *) printf "Note: %s is not on your PATH. Add it:\n  export PATH=\"%s:\$PATH\"\n" "$bindir" "$bindir" ;;
esac

printf 'Done. Run '"'"'finctl --help'"'"' to get started.\n'
