#!/bin/bash
#
# Script for fixing the CCISS vs. HPSA driver problem of renamed device names
#
# Pitfalls:
# - The script mangles white spaces in the file
#
# Author: Jesper Dangaard Brouer <hawk@comx.dk>
# SVN info: $Revision: 12876 $

DIR=/etc
if [ "$1" == "debug" ]; then
    DIR=$PWD
    e=echo
fi
INFILE=${DIR}/fstab


function usage() {
    echo -e "This script can be used for converting a /dev based /etc/fstab to"
    echo -e "an udev LABEL based one.  This script creates the same LABEL name"
    echo -e "as the mount point name.\n"
}
usage

function checkcmd() {
    # Check that a given command is available
    local cmd=$1;
    which $cmd > /dev/null 2>&1
    if [ "$?" -ne 0 ]; then
	echo "ERROR: This script need the command: $cmd"
	exit 2
    fi
}
checkcmd e2label
checkcmd mkswap
checkcmd date

read -p "Convert your /etc/fstab from /dev names to udev LABEL's (y/N)?"
if [ "$REPLY" != "y" ]; then
    echo "aborting"
    exit 0
fi

timestamp=`date +%Y%m%dT%H%M%S`
ORIG=${INFILE}.orig-${timestamp}
OUT=${INFILE}.new-${timestamp}
#:> $OUT # Truncate

cp $INFILE $ORIG

function output() {
    local dev=$1; local mount=$2; local type=$3;
    local options=$4; local rest=$5;
    echo -e "${dev}\t\t${mount}\t${type}\t${options}\t${rest}" >> $OUT
}

# Need a simple counter, if several swap partions exists
let swapcnt=0

# Process each line in fstab
while read dev mount type options rest; do

    case "$dev" in
	# Pass comments through (but ignore special whitespaces)
	"#")
	    echo $dev $mount $type $options $rest >> $OUT
	    ;;

	# Handle /dev entries which needs convertion
	"/dev/"*)
	    # Simply create the same LABEL name as the mount point
	    label="$mount"

	    # Different file system type's requires different commands
	    # for creating a "LABEL" on a device.
	    case "$type" in
		"ext2"|"ext3")  # e2label
		    echo " - Create label:[$label] on device: $dev"
		    $e e2label $dev $label
		    LABEL="LABEL=$label"
		    output $LABEL $mount $type $options $rest
		    ;;
		"swap")  # mkswap -L
		    let swapcnt++
		    label="swap${swapcnt}"
		    echo " - Create label:[$label] on device: $dev"
		    $e swapoff $dev
		    $e mkswap -L $label $dev
		    $e swapon -L $label
		    LABEL="LABEL=$label"
		    output $LABEL $mount $type $options $rest
		    ;;
		*)
		    echo -n " INFO: Don't know filesystem type: $type"
		    echo " - Copy line: \"$dev $mount $type $options $rest\""
		    output $dev $mount $type $options $rest
		    ;;
	    esac
	    ;;
	"")
	    echo "" >> $OUT
	    ;;
	*)
	    echo -n " INFO: Not converting line"
	    echo " - Copy line: \"$dev $mount $type $options $rest\""
	    output $dev $mount $type $options $rest
	    ;;
    esac

done < $INFILE

echo -e "\nConversion finished"
echo -e " - A backup of the original fstab is named: $ORIG"
echo -e " - The new modified file is named: $OUT"

#echo "  diff -u $ORIG $OUT"
read -p "Show diff between the files? (Y/n)"
[ "$REPLY" != "n" ] && diff -u $ORIG $OUT

read -p "Overwrite /etc/fstab with the new file? (y/N)"
if [ "$REPLY" == "y" ]; then
    mv $OUT $INFILE
else
    echo "NOT OVERWRITING /etc/fstab - you need to perform manual copy"
fi
