#!/bin/sh -e

PREREQS=""

prereqs() { echo "$PREREQS"; }

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

root_over_the_network() {
  if   egrep -qs '^/dev/(nbd|nd[a-z]|etherd/e)[^[:space:]]+[[:space:]]+/[[:space:]]' /etc/fstab; then
    return 0
  elif egrep -qs '^[^[:space:]#]+[[:space:]]+/[[:space:]]+nfs[234]?[[:space:]]' /etc/fstab; then
    return 0
  fi
  return 1 # false
}

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

mkdir -p $DESTDIR/lib/systemd
copy_exec /lib/systemd/systemd-udevd      /lib/systemd
copy_exec /bin/udevadm    /bin

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

mkdir -p $DESTDIR/etc/udev
cp -p /etc/udev/udev.conf $DESTDIR/etc/udev/

mkdir -p $DESTDIR/lib/udev/rules.d/
for rules in 50-firmware.rules 50-udev-default.rules 60-persistent-storage.rules \
	80-drivers.rules \
	64-md-raid.rules 60-persistent-storage-lvm.rules \
	55-dm.rules 60-persistent-storage-dm.rules; do
  if   [ -e /etc/udev/rules.d/$rules ]; then
    cp -p /etc/udev/rules.d/$rules $DESTDIR/lib/udev/rules.d/
  elif [ -e /lib/udev/rules.d/$rules ]; then
    cp -p /lib/udev/rules.d/$rules $DESTDIR/lib/udev/rules.d/
  fi
done

# try to autodetect the value of this configuration option
if [ -z "$NEED_PERSISTENT_NET" ] && root_over_the_network; then
  NEED_PERSISTENT_NET='yes'
fi

# Copy the rules for persistent network interface names for the benefit of
# systems which need to boot over the network. If this is enabled then the
# initramfs must be rebuilt every time a new network interface is added.
# See #414287 for details.
case "$NEED_PERSISTENT_NET" in
YES|yes|Yes|Y|y)
  if [ -e /etc/udev/rules.d/70-persistent-net.rules ]; then
    cp -p /etc/udev/rules.d/70-persistent-net.rules $DESTDIR/lib/udev/rules.d/
  fi
  ;;
esac

cp /lib/udev/hotplug.functions $DESTDIR/lib/udev/
for program in ata_id scsi_id; do
  copy_exec /lib/udev/$program /lib/udev
done
copy_exec /sbin/blkid /sbin
