#!/bin/sh -e

##########################################################################
#   Script description:
#       Amend /etc/fstab from an fstab fragment
#
#   Arguments:
#       Filename of fstab fragment
#       
#   History:
#   Date        Name        Modification
#   2019-01-05  Jason Bacon Begin
##########################################################################

usage()
{
    printf "Usage: $0 fstab-fragment\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################

if [ $# != 1 ]; then
    usage
fi
fstab=$1

# Factor out from cluster-setup scripts
while read mount; do
    dir=`echo $mount | awk '{ print $2 }'`
    if awk '{ print $2 }' /etc/fstab | fgrep -qw $dir; then
	printf "$dir is already in fstab.\n"
    elif echo $mount | fgrep -q $(hostname -s); then
	printf "$mount is on this host, skipping...\n"
    else
	# FIXME: Make sure mount dir is empty
	
	printf "Adding $dir to fstab...\n"
	printf "$mount\n" >> /etc/fstab
	mkdir -p $dir
    fi
done < $fstab
