. "$BST_LIB"
# ncurses-terminfo-base is DATA-ONLY: it installs the base terminfo database (no
# binary). On this image the base entries live under /etc/terminfo — the oe-core
# -terminfo-base subpackage installs to ${sysconfdir}/terminfo; the full DB under
# /usr/share/terminfo is the separate ncurses-terminfo package, not installed.
# ncurses honors $TERM by searching /etc/terminfo, then /usr/share/terminfo, so the
# test locates each entry across that standard search path.

# ti_find <term>: echo the first terminfo file for <term> across the ncurses
# default search dirs; non-zero if none. ncurses maps <term> -> <first-char>/<term>.
ti_find() {
  _t=$1; _c=$(printf %.1s "$_t")
  for _d in /etc/terminfo /usr/share/terminfo /lib/terminfo; do
    [ -f "$_d/$_c/$_t" ] && { printf '%s\n' "$_d/$_c/$_t"; return 0; }
  done
  return 1
}

# the terminfo DB dir exists on the standard search path
check ncurses-terminfo-base/dir sh -c 'test -d /etc/terminfo || test -d /usr/share/terminfo'

# canonical base entries resolve at their $TERM-derived path — this <first-char>/
# <name> mapping IS how a terminfo lookup by $TERM is honored.
for _term in xterm linux vt100 ansi dumb; do
  if ti_find "$_term" >/dev/null; then bst_ok "ncurses-terminfo-base/$_term"
  else bst_notok "ncurses-terminfo-base/$_term" "no terminfo entry for TERM=$_term on the search path"; fi
done

# validity: the xterm entry is real compiled terminfo — legacy magic 0432 (1a 01)
# or the extended-number format 0x0432e (1e 02), not an empty placeholder.
_xt=$(ti_find xterm)
if [ -n "$_xt" ]; then
  magic=$(od -An -tx1 -N2 "$_xt" 2>/dev/null | tr -d ' ')
  case "$magic" in
    1a01|1e02) bst_ok    ncurses-terminfo-base/xterm-magic ;;
    *)         bst_notok ncurses-terminfo-base/xterm-magic "unexpected magic '$magic' (want 1a01 or 1e02)" ;;
  esac
else
  bst_notok ncurses-terminfo-base/xterm-magic "xterm entry not found on the search path"
fi

# A standalone tool honoring $TERM (tput/infocmp/tset) ships in ncurses-bin, not
# this data-only package, so it is legitimately absent here. The base DB is
# consumed by the libtinfo-linked tools (less), which read terminfo on a real tty
# only and so cannot be exercised inside the piped runner.
bst_skip ncurses-terminfo-base/tput-honors-term "no terminfo-reading CLI on the image (tput/infocmp are ncurses-bin, out of scope for this data-only pickup)"
