#! /bin/sh

set -e
. /usr/share/debconf/confmodule
#set -x

newline="
"

db_capb backup

log=/var/log/messages

log() {
	logger -t grub-installer "$@"
}

error() {
	log "error: $@"
}

info() {
	log "info: $@"
}

get_serial_console() {
	local defconsole="$(sed -e 's/.*console=/console=/' /proc/cmdline)"
	if echo "${defconsole}" | grep -q console=ttyS; then
		local PORT="$(echo "${defconsole}" | sed -e 's%^console=ttyS%%' -e 's%,.*%%')"
		local SPEED="$(echo "${defconsole}" | sed -e 's%^console=ttyS[0-9]\+,%%' -e 's% .*%%')"
		local SERIAL="${PORT},${SPEED}"
		echo "console=$SERIAL"
	fi
}

serial="$(get_serial_console)"

# This is copied from update-grub. We've requested that it be moved
# to a utility or shell library.
device_map=/target/boot/grub/device.map
# Usage: convert os_device
# Convert an OS device to the corresponding GRUB drive.
# This part is OS-specific.
convert () {
#    # First, check if the device file exists.
#    if test -e "$1"; then
#		:
#    else
#		echo "$1: Not found or not a block device." 1>&2
#		exit 1
#    fi

	host_os=`uname -s | tr '[A-Z]' '[a-z]'`

    # Break the device name into the disk part and the partition part.
    case "$host_os" in
    linux*)
		tmp_disk=`echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
				  -e 's%\(fd[0-9]*\)$%\1%' \
				  -e 's%/part[0-9]*$%/disc%' \
				  -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
		tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
				  -e 's%.*/fd[0-9]*$%%' \
				  -e 's%.*/floppy/[0-9]*$%%' \
				  -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
				  -e 's%.*c[0-7]d[0-9]*p*%%'`
	;;
    gnu*)
		tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
		tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
    freebsd*)
		tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%r\1%' \
			    | sed 's%r\{0,1\}\(da[0-9]*\).*$%r\1%'`
		tmp_part=`echo "$1" \
	    		| sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
       	    	| sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
	;;
    netbsd*)
		tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
	    		| sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
		tmp_part=`echo "$1" \
	    		| sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
	;;
    *)
		echo "update-grub does not support your OS yet." 1>&2
		exit 1 ;;
    esac

    # Get the drive name.
    tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
			| sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%'`

    # If not found, print an error message and exit.
    if test "x$tmp_drive" = x; then
		echo "$1 does not have any corresponding BIOS drive." 1>&2
		exit 1
    fi

    if test "x$tmp_part" != x; then
		# If a partition is specified, we need to translate it into the
		# GRUB's syntax.
		case "$host_os" in
		linux*)
	    	echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
		gnu*)
	    	if echo $tmp_part | grep "^s" >/dev/null; then
				tmp_pc_slice=`echo $tmp_part \
		    		| sed "s%s\([0-9]*\)[a-g]*$%\1%"`
				tmp_drive=`echo "$tmp_drive" \
		    		| sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
	    	fi
	    	if echo $tmp_part | grep "[a-g]$" >/dev/null; then
				tmp_bsd_partition=`echo "$tmp_part" \
		    		| sed "s%[^a-g]*\([a-g]\)$%\1%"`
				tmp_drive=`echo "$tmp_drive" \
		    		| sed "s%)%,$tmp_bsd_partition)%"`
	    	fi
	    	echo "$tmp_drive" ;;
		freebsd*)
	    	if echo $tmp_part | grep "^s" >/dev/null; then
				tmp_pc_slice=`echo $tmp_part \
		    		| sed "s%s\([0-9]*\)[a-h]*$%\1%"`
				tmp_drive=`echo "$tmp_drive" \
		    		| sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
	    	fi
	    	if echo $tmp_part | grep "[a-h]$" >/dev/null; then
				tmp_bsd_partition=`echo "$tmp_part" \
		    		| sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
				tmp_drive=`echo "$tmp_drive" \
		    		| sed "s%)%,$tmp_bsd_partition)%"`
	    	fi
	    	echo "$tmp_drive" ;;
		netbsd*)
	    	if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
				tmp_bsd_partition=`echo "$tmp_part" \
		    		| sed "s%\([a-p]\)$%\1%"`
				tmp_drive=`echo "$tmp_drive" \
		    		| sed "s%)%,$tmp_bsd_partition)%"`
	    	fi
	    	echo "$tmp_drive" ;;
		esac
    else
		# If no partition is specified, just print the drive name.
		echo "$tmp_drive"
    fi
}

# Convert a linux non-devfs disk device name into the hurd's syntax.
hurd_convert () {
	dr_type=$(expr "$1" : '.*\([hs]d\)[a-h][0-9]*')
	dr_letter=$(expr "$1" : '.*d\([a-h]\)[0-9]*')
	dr_part=$(expr "$1" : '.*d[a-h]\([0-9]*\)')
	case "$dr_letter" in
	a) dr_num=0 ;;
	b) dr_num=1 ;;
	c) dr_num=2 ;;
	d) dr_num=3 ;;
	e) dr_num=4 ;;
	f) dr_num=5 ;;
	g) dr_num=6 ;;
	h) dr_num=7 ;;
	esac
	echo "$dr_type${dr_num}s$dr_part"
}

# Make sure mtab in the chroot reflects the currently mounted partitions.
update_mtab() {
	mtab=/target/etc/mtab
	grep /target /proc/mounts | (
	while read devpath mountpoint fstype options n1 n2 ; do
		devpath=`mapdevfs $devpath || echo $devpath`
		mountpoint=`echo $mountpoint | sed s%^/target%%`
		# The sed line removes the mount point for root.
		if [ -z "$mountpoint" ] ; then
			mountpoint="/"
		fi
		echo $devpath $mountpoint $fstype $options $n1 $n2
	done ) > $mtab
}

# Run update-grub in /target.
update_grub () {
	# Pipe from 'yes' to tell grub to create menu.lst when it is missing. A
	# better correct fix is to get update-grub to be able to run in
	# noninteractive mode.  Newer versions of the grub package support
	# '-y' for noninteractive installs.  But we'll keep it as-is for
	# Skolelinux.
	if ! chroot /target /usr/bin/yes | chroot /target /sbin/update-grub >> $log 2>&1 ; then
		error "Running 'update-grub' failed." 1>&2
		db_input critical grub-installer/update-grub-failed || [ $? -eq 30 ]
		db_go || true
		db_progress STOP
		exit 1
	fi
}

is_floppy () {
	echo "$1" | grep -q '(fd' || echo "$1" | grep -q "/dev/fd" || echo "$1" | grep -q floppy
}

findfstype () {
	mount | grep "on /target${1%/} " | cut -d' ' -f5
}

bootfstype=$(findfstype /boot)
[ -n "$bootfstype" ] || bootfstype="$(findfstype /)"
if [ "$bootfstype" = "xfs" ]; then
	# warn user that grub on xfs is not safe and let them back out to
	# main menu.
	db_input critical grub-installer/install_to_xfs
	db_go || exit 10
	db_get grub-installer/install_to_xfs
	if [ "$RET" != true ]; then
		exit 10
	fi
fi

db_progress START 0 6 grub-installer/progress/title

db_progress INFO grub-installer/progress/step_install

if ! apt-install grub ; then
	info "Calling 'apt-install grub' failed"
	# Hm, unable to install grub into /target/, what should we do?
	db_input critical grub-installer/apt-install-failed || [ $? -eq 30 ]
	if ! db_go; then
		db_progress STOP
		exit 10 # back up to menu
	fi
	db_get grub-installer/apt-install-failed
	if [ true != "$RET" ] ; then
		db_progress STOP
		exit 1
	fi
fi

db_progress STEP 1
db_progress INFO grub-installer/progress/step_os-probe
os-prober > /tmp/os-probed || true

# Work out what probed OSes can be booted from grub.
tmpfile=/tmp/menu.lst.extras
if [ -s /tmp/os-probed ]; then
	supported_os_list=""
	unsupported_os_list=""

	OLDIFS="$IFS"
	IFS="$newline"
	for os in $(cat /tmp/os-probed); do
		IFS="$OLDIFS"
		title=$(echo "$os" | cut -d: -f2)
		type=$(echo "$os" | cut -d: -f4)
		case "$type" in
			chain)
				:
			;;
			linux)
				# Check for linux systems that we don't
				# know how to boot.
				partition=$(echo "$os" | cut -d: -f1)
				if [ -z "$(linux-boot-prober $partition)" ]; then
					if [ -n "$unsupported_os_list" ]; then
						unsupported_os_list="$unsupported_os_list, $title"
					else
						unsupported_os_list="$title"
					fi
					continue
				fi
			;;
			hurd)
				:
			;;
			*)
				if [ -n "$unsupported_os_list" ]; then
					unsupported_os_list="$unsupported_os_list, $title"
				else
					unsupported_os_list="$title"
				fi
				continue
			;;
		esac

		if [ -n "$supported_os_list" ]; then
			supported_os_list="$supported_os_list, $title"
		else
			supported_os_list="$title"
		fi
		
		IFS="$newline"
	done
	IFS="$OLDIFS"
	
	if [ -n "$unsupported_os_list" ]; then
		# Unsupported OS, jump straight to manual boot device question.
		state=2
	else
		q=grub-installer/with_other_os
		db_subst $q OS_LIST "$supported_os_list"
		state=1
	fi
else
	q=grub-installer/only_debian
	state=1
fi

db_progress STEP 1
db_progress INFO grub-installer/progress/step_bootdev

while : ; do
	if [ "$state" = 1 ]; then
		db_input high $q || true
		if ! db_go; then
			# back up to menu
			db_progress STOP
			exit 10
		fi
		db_get $q
		if [ "$RET" = true ]; then
			bootdev="(hd0)"
			break
		else
			state=2
		fi
	else
		db_input critical grub-installer/bootdev || true
		if ! db_go; then
			if [ "$q" ]; then
				state=1
			else
				# back up to menu
				db_progress STOP
				exit 10
			fi
		else
			db_get grub-installer/bootdev
			bootdev=$RET
			if echo "$bootdev" | grep -qv '('; then
				mappedbootdev=$(mapdevfs "$bootdev") || true
				if [ -n "$mappedbootdev" ]; then
					bootdev="$mappedbootdev"
				fi
			fi
			break
		fi
	fi
done

db_progress STEP 1
db_subst grub-installer/progress/step_install_loader BOOTDEV "$bootdev"
db_progress INFO grub-installer/progress/step_install_loader

info "Installing grub on '$bootdev'"

update_mtab

if ! is_floppy "$bootdev"; then
	if chroot /target /sbin/grub-install -h 2>&1 | grep -q no-floppy; then
		info "grub-install supports --no-floppy"
		floppyparam="--no-floppy"
	else
		info "grub-install does not support --no-floppy"
	fi
fi

info "Running chroot /target /sbin/grub-install --recheck $floppyparam \"$bootdev\""
if chroot /target /sbin/grub-install --recheck $floppyparam "$bootdev" >> $log 2>&1 ; then
	info "grub-install ran successfully"
else
	error "Running 'grub-install --recheck $floppyparam \"$bootdev\"' failed."
	db_subst grub-installer/grub-install-failed BOOTDEV "$bootdev"
	db_input critical grub-installer/grub-install-failed || [ $? -eq 30 ]
	db_go || true
	db_progress STOP
	exit 1
fi

db_progress STEP 1
db_progress INFO grub-installer/progress/step_config_loader

# Delete for idempotency.
rm -f /target/boot/grub/menu.lst
update_grub

user_params=$(echo $(user-params)) || true
if [ -n "$user_params" ]; then
	# Modify menu.lst to include user parameters.
	sed "s!^\(# kopt=.*\)!\1 $user_params!" \
		< /target/boot/grub/menu.lst > /target/boot/grub/menu.lst.new
	mv /target/boot/grub/menu.lst.new /target/boot/grub/menu.lst
	update_grub # again, to add new options to all the Debian kernel entries
fi

# Generation menu.lst addition for other OSes.
tmpfile=/tmp/menu.lst.extras
OLDIFS="$IFS"
IFS="$newline"
for os in $(cat /tmp/os-probed); do
	IFS="$OLDIFS"
	title=$(echo "$os" | cut -d: -f2)
	type=$(echo "$os" | cut -d: -f4)
	case "$type" in
		chain)
			partition=$(mapdevfs $(echo "$os" | cut -d: -f1))
			grubdrive=$(convert "$partition") || true
			if [ -n "$grubdrive" ]; then
				cat >> $tmpfile <<EOF

# This entry automatically added by the Debian installer for a non-linux OS
# on $partition
title		$title
root		$grubdrive
savedefault
EOF
				# Only set makeactive if grub is installed
				# in the mbr.
				if [ "$bootdev" = "(hd0)" ]; then
					cat >> $tmpfile <<EOF
makeactive
EOF
				fi
				cat >> $tmpfile <<EOF
chainloader	+1

EOF
			fi
		;;
		linux)
			partition=$(echo "$os" | cut -d: -f1)
			mappedpartition=$(mapdevfs "$partition")
			IFS="$newline"
			for entry in $(linux-boot-prober "$partition"); do
				IFS="$OLDIFS"
				bootpart=$(echo "$entry" | cut -d: -f2)
				mappedbootpart=$(mapdevfs "$bootpart") || true
				if [ -z "$mappedbootpart" ]; then
					mappedbootpart="$bootpart"
				fi
				label=$(echo "$entry" | cut -d : -f3)
				if [ -z "$label" ]; then
					label="$title"
				fi
				kernel=$(echo "$entry" | cut -d : -f4)
				initrd=$(echo "$entry" | cut -d : -f5)
				if echo "$kernel" | grep -q '^/boot/' && \
				   [ "$mappedbootpart" != "$mappedpartition" ]; then
				   	# separate /boot partition
					kernel=$(echo "$kernel" | sed 's!^/boot!!')
					initrd=$(echo "$initrd" | sed 's!^/boot!!')
					grubdrive=$(convert "$mappedbootpart") || true
				else
					grubdrive=$(convert "$mappedpartition") || true
				fi
				params="$(echo "$entry" | cut -d : -f6-) $serial"
				cat >> $tmpfile <<EOF

# This entry automatically added by the Debian installer for an existing
# linux installation on $mappedpartition.
title		$label (on $mappedpartition)
root		$grubdrive
kernel		$kernel $params
EOF
				if [ -n "$initrd" ]; then
					cat >> $tmpfile <<EOF
initrd		$initrd
EOF
				fi
				cat >> $tmpfile <<EOF
savedefault
boot

EOF
				IFS="$newline"
			done
			IFS="$OLDIFS"
		;;
		hurd)
			partition=$(mapdevfs $(echo "$os" | cut -d: -f1))
			grubdrive=$(convert "$partition") || true
			hurddrive=$(hurd_convert "$partition") || true
			# Use the standard hurd boilerplate to boot it.
			cat >> $tmpfile <<EOF

# This entry automatically added by the Debian installer for an existing
# hurd installation on $partition.
title		$title (on $partition)
root		$grubdrive
kernel		/boot/gnumach.gz root=device:$hurddrive
module		/hurd/ext2fs.static --readonly \\
			--multiboot-command-line=\${kernel-command-line} \\
			--host-priv-port=\${host-port} \\
			--device-master-port=\${device-port} \\
			--exec-server-task=\${exec-task} -T typed \${root} \\
			\$(task-create) \$(task-resume)
module		/lib/ld.so.1 /hurd/exec \$(exec-task=task-create)
savedefault
boot

EOF
		;;
		*)
			info "unhandled: $os"
		;;
	esac
	IFS="$newline"
done
IFS="$OLDIFS"
rm -f /tmp/os-probed

if [ -s $tmpfile ]; then
	cat >> /target/boot/grub/menu.lst << EOF

# This is a divider, added to separate the menu items below from the Debian
# ones.
title		Other operating systems:
root

EOF
	cat $tmpfile >> /target/boot/grub/menu.lst
	rm -f $tmpfile
fi

db_progress STEP 1
db_progress INFO grub-installer/progress/step_update_etc

sed -e 's/do_bootloader = yes/do_bootloader = no/' < /target/etc/kernel-img.conf > /target/etc/kernel-img.conf.$$
if [ -z "`grep update-grub /target/etc/kernel-img.conf.$$`" ]; then
	(
		echo "postinst_hook = /sbin/update-grub"
		echo "postrm_hook   = /sbin/update-grub"
	) >> /target/etc/kernel-img.conf.$$
fi
mv /target/etc/kernel-img.conf.$$ /target/etc/kernel-img.conf

db_progress STEP 1
db_progress STOP
