#!/usr/bin/env bash
# Downloads the latest SuprMiner release on every start, checks it against the
# release's SHA256SUMS, and mines NEB on every NVIDIA GPU of this machine on a
# pool (Suprnova by default, run by the SuprMiner author). No node and no chain
# sync: the pool sends the model and the jobs.
#
# Guide: https://paraloncloud.com/resources/mine-neb-neural-engine-byte-rented-gpu
#
#   WALLET=neb1... ./neb-pool.sh           mine to your address (Ctrl+C stops it)
#   WALLET=... WORKER=rig1 ./neb-pool.sh   name this machine on the pool page
#   POOL=stratum+ssl://neb.suprnova.cc:7806 ...   encrypted (TLS) port
#   DEVICES=0,1 ./neb-pool.sh              only these GPUs
#   CHECK=1 ./neb-pool.sh                  download, verify, GPU self-test, print the command
#
# Stats: https://neb.suprnova.cc/YourStats.html (paste your address)
# Fees: Suprnova 1% PPLNS; SuprMiner 0% NEB fee.
# Needs an NVIDIA driver, 610+ recommended by SuprMiner. Outbound TCP only.
set -euo pipefail

WALLET="${WALLET:-}"
POOL="${POOL:-stratum+tcp://neb.suprnova.cc:7804}"   # TLS: stratum+ssl://neb.suprnova.cc:7806, Asia stratum+ssl://stratum-apac.suprnova.cc:7806
PASSWORD="${PASSWORD:-x}"
WORKER="${WORKER:-$(hostname | tr -c 'A-Za-z0-9_-' '-' | sed 's/-*$//' | cut -c1-32)}"
DEVICES="${DEVICES:-}"
DIR="${DIR:-$HOME/suprminer}"
REPO=ocminer/suprminer

case "$WALLET" in
  neb1*) ;;
  *) echo "set WALLET=neb1... (your address, from neb-wallet new)" >&2; exit 1 ;;
esac

mkdir -p "$DIR"
cd "$DIR"

# The newest release and its NVIDIA Linux archive.
TAG=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" \
  | python3 -I -c 'import json,sys; print(json.load(sys.stdin)["tag_name"])')
VER=${TAG#v}
ARCHIVE="suprminer-$VER-linux-x86_64.tar.gz"
BASE="https://github.com/$REPO/releases/download/$TAG"
echo "latest SuprMiner: $TAG"

fetch() {  # fetch FILE: download once into $DIR/$TAG
  [ -f "$TAG/$1" ] && return 0
  echo "downloading $1"
  curl -fL --retry 3 -o "$TAG/$1.part" "$BASE/$1"
  mv "$TAG/$1.part" "$TAG/$1"
}
mkdir -p "$TAG"
rm -f "$TAG/SHA256SUMS"   # always the release's current list
for f in SHA256SUMS "$ARCHIVE" neb-datasets.bin; do fetch "$f"; done
(cd "$TAG" && sha256sum -c --ignore-missing SHA256SUMS) \
  || { echo "checksum mismatch, deleting $DIR/$TAG; run again" >&2; rm -rf "${DIR:?}/$TAG"; exit 1; }

BIN="$DIR/$TAG/suprminer-$VER/suprminer-neptune"
[ -x "$BIN" ] || tar xzf "$TAG/$ARCHIVE" -C "$TAG"
[ -x "$BIN" ] || { echo "suprminer-neptune not found in $ARCHIVE" >&2; exit 1; }

# Keep only this release: each one is ~80 MB packed and ~250 MB unpacked.
for old in "$DIR"/v*; do
  [ "$old" = "$DIR/$TAG" ] || rm -rf "$old"
done

CMD=("$BIN" -a neb -o "$POOL" -u "$WALLET.$WORKER" -p "$PASSWORD"
     --neb-dataset "$DIR/$TAG/neb-datasets.bin" --api --api-port 4068)
[ -n "$DEVICES" ] && CMD+=(-d "$DEVICES")

echo "mining NEB to $WALLET as $WORKER on $POOL"
if [ -n "${CHECK:-}" ]; then
  "$BIN" -a neb --neb-test --neb-dataset "$DIR/$TAG/neb-datasets.bin" ${DEVICES:+-d "$DEVICES"}
  echo "${CMD[*]}"
  exit 0
fi
exec "${CMD[@]}"
