#! /bin/sh

case "$1" in
start)
	constPartFs -f /const &

	# Since we are launching constPartFs in the background, we do not really
	# know when the const partition is mounted.  On a fast system, there is a
	# race condition between the mounting of /const and the tee-supplicant.
	# For that reason we have to make sure that the /const partition is really
	# mounted before exiting this script.
	# We still have a maximum timeout of 5 seconds so the system does not get
	# fully stuck when something goes really wrong.
	maxwait=5
	while ! grep -qs '/const' /proc/mounts; do
		sleep 1

		maxwait=$((maxwait - 1))
		if [ $maxwait -le 0 ]; then
			echo "WARN: waiting for /const took too long, aborting"
			break
		fi
	done
	;;

stop)
	fusermount3 -u /const
	;;

*)
	echo "Usage: const-part-fs.sh {start|stop}"
	exit 1
esac

exit 0
