#!/bin/sh
# resaiz command line tool installer (macOS, Linux, WSL). Windows PowerShell: see https://www.resaiz.com/#/connect
#   curl -fsSL https://www.resaiz.com/install.sh | sh
# What it does: checks Node 20 or newer and npm, downloads the resaiz package from www.resaiz.com,
# checks its sha256 against cli/latest.json, installs it with npm (globally, the same as
# npm install -g), then tells you the next step: resaiz login.
set -eu

SITE="${RESAIZ_SITE:-https://www.resaiz.com}"
say() { printf '%s\n' "$*"; }
die() { printf 'resaiz install: %s\n' "$*" >&2; exit 1; }

command -v node >/dev/null 2>&1 || die "Node.js was not found. Install Node 20 or newer from https://nodejs.org and run this again."
command -v npm >/dev/null 2>&1 || die "npm was not found. It comes with Node.js from https://nodejs.org."
NODE_MAJOR="$(node -p 'process.versions.node.split(".")[0]')"
[ "$NODE_MAJOR" -ge 20 ] 2>/dev/null || die "Node $(node -v) is too old. resaiz needs Node 20 or newer."

fetch() {
  if command -v curl >/dev/null 2>&1; then curl -fsSL "$1" -o "$2"
  elif command -v wget >/dev/null 2>&1; then wget -qO "$2" "$1"
  else die "curl or wget is needed to download the package."
  fi
}

WORK="$(mktemp -d 2>/dev/null || mktemp -d -t resaiz)" || die "could not make a temporary folder"
trap 'rm -rf "$WORK"' EXIT INT TERM

fetch "$SITE/cli/latest.json" "$WORK/latest.json" || die "could not reach $SITE/cli/latest.json"
field() { node -e 'const j=JSON.parse(require("fs").readFileSync(process.argv[1],"utf8"));const v=j[process.argv[2]];process.stdout.write(typeof v==="string"?v:"")' "$WORK/latest.json" "$1"; }
URL="$(field url)"
VERSION="$(field version)"
SHA="$(field sha256)"
# RESAIZ_TARBALL_URL overrides the url for a local test against a dev server (the sha check still runs).
URL="${RESAIZ_TARBALL_URL:-$URL}"
case "$URL" in
  https://*/cli/resaiz-*.tgz|http://localhost:*/cli/resaiz-*.tgz|http://127.0.0.1:*/cli/resaiz-*.tgz) ;;
  *) die "latest.json names an unexpected package url: $URL" ;;
esac
case "$VERSION" in ''|*[!0-9A-Za-z.+-]*) die "latest.json has no usable version" ;; esac
case "$SHA" in *[!0-9a-f]*|'') die "latest.json has no sha256" ;; esac
[ "${#SHA}" -eq 64 ] || die "latest.json has no sha256"

TGZ="$WORK/resaiz-$VERSION.tgz"
say "Downloading resaiz $VERSION from $URL"
fetch "$URL" "$TGZ" || die "could not download the package"
if command -v shasum >/dev/null 2>&1; then GOT="$(shasum -a 256 "$TGZ" | cut -d' ' -f1)"
elif command -v sha256sum >/dev/null 2>&1; then GOT="$(sha256sum "$TGZ" | cut -d' ' -f1)"
else GOT="$(node -e 'const c=require("crypto"),f=require("fs");process.stdout.write(c.createHash("sha256").update(f.readFileSync(process.argv[1])).digest("hex"))' "$TGZ")"
fi
[ "$GOT" = "$SHA" ] || die "the downloaded package does not match its sha256 (expected $SHA, got $GOT). Nothing was installed."

LOG="$WORK/npm.log"
say "Installing"
if npm install -g -- "$TGZ" >"$LOG" 2>&1; then
  :
elif [ "$(id -u)" -ne 0 ] && command -v sudo >/dev/null 2>&1; then
  say "npm needs permission for its global folder; trying again with sudo."
  sudo npm install -g -- "$TGZ" >"$LOG" 2>&1 || { tail -20 "$LOG" >&2; die "npm install failed"; }
else
  tail -20 "$LOG" >&2
  die "npm install failed"
fi

if command -v resaiz >/dev/null 2>&1; then
  say "Installed: $(resaiz --version 2>/dev/null || echo "resaiz $VERSION")"
else
  say "Installed resaiz $VERSION, but the resaiz command is not on your PATH yet."
  say "Add npm's global bin folder to PATH: $(npm prefix -g)/bin, then open a new terminal."
fi

# Counts installs: one anonymous +1 to today's total, sends nothing about this machine. RESAIZ_NO_COUNT=1 skips it.
if [ "${RESAIZ_NO_COUNT:-}" != "1" ]; then
  if command -v curl >/dev/null 2>&1; then curl -fsS -m 5 "$SITE/api/event?name=cli_install" >/dev/null 2>&1 || true
  elif command -v wget >/dev/null 2>&1; then wget -q -T 5 -O /dev/null "$SITE/api/event?name=cli_install" >/dev/null 2>&1 || true
  fi
fi

CHROME_OK=0
if [ "$(uname)" = "Darwin" ]; then
  [ -d "/Applications/Google Chrome.app" ] && CHROME_OK=1
else
  command -v google-chrome >/dev/null 2>&1 && CHROME_OK=1
  command -v google-chrome-stable >/dev/null 2>&1 && CHROME_OK=1
fi
[ "$CHROME_OK" -eq 1 ] || say "Note: renders and exports use Google Chrome. Install it from https://www.google.com/chrome/ (editing works without it)."

say ""
say "Next:"
say "  resaiz login            sign in (your browser opens; click Authorize)"
say "  resaiz skill install    teach Claude Code how to use it"
say ""
say "Guide: https://www.resaiz.com/install"
