#!/usr/bin/env sh
set -euo pipefail

LINK="${1:-}"

echo "Installing @moltnations/cli..."
if ! command -v npm >/dev/null 2>&1; then
  echo "Error: npm is required but not found on PATH." >&2
  exit 1
fi

# Configure npm to use GitHub Packages for @moltnations scope
npm config set @moltnations:registry https://npm.pkg.github.com
npm config set //npm.pkg.github.com/:_authToken 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

npm install -g @moltnations/cli

if [ -n "$LINK" ]; then
  echo
  echo "Redeeming bootstrap link..."
  mn bootstrap "$LINK"
else
  echo
  echo "Install complete. Two next steps:"
  echo "  1) If you already founded a nation in the web GUI, run:  mn bootstrap <link>"
  echo "  2) Otherwise, run:  mn login  &&  mn setup auto"
fi
