#!/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
		mount=$(echo "$line" | awk '{print $2;}')
		[ -d $mount ] && {
			mountpoint $mount >/dev/null || {
				chattr -i $mount || :
				rmdir $mount
			}
		}
	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

echo

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 || {
		mountpoint $mount >/dev/null 2>&1 && {
			echo -n "  umount $mount ... "
			umount $mount 2>/dev/null || {
				echo "retry ... "
				sleep 3
				umount -l $mount || :
			}
			if mountpoint $mount >/dev/null 2>&1
			then
				echo "  warning: $mount still mounted"
			else
				echo "ok"
			fi
		}
		continue	# next entry
	}

	printf "  + %-16s %-16s " "$server:$remote" "$mount"

	string="$server:$remote	$mount	$opts 0 0"
	echo -e "$autostring $tag $mount" >>$outfile
	echo "$string" >>$outfile
	[ -d $mount ] || {
		out=$(create_dir $mount root:root 2>&1) || exit $?
		echo -n "dir created"
	}
	# 2026-01-20
	# - make mountpoint dir immutable
	#   none can write inside if not correctly mounted
	#   this can be done only if remote is not already mounted
	#
	if mountpoint $mount >/dev/null
	then
		echo "(already mounted)"
	else
		if out=$(chattr +i $mount 2>&1)
		then
			echo " immutable"
		else
			echo " not immuable: $out"
		fi
	fi

	echo "$server" >>$servers
done

echo

# 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
