#!/bin/sh
#
# $Id: label-partitions-as-necessary 3330 2014-12-23 06:49:06Z bruno $
#
############################################


read_partition_line() {
    local label mountpt command format device d m

	device=`echo "$1" | awk '{print $1}'`
	d=`echo "$device" | cut -c1`
	format=`echo "$1" | awk '{print $3}'`
	label=`echo "$device" | cut -s -d'=' -f2`
	if [ "$d" = "/" ] && [ "$label" = "" ]; then
		m=`echo "$1" | awk '{print $2}'`
		# We force UUID even if nothing in fstab as some new distro like RHEL7 rely on it
		[ "$format" = "reiserfs" ] && opttun="-u" || opttun="-U"
		label=`awk '{print $2,$5}' $mountlist | grep -E "^$m " | awk '{print $2}'`
	else
		if [ "`echo "$1" | grep -E 'LABEL='`" != "" ] ; then
			[ "$format" = "reiserfs" ] && opttun="-l" || opttun="-L"
		elif [ "`echo "$1" | grep -E 'UUID='`" != "" ] ; then
			[ "$format" = "reiserfs" ] && opttun="-u" || opttun="-U"
		else
			LogIt "Nothing to do on $1"
			return
		fi
	fi
	mountpt=`awk '{print $1,$5}' $mountlist | grep " $label$" | awk '{print $1}'`

	if [ ! "$mountpt" ] ; then
		LogIt "Not labeling anything as ($label) because ($mountpt) is not a mountpoint"
	elif [ ! "$label" ] ; then
		LogIt "Not labeling ($mountpt) as anything because ($label) is not a label/uuid"
	else
		if [ "$format" = "ext2" ] || [ "$format" = "ext3" ] || [ "$format" = "ext4" ]; then
			if [ "$format" = "ext4" ] && [ -x "/sbin/tune4fs" ]; then
				command="/sbin/tune4fs $opttun $label $mountpt"
			else
				command="tune2fs $opttun $label $mountpt"
			fi
			LogIt "Running $command"
			$command
		elif [ "$format" = "reiserfs" ]; then
			command="reiserfstune $opttun $label $mountpt"
			LogIt "Running $command"
			$command
		elif [ "$format" = "btrfs" ]; then
			# Also see https://btrfs.wiki.kernel.org/index.php/Project_ideas#Filesystem_UUID_change_-_off-line
			command="btrfs filesystem label $mountpt $label"
			LogIt "Running $command"
			$command
		elif [ "$format" = "xfs" ]; then
			command="xfs_admin $opttun $label $mountpt"
			LogIt "Running $command"
			$command
		elif [ "$format" = "swap" ] ; then
			if [ "$opttun" = "-U" ]; then
				LogIt "Creating uuid $label on swap partition $mountpt"
				if [ -x "/sbin/swaplabel" ]; then
					/sbin/swaplabel $opttun $label $mountpt
				else
					echo -n "$label" | perl -ne 's/-//g;chomp;print pack "H*",$_' | dd conv=notrunc "of=$mountpt" obs=1 seek=1036
				fi
			else
				if [ -x "/sbin/swaplabel" ]; then
					command="/sbin/swaplabel $opttun $label $mountpt"
				else
					command="mkswap $opttun $label $mountpt"
				fi
				LogIt "Running $command"
				$command
			fi
		else
			LogIt "I am NOT going to run tune2|4fs/reiserfstune: the partition is format '$format', which doesn't like tune2|4fs/reiserfstune anyway"
		fi
	fi
}


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

LogIt "Identifying your drives with FS tune tool"
if [ "$#" -ne "1" ] ; then
    LogIt "label-partitions-as-necessary /tmp/mountlist.txt < /tmp/fstab.new" 1
    exit 1
fi
mountlist=$1
noof_blank_lines=0
read line
# We end after 50 empty lines, which hopefully means we reach the end of the file
while [ "$noof_blank_lines" -le "50" ] ; do
    if [ "$line" = "" ] ; then
		noof_blank_lines=$(($noof_blank_lines+1))
    else
		noof_blank_lines=0
		read_partition_line "$line"
    fi
    read line
done
LogIt "Labeling complete."
exit 0
