#!/bin/bash
# FVWM-Crystal helper script that check for mount changes
# Usage: Exec exec $[FVWM_SYSTEMDIR]/scripts/DesktopCheckMounts <delay [s]>

# debug
#env >> /tmp/examine_env.$$ 2>1&

# be sure no old instance are runninng
for i in $(pgrep -f DesktopCheckMounts) ; do
	[ "$i" != "$$" ] && kill $i
done
rm /tmp/crystal_desktopcheckmount_* 2>/dev/null

# the PID to kill if fvwm is interrupted
TMPFILE="/tmp/crystal_desktopcheckmount_$$"
touch ${TMPFILE}

cleanup() {
	rm ${TMPFILE}
	exit 0
}

trap cleanup INT QUIT TERM

# We only want the partitions the user have access to => use their path
MountedP=$(mount|cut -d" " -f3)
check_access() {
	AccTo=""
	for part in $1 ; do
		if [[ $(ls $part 2>/dev/null) ]] ; then
			AccTo="${AccTo} $part"
		fi
	done
	echo "${AccTo}"
}
AMountedP="$(check_access "$MountedP")"

# the main loop
while :
do
	sleep "$1"
	# fvwm is started by exec; be sure it is running
	pidof fvwm 1>/dev/null || pidof fvwm3 1>/dev/null || cleanup

	NMountedP=$(mount|cut -d" " -f3)
	NAMountedP="$(check_access "$NMountedP")"
	if [[ "$NAMountedP" != "$AMountedP" ]]; then
		AMountedP="${NAMountedP}"
		if [[ "${FVWM_IS_FVWM3}" == "1" ]]; then
			FvwmPrompt LoadDesktopIcons
		else
			FvwmCommand LoadDesktopIcons
		fi
	fi
done
