#!/bin/sh
#
# $Id: grub-MR 2095 2008-12-17 16:26:31Z bruno $
#
# grub-MR ------ a rudimentary replacement for grub-install
#
#

Die() {
    echo "$1" >> /dev/stderr
    exit 1
}


FindBootPart() {
    local i
    bootpart=""
    BOOTPATHNAME=""
    for sz in /boot / ; do
	bootpart=`grep " $sz " $2 | cut -d' ' -f1 | head -n1`
	[ "$bootpart" ] && [ -e "/mnt/RESTORING/$bootpart" ] && break
    done
    [ ! "$bootpart" ] && Die "Cannot find boot partition in mountlist"

    if [ "$sz" = "/boot" ] ; then
	BOOTPATHNAME=/grub
    else
	BOOTPATHNAME=/boot/grub
    fi
    if [ -e "$MNT_RESTORING/boot/grub/stage1" ] ; then
	echo "Alright then."
	return
    fi
    
    cd $MNT_RESTORING
    for i in usr/lib/grub/* ; do
	echo "Checking $i"
        if [ -e "$i/stage1" ] ; then
	    BOOTPATHNAME=/$i
	    mkdir -p /boot/grub
	    cp -au $i/* /boot/grub/
	    echo "BOOTPATHNAME is now $BOOTPATHNAME"
	    return 0
	fi
    done
    cd /
    echo "Cannot find BOOTPATHNAME"
    return 1
}

FindPathOfRESTExe() {
    local path
    for path in /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin \
/bin /sbin ; do
        if [ -f "/mnt/RESTORING/$path/$1" ] ; then 
            echo "/mnt/RESTORING/$path/$1"
	    return
        fi
    done
    which grub
}


try_grub_hack() {
    local driveno extraline partno
    driveno=$1
    partno=$2
    extraline="$3"

    echo -en "\
	device (hd$driveno) $mbrpart\n\
	$extraline\n\
	root (hd$driveno,$partno)\n\
	setup (hd$driveno)\n\
	quit \n" > /tmp/feed.txt
    cat /tmp/feed.txt
	log_file=/tmp/grub.output
    if [ "$MNT_RESTORING" ] ; then
        chroot /mnt/RESTORING grub --batch < /tmp/feed.txt > $log_file
    else
        grub --batch < /tmp/feed.txt > $log_file
    fi
	cat $log_file
	if grep "Error [0-9]*: " $log_file >/dev/null; then
			return 1
	else
			return 0
	fi
}


# ---------------------------------- main -----------------------------------

if [ "$#" -ne "2" ] ; then
    Die "grub-MR <MBR drive> <mountlist filename>; e.g. grub-MR /dev/hda /tmp/mountlist.txt"
fi
[ ! -f "$2" ] && Die "mountlist file not found"

if [ -e "/mnt/RESTORING/boot" ] ; then
    MNT_RESTORING=/mnt/RESTORING
else
    MNT_RESTORING=""
fi

echo "Now I'll use grub-install" >> $LOGFILE
chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
res=$?
echo "grub-install returned $res" >> $LOGFILE
[ "$res" -eq "0" ] && exit 0

echo "Trying a hack" >> $LOGFILE
FindBootPart $1 $2
mbrpart=$1
if echo $bootpart | grep "/cciss/" > /dev/null ; then
	partno=`basename $bootpart | cut -d'p' -f2`
else
	partno=`basename $bootpart | sed s/[a-z]*//`
fi
if [ ! "$partno" ] ; then
    partno=0
else
    partno=$(($partno-1))
fi
if echo $bootpart | grep "/md" > /dev/null ; then
    base=`basename $bootpart`
    line=`grep $base /proc/mdstat | head -n1`
    echo "mbrpart was $mbrpart"
    mbrpart=`parted2fdisk -l | grep /dev/ | head -n1 | tr ':' ' ' \
| cut -d' ' -f2`
    echo "mbrpart is $mbrpart"
    partno="0"; # cheating - FIXME    
fi
echo ".............Cool."

grub=`FindPathOfRESTExe grub`
mkdir -p /boot
[ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub

# ---------------------------------

for driveno in 0 1 2 ; do
    try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
    try_grub_Hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
done
res=$?
echo "Hack returned res=$res" >> $LOGFILE
[ "$res" -eq "0" ] && exit 0

# ---------------------------------

echo "Now I'll use grub-install.patched" >> $LOGFILE
cp -f `which grub-install.patched` /mnt/RESTORING/sbin
chroot /mnt/RESTORING grub-install.patched $1 >> $LOGFILE
res=$?
echo "grub-install.patched returned $res" >> $LOGFILE
[ "$res" -eq "0" ] && exit 0

# ---------------------------------

echo "Trying to use the existing menu.lst file in batch mode" >> $LOGFILE
chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/boot/grub/menu.lst
res=$?
echo "menu.lst approach returned $res" >> $LOGFILE
[ "$res" -eq "0" ] && exit 0

#echo "Now I'll use grub-install" >> $LOGFILE
#chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
#res=$?
#echo "grub-install returned $res" >> $LOGFILE
#[ "$res" -eq "0" ] && exit 0

echo "grub-MR returned res=$res" >> $LOGFILE

exit $res
