#!/bin/sh

PREREQ=""

prereqs() {
	echo "$PREREQ"
}

need_ethernet() {
		if [ -n "$(lsmod |egrep 'ixp400_eth|ixp4xx_npe')" ]; then
			return 0
		else
			return 1
		fi
}

pause_error() {
	#exit 1 # won't work; initramfs-tools ignores hook script exit code
	echo "" >&2
	if [ "$DEBIAN_FRONTEND" = "noninteractive" ] ; then
		echo "Unable to abort; system will probably be broken!" >&2
	else
		echo "Press Ctrl-C to abort build, or Enter to continue" >&2
		read
	fi
}

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

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

# The ethernet driver is ixp400_eth for 2.6.17 and below, which is a
# non-free driver that should be loaded if available. In 2.6.18, it
# became a free driver, ixp4xx_npe, that does not need to be included in
# the initramfs by default, but which needs some non-free firmware. As a
# safety net, if either driver is loaded, refuse to build an initramfs,
# unless the new kernel has the necessary driver or firmware.
case "$version" in
2.6.1[1-7]*)
	manual_add_modules ixp400_eth
	if [ -n "$an_error_occured" ] || [ -z "$(find $DESTDIR \( -type f -or -type l \) | grep ixp400_eth)" ]; then
		echo "Warning: ixp400_eth ethernet driver not found, not included on image" >&2
		if need_ethernet; then
			echo "Warning: This system has the ethernet module loaded;" >&2
			echo "it's not safe to create an initramfs image that does not contain the module." >&2
			pause_error
		fi
	fi
;;
*)
	if [ ! -e "/lib/firmware/NPE-B" ]; then
		echo "Warning: ixp4xx_npe ethernet driver firmware file /lib/firmware/NPE-B not found" >&2
		if need_ethernet; then
			echo "Warning: This system has an ethernet module loaded; it's not safe to" >&2
			echo "create an initramfs image for the new kernel without the firmware file." >&2
			pause_error
		fi
	fi
;;
esac

# Record the root filesystem device for use during boot, since the slug's
# bootloader is hardcoded to use root=/dev/ram.
rootdev=$(egrep '^[^# 	]+[ 	]+/[ 	]' /etc/fstab | awk '{print $1}') || true

# Translate LABEL and UUID entries into a proper device name.
if echo "$rootdev" | grep -q "="; then
	a=$(echo "$rootdev" | cut -d "=" -f 1)
	b=$(echo "$rootdev" | cut -d "=" -f 2)
	case "$a" in
		LABEL)
			# Not all labels show up in /dev/disk/by-label:
			# e.g. LABEL=/ doesn't.
			found=0
			if [ -e /dev/disk/by-label ]; then
				for i in $(ls -1 /dev/disk/by-label/); do
					[ "$i" = "$b" ] && found=1
				done
			fi
			if [ $found -eq 0 ]; then
				echo "/etc/fstab parse error; $rootdev not supported or found" >&2
				rootdev=/dev/sda1
				pause_error
			else
				rootdev="/dev/disk/by-label/$b"
			fi
		;;
		UUID)
			rootdev=/dev/disk/by-uuid/$b
		;;
		*)
			echo "/etc/fstab parse error; cannot recognize root $rootdev" >&2
			rootdev=/dev/sda1
			pause_error
		;;
	esac
fi

if [ ! -e "$rootdev" ]; then
	rootdev=/dev/sda1
	echo "Warning: /etc/fstab parse error; guessing that the root device is $rootdev" >&2
fi
install -d $DESTDIR/conf
echo ROOT="$rootdev" >> $DESTDIR/conf/param.conf
