#!/bin/sh
#
# $Id: stabraw-me 2462 2009-10-20 14:07:00Z bruno $
#
#####################################################################


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

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_fstab ; do
	LogIt "Backing up $i"
        [ -e "$i" ] && [ ! -e "$i.prehack" ] && cp -f $i $i.prehack
    done
}


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


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

if [ "$#" -ne "1" ] ; then
    LogIt "stabraw-me </dev/bootdrive>"
    exit 1
fi

bootdrive=$1

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

MakeBackups

new_fstab=/tmp/fstab.new
[ ! -e "$new_fstab" ] && new_fstab=/tmp/fstab

LogIt "Modifying fstab..." 2
outval=0
hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab
res=$?
if [ "$res" -ne "0" ] ; then
    echo "Warning - hack-fstab failed"
    outval=$(($outval+$res))
fi
   
if [ "$outval" -ne "0" ] ; then
    LogIt "Fstab modifications failed" 3
    RestoreBackups
else
    LogIt "Modifications succeeded." 2
    LogIt "Copying over $old_fstab" 2
    cp -f $new_fstab $old_fstab
    outval=$(($outval+$?))
    if [ "$outval" -ne "0" ] ; then
	LogIt "Modifications (copying) failed. Restoring from backups." 3
	RestoreBackups
    else
	LogIt "Fstab modified ok." 2
    fi
    cd $where_they_live
    cd ..
    raw-MR $bootdrive
    mbr_res=$?
    if [ "$mbr_res" -ne "0" ] ; then
	LogIt "MBR failed to be installed." 3
    else
        LogIt "MBR installed ok." 2
    fi
fi

if [ "$outval" -ne "0" ] ; then
    LogIt "Error(s) occurred during mbr/fstab processing.              "
    LogIt "Restoring originals.                                         "
    RestoreBackups
elif [ "$mbr_res" -ne "0" ] ; then
    LogIt "Fstab was modified OK but mbr failed to be installed.                 "
    outval=$(($outval+1))
else
    LogIt "/etc/fstab was modified ok; mbr was installed ok, too.       "
fi
LogIt "stabraw-me --- leaving"
echo -en "\n\n\n"
exit $outval





