#!/usr/bin/env bash
# ============================================================================
# Unova node installer — generated by https://nodes.unova.io
# Network:   test
# Node type: type3
# Node ID:   (unregistered)
# ============================================================================
set -euo pipefail

# --- Pre-baked configuration from the launchpad ---
export NETWORK_SELECT="test"
export NODE_TYPE="type3"
export NODE_URL="https://uvmtest.unova.io"

# --- Colours ---
G='\033[0;32m'; Y='\033[1;33m'; R='\033[0;31m'; N='\033[0m'
log()  { echo -e "${G}[unova]${N} $1"; }
warn() { echo -e "${Y}[unova]${N} $1"; }
err()  { echo -e "${R}[unova]${N} $1" >&2; }

# --- Sanity checks ---
if [ "$EUID" -ne 0 ]; then
  err "Please run with sudo: curl -sSL https://nodes.unova.io/install/setup.sh | sudo bash"
  exit 1
fi

# --- Working directory (same path the legacy NOP uses) ---
WORK_DIR="/home/ubuntu"
mkdir -p "$WORK_DIR"
cd "$WORK_DIR"

# --- Apt prerequisites ---
log "Updating apt and installing prerequisites…"
apt-get update -y
apt-get install -y curl ca-certificates apt-transport-https software-properties-common gnupg jq

# --- Install Docker if needed ---
if ! command -v docker &> /dev/null; then
  log "Installing Docker…"
  curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
    | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" \
    > /etc/apt/sources.list.d/docker.list
  apt-get update -y
  apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
  systemctl enable --now docker
else
  log "Docker already installed: $(docker --version)"
fi

# --- Optional: import existing operator key, otherwise NOP generates one ---
if [ -z "${WEB3_NODE_PRIVATEKEY:-}" ]; then
  if [ "${NODE_TYPE}" = "type1" ] || [ "${NODE_TYPE}" = "type2" ]; then
    echo ""
    warn "A Type-1/Type-2 node needs an operator private key (the validator address)."
    warn "If you already have one (the address you submitted to the launchpad), paste it now."
    warn "Leave EMPTY to have NOP generate a fresh keypair."
    read -p "Operator private key (0x… 64 hex chars), or ENTER to generate: " WEB3_NODE_PRIVATEKEY
    export WEB3_NODE_PRIVATEKEY
  fi
fi

# --- Run the NOP bootstrap container ---
log "Pulling unova/nop bootstrap container…"
docker pull unova/nop:latest

log "Generating node configuration via NOP…"
docker rm -f nop &>/dev/null || true
docker run \
  --rm \
  --name nop \
  -v "$WORK_DIR/unova-nop:/app/output" \
  -e WEB3_NODE_PRIVATEKEY="${WEB3_NODE_PRIVATEKEY:-}" \
  -e NODE_URL="${NODE_URL}" \
  -e NETWORK_SELECT="${NETWORK_SELECT}" \
  -e NODE_TYPE="${NODE_TYPE}" \
  -e KEYSTOREFILE_PASSWORD="${KEYSTOREFILE_PASSWORD:-}" \
  -e KEYSTOREFILE="${KEYSTOREFILE:-}" \
  unova/nop:latest

# --- Start the actual node from the generated docker-compose ---
cd "$WORK_DIR/unova-nop"
if [ ! -f "./docker-compose.yml" ]; then
  err "NOP did not generate a docker-compose.yml — something went wrong."
  err "Check the output above and try again."
  exit 1
fi

log "Pulling node images…"
docker compose pull

log "Starting node in the background…"
docker compose up -d

log ""
log "✅  Unova node is starting on the test network."
log ""
log "Useful commands:"
log "  cd $WORK_DIR/unova-nop && docker compose logs -f    # tail node logs"
log "  cd $WORK_DIR/unova-nop && docker compose ps         # container status"
log "  cd $WORK_DIR/unova-nop && docker compose restart    # restart the node"
log ""
log "Your node will appear at https://nodes.unova.io once it peers up."

