#!/bin/sh

# This script is originally shipped with the Meinberg driver package
# for Linux (mbgtools-lx). See:
#
# https://kb.meinbergglobal.com/kb/driver_software/driver_software_for_linux/
#
# On systems that use 'initramfs-tools' to generate initrd (Initial RAM Disk)
# files, this script should be copied to /etc/initramfs-tools/hooks/.
#
# The script prevents the mbgclock.ko kernel module from being included
# when creating or updating the initrd file.
#
# The mbgclock.ko kernel module is created from the mbgtools-lx
# driver package and is updated every time the driver package
# is updated or reinstalled.
#
# If the module were included in the initrd file, every time the
# system boots, the driver version that was current at the time
# the initrd file was created would be loaded.
#
# Omitting the module from initrd ensures that the driver version
# from the latest update/installation of the driver package is
# always loaded at startup. The kernel driver file is usually
# located in /lib/modules/$(uname -r)/extra/.

PREREQ=""

# Function to define pre-requisite hooks (if any).
prereqs() {
    echo "$PREREQ"
}

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


# Omit the mbgclock module from initrd.
# The module extension can be ".ko" or ".ko.gz" or similar,
# so we use ".ko*"
MODULE="mbgclock"
MODULE_PATTERN="lib/modules/${version}/extra/${MODULE}.ko*"

if [ -e "${DESTDIR}/${MODULE_PATTERN}" ]; then
  echo "Omitting ${MODULE} from initramfs"
  rm -f "${DESTDIR}/${MODULE_PATTERN}"
fi


# Omit the udev rules for the mbgclock module from intrd.
UDEV_RULE="55-mbgclock.rules"
UDEV_RULE_PATTERN="usr/lib/udev/rules.d/${UDEV_RULE}"

if [ -e "${DESTDIR}/${UDEV_RULE_PATTERN}" ]; then
  echo "Omitting ${UDEV_RULE} from initramfs"
  rm -f "${DESTDIR}/${UDEV_RULE_PATTERN}"
fi

