#!/bin/sh -e

if [ "$1" = "prereqs" ]; then exit 0; fi

. /usr/share/initramfs-tools/hook-functions

copy_exec /usr/bin/kmod
cp -aZ /usr/sbin/modprobe /usr/sbin/rmmod "$DESTDIR/usr/sbin/"

mkdir -p "$DESTDIR/usr/lib/modprobe.d/"
if [ "$(echo /usr/lib/modprobe.d/*)" != "/usr/lib/modprobe.d/*" ]; then
  cp -aZ /usr/lib/modprobe.d/* "$DESTDIR/usr/lib/modprobe.d/"
fi

kconfig_yes() {
  [ -e "/boot/config-$version" ] || return 1
  egrep -q "^$1=y$" "/boot/config-$version" || return 1
  return 0
}

# If CONFIG_MODULE_DECOMPRESS=n and compressed modules have been copied to
# the initramfs then also copy the library that kmod will use with dlopen(3)
# to uncompress them.
if ! kconfig_yes CONFIG_MODULE_DECOMPRESS; then
  libdir="$(env --unset=LD_PRELOAD ldd /usr/bin/kmod | sed -nr '/.*=>\s*(\S+)\/libc\.so\.[0-9]+ .*/ {s//\1/p;q}')"

  if [ "$libdir" -a "$(find "$DESTDIR/lib/modules/" -name "*.xz")" ]; then
    copy_exec "$libdir/*-linux-gnu/liblzma.so.5"
  fi
  if [ "$libdir" -a "$(find "$DESTDIR/lib/modules/" -name "*.zst")" ]; then
    copy_exec "$libdir/*-linux-gnu/libzstd.so.1"
  fi
fi

