#!/bin/sh
# Orbit one-line setup: clones the pinned release tag, then runs install.sh.
# POSIX sh. Fails closed on unpinned versions, missing release keys, and
# checksum/signature mismatch (see install.sh).
#
#   curl -fsSL https://orbit.gobrowse.dev/install.sh | sh -s -- --latest
#
# Flags are passed through to install.sh: --version (required), --yes,
# --dry-run, --upgrade, --uninstall, --profile pi|full.
set -eu
REPO="${ORBIT_RELEASE_REPO:-fedoragobrowse-design/orbit}"
DIR="${ORBIT_DIR:-./orbit}"
VERSION=""; LATEST=0
PASSTHROUGH=""
while [ $# -gt 0 ]; do
  case "$1" in
    --version) [ $# -ge 2 ] || { echo "error: --version needs a value, e.g. --version vX.Y.Z" >&2; exit 1; }; VERSION="$2"; PASSTHROUGH="$PASSTHROUGH --version $2"; shift 2;;
    --version=*) VERSION="${1#--version=}"; PASSTHROUGH="$PASSTHROUGH $1"; shift;;
    --latest) LATEST=1; PASSTHROUGH="$PASSTHROUGH --latest"; shift;;
    --yes|--dry-run|--upgrade|--uninstall) PASSTHROUGH="$PASSTHROUGH $1"; shift;;
    --profile) [ $# -ge 2 ] || { echo "error: --profile needs a value (pi or full)" >&2; exit 1; }; PASSTHROUGH="$PASSTHROUGH --profile $2"; shift 2;;
    --profile=*) PASSTHROUGH="$PASSTHROUGH $1"; shift;;
    --help|-h) echo "usage: curl -fsSL https://orbit.gobrowse.dev/install.sh | sh -s -- (--version vX.Y.Z | --latest) [--yes] [--dry-run] [--profile pi|full]"; exit 0;;
    *) echo "error: unknown flag: $1 (see --help)" >&2; exit 1;;
  esac
done
if [ "$LATEST" -eq 1 ]; then [ -z "$VERSION" ] || { echo "error: --latest and --version are exclusive" >&2; exit 1; }; RELEASE_JSON="$(curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 "https://api.github.com/repos/${REPO}/releases/latest" 2>/dev/null)" || { echo "error: --latest needs network to api.github.com" >&2; exit 1; }; VERSION="$(printf '%s' "$RELEASE_JSON" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"; [ -n "$VERSION" ] || { echo "error: --latest could not read tag_name" >&2; exit 1; }; echo "latest release: ${VERSION}"; fi
case "$VERSION" in v[0-9]*.[0-9]*.[0-9]*) ;; *) echo "error: refusing unpinned setup: pass --version vX.Y.Z or --latest (got '${VERSION:-empty}')" >&2; exit 1;; esac
command -v git >/dev/null 2>&1 || { echo "error: git is required" >&2; exit 1; }
command -v docker >/dev/null 2>&1 || { echo "error: docker is required (https://docs.docker.com/get-docker)" >&2; exit 1; }
if [ -d "$DIR/.git" ]; then
  echo "reusing checkout at $DIR"
  git -C "$DIR" fetch --tags --quiet || { echo "error: could not fetch release tags" >&2; exit 1; }
elif [ -e "$DIR" ]; then
  echo "error: $DIR exists from an earlier try and is not a git checkout - remove it (rm -rf $DIR) and run this line again" >&2; exit 1
else
  echo "cloning orbit ${VERSION} into ${DIR}"
  git clone --quiet --branch "$VERSION" --depth 1 "https://github.com/${REPO}.git" "$DIR" || { echo "error: release tag ${VERSION} not found" >&2; exit 1; }
fi
cd "$DIR"
git checkout --quiet "$VERSION" 2>/dev/null || true
# shellcheck disable=SC2086
exec ./install.sh $PASSTHROUGH
