#!/usr/bin/env bash
# SUBSTRATA node kit — builds original upstream rsReticulum + rsLXMF source.
# The code runs on your machine, with your own links and bandwidth. Review it
# before running it. No sudo, service registration, or unattended start.
set -euo pipefail

if ! command -v git >/dev/null 2>&1; then
  printf '%s\n' 'git is required. Install it and retry.' >&2
  exit 1
fi
if ! command -v cargo >/dev/null 2>&1; then
  printf '%s\n' 'Rust Cargo is required. Install it from https://rustup.rs and retry.' >&2
  exit 1
fi

kit_root="$PWD/substrata-node"
mkdir -p "$kit_root"
kit_root="$(cd "$kit_root" && pwd)"

for repo in rsReticulum rsLXMF; do
  case "$repo" in
    rsReticulum) revision=562bc99da79ce97321db8967bf8d99862b75b9f0 ;;
    rsLXMF) revision=769d0eb0a33eb4ea7a89dc234facbe292cd711c7 ;;
  esac
  target="$kit_root/$repo"
  if [ -e "$target" ]; then
    if [ ! -d "$target/.git" ]; then
      printf 'Refusing to use a non-Git directory: %s\n' "$target" >&2
      exit 1
    fi
    printf 'Preserving the existing %s checkout. No pull or overwrite.\n' "$repo"
    if [ "$(git -C "$target" rev-parse HEAD)" != "$revision" ] ||
       [ -n "$(git -C "$target" status --porcelain)" ]; then
      printf 'Refusing to build %s: use a clean checkout at tested revision %s in a new working directory.\n' "$repo" "$revision" >&2
      exit 1
    fi
  else
    git clone "https://github.com/ratspeak/$repo.git" "$target"
    git -C "$target" checkout --detach "$revision"
  fi
done

printf '%s\n' 'Building the upstream Reticulum daemon...'
(cd "$kit_root/rsReticulum" && cargo build --release --locked -p rns-tools --bin rnsd-rs --bin rnstatus-rs --bin rnid-rs)
printf '%s\n' 'Building the upstream LXMF daemon...'
(cd "$kit_root/rsLXMF" && cargo build --release --locked -p lxmf-tools --bin lxmd-rs)

rns_cfg="$kit_root/config/reticulum"
lxmf_cfg="$kit_root/config/lxmf"
mkdir -p "$rns_cfg" "$lxmf_cfg"
if [ ! -e "$rns_cfg/config" ]; then
  "$kit_root/rsReticulum/target/release/rnsd-rs" --exampleconfig > "$rns_cfg/config"
fi
if [ ! -e "$lxmf_cfg/config" ]; then
  "$kit_root/rsLXMF/target/release/lxmd-rs" --exampleconfig > "$lxmf_cfg/config"
fi

printf '\nSUBSTRATA NODE KIT READY.\n'
printf 'Inspect the generated configuration before opening interfaces:\n  %s\n  %s\n' "$rns_cfg/config" "$lxmf_cfg/config"
printf '\nStart Reticulum in one terminal:\n  "%s/rsReticulum/target/release/rnsd-rs" --config "%s"\n' "$kit_root" "$rns_cfg"
printf '\nCheck status in a second terminal:\n  "%s/rsReticulum/target/release/rnstatus-rs" --config "%s"\n' "$kit_root" "$rns_cfg"
printf '\nRun LXMF in another terminal:\n  "%s/rsLXMF/target/release/lxmd-rs" --config "%s" --rnsconfig "%s"\n' "$kit_root" "$lxmf_cfg" "$rns_cfg"
printf '\nNo daemon was started automatically. Node visibility depends on your configured transports.\n'
printf '%s\n' 'Before receiving browser capsules, set delivery_transfer_max_accepted_size = 160 in [lxmf].'
printf '%s\n' 'Use the same private spool path for the Python bridge and --on-inbound hook.'
printf '%s\n' 'For the local website, pass --origin http://localhost:8000 to the bridge.'
