#!/bin/sh

# This code is covered by the GNU General Public License (GPLv2 or higher)

PREREQ=""

prereqs() {
	echo "$PREREQ"
}

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
}

# Should we override the root device or merely provide a default root
# device?
root_type() {
	case "$1" in
		"Buffalo Linkstation Pro/Live")
			echo "override"
		;;
		"Buffalo/Revogear Kurobox Pro")
			echo "override"
		;;
		"D-Link DNS-323")
			echo "override"
		;;
		"GLAN Tank")
			echo "override"
		;;
		"HP Media Vault mv2120")
			echo "override"
		;;
		"Linksys NSLU2")
			echo "default"
		;;
		"QNAP TS-109/TS-209")
			echo "override"
		;;
		"QNAP TS-119/TS-219")
			echo "override"
		;;
		"QNAP TS-409")
			echo "override"
		;;
		"QNAP TS-41x")
			echo "override"
		;;
		"Marvell OpenRD Base Board")
			echo "default"
		;;
		"Marvell OpenRD Client Board")
			echo "default"
		;;
		"Marvell SheevaPlug Reference Board")
			echo "default"
		;;
		"Marvell eSATA SheevaPlug Reference Board")
			echo "default"
		;;
		"Thecus N2100")
			echo "override"
		;;
		"Lanner EM7210")
			echo "override"
		;;
		*)
			echo
		;;
	esac
}

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

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

# Only run this hook on some machines
machine=$(grep "^Hardware" /proc/cpuinfo | sed 's/Hardware\s*:\s*//')
if [ -z "$(root_type "$machine")" ]; then
	exit 0
fi

# Record the root filesystem device for use during boot
rootdev=$(egrep '^[^# 	]+[ 	]+/[ 	]' /etc/fstab | awk '{print $1}') || true

# Map LVM devices in the form of /dev/vg/lv to /dev/mapper/..., otherwise
# initramfs won't initialize them.
path=$(readlink -f $rootdev)
if echo "$path" | grep -q "^/dev/mapper/"; then
	rootdev=$path
fi

# 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- | sed -e 's/^"\(.*\)"$/\1/')
	case "$a" in
		LABEL)
			c=$(echo "$b" | sed 's#/#\\x2f#g')
			if [ -e /dev/disk/by-label/$c ]; then
				rootdev="/dev/disk/by-label/$c"
			else
				echo "Label $b not found in /dev/disk/by-label" >&2
			fi
		;;
		UUID)
			rootdev=/dev/disk/by-uuid/$b
			if [ ! -e $rootdev ]; then
				echo "UUID $b doesn't exist in /dev/disk/by-uuid" >&2
			fi
		;;
		*)
			echo "/etc/fstab parse error; cannot recognize root $rootdev" >&2
		;;
	esac
fi

if [ ! -e "$rootdev" ]; then
	rootdev=/dev/sda2
	echo "Warning: /etc/fstab parse error; guessing that the root device is $rootdev" >&2
	pause_error
fi
case "$(root_type "$machine")" in
	# The boot loader doesn't pass root= on the command line, so
	# provide a default.
	"default")
		install -d $DESTDIR/conf/conf.d
		echo "ROOT=\"$rootdev\"" > $DESTDIR/conf/conf.d/default_root
	;;
	# The boot loader passes a bogus root= (e.g. root=/dev/ram), so
	# override the command line parameter.
	"override")
		install -d $DESTDIR/conf
		echo "ROOT=\"$rootdev\"" >> $DESTDIR/conf/param.conf
	;;
	# This shouldn't happen - only if a device from the case statement
	# above wasn't added here.
	*)
		echo "Device $machine not supported.  Please file a bug." >&2
		pause_error
	;;
esac

