#!/bin/sh
#
# DKMS post-install kernel hook.
#
# Arguments passed to this script: $1 pkgname, $2 version.
#
PKGNAME="$1"
VERSION="$2"
ARCH=$(uname -m)

if [ ! -x /usr/bin/dkms ]; then
	exit 0
fi

if [ ! -e /lib/modules/${VERSION}/build/include ] ; then
	echo "WARNING: cannot build DKMS modules! missing kernel headers package for ${VERSION}."
	exit 0
fi

[ -r /etc/default/dkms ] && . /etc/default/dkms
: "${DKMS_JOBS:=$(nproc)}"

export IGNORE_CC_MISMATCH=1

if [ ! -f /lib/modules/${VERSION}/build/scripts/basic/fixdep ] || [ ! -f /lib/modules/${VERSION}/build/scripts/mod/modpost ]; then
	yes "" | make -j "${DKMS_JOBS}" -C /lib/modules/${VERSION}/build prepare0
fi

# Check available DKMS modules
for _mod_ in /var/lib/dkms/*; do
	[ ! -d ${_mod_} ] && continue
	module=$(basename ${_mod_})
	for _modver_ in ${_mod_}/*; do
		if [ -d ${_modver_} -a ! -h ${_modver_} ]; then
			modulever=$(basename ${_modver_})
			echo "Available DKMS module: ${module}-${modulever}."
			if [ -z "${DKMS_MODULES}" ]; then
				DKMS_MODULES="${module} ${modulever}"
			else
				DKMS_MODULES="${DKMS_MODULES} ${module} ${modulever}"
			fi
		fi
	done
done

# This section builds and install the available modules.
#
# If either building or installing a module fails, a warning is
# printed and it is skipped, but the script still tries to build
# the other modules.
#
# The list of available modules is in the form
# [module1, modulever1, module2, modulever2, ...]
#
set -- ${DKMS_MODULES}
while [ $# -gt 1 ]; do
	module="$1"
	shift
	modulever="$1"
	shift

	# If adding a module, depmod is necessary unless dkms runs it
	do_depmod="yes"

	status=$(/usr/bin/dkms status -m ${module} -v ${modulever} -k ${VERSION})
	if [ $(echo "$status"|grep -c ": built") -eq 0 ]; then
		# Check if the module is still there.
		if [ ! -f /usr/src/${module}-${modulever}/dkms.conf ]; then
			echo "Skipping nonexistent DKMS module: ${module}-${modulever}."
			continue
		fi
		# Build the module
		echo -n "Building DKMS module: ${module}-${modulever}... "
		/usr/bin/dkms build -j "${DKMS_JOBS}" -q -m ${module} -v ${modulever} -k ${VERSION} -a ${ARCH}
		rval=$?
		# If the module was skipped or failed, go to the next module.
		if [ $rval -eq 0 ]; then
			echo "done."
		elif [ $rval -eq 9 ]; then
			echo "skipped!"
			continue
		else
			echo "FAILED!"
			continue
		fi
		status=$(/usr/bin/dkms status -m ${module} -v ${modulever} -k ${VERSION})
	fi

	# If the module is built (either pre-built or just now), install it
	if [ $(echo "$status"|grep -c ": built") -eq 1 ] &&
	   [ $(echo "$status"|grep -c ": installed") -eq 0 ]; then
		echo -n "Installing DKMS module: ${module}-${modulever}... "
		/usr/bin/dkms install --force -j "${DKMS_JOBS}" -q -m ${module} -v ${modulever} -k ${VERSION} -a ${ARCH}
		rval=$?
		# If the module failed installation, go to the next module.
		if [ $rval -eq 0 ]; then
			# dkms runs depmod as part of the installation
			unset do_depmod
			echo "done."
		else
			echo "FAILED!"
			continue
		fi
	fi
done

if [ -z "$do_depmod" ] || [ ! -x /usr/bin/depmod ]; then
	exit 0
fi

echo -n "Generating kernel module dependency lists... "
/usr/bin/depmod -a ${VERSION}
rval=$?
if [ $rval -eq 0 ]; then
	echo "done."
else
	echo "FAILED!"
	exit $rval
fi

exit 0
