# Stage the prebuilt narOS kernel identity shim (os/src/naros-identity, built per-arch
# into dist/bin by that dir's build.sh / the CI binaries job).
#
# /etc/ld.so.preload is written by the postinst rather than shipped in files/: dpkg
# unpacks a package's files in no guaranteed order, so a shipped preload file could land
# before the .so it names and make every binary exec'd for the rest of that transaction —
# including dpkg's own maintainer scripts — emit a loader warning. The postinst runs
# after the whole package is on disk, so the reference is never dangling.
# The /lib/modules alias unit rides along with a static enable symlink (rather than a
# `systemctl enable` in the postinst) so it takes effect inside the mmdebstrap chroot,
# where no systemd is running — the same pattern nucleic-linux-agent uses.
stage() {
  local dest="$1" arch="$2"
  local lib="$OS_DIR/dist/bin/libnaros-uname-$arch.so"
  local unit="$OS_DIR/src/naros-identity/naros-identity-modules.service"
  if [ ! -f "$lib" ]; then
    echo "prebuilt shim missing: dist/bin/libnaros-uname-$arch.so" > "$dest/.skip-reason"
    return 1
  fi
  install -D -m 0644 "$lib" "$dest/usr/lib/naros/libnaros-uname.so"
  install -D -m 0644 "$unit" "$dest/usr/lib/systemd/system/naros-identity-modules.service"
  install -d "$dest/etc/systemd/system/sysinit.target.wants"
  ln -sf /usr/lib/systemd/system/naros-identity-modules.service \
    "$dest/etc/systemd/system/sysinit.target.wants/naros-identity-modules.service"
}
