#!/usr/bin/env bash
# Install the "farmer" skill (Tiny Farm for agents): download the package, verify its checksum, install to ~/.agents/skills/farmer and create the `farmer` command.
#   curl -fsSL https://tiny-farm.xyz/farmer/skill/install.sh | bash
set -euo pipefail
SITE="${FARMER_SITE:-https://tiny-farm.xyz/farmer}"
DIR="${FARMER_DIR:-$HOME/.agents/skills/farmer}"
BIN="${FARMER_BIN:-$HOME/.local/bin}"

command -v node >/dev/null 2>&1 || { echo "Node.js 20+ is required (node not found)." >&2; exit 1; }
major="$(node -p 'process.versions.node.split(".")[0]')"
[ "$major" -ge 20 ] || { echo "Node.js 20+ is required (installed: $(node -v))." >&2; exit 1; }
command -v curl >/dev/null 2>&1 || { echo "curl is required." >&2; exit 1; }
command -v tar >/dev/null 2>&1 || { echo "tar is required." >&2; exit 1; }

API="${FARMER_API:-https://tiny-farm.xyz/farmer-agent}"
info="$(curl -fsS --max-time 15 "$API/info" 2>/dev/null || curl -fsS --max-time 15 "https://tinyfarm.194.233.83.34.nip.io/farmer-agent/info" 2>/dev/null || true)"
case "$info" in
  *'"mode":"mainnet"'*) ;;
  *) echo "Tiny Farm is not live yet, so the skill cannot be installed." >&2; echo "Join the whitelist: https://tiny-farm.xyz/farmer/whitelist.html   Follow @tiny_farming for the opening." >&2; exit 1;;
esac

PHASE_CLOSED=0
case "$info" in *'"gameOpen":false'*) PHASE_CLOSED=1;; esac
if [ "$PHASE_CLOSED" = 1 ]; then
  echo "==============================================================" >&2
  echo " Tiny Farm: the game is NOT open for play yet (opening soon)." >&2
  echo " Right now only the free-mint WHITELIST is open." >&2
  echo " You can install, log in and join it:  farmer whitelist apply" >&2
  echo " Follow @tiny_farming for the opening announcement." >&2
  echo "==============================================================" >&2
  echo >&2
fi

tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
if ! curl -fsSL --max-time 30 "$SITE/skill/farmer.tgz" -o "$tmp/farmer.tgz"; then
  if [ -z "${FARMER_SITE:-}" ]; then SITE="https://tinyfarm.194.233.83.34.nip.io/farmer"; echo "Main site unreachable, using the backup address." >&2; curl -fsSL "$SITE/skill/farmer.tgz" -o "$tmp/farmer.tgz"; else exit 1; fi
fi
curl -fsSL "$SITE/skill/farmer.tgz.sha256" -o "$tmp/farmer.tgz.sha256"
want="$(tr -d ' \r\n' < "$tmp/farmer.tgz.sha256" | cut -c1-64)"
if command -v sha256sum >/dev/null 2>&1; then got="$(sha256sum "$tmp/farmer.tgz" | cut -d' ' -f1)"; elif command -v shasum >/dev/null 2>&1; then got="$(shasum -a 256 "$tmp/farmer.tgz" | cut -d' ' -f1)"; else got=""; fi
if [ -n "$got" ] && [ "$got" != "$want" ]; then echo "Package checksum mismatch - installation aborted." >&2; exit 1; fi
[ -z "$got" ] && echo "Warning: sha256sum/shasum not found, checksum not verified." >&2

tar -xzf "$tmp/farmer.tgz" -C "$tmp"
[ -f "$tmp/farmer/SKILL.md" ] || { echo "Broken package: SKILL.md is missing." >&2; exit 1; }

mkdir -p "$(dirname "$DIR")" "$BIN"
rm -rf "$DIR.new"; cp -R "$tmp/farmer" "$DIR.new"
rm -rf "$DIR"; mv "$DIR.new" "$DIR"
chmod +x "$DIR/farmer-cli/farmer.mjs"
ln -sf "$DIR/farmer-cli/farmer.mjs" "$BIN/farmer"
"$BIN/farmer" version

echo
echo "Skill installed at: $DIR  (read $DIR/SKILL.md)"
command -v baw >/dev/null 2>&1 || echo "Note: 'baw' (Binance Agentic Wallet) is not installed. Install it with: npm i -g @binance/agentic-wallet"
case ":$PATH:" in *":$BIN:"*) ;; *) echo "Note: $BIN is not in your PATH. Add: export PATH=\"$BIN:\$PATH\"";; esac
if [ "$PHASE_CLOSED" = 1 ]; then
  echo
  echo "Game status: NOT OPEN YET (whitelist phase). Playing opens later."
  echo "Next step:   farmer login --address 0xYOUR_AGENTIC_WALLET   then   farmer whitelist apply"
else
  echo "Next step:  farmer preflight"
fi
