#!/bin/sh -e

## set -x

PREREQ=""

prereqs() { echo "$PREREQ"; }

case "$1" in
    prereqs)
    prereqs
    exit 0
    ;;
esac

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

# Needed for keyboard

force_load evdev || echo "eventfs (mod evdev ?) not preloaded" && sleep 2

# Push helpers to initrd

mkdir -p $DESTDIR/lib/vdev
for prog in /lib/vdev/*; do

   # shell script or library?
   if [ -z "${prog#*.sh}" ]; then
      cp -a $prog $DESTDIR/lib/vdev/
   else
      # binary?
      if [ -f $prog -a -x $prog ]; then
         copy_exec $prog /lib/vdev
      fi
   fi
done

# Push libudev-compat to initrd
if [ -f /lib/libudev.so.1 ] ; then
    copy_exec /lib/libudev.so.1
fi

# Push the vdevd config to initrd
mkdir -p $DESTDIR/etc/vdev
cp -a /etc/vdev/* $DESTDIR/etc/vdev/

# Push the vdevd daemon itself
copy_exec /sbin/vdevd

# Utility function for copying binaries needed by helpers
# TODO: Helper dependencies should rather be declared with the helpers
vdevd_helper() {
    local args
    args=""
    while [ -n "$2" ] ; do
	if [ -f "$1" ] ; then
	    copy_exec $1
	    return 0
	fi
	args="$args $1"
	shift
    done
    echo "INFO:$args not installed. $1"
}

vdevd_helper /sbin/blkid "/dev/disk symlinks will not be created."
vdevd_helper /sbin/lvs "Logical volume symlinks will not be created."
vdevd_helper /sbin/pvs "Physical volume symlinks will not be created."
vdevd_helper /sbin/lvm "Logical volume symlinks will not be created."
vdevd_helper /sbin/ip /bin/ip /sbin/ifconfig \
	     "Network interfaces will not be configured."
vdevd_helper /sbin/restorecon "SELinux contexts will not be restored."
vdevd_helper /sbin/MAKEDEV "A dummy MAKEDEV link will be added."
vdevd_helper /sbin/dmsetup "Device mapper symlinks will not be created."

# hardware database
vdevd_helper /sbin/losetup "Cannot set up the hardware database."
if [ -f /lib/vdev/hwdb/hwdb.squashfs ]; then
   mkdir -p $DESTDIR/lib/vdev/hwdb
   cp -a /lib/vdev/hwdb/hwdb.squashfs $DESTDIR/lib/vdev/hwdb
else
    echo "WARN: could not find hardware database in /lib/vdev/hwdb.  Some hardware metadata may not be generated."
    sleep 2
fi

# GNU tools (not the busybox equivalents)
copy_exec /bin/sed
copy_exec /bin/grep
copy_exec /bin/fgrep
copy_exec /bin/egrep

# Linux tools
copy_exec /bin/kmod

# users and groups databases
cp -a /etc/passwd $DESTDIR/etc/passwd
cp -a /etc/group $DESTDIR/etc/group

#
# extra libc goodies
# Falling to i386 if i686 is the uname machine type
# and the i686 libc version is not found.
#
if [ -d /lib/$(arch)-linux-gnu ]; then
    LIBC=/lib/$(arch)-linux-gnu
elif [ -d /lib/i386-linux-gnu ]; then
    LIBC=/lib/i386-linux-gnu
else
    # make noise and trigger -e if set
    echo "Unable to find libc shared object files for $(arch)"
    sleep 1
    echo "Please check dependencies or package install order"
    sleep 2
    /bin/false
fi

copy_exec $LIBC/libc.so.6 || echo "$LIBC/libc.so.6 (still) not found"
copy_exec $LIBC/libnss_compat.so.2
copy_exec $LIBC/libnsl.so.1
copy_exec $LIBC/libnss_nis.so.2
copy_exec $LIBC/libnss_files.so.2

#
# since vdevd is multi-threaded,
# libpthread will need to dynamically load libgcc_s
# (and copy_exec won't detect this)
#
copy_exec $LIBC/libgcc_s.so.1 || echo "libpthread needs libgcc_s"

# until we're confident that the scripts work with busybox's sh, use dash
copy_exec /bin/dash

# be polite
true
