#!/bin/sh
# TOA installer (secondary path). The primary is: npx toa-skill
# Usage: curl -fsSL https://donatetokens.org/install.sh | sh            (interactive)
#        curl -fsSL https://donatetokens.org/install.sh | sh -s -- detect
set -eu
HOST="https://donatetokens.org"
if ! command -v node >/dev/null 2>&1 || ! command -v npx >/dev/null 2>&1; then
  echo "TOA needs Node.js 18+ (for npx). Install it from https://nodejs.org and run this again." >&2
  exit 1
fi
MAJOR=$(node -p 'process.versions.node.split(".")[0]')
if [ "$MAJOR" -lt 18 ]; then echo "Node.js $MAJOR is too old; TOA needs 18+." >&2; exit 1; fi
# Prefer the npm package; fall back to the tarball served by this site.
PKG="toa-skill"
if ! npm view toa-skill version >/dev/null 2>&1; then PKG="$HOST/toa-skill.tgz"; fi
RUN="npx -y --package=$PKG toa-skill"
# Reattach stdin to the terminal so the interactive prompts work when piped from curl.
if [ $# -eq 0 ] && [ -r /dev/tty ]; then exec $RUN --host "$HOST" < /dev/tty; fi
exec $RUN "$@" --host "$HOST"
