#!/bin/sh
#
# $Id: hack-fstab 1548 2007-07-23 23:39:36Z bruno $
#
# Recreate /etc/fstab from first mountlist analysis 
# and then complement wirh old fstab content.

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


AddFancyParams() {
    local incoming device mountpoint format size original_fstab_line label uuid

    incoming=`echo "$1" | tr -s '\t' ' '`
    [ "$incoming" = "" ] && return
    device=`echo "$incoming"     | cut -d' ' -f1`
    mountpoint=`echo "$incoming" | cut -d' ' -f2`
    format=`echo "$incoming"     | cut -d' ' -f3`
    size=`echo "$incoming"       | cut -d' ' -f4`
    label=`echo "$incoming"       | cut -d' ' -f5`
    uuid=`echo "$incoming"       | cut -d' ' -f6`
    original_fstab_line=`grep " $mountpoint " $old_fstab | grep -v "#" | tr -s ' ' ' '`
	if [ "$format" = "ext2" ] || [ "$format" = "ext3" ] ; then
    	if [ "`echo "$original_fstab_line" | grep "LABEL="`" != "" ] ; then
	    	device="LABEL=$label"
		fi
    	if [ "`echo "$original_fstab_line" | grep "UUID="`" != "" ] ; then
	    	device="UUID=$uuid"
		fi
    fi

    echo -e -n "$device $mountpoint $format "

    if [ "$original_fstab_line" != "" ] ; then
		echo "$original_fstab_line" | cut -d' ' -f4- | tr -s ' ' ' '
    else
		echo -e "defaults 0 0"
    fi
}


ProcessFstab() {
    local incoming dev mountlist_entry blanklines new_i

    read incoming
    blanklines=0
    while [ "$blanklines" -lt "5" ] ; do
		if [ "$incoming" = "" ] ; then
	    	blanklines=$(($blanklines+1))
	    	read incoming
	    	continue
		fi
		incoming=`echo "$incoming" | tr -s '\t' ' '`
		if [ "`echo "$incoming" | grep -vE "LABEL=|UUID="`" ] ; then
	    	dev=`echo "$incoming" | cut -d' ' -f1`
	    	mountlist_entry=`grep "$dev " $old_mountlist`
	    	if [ "$mountlist_entry" = "" ] ; then
				echo "$incoming"
	    	fi
		fi
		read incoming
    done
}


HackIncomingIfLABELused() {
    local incoming col1 col2 col_rest orig out result

	# Keep LABEL or UUID if originally there in fstab
    result=""
    incoming=`echo "$1" | tr -s '\t' ' '`
    col1=`echo "$incoming" | cut -d' ' -f1`
    col2=`echo "$incoming" | cut -d' ' -f2`
    col_rest=`echo "$incoming" | cut -d' ' -f3- | tr -s ' ' ' '`
    orig="`grep " $col2 " $old_fstab | cut -d' ' -f1`"
    if [ "`echo "$orig" | grep -E "LABEL=|UUID="`" != "" ] ; then
		echo "orig = $orig" >> /dev/stderr
		echo -e "$orig $col2 $col_rest" | tr -s ' ' ' '
    fi
}


#HackIncomingIfLABELused "LABEL=/ / ext2 defaults 0,0,0"
#exit 0



ProcessMountlist() {
    local incoming outstr res spc

    read incoming
    while [ "$incoming" != "" ] ; do
		incoming=`echo "$incoming" | tr -s '\t' ' '`
		res=`HackIncomingIfLABELused "$incoming"`
		if [ ! "$res" ] ; then
	    	outstr=`AddFancyParams "$incoming"`
		else
	    	outstr=`AddFancyParams "$res"`
		fi
		spc="`echo "$outstr" | tr -s '\t' ' '`"
		if [ "$spc" != "" ] && [ "$spc" != " " ] && [ "`echo "$spc" | grep "raid raid"`" = "" ] ; then
	    	echo "$spc"
		fi
		read incoming
    done
}

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

LogIt "hack-fstab --- starting"

if [ "$#" -ne "4" ] ; then
    LogIt "hack-fstab <old mountlist> <old fstab> <new mountlist> <new fstab>" 1
    LogIt "NB: the new fstab file is outgoing; all other files are incoming." 1
    exit 1
fi

LogIt "hack-fstab '$1' '$2' '$3' '$4'"

old_mountlist=$1
old_fstab=$2
new_mountlist=$3
outfile=$4

> $outfile
LogIt "Processing mountlist"
ProcessMountlist < $new_mountlist >> $outfile
LogIt "Processing fstab"
ProcessFstab < $old_fstab >> $outfile
if [ ! -e "$outfile.old" ] ; then
    LogIt "Moving $outfile to $outfile.old" 1
    mv $outfile $outfile.old
else
    LogIt "$outfile.old exists already - assuming it _is_ the old one & using it" 1
fi
cat $outfile.old | tr -s ' ' ' ' \
| gawk '{printf "%-15s %-18s %-10s ",$1,$2,$3; for(i=4;i<=NF;i++) {printf "%s ",$i;};print "";};' \
| sort -u > $outfile

LogIt "Finished writing to outfile ($outfile)"
LogIt "hack-fstab --- leaving"
exit 0
