#!/bin/sh

set -x 
DIA=zenity

if [ $UID != 0 ] ; then
    T1="Not Allowed"
    M1="This script needs root privileges. Use gksu/su to run the script"
    $DIA --title "$T1" --error --text="$M1"
  exit 0
fi

TMP=/tmp/redo-mbr.$$

LOOP=/tmp/loop
if [ -d $LOOP ]; then
    T1="Folder exists"
    M1="Folder used exists. If you are sure it shouldn't be there delete it and rerun the script"
    $DIA --title "$T1" --error --text="$M1"
else
    mkdir -p $LOOP
fi

sudo fdisk -l|grep "^/dev.*Linux$"|tr -d '*'|
while read dev x x size x 
do
    mount $dev $LOOP
    if [ -d $LOOP/boot/ ]; then
	echo  "FALSE" $dev >> $TMP
    fi
    umount $LOOP
done

NUMUBU="`wc -l <$TMP`"
UBUPART="`cat $TMP`"

T1="Linux partition selection"
M1="No Linux installation could be found on your hard disks. The script
will be terminated."

if [ -z $NUMUBU ]; then
	$DIA --title "$T1" --warning --text="$M1"
	rm -f $TMP
	exit 0
fi

if [ $NUMUBU -eq 1 ]; then
	UBUPART="`sed -e 's/^FALSE //' $TMP`"
fi

if [ $NUMUBU -gt 1 ]; then
	#more than one Linux partition
	M1="More than one Linux installation found, Please select the
one you want to reinstall mbr from"
	$DIA --title "$T1" --text "$M1" --list --radiolist --column "Check" \
        --column "Partition" $UBUPART > $TMP
	FSCHOICE="`cat $TMP`"

	M1="No partition chosen. The script will be terminated."

	if [ -z "$FSCHOICE" ] ; then
		$DIA --title "$T1" --warning --text="$M1" 
		rm -f $TMP
		exit 0
	fi
	dev=$FSCHOICE
	NUMUBU=1
else
	dev="`echo $UBUPART|awk '{print $1}'`"
fi

grub-install --recheck --no-floppy /dev/null &>/dev/null

while [ 1 ] ;do
  GRUB_MBR=$(grep "(hd0)" /boot/grub/device.map | awk '{print $2}')
  sleep 1
  if [ -n "$GRUB_MBR" ]; then
	break
  fi
done
  
mount $dev $LOOP

CANCELED="Cancelled. The script will be terminated."
       
if [ -e $LOOP/boot/grub/menu.lst ]; then
	
	M1="The bootloader configuration of the chosen partition will displayed.\n\
	    You can edit the the configuration and save your changes.\n\
	    You will be asked if you want to recreate your bootloader."
        $DIA --title "$T1" --info --text="$M1"

	
	M1="Do you want to edit the configuration file?"
	$DIA --title "$T1" --question --text="$M1"
	x=$?
	if [ $x = 0 ] ; then
	    gedit $LOOP/boot/grub/menu.lst
	fi
	M1="Do you want to restore GRUB ?"
	$DIA --title "$T1" --question --text="$M1"
	x=$?
	if [ $x != 0 ] ; then
		$DIA --title "$T1" --warning --text="$CANCELED"
		rm -f $TMP
		exit 0
	fi
	# the actual reset is commented out for debugging
	mount -t proc none $LOOP/proc
	mount -t none /dev $LOOP/dev -o bind
	chroot $LOOP grub-install $GRUB_MBR 
	umount $LOOP/dev
	umount $LOOP/proc
else
	M1="Grub configuration file not found.\n\n
	     Do you want to regenerate the menu.lst file?"
	$DIA --title="$T1" --question --text="$M1"
	x=$?
	if [ $x !=0 ]; then
	    $DIA --title "$T1" --warning --text="$CANCELED"
            rm -f $TMP
            exit 0
	fi

        mount -t proc none $LOOP/proc
        mount -t none /dev $LOOP/dev -o bind
        chroot $LOOP bash /sbin/update-grub -y
        umount $LOOP/dev
        umount $LOOP/proc

fi

umount $LOOP
rm -f $TMP
rmdir $LOOP
T1="Finished"
M1="Grub is restored"
$DIA --title "$T1" --info --text="$M1"
