#!/bin/sh
# Columbia Pages CLI installer.
#
#   curl -fsSL https://pages.col-agents.com/install | sh
#
# Downloads the current platform binary through the SvelteKit release
# endpoints, verifies it against the release checksum manifest, and installs
# it at ~/.local/bin/cpages by default.
set -eu

ORIGIN="${COLUMBIA_PAGES_ORIGIN:-https://pages.col-agents.com}"
INSTALL_DIR="${COLUMBIA_PAGES_INSTALL_DIR:-$HOME/.local/bin}"

log() {
  printf 'cpages-install: %s\n' "$*" >&2
}

die() {
  log "$*"
  exit 1
}

need() {
  command -v "$1" >/dev/null 2>&1 || die "'$1' is required to install cpages"
}

detect_os() {
  case "$(uname -s)" in
    Linux) echo linux ;;
    Darwin) echo darwin ;;
    *) die "unsupported OS: $(uname -s) (cpages ships Linux and macOS binaries)" ;;
  esac
}

detect_arch() {
  case "$(uname -m)" in
    x86_64 | amd64) echo amd64 ;;
    aarch64 | arm64) echo arm64 ;;
    *) die "unsupported architecture: $(uname -m) (cpages ships amd64 and arm64 binaries)" ;;
  esac
}

need curl
need uname
need mktemp
need mkdir
need chmod
need mv
need awk

os=$(detect_os)
arch=$(detect_arch)
asset="cpages_${os}_${arch}"

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT INT TERM HUP

log "resolving latest version..."
version=$(curl -fsSL "${ORIGIN}/releases/latest") \
  || die "could not resolve the latest version from ${ORIGIN}/releases/latest"

case "$version" in
  *[!0-9.]* | "" | .* | *. | *..* | *.*.*.*)
    die "unexpected version string from ${ORIGIN}/releases/latest: '${version}'" ;;
  *.*.*) ;;
  *) die "unexpected version string from ${ORIGIN}/releases/latest: '${version}'" ;;
esac

base_url="${ORIGIN}/releases/${version}"
asset_url="${base_url}/${asset}"
checksums_url="${base_url}/checksums.txt"

log "downloading ${asset} ${version}..."
curl -fsSL "$asset_url" -o "$tmpdir/$asset" \
  || die "download failed: $asset_url"
curl -fsSL "$checksums_url" -o "$tmpdir/checksums.txt" \
  || die "download failed: $checksums_url"

expected=$(awk -v want="$asset" '$2 == want { print $1; found=1 } END { if (!found) exit 1 }' "$tmpdir/checksums.txt") \
  || die "no checksum entry for ${asset} in checksums.txt"

if command -v sha256sum >/dev/null 2>&1; then
  actual=$(sha256sum "$tmpdir/$asset" | awk '{ print $1 }')
elif command -v shasum >/dev/null 2>&1; then
  actual=$(shasum -a 256 "$tmpdir/$asset" | awk '{ print $1 }')
else
  die "neither sha256sum nor shasum found; cannot verify ${asset}"
fi

[ "$actual" = "$expected" ] \
  || die "checksum mismatch for ${asset}: got ${actual}, want ${expected}"

mkdir -p "$INSTALL_DIR"
chmod 0755 "$tmpdir/$asset"
mv "$tmpdir/$asset" "$INSTALL_DIR/cpages"

log "installed cpages to ${INSTALL_DIR}/cpages"
case ":${PATH}:" in
  *":${INSTALL_DIR}:"*) ;;
  *) log "note: add ${INSTALL_DIR} to your PATH" ;;
esac

if installed=$("$INSTALL_DIR/cpages" version 2>/dev/null); then
  log "${installed} ready"
else
  die "installed binary did not run cleanly"
fi
