#!/bin/sh

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
# USA.

PREREQ=""

prereqs() {
	echo "$PREREQ"
}

pause_error() {
	local _ignored

	#exit 1 # won't work; initramfs-tools ignores hook script exit code
	echo "" >&2

	# Script is called from update-initramfs which in turn can be
	# called manually by the admin from the CLI or via various
	# postinst and trigger hooks or from the installer.
	#
	# If debconf appears to be running then it is important that
	# we do not block on stdin since this would hang the
	# installer.
	if [ "$DEBIAN_HAS_FRONTEND" ] || [ "$DEBIAN_FRONTEND" ]; 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 _ignored
	fi
}

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

FK_DIR="/usr/share/flash-kernel"

. "${FK_CHECKOUT:-$FK_DIR}/functions"

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

get_machine

# Should we override the root device or merely provide a default root
# device?
blsr="$(get_machine_field "$machine" "Bootloader-sets-root")"

if [ "$blsr" = "yes" ] || [ -n "$FLASH_KERNEL_SKIP" ]; then
	exit 0
fi

# Do not run inside an LXC container
if systemd-detect-virt --quiet --container; 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.
if [ -n "$rootdev" ]; then
	path=$(readlink -f $rootdev)
	if echo "$path" | grep -q "^/dev/mapper/"; then
		rootdev=$path
	fi
fi

# Translate LABEL, UUID, and PARTUUID 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
		;;
		PARTUUID)
			rootdev=/dev/disk/by-partuuid/$b
			if [ ! -e $rootdev ]; then
				echo "PARTUUID $b doesn't exist in /dev/disk/by-partuuid" >&2
			fi
		;;
		PARTLABEL)
			rootdev=/dev/disk/by-partlabel/$b
			if [ ! -e $rootdev ]; then
				echo "PARTLABEL $b doesn't exist in /dev/disk/by-partlabel" >&2
			fi
		;;
		*)
			echo "/etc/fstab parse error; cannot recognize root $rootdev" >&2
			rootdev=/dev/sda2
			echo "guessing that the root device is $rootdev" >&2
		;;
	esac
fi

get_machine

# Should we override the root device or merely provide a default root
# device?
blsir="$(get_machine_field "$machine" "Bootloader-Sets-Incorrect-Root")"
if [ -z "$blsir" ] ; then
	blsir="$(get_machine_field "$machine" "Bootloader-Sets-Root")"
	if [ -n "$blsir" ] ; then
		echo "WARNING: flash-kernel configuration contains deprecated" >&2
		echo "WARNING: \"Bootloader-Sets-Root\" option please update to" >&2
		echo "WARNING: use \"Bootloader-Sets-Incorrect-Root\" instead." >&2
	fi
fi

if [ ! -e "$rootdev" ]; then
	echo "Warning: root device $rootdev does not exist" >&2
	if [ "$blsir" = "yes" ]; then
		pause_error
	fi
fi

if [ "$blsir" = "yes" ]; then
	# The boot loader passes a bogus root= (e.g. root=/dev/ram), so
	# override the command line parameter.
	install -d $DESTDIR/conf
	echo "ROOT=\"$rootdev\"" >> $DESTDIR/conf/param.conf
else
	# The boot loader may or may not pass root= on the command line, so
	# provide a default.
	install -d $DESTDIR/conf/conf.d
	echo "ROOT=\"$rootdev\"" > $DESTDIR/conf/conf.d/default_root
fi

# vim:noexpandtab shiftwidth=8
