#!/bin/sh

PREREQ=""

prereqs() {
	echo "$PREREQ"
}

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

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

# Only run if we're on a GLAN Tank machine
machine=$(grep "^Hardware" /proc/cpuinfo | sed 's/Hardware\s*:\s*//')
if ! echo $machine | grep -q "^GLAN Tank"; then
        exit 0
fi

# Record the root filesystem device for use during boot, since the GLAN Tank
# bootloader is hardcoded to use root=/dev/hda2. This setting will no longer
# work if the ARTOP PATA driver is used.
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/sda2
				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/sda2
			pause_error
		;;
	esac
fi

if [ ! -e "$rootdev" ]; then
	rootdev=/dev/hda2
	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
