#! /bin/sh

# copyright Thomas Lange 2001-2003, lange@debian.org
# LILO support may be dropped in a future release

gmenu=$target/boot/grub/menu.lst
grubvars() {
    # define variables for grub
    fp=$1
    #fullpath=$(ls $target/boot/vmlinuz-*)

    dfline=$(df $fp | grep '^/dev/')
    dev=$(echo $dfline | cut -d ' ' -f1)
    mntp=$(echo $dfline | awk '{print $6}')
    broot=`device2grub $dev`
    gpath=${fp#$mntp}
}

# this is the function: AppendIfNoSuchLine
egrep -q "^auto" $target/etc/modules 2>/dev/null || echo "auto" >> $target/etc/modules

# a list of modules which are loaded at boot time
for module in $moduleslist; do
    echo $module >> $target/etc/modules
done

FFLAGS=${FAI_FLAGS// /,}

cd $target/boot && kernelversion=`ls vmlinuz-*`

# if class MBR is defined, write lilo into master boot record
ifclass MBR || BOOT_DEVICE=$ROOT_PARTITION

if ifclass LILO; then
  fcopy /etc/lilo.conf || {
    # if the installed kernel is using an initial ramdisk
    if [ -L $target/initrd.img ]; then
	initrdopt="initrd=/initrd.img"
    fi
    # write lilo.conf and install lilo to disk
    echo "Creating lilo.conf for /vmlinuz"
    cat > $target/etc/lilo.conf <<-EOF
	boot=$BOOT_DEVICE
	root=$ROOT_PARTITION
	install=/boot/boot.b
	map=/boot/map
	vga=normal
	delay=50

	image=/vmlinuz
	append="devfs=nomount"
	$initrdopt
	label=linux
        read-only

	image=/vmlinuz.old
	label=linux.old
	read-only
	optional
	$liloappend
EOF
  }
fi

if ifclass GRUB; then
    grub-install --root-directory=$target $BOOT_DEVICE

    fcopy /boot/grub/menu.lst || {
    fullpath=$(ls $target/boot/vmlinuz-*)
    grubvars $fullpath
    if [ -L $target/initrd.img ]; then
	iname=$(cd $target/boot; ls initrd.img*)
	t="$target/boot"
	ipath=${t#$mntp}
	initrdopt="initrd $ipath/$iname"
    fi
    cat > $gmenu <<-EOF

	# created by FAI
	
	timeout 10
	default 0
	color cyan/blue white/blue

	title Debian GNU/Linux, $kernelversion
	root $broot
	kernel $gpath root=$ROOT_PARTITION
	$initrdopt
EOF
    if ifclass BOOTWINDOWS; then
    cat >> $gmenu <<-EOF

	# created by FAI

	# For booting Windows NT or Windows95
	title Windows
	rootnoverify (hd0,0)
	makeactive
	chainloader  +1
EOF
    fi

    # add entry for memtest
    if dpkg -l memtest86 > /dev/null 2>&1 ; then
    t="$target/boot"
    gpath=${t#$mntp}
    cat >> $gmenu <<-EOF

	title  memtest
	root   $broot
	kernel $gpath/memtest86.bin
EOF
    fi
    }
    echo "Grub installed for $kernelversion on $broot"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ifclass FAI_BOOTPART && {

    mkdir -p $target/fai-boot/boot
    cp -a /boot/vmlinuz* $target/fai-boot/boot

    ifclass LILO && {
    # write lilo.conf and install lilo to disk
	echo "Add /fai-boot partition to lilo.conf"

	cat >> $target/etc/lilo.conf <<-EOF
	image=/fai-boot/boot/$kernelversion
	$initrdopt
	append="devfs=nomount"
	label=FAI
EOF
	mkdir $target/fai-boot/etc
	cp $target/etc/lilo.conf $target/fai-boot/etc
    }

    ifclass GRUB && {

	echo "Add /fai-boot partition to menu.lst for grub"
	cd $target/fai-boot/ && kernelname=/`ls boot/vmlinuz-*`
	fullpath=$(ls $target/fai-boot/boot/vmlinuz-*)
	grubvars $fullpath
	cat >> $gmenu <<-EOF

	title start FAI with FAI_ACTION=sysinfo
	root $broot
	kernel $kernelname root=/dev/nfs ip=dhcp FAI_FLAGS=$FFLAGS FAI_LOCATION=$FAI_LOCATION FAI_ACTION=sysinfo

	title FAI re-Installation
	root $broot
	kernel $kernelname root=/dev/nfs ip=dhcp FAI_FLAGS=$FFLAGS FAI_LOCATION=$FAI_LOCATION FAI_ACTION=install
EOF
	echo "Grub for $kernelversion installed on $broot"
    }
}

# now call lilo
# you can also use this
# $ROOTCMD lilo -v
if ifclass LILO; then
    $target/sbin/lilo -r $target
fi
