# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler FilesystemsOptions
AddConfigHelp "Unmount <mount point or device> [...]" "If you have network shares or external devices that should be unmounted before suspending, list them here."
AddConfigHelp "Mount <mount point or device> [...]" "If you have network shares or external devices that should be mounted after resuming, list them here."

FilesystemsUnmount() {
    local mnt
    local ret
    local needsleep
    [ -z "$FS_UNMOUNTS" ] && return 0

    needsleep=0

    # Send TERM to processes first
    if [ x"$KILL_PROGRAMS" = "x1" ] ; then
	for mnt in $FS_UNMOUNTS ; do
	    if ! awk '($2 == "'$mnt'"){exit 1}' /proc/mounts ; then
		vecho 1 "Sending SIGTERM to processes using $mnt..."
		fuser -s -k -15 $mnt
		fuser -s $mnt && needsleep=1
	    fi
	done
    fi

    [ "$needsleep" -eq 1 ] && sleep 1

    # Send KILL to processes and unmount
    ret=0
    for mnt in $FS_UNMOUNTS ; do
	if ! awk '($2 == "'$mnt'"){exit 1}' /proc/mounts ; then
	    if [ x"$KILL_PROGRAMS" = "x1" ] ; then
		vecho 1 "Sending SIGKILL to processes using $mnt..."
		fuser -s -k -9 $mnt
	    fi
	    vecho 1 "Unmounting $mnt ..."
	    umount $mnt || ret=1
	fi
    done
    return $ret
}

FilesystemsMount() {
    local mnt
    local ret
    [ -z "$FS_MOUNTS" ] && return 0

    ret=0
    for mnt in $FS_MOUNTS ; do
	vecho 1 "Mounting $mnt ..."
	mount $mnt || ret=1
    done
    return $ret
}

FilesystemsOptions() {
    case $1 in
	unmount)
	    [ -z "$FS_UNMOUNTS" ] && AddSuspendHook 50 FilesystemsUnmount
	    shift
	    FS_UNMOUNTS="$FS_UNMOUNTS $@"
	    ;;
	mount)
	    [ -z "$FS_MOUNTS" ] && AddResumeHook 50 FilesystemsMount
	    shift
	    FS_MOUNTS="$FS_MOUNTS $@"
	    ;;
	*)
	    return 1
    esac
    return 0
}

# $Id: filesystems 423 2004-08-20 04:05:49Z dagobah $
