#!/bin/bash
#
# ::do_not_edit::
# ::maintainer::
#
CMD=$(basename "$0")
CMDVER="1.1"
CMDSTR="$CMD v$CMDVER (2026-05-24)"

set -e -u

# (MAIN)

# removes actual symlinks
#
for name in * .*
do
	[ -L $name ] && rm $name
done
echo

# relink whole filesytems
#
sed -e 's/\s*#.*//' -e 's/^\s*$//g' -e '/^$/d' relink.cfg | while :
do
	read name uuid || break
	case $name in
	  _*)	;;	# OK
	  *)	continue ;;
	esac

	ln -s ../srv/dev-disk-by-uuid-$uuid $name
	printf "  FS: %-12s  %s\n" "$name" "$uuid"
done
echo

# relink directories under each filesystem
#
for fs in _*
do
	[ -L $fs ] || continue

	for dir in $(ls $fs/)
	do
		case $dir in
		  lost+found|homes) continue ;;
		esac

		linkname=$(grep "^rename\s\s*$dir\s" relink.cfg | sed -e 's/.* //')
		linkname=${linkname:-$dir}

		ln -s $fs/$dir $linkname
		printf "  %-16s: %-16s  " "$fs" "$linkname"

		nfiles=$(find $linkname/ -type f | wc -l)
		ndirs=$(find $linkname/ -type d | wc -l)
		printf "%6d files  %6d dirs  " $nfiles $ndirs

		if grep -q "^usage\s\s*$dir$" relink.cfg
		then
			usage=$(du -ms "$linkname"/)
			usage=${usage/	*/}
			printf "%8s Mb\n" "$usage"
		else
			echo
		fi
	done
	echo
done

exit 0
