#!/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"
  printf '  [3/3] Installing the CLI...\n' >&2
  install_dir=${OUTFIT_INSTALL_DIR:-"$HOME/.local/bin"}
  mkdir -p "$install_dir"
  install_dir=$(cd "$install_dir" && pwd -P)
  [ ! -L "$install_dir/outfit" ] || { echo 'Refusing to replace a symlink at the Outfit install path.' >&2; exit 1; }
  staged=$(mktemp "$install_dir/.outfit-XXXXXXXX")
  cp "$binary" "$staged"
  chmod 755 "$staged"
  mv -f "$staged" "$install_dir/outfit"

  # Quote paths as literal shell words, including spaces and apostrophes.
  outfit_quote_path() {
    _outfit_value=$1
    printf "'"
    while :; do
      case $_outfit_value in
        *"'"*)
          printf '%s' "${_outfit_value%%"'"*}" "'\''"
          _outfit_value=${_outfit_value#*"'"}
          ;;
        *) printf "%s'" "$_outfit_value"; break ;;
      esac
    done
  }

  if [ -t 1 ] && [ "${TERM:-dumb}" != dumb ] && [ -z "${NO_COLOR+x}" ]; then
    printf '\n  \033[32m[OK] Outfit v0.1.0-alpha.3 installed successfully.\033[0m\n'
  else
    printf '\n  [OK] Outfit v0.1.0-alpha.3 installed successfully.\n'
  fi
  printf '       Binary: %s/outfit\n' "$install_dir"
  printf '       Download verified with SHA-256.\n\n'
  resolved=$(command -v outfit || true)
  if [ -n "$resolved" ] && [ "$resolved" -ef "$install_dir/outfit" ]; then
    printf '  PATH is ready. Start here:\n'
  else
    if [ -n "$resolved" ]; then
      printf '  Another Outfit is earlier on PATH: %s\n' "$resolved"
    fi
    quoted_dir=$(outfit_quote_path "$install_dir")
    login_shell=${SHELL:-sh}
    login_shell=${login_shell##*/}
    case $login_shell in
      fish)
        printf '  Make Outfit available in fish now and in future terminals:\n\n'
        printf '    fish_add_path --move %s\n\n' "$quoted_dir"
        ;;
      *)
        path_line=$(printf 'export PATH=%s:"$PATH"' "$quoted_dir")
        printf '  Enable Outfit in your terminal:\n\n    %s\n\n' "$path_line"
        case $login_shell in
          zsh) startup_file=${ZDOTDIR:-$HOME}/.zshrc ;;
          bash)
            if [ "$os" = darwin ]; then startup_file=$HOME/.bash_profile
            else startup_file=$HOME/.bashrc; fi
            ;;
          sh|dash|ksh) startup_file=$HOME/.profile ;;
          *) startup_file='' ;;
        esac
        if [ -n "$startup_file" ]; then
          printf '  For future terminals, add that line to %s.\n' "$(outfit_quote_path "$startup_file")"
        else
          printf '  This export command is for POSIX-compatible shells. Add it to your shell startup file.\n'
        fi
        printf '  Your shell files have not been changed.\n\n'
        ;;
    esac
    printf '  Then start here:\n'
  fi
  printf '\n    outfit version      # Check the CLI\n'
  printf '    outfit setup        # Choose a profile and review software\n'
  printf '    outfit doctor       # Check tooling and credential setup\n\n'
  printf '  Dotfiles are separate: review with outfit dotfiles before applying.\n'
  printf '  Help and profiles: https://getoutfit.dev/\n'
}
outfit_bootstrap "$@"
