#!/usr/bin/env bash
# install_node.sh — one-command provisioning of a ZANOS compute worker.
#
# The worker registers an M2M agent on the network, picks up proof-of-
# compute micro-tasks (doubleSHA256 nonce search), submits signed proofs,
# and accrues $ZNS on its own address — settled on-chain by the node's
# automatic payout engine (see zanos_getPayouts).
#
# Usage:
#   curl -fsSL https://zanos.tech/install_node.sh | sh -s -- \
#       --endpoint https://rpc.zanos.tech:7777 [--name my-worker] [--referrer CODE]
#
# Flags:
#   --endpoint URL   JSON-RPC/REST base of the network (default: public gateway)
#   --name NAME      worker label shown in the agent registry (default: hostname)
#   --referrer CODE  referral code (agentId or zns1... address) of the inviter
#   --idle-min SEC   minimum pause between task rounds (default 120)
#   --idle-max SEC   maximum pause between task rounds (default 600)
#   --dry-run        print the plan and exit
#   --help           this text

set -euo pipefail

ENDPOINT="https://rpc.zanos.tech:7777"
NAME="$(hostname -s 2>/dev/null || echo zanos-worker)"
REFERRER=""
IDLE_MIN=120
IDLE_MAX=600
DRY_RUN=0

while [ $# -gt 0 ]; do
  case "$1" in
    --endpoint) ENDPOINT="$2"; shift 2;;
    --name) NAME="$2"; shift 2;;
    --referrer) REFERRER="$2"; shift 2;;
    --idle-min) IDLE_MIN="$2"; shift 2;;
    --idle-max) IDLE_MAX="$2"; shift 2;;
    --dry-run) DRY_RUN=1; shift;;
    --help|-h) sed -n '2,30p' "$0"; exit 0;;
    *) echo "install_node.sh: unknown flag '$1' (see --help)" >&2; exit 2;;
  esac
done

INSTALL_DIR="/opt/zanos-worker"
STATE_DIR="/var/lib/zanos-worker"
UNIT="zanos-worker.service"

echo "==> ZANOS compute worker: name=$NAME endpoint=$ENDPOINT"
[ "$DRY_RUN" = 1 ] && { echo "dry-run: would install to $INSTALL_DIR + systemd unit $UNIT"; exit 0; }

if [ "$(id -u)" != "0" ]; then
  echo "Run as root (sudo sh -s -- ...)" >&2; exit 1
fi

export DEBIAN_FRONTEND=noninteractive
apt-get update -qq >/dev/null 2>&1 || true
apt-get install -y -qq python3 ca-certificates >/dev/null

mkdir -p "$INSTALL_DIR" "$STATE_DIR"

# The worker is a single stdlib-only Python file, also published standalone
# at https://zanos.tech/agent_worker.py so Windows/macOS users can run the
# exact same code without this installer.
WORKER_URL="https://zanos.tech/agent_worker.py"
curl -fsSL "$WORKER_URL" -o "$INSTALL_DIR/agent_worker.py"

chmod +x "$INSTALL_DIR/agent_worker.py"

cat > "/etc/systemd/system/$UNIT" <<EOF
[Unit]
Description=ZANOS compute worker ($NAME)
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 $INSTALL_DIR/agent_worker.py \
  --endpoint $ENDPOINT \
  --name $NAME \
  --state $STATE_DIR/$NAME.json \
  --idle-min $IDLE_MIN --idle-max $IDLE_MAX \
  $([ -n "$REFERRER" ] && echo --referrer $REFERRER)
Restart=always
RestartSec=30
CPUQuota=25%
MemoryMax=512M
Nice=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=zanos-worker

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now "$UNIT"
sleep 3
systemctl --no-pager -l status "$UNIT" | head -8

echo
echo "==> worker '$NAME' is live."
echo "    logs:  journalctl -u $UNIT -f"
echo "    state: $STATE_DIR/$NAME.json"
echo "    verify your earnings: https://zanos.tech/explorer  (agent registry)"
