#!/bin/zsh

# to install the grub bootloader manually, read the
# http://www.gnu.org/software/grub/manual/
# in brief what we do is:
# install on a partition, eg :
# grub-install --root-directory=/mnt/hd1/1 /dev/hda
# then copy a menu.lst grub configuration in it
# and then give grub commands
# root (hd0,0)
# setup (hd0)


source /boot/dynenv
source /lib/dyne/utils.sh
source /lib/dyne/dialog.sh

TMP=/tmp/grubconfig
BOOT_TMP=/tmp/boot
CONSOLETYPE=standard
CONSOLENUM=normal
KRN=`uname -r`

# compose the kernel file name removing all . or -
krnfile="`echo ${KRN} | sed -e 's/\.//g' | sed -r 's/-//g' | cut -c 1-8`.krn"


rm -rf $TMP $BOOT_TMP
mkdir -p $TMP $BOOT_TMP



select_destination() {
    DEVLIST=`cat /boot/volumes | grep -E '^hdisk|^usb'`
    c=0
    rm -f $TMP/hdselect_dialog
    cat <<EOF > $TMP/hdselect_dialog
"\n\n The BOOTLOADER will boot the operating system without\
 the need of a CD, from hard disk or USB stick.\n\
 Be careful as this operation will overwrite previously installed bootloaders.\n\
 Choose your device:\n\n\n" 20 51 4
"Quit" "Don't install anything" "I'm not sure what i'm doing, i don't want to damage anything, nevermind"
EOF
    
    for i in ${(f)DEVLIST}; do
	c=`expr $c + 1`
	type=`echo $i| awk '{print $1}'`
	dev=`echo $i| awk '{print $2}'`
	fs=`/lib/udev/vol_id -t $dev` 
	echo \
	    "\"$dev\" \"$type\" \"formatted $fs\"" >> $TMP/hdselect_dialog
    done
    
    # now render the dialog
    dynedialog --clear --item-help --title \
	" Install GRUB bootloader on devices " \
	--menu --file $TMP/hdselect_dialog 2> $TMP/hdselect_choice
    
    rm -f $TMP/hdselect_dialog
}

select_destination;

# use_framebuffer;

sel=`cat $TMP/hdselect_choice`

if [ "$sel" = "Abort" ]; then
    clear;
    act "operation aborted, bye"
    rm -f $TMP/hdselect_choice
    exit
fi

if [ -r $sel ]; then

    ask_yesno -1 "Bootloader installation selected for device $sel\nAre you sure you want to proceed?"
    
    if [ $? = 1 ]; then # proceed

	mkdir -p $TMP/mnt
	mount -o sync $sel $TMP/mnt
	grub-install --no-floppy --root-directory=$TMP/mnt $sel
	# now let's guess how the device is called in grub.
	# i approach the problem in a different way than conversion
	rm -f $TMP/grubselect_choice
	touch $TMP/mnt/00grubconfig # to be found by grub find
	echo "find /00grubconfig\nquit" | grub --no-floppy --batch 1>$TMP/grubselect_choice 2>/dev/null
	grubsel=`cat $TMP/grubselect_choice | awk '/^ \(/ {print $1}'`
	rm -f $TMP/mnt/00grubconfig
	notice "installing GRUB on device $grubsel"
	cd $TMP/mnt/boot/grub
	if [ -r menu.lst ]; then mv menu.lst backup.menu.lst; fi
	cat <<EOF > menu.lst
# GRUB configuration file $grub_config
# generated by grubconfig on dyne:bolic `date +%c 2>$TMP/null`
#
# for documentation about this bootloader see:
# http://www.gnu.org/software/grub/manual/
#
# Start GRUB global section
timeout 30
color light-gray/blue black/light-gray
# End GRUB global section

# Start dyne:bolic entry
title dyne:II DHORUBA
root $grubsel
kernel /dyne/$krnfile root=/dev/ram0 rw load_ramdisk=1 max_loop=64 vga=791
initrd /dyne/initrd.gz

# for a windlows partition use the following entry
#title Windblows
#rootnoverify (hd0,0)
#chainloader +1
EOF
	chmod 644 menu.lst
	cd -

	umount $TMP/mnt
	grubsetup="(`echo $grubsel | cut -d'(' -f2 | cut -b 0-3`)"
	echo "root $grubsel\nsetup $grubsetup\nquit\n" | grub --no-floppy --batch
	rm -f $TMP/hdselect_choice
	notice "Operation succesful!"

    fi

else
    error "Error: device $sel doesn't exists. aborting."
    rm -f $TMP/hdselect_choice
    clear;
    act "operation aborted, bye"
fi

