#!/bin/sh
# Gush TUI installer for macOS and Linux. Requires Node.js 22.19.0+ and npm.
# Run as your normal user; this script never invokes sudo or changes shell files.
gush_install() (
  set -eu
  if ! command -v node >/dev/null 2>&1 || ! command -v npm >/dev/null 2>&1; then
    printf '%s\n' 'Gush TUI requires Node.js 22.19.0+ and npm. Install them from https://nodejs.org/en/download and try again.' >&2
    exit 1
  fi
  if ! node -e 'const [major, minor] = process.versions.node.split(".").map(Number); process.exit(major > 22 || (major === 22 && minor >= 19) ? 0 : 1)'; then
    printf '%s\n' 'Please upgrade Node.js to 22.19.0 or newer: https://nodejs.org/en/download' >&2
    exit 1
  fi
  printf '%s\n' 'Installing Gush TUI from https://registry.npmjs.org/ ...'
  if ! npm install --global --ignore-scripts --registry=https://registry.npmjs.org/ gush-tui@latest; then
    printf '%s\n' 'Installation failed. Check the npm error above. For permission errors, use a user-owned Node.js installation or npm global prefix, then retry.' >&2
    exit 1
  fi
  gush_prefix=$(npm prefix --global)
  gush_entry="$gush_prefix/bin/gush-tui"
  if [ ! -x "$gush_entry" ]; then
    printf '%s\n' "Cannot find the installed command at $gush_entry. Check your npm global prefix." >&2
    exit 1
  fi
  "$gush_entry" --version
  printf '%s\n' 'Gush TUI installed. In your project directory, run: gush-tui'
  case ":${PATH:-}:" in
    *":$gush_prefix/bin:"*) ;;
    *) printf '%s\n' "Add this directory to your shell PATH, then reopen the terminal: $gush_prefix/bin" ;;
  esac
)

gush_install
