# dpkg (BRINTOS-52) — the wasm32-hwjs glibc arm ships dpkg / dpkg-deb / dpkg-query
# and a populated /var/lib/dpkg status DB (retained by the package-management
# IMAGE_FEATURE). Coverage is the wasm32 build identity, the shipped status DB,
# and the local-.deb lifecycle: install / query / postinst / remove / purge.
#
# The lifecycle fixture ships prebuilt in data/ (dpkg-selftest 1.0, Architecture:
# wasm32): a regular file under /usr/share/dpkg-selftest plus postinst/postrm
# maintainer scripts. No symlinks, hardlinks, or non-root ownership — all unwired
# on this runtime. It is prebuilt rather than packed here because on-target
# packing is broken (see dpkg/deb-build below).
. "$BST_LIB"
require dpkg dpkg/present

# --- the tools are present and are the wasm32 build ---------------------------
check_out dpkg/version       "1.22"   dpkg --version
check_out dpkg/arch          "wasm32" dpkg --print-architecture
check_out dpkg/deb-version   "1.22"   dpkg-deb --version
check_out dpkg/query-version "1.22"   dpkg-query --version

# --- the status DB survived rootfs assembly and is readable -------------------
# package-management IMAGE_FEATURE keeps /var/lib/dpkg populated; without it the
# rootfs would ship with the DB stripped and every query below would come back empty.
check dpkg/list dpkg -l
n=$(dpkg -l 2>/dev/null | grep -c '^ii')
if [ "${n:-0}" -gt 0 ]; then bst_ok dpkg/status-db
else bst_notok dpkg/status-db "no 'ii' packages: status DB empty or unreadable"; fi

check_out dpkg/query-self  "dpkg" dpkg-query -W -f='${Package}\n' dpkg
check_out dpkg/query-field "1.22" dpkg-query -W -f='${Version}\n' dpkg
check dpkg/query-listfiles dpkg-query -L dpkg

# --- local-.deb lifecycle over the prebuilt fixture ---------------------------
deb="$BST_DATA/dpkg-selftest.deb"

# start from a clean slate in case a prior run left the package half-installed
dpkg -P dpkg-selftest >/dev/null 2>&1 || true

check     dpkg/fixture   test -f "$deb"
# install: unpack + configure (runs postinst)
check     dpkg/install   dpkg -i "$deb"
# the shipped payload file is on disk
check     dpkg/payload   test -f /usr/share/dpkg-selftest/hello
# the package is recorded in the status database
check_out dpkg/query     "dpkg-selftest"      dpkg-query -W dpkg-selftest
# the postinst maintainer script ran
check_out dpkg/postinst  "postinst:configure" cat /usr/share/dpkg-selftest/postinst.done
# the installed package's file list is tracked
check_out dpkg/files     "/usr/share/dpkg-selftest/hello" dpkg -L dpkg-selftest
# remove: runs postrm and unlinks the payload (the db record survives a plain
# remove; dpkg-query still knows the package until it is purged)
check     dpkg/remove    dpkg -r dpkg-selftest
check     dpkg/removed   sh -c "! test -e /usr/share/dpkg-selftest/hello"
# purge: drops the package record from the status database
check     dpkg/purge     dpkg -P dpkg-selftest
check_rc  dpkg/gone      1 dpkg-query -W dpkg-selftest

# --- on-target packing --------------------------------------------------------
# dpkg-deb --build shells out to tar to build data.tar; the image's tar is
# BusyBox, which does not implement the options dpkg-deb passes. The tool is
# present but this path does not work, so it is reported rather than omitted —
# a green suite must not hide a capability the image claims to ship.
work=${TMPDIR:-/tmp}/bst-dpkg.$$
root=$work/bst-hello
mkdir -p "$root/DEBIAN" "$root/usr/bin" 2>/dev/null
printf 'Package: bst-hello\nVersion: 1.0-1\nArchitecture: wasm32\nMaintainer: Emcraft Systems <support@emcraft.com>\nSection: misc\nPriority: optional\nDescription: brintos-selftests on-target packing probe\n' > "$root/DEBIAN/control"
printf '#!/bin/sh\necho BST-HELLO-OK\n' > "$root/usr/bin/bst-hello"
chmod 0755 "$root/usr/bin/bst-hello" 2>/dev/null
check dpkg/deb-build dpkg-deb --build "$root" "$work/bst-hello.deb"
rm -rf "$work"
