#!/usr/bin/env sh
# Bootstrap spanreed on Linux/macOS, then run interactive setup.
#
# Public one-liner (canonical — served by fabrials.com, not GitHub Releases):
#   curl -fsSL https://fabrials.com/install/spanreed.sh | sh
#
# Local / from a checkout:
#   ./scripts/install.sh --from-path ./target/release/spanreed
#   ./scripts/install.sh --yes --service
#
# Env:
#   SPANREED_REPO   GitHub owner/repo for binary assets (default: grok-insider/spanreed)
#   SPANREED_TAG    Release tag (default: latest)
#
# Binaries always come from GitHub Releases; this script is only the bootstrapper.

set -eu

REPO="${SPANREED_REPO:-grok-insider/spanreed}"
TAG="${SPANREED_TAG:-latest}"
FROM_PATH=""
SETUP_ARGS=""

usage() {
  echo "usage: install.sh [--from-path PATH] [--yes] [--service] [--dry-run] [--no-wire]"
}

while [ $# -gt 0 ]; do
  case "$1" in
    --from-path)
      FROM_PATH="${2:-}"
      shift 2
      ;;
    --yes|-y|--service|--dry-run|--no-wire|--from-current-exe)
      SETUP_ARGS="$SETUP_ARGS $1"
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "unknown arg: $1" >&2
      usage >&2
      exit 1
      ;;
  esac
done

os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$arch" in
  x86_64|amd64) arch="x86_64" ;;
  aarch64|arm64) arch="aarch64" ;;
  *)
    echo "unsupported arch: $arch" >&2
    exit 1
    ;;
esac

# Release assets are static musl on Linux (cargo-zigbuild).
case "$os" in
  linux) target="${arch}-unknown-linux-musl" ;;
  darwin) target="${arch}-apple-darwin" ;;
  *)
    echo "unsupported OS: $os" >&2
    exit 1
    ;;
esac

install_dir="${HOME}/.local/bin"
mkdir -p "$install_dir"
dest="${install_dir}/spanreed"

http_get() {
  # $1 url  $2 outfile (optional: stdout)
  if command -v curl >/dev/null 2>&1; then
    if [ -n "${2:-}" ]; then
      curl -fsSL "$1" -o "$2"
    else
      curl -fsSL "$1"
    fi
  else
    if [ -n "${2:-}" ]; then
      wget -qO "$2" "$1"
    else
      wget -qO- "$1"
    fi
  fi
}

resolve_tag() {
  if [ "$TAG" != "latest" ]; then
    echo "$TAG"
    return
  fi
  # Asset names include the semver: spanreed-0.0.1-<target>.tar.gz
  api="https://api.github.com/repos/${REPO}/releases/latest"
  tag=$(http_get "$api" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n1)
  if [ -z "$tag" ]; then
    echo "could not resolve latest release tag for ${REPO}" >&2
    exit 1
  fi
  echo "$tag"
}

if [ -n "$FROM_PATH" ]; then
  if [ ! -f "$FROM_PATH" ]; then
    echo "binary not found: $FROM_PATH" >&2
    exit 1
  fi
  cp "$FROM_PATH" "$dest"
  chmod 755 "$dest"
  echo "Installed $dest from $FROM_PATH"
else
  tag=$(resolve_tag)
  version="${tag#v}"
  base="https://github.com/${REPO}/releases/download/${tag}"
  asset="spanreed-${version}-${target}.tar.gz"
  url="${base}/${asset}"
  tmp=$(mktemp -d)
  trap 'rm -rf "$tmp"' EXIT
  echo "Downloading $url"
  http_get "$url" "$tmp/$asset"
  tar -xzf "$tmp/$asset" -C "$tmp"
  bin=$(find "$tmp" -type f -name spanreed | head -n1)
  if [ -z "$bin" ]; then
    echo "archive did not contain spanreed" >&2
    exit 1
  fi
  cp "$bin" "$dest"
  chmod 755 "$dest"
  echo "Installed $dest (release ${tag})"
fi

# shellcheck disable=SC2086
exec "$dest" setup --from-current-exe $SETUP_ARGS
