#!/bin/bash

. ${TOOLKIT}-functions.sh

#----------------------------------------------------------------
# /etc/fstab patching (adds nfs mounts)
#----------------------------------------------------------------

outfile=fstab.tmp
orig=/etc/fstab
cp $orig $outfile || exit $?

autostring="#kusa-cli-nfs "
shares=$(jtconf --list shares.nfs.) || :

# first, wipe out from existing file the entries added by kusa
#
# the tags are inserted as comments in the line before the
# entry, in the form:
#
#	#kusa TAG comment
#
exec 9<&0 <$outfile
exclude_next_line=false

while read line
do
	case $line in
	   ${autostring}*)
		exclude_next_line=true	# exclude next line (and this, too)
		continue
		;;
	esac
	if $exclude_next_line
	then
		echo "  - $line" >&2
	else
		echo "$line"
	fi
	exclude_next_line=false
done >$outfile.1.tmp
exec 0<&9 9<&-
mv $outfile.1.tmp $outfile || exit $?


# then re-adds entries
#
myself=$(uname -n)
myself_short=$(uname -n | sed -e 's/\..*//')
servers="servers.tmp"
cp /dev/null $servers

for share in $(jtconf --list shares.nfs. | sort)
do
	# silently ignore if undefined 'remote', assume that is a placeholder
	# entry for setting defaults
	#
	remote=$(jtconf $share.remote) || continue

	tag=$(	 jtconf $share.tag)		|| exit_missing_define $share.tag
	server=$(jtconf $share.server)		|| exit_missing_define $share.server
	mount=$( jtconf $share.mountpoint)	|| exit_missing_define $share.mountpoint
	opts=$(	 jtconf $share.opts_client)	|| exit_missing_define $share.opts_client

	include=$(jtconf $share.clients 2>/dev/null)	|| :
	exclude=$(jtconf $share.clients_no 2>/dev/null)	|| :
	
	# my own share?
	#
	[ $server = $myself -o $server = $myself_short ] && continue	# next entry

	must_include=true

	# check include list, if any
	#
	[ "X$include" != "X" ] && {

		must_include=false

		for name in $(echo $include | tr ',' ' ')
		do
			[ $myself = $name -o $myself_short = $name ] && {
				must_include=true
				break
			}
		done
	}

	# check exclude list, if any
	#
	$must_include && {
		for name in $(echo "$exclude" | tr ',' ' ')
		do
			[ $myself = $name -o $myself_short = $name ] && {
				must_include=false
				break
			}
		done
	}

	$must_include || {
		umount $mount 2>/dev/null || :
		sleep 3
		umount -l $mount 2>/dev/null || :
		continue	# next entry
	}

	string="$server:$remote	$mount	$opts 0 0"
	echo "  + $string"
	echo -e "$autostring $tag $mount" >>$outfile
	echo "$string" >>$outfile
	[ -d $mount ] || {
		create_dir $mount root:root || exit $?
	}

	echo "$server" >>$servers
done

# updates kuwatcher rulefile
#
kuw_file="auto-cli-nfs.tmp"

echo "# /etc/kuwatcher/conf.d/auto-cli-nfs
#
# checks for main NFS server availability for autogenerated entries
# note: do not edit, this file will be overwritten on next update!
#
# ::copy::
# ::maintainer::
#" >>$kuw_file

for server in $(sort -u $servers)
do
	tag=$(echo $server | sed -e 's/\./+/g')
	echo "[check.auto-cli-nfs-${tag}]"		>>$kuw_file
	echo "  savestate   true"			>>$kuw_file
	echo "  test        PING $server"		>>$kuw_file
	echo "  action_ok   MOUNT_NFS $server"		>>$kuw_file
	echo "  action_fail UMOUNT_NFS $server"		>>$kuw_file
	echo "" >>$kuw_file
done

exit 0
