#!/usr/bin/env bash # _nc_common.sh — shared resolver sourced by every bin/ short-command wrapper # AND by the bash-completion script (bin/nc-completion.bash). # # It is NOT a standalone tool — it only defines helpers. The wrappers are thin: # they resolve the lib/ toolkit dir, then exec the right lib/nc-*.sh with the # user's args mapped to that tool's flags. Putting the wrappers on PATH is what # makes `tbn adt` work directly (no `larry tools` / `nc-larry` prefix). # # Resolution order for the lib/ dir (first hit wins): # 1. $LARRY_LIB_DIR if set and valid # 2. /../lib (repo layout: bin/.. /lib) # 3. $LARRY_HOME/lib (installed layout) # 4. /../../lib (defensive: bin symlinked one level deeper) # # Site/thread enumeration is LIVE off the NetConfig tree under $HCIROOT, reusing # the SAME parser the tools use (lib/nc-parse.sh) so completion can never drift # from what the tools actually see. # --- lib/ dir resolution ----------------------------------------------------- _nc_resolve_lib() { if [ -n "${LARRY_LIB_DIR:-}" ] && [ -f "$LARRY_LIB_DIR/nc-parse.sh" ]; then printf '%s' "$LARRY_LIB_DIR"; return 0 fi # dir of the *real* file backing this source (follow one symlink level) local src="${BASH_SOURCE[0]}" if [ -L "$src" ]; then src="$(readlink "$src")"; fi local bindir bindir="$(cd "$(dirname "$src")" 2>/dev/null && pwd)" local c for c in "$bindir/../lib" "${LARRY_HOME:-$HOME/.larry}/lib" "$bindir/../../lib"; do if [ -d "$c" ] && [ -f "$c/nc-parse.sh" ]; then ( cd "$c" && pwd ); return 0 fi done return 1 } # --- HCIROOT resolution (env, then a couple of sane fallbacks) ---------------- _nc_hciroot() { if [ -n "${HCIROOT:-}" ] && [ -d "$HCIROOT" ]; then printf '%s' "$HCIROOT"; return 0; fi printf '%s' "${HCIROOT:-}"; return 0 } # --- live site enumeration ---------------------------------------------------- # A "site" = an immediate subdir of $HCIROOT that contains a NetConfig file. # This is what nc-find / nc-paths actually walk, so completion stays truthful. _nc_sites() { local root; root="$(_nc_hciroot)" [ -n "$root" ] && [ -d "$root" ] || return 0 local d for d in "$root"/*/; do [ -f "${d}NetConfig" ] && basename "${d%/}" done 2>/dev/null } # --- live thread enumeration -------------------------------------------------- # Every protocol (thread) name across every site's NetConfig, deduped. Uses the # library parser so it matches the tools' view exactly. Optional arg = restrict # to one site. _nc_threads() { local only_site="${1:-}" local root lib; root="$(_nc_hciroot)"; lib="$(_nc_resolve_lib)" || return 0 [ -n "$root" ] && [ -d "$root" ] || return 0 local nc site for nc in "$root"/*/NetConfig; do [ -f "$nc" ] || continue site="$(basename "$(dirname "$nc")")" [ -n "$only_site" ] && [ "$site" != "$only_site" ] && continue bash "$lib/nc-parse.sh" list-protocols "$nc" 2>/dev/null done | sort -u }