#!/bin/sh
set -eu

NEONDIFF_VERSION="${NEONDIFF_VERSION:-latest}"
DRY_RUN=0
PREFIX="${NPM_PREFIX:-}"

usage() {
  cat <<'USAGE'
Install NeonDiff from npm.

Usage:
  sh install.sh [--dry-run] [--prefix /path/to/prefix]

Environment:
  NEONDIFF_VERSION  Version or dist-tag to install, defaults to latest.
  NPM_PREFIX        Optional npm global prefix.

Requires Node.js 26 or newer and npm.
USAGE
}

while [ "$#" -gt 0 ]; do
  case "$1" in
    --dry-run)
      DRY_RUN=1
      shift
      ;;
    --prefix)
      if [ "$#" -lt 2 ]; then
        echo "error: --prefix requires a path" >&2
        exit 2
      fi
      PREFIX="$2"
      shift 2
      ;;
    --help|-h)
      usage
      exit 0
      ;;
    *)
      echo "error: unknown argument: $1" >&2
      usage >&2
      exit 2
      ;;
  esac
done

if ! command -v node >/dev/null 2>&1; then
  echo "error: Node.js 26 or newer is required before installing NeonDiff" >&2
  exit 1
fi

if ! command -v npm >/dev/null 2>&1; then
  echo "error: npm is required before installing NeonDiff" >&2
  exit 1
fi

NODE_MAJOR="$(node -p 'process.versions.node.split(".")[0]')"
if [ "$NODE_MAJOR" -lt 26 ]; then
  echo "error: Node.js 26 or newer is required; found $(node -v)" >&2
  exit 1
fi

set -- npm install -g "neondiff@${NEONDIFF_VERSION}"
if [ -n "$PREFIX" ]; then
  set -- "$@" --prefix "$PREFIX"
fi

echo "NeonDiff installer"
echo "version: ${NEONDIFF_VERSION}"
if [ -n "$PREFIX" ]; then
  echo "prefix: ${PREFIX}"
fi
printf 'command:'
for part in "$@"; do
  printf ' %s' "$part"
done
printf '\n'
echo "license: from NeonDiff 1.1.0, public repositories are free through the GitHub Action, neondiff review-pr --ci, and the self-hosted worker (neondiff daemon / run-once from the npm CLI 1.1.0+); private repositories need an active license"
echo "license: the Mac app still needs a license key for now"
echo "license: the npm CLI 1.0.x still requires API-backed activation for every repository"

if [ "$DRY_RUN" -eq 1 ]; then
  echo "dry-run: no changes made"
  exit 0
fi

"$@"
echo "installed: initialize config; for the Mac app, non-public review, or any repository on npm CLI 1.0.x, activate from an approved secret store, then verify status"
echo "  neondiff init --config config.local.json"
echo "macOS Keychain example: the first command prompts without putting the key in shell history"
echo "  security add-generic-password -a \$USER -s neondiff-license -U -w"
echo "  security find-generic-password -s neondiff-license -w | neondiff license activate --config config.local.json --license-key-stdin true --json"
echo "Linux, Windows, and other platforms: save the key with your approved secret manager UI, then pipe that manager's retrieval command to the same activation command."
echo "  neondiff license status --config config.local.json --json"
echo "linux: Codex checkout reads need unprivileged user namespaces, which Ubuntu 23.10+ blocks; allow them (sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0) or reviews run diff-only. See https://github.com/electricsheephq/evaos-code-review-bot-neondiff/blob/main/docs/SETUP.md#codex-sandbox-on-linux"
