#!/bin/sh
# install.sh — installer for the bettor-help CLI.
#
#   curl -fsSL https://get.bettor.help | sh
#   curl -fsSL https://get.bettor.help | sh -s -- --channel dev
#
# Supports macOS + Linux (darwin/linux, amd64/arm64). For Windows, use
# install.ps1 instead.
set -eu

BASE="${BETTOR_HELP_INSTALL_BASE:-https://get.bettor.help}"
CHANNEL="stable"

while [ $# -gt 0 ]; do
  case "$1" in
    --channel)
      if [ $# -lt 2 ]; then
        echo "error: --channel requires an argument (stable|dev)" >&2
        exit 1
      fi
      CHANNEL="$2"
      shift 2
      ;;
    --channel=*)
      CHANNEL="${1#--channel=}"
      shift
      ;;
    -h|--help)
      echo "usage: install.sh [--channel stable|dev]"
      exit 0
      ;;
    *)
      echo "unknown option: $1" >&2
      exit 1
      ;;
  esac
done

if ! command -v curl >/dev/null 2>&1; then
  echo "error: curl is required to install bettor-help but was not found in PATH" >&2
  exit 1
fi

if ! command -v mktemp >/dev/null 2>&1; then
  echo "error: mktemp is required to install bettor-help but was not found in PATH" >&2
  exit 1
fi

case "$CHANNEL" in
  stable) MANIFEST_URL="$BASE/latest.json" ;;
  dev)    MANIFEST_URL="$BASE/dev/latest.json" ;;
  *) echo "unknown channel: $CHANNEL (stable|dev)" >&2; exit 1 ;;
esac

OS=$(uname -s | tr '[:upper:]' '[:lower:]')   # darwin|linux
case "$OS" in
  darwin|linux) ;;
  *) echo "unsupported OS: $OS (bettor-help supports darwin and linux; use install.ps1 on Windows)" >&2; exit 1 ;;
esac

ARCH=$(uname -m)
case "$ARCH" in
  x86_64|amd64) ARCH=amd64 ;;
  aarch64|arm64) ARCH=arm64 ;;
  *) echo "unsupported arch: $ARCH" >&2; exit 1 ;;
esac
KEY="${OS}_${ARCH}"

MANIFEST=$(curl -fsSL "$MANIFEST_URL") || {
  echo "error: could not fetch $MANIFEST_URL — check your network and retry" >&2; exit 1; }

if [ -z "$MANIFEST" ]; then
  echo "error: manifest at $MANIFEST_URL was empty — check your network and retry" >&2
  exit 1
fi

# No jq dependency: extract url + sha256 for our platform key with sed.
ENTRY=$(printf '%s' "$MANIFEST" | tr -d '\n' | sed -n "s/.*\"$KEY\"[^{]*{\([^}]*\)}.*/\1/p")
URL=$(printf '%s' "$ENTRY" | sed -n 's/.*"url"[^"]*"\([^"]*\)".*/\1/p')
SHA=$(printf '%s' "$ENTRY" | sed -n 's/.*"sha256"[^"]*"\([^"]*\)".*/\1/p')
[ -n "$URL" ] && [ -n "$SHA" ] || { echo "no binary for $KEY in manifest" >&2; exit 1; }

TMP=$(mktemp)
trap 'rm -f "$TMP"' EXIT INT TERM

curl -fsSL "$URL" -o "$TMP" || { echo "download failed — retry: curl -fsSL $BASE | sh" >&2; exit 1; }

if [ ! -s "$TMP" ]; then
  echo "error: downloaded file for $KEY is empty — retry: curl -fsSL $BASE | sh" >&2
  exit 1
fi

ACTUAL=$( (shasum -a 256 "$TMP" 2>/dev/null || sha256sum "$TMP" 2>/dev/null) | cut -d' ' -f1 )
if [ -z "$ACTUAL" ]; then
  echo "error: no sha256 tool found (need shasum or sha256sum) — cannot verify download" >&2
  exit 1
fi
[ "$ACTUAL" = "$SHA" ] || { echo "checksum mismatch — aborting" >&2; exit 1; }

DEST="$HOME/.local/bin"
mkdir -p "$DEST" 2>/dev/null || DEST="/usr/local/bin"
mkdir -p "$DEST" 2>/dev/null || {
  echo "error: could not create install directory $DEST — try running with sufficient permissions" >&2
  exit 1
}

if ! install -m 755 "$TMP" "$DEST/bettor-help" 2>/dev/null; then
  echo "error: could not install to $DEST/bettor-help — try running with sufficient permissions (e.g. sudo)" >&2
  exit 1
fi

if [ "$CHANNEL" = "dev" ]; then "$DEST/bettor-help" update --channel dev --check >/dev/null 2>&1 || true; fi

VERSION=$(printf '%s' "$MANIFEST" | tr -d '\n' | sed -n 's/.*"version"[^"]*"\([^"]*\)".*/\1/p')
echo "installed bettor-help ${VERSION:-unknown} to $DEST/bettor-help"
case ":$PATH:" in *":$DEST:"*) ;; *) echo "note: add $DEST to your PATH" ;; esac
