#!/bin/sh
#
# $Id: mr-grub 3683 2017-08-18 10:16:52Z bruno $
#
# mr-grub ------ a rudimentary replacement for grub-install
#
#

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


FindBootPart() {
	# Work only for grub v1
    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 "All right 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) $mbrdev\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 "mr-grub <boot drive> <mountlist filename>; e.g. mr-grub /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

# For some OpenSuSE
res=1
if [ "$MNT_RESTORING" ] ; then
	if [ -x $MNT_RESTORING/usr/sbin/grub-install.unsupported ]; then
		echo "Now I'll use OpenSuSE/SLES new grub-install in chroot" >> $LOGFILE
		chroot $MNT_RESTORING /usr/sbin/grub-install >> $LOGFILE 2>> $LOGFILE
		res=$?
		echo "grub-install in chroot returned $res" >> $LOGFILE
	fi
else
	if [ -x /usr/sbin/grub-install.unsupported ]; then
		echo "Now I'll use OpenSuSE/SLES new grub-install locally" >> $LOGFILE
		/usr/sbin/grub-install >> $LOGFILE 2>> $LOGFILE
		res=$?
		echo "grub-install returned $res" >> $LOGFILE
	fi
fi
[ "$res" -eq "0" ] && exit 0

echo "Now I'll use grub-install" >> $LOGFILE
if [ "$MNT_RESTORING" ] ; then
	echo "Launching: chroot $MNT_RESTORING grub-install $1" >> $LOGFILE
	chroot $MNT_RESTORING grub-install $1 >> $LOGFILE 2>> $LOGFILE
	res=$?
else
	echo "Launching: grub-install $1" >> $LOGFILE
	grub-install $1 >> $LOGFILE 2>> $LOGFILE
	res=$?
fi
echo "grub-install returned $res" >> $LOGFILE
[ "$res" -eq "0" ] && exit 0


echo "Now I'll use grub2-install" >> $LOGFILE
if [ "$MNT_RESTORING" ] ; then
	chroot $MNT_RESTORING grub2-install --force $1 >> $LOGFILE 2>> $LOGFILE
	res=$?
else
	grub2-install --force $1 >> $LOGFILE 2>> $LOGFILE
	res=$?
fi
echo "grub2-install returned $res" >> $LOGFILE
[ "$res" -eq "0" ] && exit 0

echo "Trying a hack" >> $LOGFILE
FindBootPart $1 $2 2>&1 | tee -a $LOGFILE
mbrdev=`echo $1 | sed 's/\([^0-9]*\)[0-9]*$/\1/'`
if echo $mbrdev | grep "/cciss/" > /dev/null ; then
	partno=`basename $mbrdev | cut -d'p' -f2`
	mbrdev=`echo $mbrdev | cut -d'p' -f1`
elif echo $mbrdev | grep "/mapper/mpath" > /dev/null ; then
	partno=`basename $mbrdev | cut -d'p' -f3`
	mbrdev=`echo $mbrdev | cut -d'p' -f1-4`
else
	partno=`basename $mbrdev | sed 's/[a-z]*//'`
fi
if [ ! "$partno" ] ; then
    partno=0
else
    partno=$(($partno-1))
fi
if echo $mbrdev | grep "/md" > /dev/null ; then
	# FIXME: Why this if not used later
    base=`basename $bootpart`
    line=`grep $base /proc/mdstat | head -n1`
    echo "mbrdev was $mbrdev" 2>&1 | tee -a $LOGFILE
    mbrdev=`mr-parted2fdisk -l 2>/dev/null | grep /dev/ | head -n1 | tr ':' ' ' | cut -d' ' -f2`
    echo "mbrdev is $mbrdev" 2>&1 | tee -a $LOGFILE
    partno="0"; # cheating - FIXME    
fi
echo ".............Cool." 2>&1 | tee -a $LOGFILE

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

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

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 mr-grub-install.patched" >> $LOGFILE
cp -f `which mr-grub-install.patched` /mnt/RESTORING/sbin
chroot /mnt/RESTORING mr-grub-install.patched $1 >> $LOGFILE 2>> $LOGFILE
res=$?
echo "mr-grub-install.patched returned $res" >> $LOGFILE
[ "$res" -eq "0" ] && exit 0

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

if [ -f "/mnt/RESTORING/boot/grub/menu.lst" ]; then
	grep -vE '^#' /boot/grub/menu.lst > /mnt/RESTORING/tmp/grub.conf
elif [ -f "/mnt/RESTORING/boot/grub/grub.cfg" ]; then
	grep -vE '^#' /boot/grub/grub.cfg > /mnt/RESTORING/tmp/grub.conf
elif [ -f "/mnt/RESTORING/boot/grub2/grub.cfg" ]; then
	grep -vE '^#' /boot/grub2/grub.cfg > /mnt/RESTORING/tmp/grub.conf
elif [ -d "/mnt/RESTORING/boot/efi" ]; then
	grep -vE '^#' `find /boot/efi -name grub.conf` > /mnt/RESTORING/tmp/grub.conf
else
	echo "Unable to find Grub conf file" | tee -a $LOGFILE
	exit -1
fi
echo "Trying to use the existing grub conf file in batch mode" >> $LOGFILE
cat /mnt/RESTORING/tmp/grub.conf >> $LOGFILE
chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/tmp/grub.conf
res=$?
echo "grub conf file approach returned $res" >> $LOGFILE
rm -f /mnt/RESTORING/tmp/grub.conf
[ "$res" -eq "0" ] && exit 0

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

exit $res
