#!/bin/bash
# Map env → nym-node run. Bonding / wallet stay outside this container.
set -euo pipefail

export HOME="${NYM_HOME:-/var/lib/nym}"
ID="${NYMNODE_ID:-koopa-nym}"
MODE="${NYMNODE_MODE:-mixnode}"
PUBLIC_IPS="${NYMNODE_PUBLIC_IPS:-}"
LOCATION="${NYMNODE_LOCATION:-}"
HOSTNAME_OPT="${NYMNODE_HOSTNAME:-}"
WG="${NYMNODE_WG_ENABLED:-false}"
# Prefer 0.0.0.0 for rootless podman host port publishing (IPv4 WAN / DNAT).
HTTP_BIND="${NYMNODE_HTTP_BIND_ADDRESS:-0.0.0.0:8080}"
MIX_BIND="${NYMNODE_MIXNET_BIND_ADDRESS:-0.0.0.0:1789}"
VERLOC_BIND="${NYMNODE_VERLOC_BIND_ADDRESS:-0.0.0.0:1790}"
ENTRY_BIND="${NYMNODE_ENTRY_BIND_ADDRESS:-0.0.0.0:9000}"
ACCEPT_TC="${NYMNODE_ACCEPT_OPERATOR_TERMS:-true}"

ARGS=(run --id "$ID" --mode "$MODE")
ARGS+=(--http-bind-address "$HTTP_BIND")
ARGS+=(--mixnet-bind-address "$MIX_BIND")
ARGS+=(--verloc-bind-address "$VERLOC_BIND")
ARGS+=(--entry-bind-address "$ENTRY_BIND")

if [ -n "$PUBLIC_IPS" ]; then
  ARGS+=(--public-ips "$PUBLIC_IPS")
fi
if [ -n "$LOCATION" ]; then
  ARGS+=(--location "$LOCATION")
fi
if [ -n "$HOSTNAME_OPT" ]; then
  ARGS+=(--hostname "$HOSTNAME_OPT")
fi
if [ "$WG" = "true" ] || [ "$WG" = "1" ]; then
  ARGS+=(--wireguard-enabled true)
  ARGS+=(--wireguard-userspace true)
fi
if [ "$ACCEPT_TC" = "true" ] || [ "$ACCEPT_TC" = "1" ]; then
  ARGS+=(--accept-operator-terms-and-conditions)
fi

if [ -n "${NYMNODE_EXTRA_ARGS:-}" ]; then
  # shellcheck disable=SC2206
  EXTRA=( $NYMNODE_EXTRA_ARGS )
  ARGS+=("${EXTRA[@]}")
fi

exec nym-node "${ARGS[@]}"
