#!/bin/sh -e

PREREQS=""

prereqs() { echo "$PREREQS"; }

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

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

copy_exec /bin/udevadm    /bin

# copy compat symlink
cp -a /sbin/udevadm $DESTDIR/sbin/udevadm

# helper programs
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
      fi
   fi
done

# libvdev
if [ -f /lib/`gcc -print-multiarch`/libvdev.so.1 ] ; then 
   copy_exec /lib/`gcc -print-multiarch`/libvdev.so.1
fi

# libudev-compat
if [ -f /lib/`gcc -print-multiarch`/libudev.so.1 ] ; then
    copy_exec /lib/`gcc -print-multiarch`/libudev.so.1
fi

# cdrom_id
if [ -f /lib/udev/cdrom_id ] ; then
    copy_exec /lib/udev/cdrom_id
fi

# config files
mkdir -p $DESTDIR/etc/vdev
cp -a /etc/vdev/vdevd.initrd.conf $DESTDIR/etc/vdev

# helpers
mkdir -p $DESTDIR/etc/vdev
cp -a /etc/vdev/actions.initrd $DESTDIR/etc/vdev

# the daemon itself 
copy_exec /sbin/vdevd

if [ -f /sbin/runlevel ]; then 
   copy_exec /sbin/runlevel
fi

# blkid (needed by vdevd's helpers)
if [ -f /sbin/blkid ]; then 
   copy_exec /sbin/blkid
else
   echo "WARN: could not find blkid in /sbin/blkid.  /dev/disk symlinks will not be created."
fi

# lvm tools (needed by vdevd's helpers)
if [ -f /sbin/lvs ]; then
   copy_exec /sbin/lvs
else
   echo "WARN: could not find lvs in /sbin/lvs.  Logical volume symlinks will not be created."
fi

if [ -f /sbin/pvs ]; then 
   copy_exec /sbin/pvs
else
   echo "WARN: could not find pvs in /sbin/pvs.  Physical volume symlinks will not be created."
fi

if [ -f /sbin/lvm ]; then 
   copy_exec /sbin/lvm
else
   echo "WARN: could not find lvm in /sbin/lvm.  Logical volume symlinks will not be created."
fi

if [ -f /sbin/vgchange ]; then 
   copy_exec /sbin/vgchange
else
   echo "WARN: could not find vgchange in /sbin/vgchange"
fi

# network tools (needed by vdevd's helpers)
if [ -f /bin/ip ]; then
   copy_exec /bin/ip
fi

if [ -f /sbin/ip ]; then 
   copy_exec /sbin/ip
fi

if ! [ -f /sbin/ip ] && ! [ -f /bin/ip ]; then 
   echo "WARN: could not find iproute2 in /bin/ip or /sbin/ip.  Network interfaces will not be configured."
fi

# SELinux tools
if [ -f /sbin/restorecon ]; then 
   copy_exec /sbin/restorecon || true
fi

# device tools
if [ -f /sbin/MAKEDEV ]; then 
   copy_exec /sbin/MAKEDEV || true
fi

# device mapper tools
if [ -f /sbin/dmsetup ]; then 
    copy_exec /sbin/dmsetup 
else
    echo "WARN: could not find dmsetup in /sbin/dmsetup.  Device mapper symlinks will not be created."
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

#for module in i915 radeon nouveau ; do
#    manual_add_modules $module
#done

# iSCSI
if [ -x /usr/sbin/iscsistart ]
then
    [ "${QUIET}" ] || echo -n " iscsi"
    copy_exec /usr/sbin/iscsistart /bin
    #manual_add_modules ib_iser
    manual_add_modules iscsi_tcp
    manual_add_modules crc32c
fi

# extra libc goodies 
copy_exec /lib/`gcc -print-multiarch`/libc.so.6
copy_exec /lib/`gcc -print-multiarch`/libnss_compat.so.2
copy_exec /lib/`gcc -print-multiarch`/libnsl.so.1
copy_exec /lib/`gcc -print-multiarch`/libnss_files.so.2

#for _SHLIB in $(find /lib /usr/lib -name 'libnss_dns.so.*' -o -name 'libnss_files.so.*')
#do
#	copy_exec "${_SHLIB}"
#done

# since vdevd is multi-threaded, libpthread will dynamically load libgcc_s
# (and copy_exec won't detect this)
copy_exec /lib/`gcc -print-multiarch`/libgcc_s.so.1

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