#!/bin/sh
set -eu

# Download this installer from your Whisper site's /download page.
SOURCE=${1:-}
if [ -n "$SOURCE" ] && [ ! -f "$SOURCE" ]; then
  echo "Client file not found: $SOURCE" >&2
  exit 1
fi

if ! command -v node >/dev/null 2>&1; then
  echo "Install Node.js 22 or newer on this laptop, then rerun this installer." >&2
  exit 1
fi
NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]')
if [ "$NODE_MAJOR" -lt 22 ]; then
  echo "Whisper terminal needs Node.js 22 or newer (found $NODE_MAJOR)." >&2
  exit 1
fi

BIN_DIR="$HOME/.local/bin"
APP_DIR="$HOME/.local/share/whisper-terminal"
TARGET="$BIN_DIR/whisper"
BUNDLE_URL=https://whisper.rachida.dev/downloads/whisper.mjs
API_URL=https://whisper-api.rachida.dev
EXISTING=$(command -v whisper 2>/dev/null || true)
NEED_PRIORITY=0
if [ -e "$TARGET" ] && ! grep -q '^# Whisper terminal launcher v1$' "$TARGET"; then
  echo "An unrelated command exists at $TARGET. Nothing was overwritten." >&2
  exit 1
fi
if [ -n "$EXISTING" ] && [ "$EXISTING" != "$TARGET" ]; then
  NEED_PRIORITY=1
  echo "An existing whisper command was found at $EXISTING."
  echo "The other command and its files will stay intact."
  if [ ! -t 0 ]; then
    echo "Run interactively to decide which whisper takes priority." >&2
    exit 1
  fi
  printf 'Install this Whisper launcher in ~/.local/bin and give it PATH priority? [y/N] ' > /dev/tty
  read ANSWER < /dev/tty
  case "$ANSWER" in
    y|Y|yes|YES) ;;
    *) echo "Installation cancelled."; exit 0 ;;
  esac
fi

umask 077
mkdir -p "$BIN_DIR" "$APP_DIR"
TEMP_BUNDLE="$APP_DIR/whisper.mjs.$$.tmp"
trap 'rm -f "$TEMP_BUNDLE" "$TARGET.tmp"' EXIT HUP INT TERM
if [ -n "$SOURCE" ]; then
  cp "$SOURCE" "$TEMP_BUNDLE"
elif command -v curl >/dev/null 2>&1; then
  curl -fsSL --retry 2 -o "$TEMP_BUNDLE" "$BUNDLE_URL"
elif command -v wget >/dev/null 2>&1; then
  wget -q -O "$TEMP_BUNDLE" "$BUNDLE_URL"
else
  echo "Install curl or wget, or download whisper.mjs and pass its path to this installer." >&2
  exit 1
fi
if ! head -n 1 "$TEMP_BUNDLE" | grep -qx '#!/usr/bin/env node'; then
  echo 'The downloaded file is not the Whisper terminal client.' >&2
  exit 1
fi
chmod 755 "$TEMP_BUNDLE"
mv "$TEMP_BUNDLE" "$APP_DIR/whisper.mjs"
cat > "$TARGET.tmp" <<'LAUNCHER'
#!/bin/sh
# Whisper terminal launcher v1
exec node "$HOME/.local/share/whisper-terminal/whisper.mjs" "$@"
LAUNCHER
chmod 755 "$TARGET.tmp"
mv "$TARGET.tmp" "$TARGET"
node "$APP_DIR/whisper.mjs" server "$API_URL"

case ":${PATH}:" in
  *":$BIN_DIR:"*) ;;
  *) NEED_PRIORITY=1 ;;
esac
if [ "$NEED_PRIORITY" -eq 1 ]; then
    case "${SHELL:-}" in
      */zsh) PROFILE="$HOME/.zshrc" ;;
      *) PROFILE="$HOME/.bashrc" ;;
    esac
    if ! grep -q '# Whisper terminal PATH' "$PROFILE" 2>/dev/null; then
      printf '\n# Whisper terminal PATH\nexport PATH="$HOME/.local/bin:$PATH"\n' >> "$PROFILE"
    fi
fi

echo "Installed Whisper terminal."
echo 'Open a new terminal, then run: whisper'
if [ -n "$EXISTING" ] && [ "$EXISTING" != "$TARGET" ]; then
  echo "Previous command: $EXISTING (still installed)."
  echo 'If it still runs first, put ~/.local/bin before its directory in PATH.'
fi
