#!/usr/bin/env bash # Merge Chain 138 XDC-Zero fragments into an existing endpointconfig.json (upstream XDC-Zero layout). # Requires jq. Backs up the target file unless --dry-run or explicit stdout. # # Usage: # bash scripts/xdc-zero/merge-endpointconfig-chain138.sh path/to/endpointconfig.json # bash scripts/xdc-zero/merge-endpointconfig-chain138.sh path/to/endpointconfig.json path/to/out.json # bash scripts/xdc-zero/merge-endpointconfig-chain138.sh path/to/endpointconfig.json --dry-run # bash scripts/xdc-zero/merge-endpointconfig-chain138.sh --dry-run path/to/endpointconfig.json # Or set XDC_ZERO_ENDPOINT_DIR to an XDC-Zero/endpoint clone path (uses .../endpointconfig.json). # # Sources: config/xdc-zero/endpointconfig.fragment.chain138.example.json, # config/xdc-zero/xdcparentnet-register-chain138.fragment.json set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" FRAG="$PROJECT_ROOT/config/xdc-zero/endpointconfig.fragment.chain138.example.json" REG_FRAG="$PROJECT_ROOT/config/xdc-zero/xdcparentnet-register-chain138.fragment.json" DRY_RUN=false BASE="" OUT="" POSITIONAL=() for a in "$@"; do if [[ "$a" == "--dry-run" ]]; then DRY_RUN=true else POSITIONAL+=("$a") fi done if [[ ${#POSITIONAL[@]} -ge 1 ]]; then BASE="${POSITIONAL[0]}" fi if [[ ${#POSITIONAL[@]} -ge 2 ]]; then OUT="${POSITIONAL[1]}" fi if [[ -z "$BASE" && -n "${XDC_ZERO_ENDPOINT_DIR:-}" ]]; then BASE="${XDC_ZERO_ENDPOINT_DIR%/}/endpointconfig.json" fi if [[ -z "$BASE" || ! -f "$BASE" ]]; then echo "Usage: $0 [out.json] [--dry-run]" >&2 echo " Or: XDC_ZERO_ENDPOINT_DIR=/path/to/XDC-Zero/endpoint $0 [--dry-run]" >&2 exit 1 fi if [[ ! -f "$FRAG" || ! -f "$REG_FRAG" ]]; then echo "ERROR: missing fragment under config/xdc-zero/" >&2 exit 1 fi command -v jq >/dev/null 2>&1 || { echo "ERROR: jq required" >&2; exit 1; } _step1="$(jq --slurpfile f "$FRAG" '.chain138 = $f[0].chain138' "$BASE")" MERGED="$(printf '%s\n' "$_step1" | jq --slurpfile r "$REG_FRAG" ' (if .xdcparentnet == null then . + {xdcparentnet: {registers: []}} else . end) | .xdcparentnet.registers += $r[0] ')" _c138_count="$(printf '%s' "$MERGED" | jq '[.xdcparentnet.registers[]? | select(.chainId == 138)] | length')" if [[ "${_c138_count:-0}" -gt 1 ]]; then echo "WARN: xdcparentnet.registers has ${_c138_count} entries with chainId 138 — review manually." >&2 fi if $DRY_RUN; then printf '%s\n' "$MERGED" exit 0 fi if [[ -n "$OUT" ]]; then printf '%s\n' "$MERGED" >"$OUT" echo "Wrote $OUT" exit 0 fi cp -a "$BASE" "${BASE}.bak.$(date +%Y%m%d%H%M%S)" printf '%s\n' "$MERGED" >"$BASE" echo "Merged into $BASE (backup beside file)."