#!/usr/bin/env sh
# multiclone installer: puts the `multiclone` CLI on your PATH and checks Chrome.
# Download this file, read it, then run: sh install.sh
# Optional: MULTICLONE_VERSION=0.2.0 to pin a version.
set -eu

version="${MULTICLONE_VERSION:-latest}"
say() { printf '%s\n' "$*"; }
fail() { printf 'multiclone install: %s\n' "$*" >&2; exit 1; }

command -v node >/dev/null 2>&1 || fail "Node.js 22.12 or newer is required: https://nodejs.org"
node -e 'const [a,b]=process.versions.node.split(".").map(Number);process.exit(a>22||(a===22&&b>=12)?0:1)' \
  || fail "Node.js 22.12 or newer is required (found $(node -v))."
command -v npm >/dev/null 2>&1 || fail "npm is required (it ships with Node.js)."

say "Installing multiclone@${version}…"
# --min-release-age=0: some setups hold back packages published in the last days.
if ! npm install -g "multiclone@${version}" --no-audit --no-fund --loglevel=error --min-release-age=0 2>/dev/null; then
  npm install -g "multiclone@${version}" --no-audit --no-fund --loglevel=error \
    || fail "npm install -g failed. For a permissions error, set an npm prefix you own: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally"
fi

command -v multiclone >/dev/null 2>&1 \
  || fail "installed, but \`multiclone\` is not on your PATH. Add \"$(npm prefix -g)/bin\" to PATH."

multiclone setup --global >/dev/null 2>&1 || say "Could not install the agent method; run: multiclone setup --global"
multiclone doctor || say "doctor found a problem above; multiclone is still installed."
say ""
say "Done. Open your agent in any folder and ask it to clone a site, or run:  multiclone start <url>"
say "(Cursor only: run  multiclone setup  once in the project folder.)"
