#!/bin/sh
#
# 2004-03-08: First draft Bruno Cornec <bruno@mondorescue.org>
#

QuitIfNotFound() {
    if [ ! -f "$1" ] ; then
	LogIt "(mr-stabelilo-me) Where's $1? I cannot continue." 1
	exit 1
    fi
}




FindLiloDir() {
    list=`find / | grep -name elilo.conf`
}



LocateOldElilo() {
    old_eliloconf=""
    if [ -f "/mnt/RESTORING/boot/efi/efi/redhat/elilo.conf" ] ; then
        LogIt "elilo.conf found at /mnt/RESTORING/boot/efi/efi/redhat/elilo.conf" 2
		old_eliloconf="/mnt/RESTORING/boot/efi/efi/redhat/elilo.conf"
		return 0
	elif [ -f "/mnt/RESTORING/boot/efi/efi/SuSE/elilo.conf" ] ; then
        LogIt "elilo.conf found at /mnt/RESTORING/boot/efi/efi/SuSE/elilo.conf" 2
		old_eliloconf="/mnt/RESTORING/boot/efi/efi/SuSE/elilo.conf"
		return 0
	elif [ -f "/mnt/RESTORING/boot/efi/efi/debian/elilo.conf" ] ; then
        LogIt "elilo.conf found at /mnt/RESTORING/boot/efi/efi/debian/elilo.conf" 2
		old_eliloconf="/mnt/RESTORING/boot/efi/efi/debian/elilo.conf"
		return 0
	elif [ -f "/mnt/RESTORING/boot/efi/debian/elilo.conf" ] ; then
        LogIt "elilo.conf found at /mnt/RESTORING/boot/efi/debian/elilo.conf" 2
		old_eliloconf="/mnt/RESTORING/boot/efi/debian/elilo.conf"
		return 0
    else
        LogIt "Looking for elilo.conf. Please wait." 2
        elilo_list=`find /mnt/RESTORING/boot -name elilo.conf 2> /dev/null`
    fi
    where_they_live=""
    for curr_elilo in $elilo_list ; do
		curr_dir=`dirname $curr_elilo`
		if [ "$?" -eq "0" ] ; then
	    	if [ "$where_they_live" != "" ] ; then
				LogIt "Two directories found! One is $where_they_live, the other $curr_dir" 1
				LogIt "I don't know which to choose. I'll abort the search." 1
				return 1
	    	fi
		    where_they_live=$curr_dir
		fi
    done
    if [ "$where_they_live" = "" ] ; then
		LogIt "Cannot find any folder which holds elilo.conf" 1
		return 1
    fi
    old_eliloconf=$where_they_live/elilo.conf
    LogIt "ELILO found." 2
    return 0
}

LocateOldFstab() {
    old_fstab=""
    if [ -f "/mnt/RESTORING/etc/fstab" ] ; then
        LogIt "No need for fstab search." 2
		old_fstab=/mnt/RESTORING/etc/fstab
		return 0
    elif [ -f "/mnt/cdrom/archives/CUCKOO" ] ; then
        LogIt "Because I'm cuckoo, I'll stop searching." 2
        return 1
    else
        LogIt "Looking for fstab. Please wait." 2
        fstab_list=`find /mnt/RESTORING -name fstab 2> /dev/null`
    fi
    where_they_live=""
    for curr_fstab in $fstab_list ; do
		curr_dir=`echo $curr_fstab | gawk '{i=index($0,"/fstab");print substr($0,0,i-1);}'`
		resA=$?
		curr_inetd=`find $curr_dir -name inetd.conf | grep -v "linuxconf"`
		resB=$?
		if [ "$resA" -eq "0" ] ; then
	    	if [ "$where_they_live" != "" ] ; then
				LogIt "Two directories found! One is $where_they_live, the other $curr_dir" 1
				LogIt "I don't know which to choose. I'll abort the search." 1
				return 1
	    	fi
		    where_they_live=$curr_dir
		fi
    done
    if [ "$where_they_live" = "" ] ; then
		LogIt "Cannot find any folder which holds fstab _and_ inetd.conf" 1
		return 1
    fi
    old_fstab=$where_they_live/fstab
    LogIt "fstab found." 2
    return 0
}


MakeBackups() {
    LogIt "Backing up original files before modifying them..." 2
    for i in $old_eliloconf $old_fstab ; do
	LogIt "Backing up $i"
        [ -f "$i" ] && cp $i $i.prehack -f
    done
}


RestoreBackups() {
    LogIt "Restoring original versions of modified files..." 2
    cp -f $old_eliloconf /tmp/elilo.conf.ugly
    cp -f $old_fstab /tmp/fstab.ugly
    for i in $old_eliloconf $old_fstab ; do
	LogIt "Restoring $i"
	[ -f "$i.prehack" ] && cp $i.prehack $i -f
    done
}


WhatIsFirstDriveCalled() {
    cut -d' ' -f1 /tmp/mountlist.txt \
| sed s/[0-9]// | sed s/[0-9]// \
| sort -u | head -n 1
}


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

LogIt "mr-stabelilo-me --- starting"
LocateOldFstab
LocateOldElilo
old_mountlist=/tmp/mountlist.original
new_mountlist=/tmp/mountlist.txt
QuitIfNotFound $old_mountlist
QuitIfNotFound $new_mountlist
QuitIfNotFound $old_fstab
QuitIfNotFound $old_eliloconf
LogIt "OK so far: I've found all the files I need." 2

MakeBackups

new_fstab=/mnt/RESTORING/etc/fstab.NEW
[ ! -e "$new_fstab" ] && new_fstab=/mnt/RESTORING/etc/fstab
new_eliloconf=/mnt/RESTORING/etc/elilo.conf.NEW
[ ! -e "$new_eliloconf" ] && new_eliloconf=/mnt/RESTORING/etc/elilo.conf

LogIt "Modifying fstab..." 2
outval=0
mr-hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab
res=$?
if [ "$res" -ne "0" ] ; then
    echo "Warning - mr-hack-fstab failed"
    outval=$(($outval+$res))
fi
   
LogIt "Modifying elilo.conf. Please wait..." 2
mr-hack-elilo $old_mountlist $old_fstab $old_liloconf $new_mountlist $new_fstab $new_liloconf
res=$?
outval=$(($outval+$res))
if [ "$outval" -ne "0" ] ; then
    LogIt "Fstab and/or elilo modifications failed" 3
#    RestoreBackups
else
    LogIt "Modifications succeeded." 2
    LogIt "Copying over $old_fstab" 2
    cp -f $new_fstab $old_fstab
    outval=$(($outval+$?))
    LogIt "Copying over $old_eliloconf" 2
    cp -f $new_liloconf $old_eliloconf
    outval=$(($outval+$?))
    if [ "$outval" -ne "0" ] ; then
	LogIt "Modifications (copying) failed. Restoring from backups." 3
	RestoreBackups
    else
	LogIt "Fstab and elilo files modified ok." 2
    fi
    cd $where_they_live
    cd ..
fi

if [ "$outval" -ne "0" ] ; then
    LogIt "Error(s) occurred during elilo/fstab processing." 3
    LogIt "Restoring originals." 3
    RestoreBackups
else
    LogIt "/etc/fstab and /etc/elilo.conf were modified ok." 3
fi
LogIt "mr-stabelilo-me --- leaving"
exit $outval


