#!/bin/sh
# Outfit v0.1.0-alpha.3. Generated from a release lock; all downloads verify before execution.
# The final function invocation is deliberately last so a truncated download cannot start setup.
outfit_bootstrap() {
  set -eu
  case "$(uname -s)" in Darwin) os=darwin ;; Linux) os=linux ;; *) echo 'Use install.ps1 or a supported Linux/macOS shell.' >&2; exit 1 ;; esac
  case "$(uname -m)" in arm64|aarch64) arch=arm64 ;; x86_64|amd64) arch=amd64 ;; *) echo 'Unsupported architecture.' >&2; exit 1 ;; esac
  case "${os}_${arch}" in
    darwin_amd64) expected=3b46228670f5b8bef90ba6f581a730f458c9bc6e8785fa29093bae6f369552fb ;;
    darwin_arm64) expected=15ad8fa4062e54b79b9f0ca8164871a03f47e82e9092de4ae8161f8e82e39fb6 ;;
    linux_amd64) expected=a50edcaa6e29b09fc49551c0db64258404395556f496d4a925e1c2c644f30429 ;;
    linux_arm64) expected=83f794bde5778dcf7c2a77b0966c361ffc087fb9854d50411e835ed3c243c436 ;;
    *) echo 'No verified release for this platform.' >&2; exit 1 ;;
  esac
  command -v curl >/dev/null 2>&1 || { echo 'curl is required.' >&2; exit 1; }
  tmp=$(mktemp -d "${TMPDIR:-/tmp}/outfit.XXXXXXXX")
  trap 'rm -rf "$tmp"' EXIT
  trap 'exit 130' INT
  trap 'exit 143' TERM
  binary="$tmp/outfit"
  printf '  [1/3] Downloading Outfit v0.1.0-alpha.3 for %s/%s...\n' "$os" "$arch" >&2
  curl -fsSL --proto '=https' --proto-redir '=https' --retry 2 --connect-timeout 15 --max-time 300 'https://github.com/johnhenning/outfit/releases/download/v0.1.0-alpha.3/outfit_v0.1.0-alpha.3_'"${os}_${arch}" -o "$binary"
  printf '  [2/3] Verifying the download...\n' >&2
  if command -v sha256sum >/dev/null 2>&1; then
    actual=$(sha256sum "$binary"); actual=${actual%% *}
  elif command -v shasum >/dev/null 2>&1; then
    actual=$(shasum -a 256 "$binary"); actual=${actual%% *}
  elif command -v openssl >/dev/null 2>&1; then
    actual=$(openssl dgst -sha256 "$binary"); actual=${actual##* }
  else
    echo 'A SHA-256 tool (sha256sum, shasum or openssl) is required.' >&2; exit 1
  fi
  [ "$actual" = "$expected" ] || { echo 'Outfit checksum mismatch; nothing was executed.' >&2; exit 1; }
  chmod 700 "$binary"
  # The downloaded program gets terminal input, never the script's pipe.
  printf '  [3/3] Starting setup with a temporary CLI...\n' >&2
  unattended=false
  for argument in "$@"; do case "$argument" in --yes|-y|--dry-run) unattended=true ;; esac; done
  if [ "$unattended" = true ]; then
    "$binary" setup --source 'https://github.com/johnhenning/outfit-profiles/releases/download/v0.1.0-alpha.2/outfit-profiles_v0.1.0-alpha.2.tar.gz' --sha256 'f1e92263e7f36846bedfad2082414ed0b2b25d24b03b7a05007af2226eb7fe67' --profile 'media' "$@"
  elif [ -t 0 ]; then
    "$binary" setup --source 'https://github.com/johnhenning/outfit-profiles/releases/download/v0.1.0-alpha.2/outfit-profiles_v0.1.0-alpha.2.tar.gz' --sha256 'f1e92263e7f36846bedfad2082414ed0b2b25d24b03b7a05007af2226eb7fe67' --profile 'media' "$@"
  elif ( : </dev/tty ) 2>/dev/null; then
    "$binary" setup --source 'https://github.com/johnhenning/outfit-profiles/releases/download/v0.1.0-alpha.2/outfit-profiles_v0.1.0-alpha.2.tar.gz' --sha256 'f1e92263e7f36846bedfad2082414ed0b2b25d24b03b7a05007af2226eb7fe67' --profile 'media' "$@" </dev/tty
  else
    echo 'Interactive setup needs a terminal. Inspect the profile, then rerun with --yes for unattended setup.' >&2
    exit 1
  fi
}
outfit_bootstrap "$@"
