#!/bin/sh 

set -e

if [ -f /usr/share/debconf/confmodule ]; then
  . /usr/share/debconf/confmodule
fi

# Automatically added by dh_installmenu
if [ "$1" = "configure" ] && [ -x "`which update-menus 2>/dev/null`" ]; then
	update-menus
fi
# End automatically added section

case "$1" in
    configure|reconfigure)
    
	# if a new install then we create the link and exit
	if grep -q "^# UNCONFIGURED FSTAB FOR BASE SYSTEM" /etc/fstab ; then
	  exit 0
	fi

	# Check whether /boot is on another partition and mount it. See Bug#216250

	if grep "[[:space:]]/boot[[:space:]]" /etc/fstab | grep -vq ^#; then
	   if ! grep -q "[[:space:]]/boot[[:space:]]" /etc/mtab ; then
	   	mount /boot 2>&1 > /dev/null

		if [ $? -ne 0 ]; then
			echo
			echo "WARNING: /boot is in another partition but could not be mounted."
			echo "LILO may fail in the next steps."
		fi
	   fi
	fi

	cp /usr/share/lilo/*.bmp /boot
	
	if [ -L /boot/debian.bmp ]; then
		if [ x`readlink /boot/debian.bmp | cut -d/ -f 2` != x"boot" ]; then
			ln -sf /boot/sid.bmp /boot/debian.bmp
		fi
	fi

	if [ ! -e /boot/debian.bmp ]; then
		ln -sf /boot/sarge.bmp /boot/debian.bmp
	fi

	# Nasty part to create network block devices if needed. Bug#235805.
	if [ `uname -r | sed -e 's/-.*//g' -ne 's/\(^[0-9]\{1\}\.[0-9]\{1,2\}\).*/\1/p'` = "2.6" ] && \
	   [ `uname -r | sed -e 's/-.*//g' -ne 's/.*\.\([0-9]\{1,3\}\).*/\1/p'` -ge "3" ]; then

	   garbage=$(cat /proc/partitions | sed -ne 's/^[ \t]*//pg' | sed -ne '/^43/p')

	   if [ x"$garbage" != "x" ]; then
		
		# Create the missing devices
		echo -n "Creating network block devices... "
		(cd /dev; ./MAKEDEV nb) > /dev/null
		echo "done."  
	    fi
	fi

	db_get lilo/add_large_memory || true;
	if [ x"$RET" = x"true" -a -e /etc/lilo.conf ]; then
		if ! grep -q "^[[:space:]]*large-memory" /etc/lilo.conf; then
			sed -i -e '1i\# Automatically added by lilo postinst script\nlarge-memory\n' /etc/lilo.conf
			echo "WARNING: Added large-memory option, please run lilo before you reboot"
		fi
	elif [ -e /vmlinuz -a -e /initrd.img -a -e /etc/lilo.conf ]; then
		kernel_size=$(stat -L -c %s /vmlinuz)
		initrd_size=$(stat -L -c %s /initrd.img)
		size=$(($kernel_size+$initrd_size))
		large_mem=$(grep -c "^[[:space:]]*large-memory" /etc/lilo.conf) || true
		if [ $large_mem -eq 0 -a $size -ge 8388608 ]; then
			echo
			echo "WARNING: You have a large kernel+initrd and large-memory is not set in lilo.conf."
			echo "WARNING: Do NOT reboot or LILO may fail to boot."
			echo "WARNING: Please read /usr/share/doc/lilo/README.Debian"
			echo 
		fi
	else
		echo
		echo "WARNING: kernel & initrd not found in the root directory (/vmlinuz & /initrd.img)"
		echo "WARNING: Do NOT reboot or LILO may fail to boot if your kernel+initrd is large."
		echo "WARNING: Please read /usr/share/doc/lilo/README.Debian"
		echo
	fi

	db_get lilo/runme || true;
	if [ x"$RET" = x"true" ]; then
		echo "Running lilo..."
		/sbin/lilo -H
	fi

	;;
    abort-upgrade|abort-remove|abort-deconfigure)

        ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
	exit 1
        ;;
esac

exit 0
