#!/bin/sh
#
# $FreeBSD: ports/sysutils/fusefs-kmod/files/fusefs.in,v 1.6 2008/02/16 18:05:38 alepulver Exp $
#
# PROVIDE: fusefs
# REQUIRE: sysctl
# BEFORE: mountlate
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# fusefs_enable (bool):		Set to NO by default.
#				Set it to YES to enable fusefs.
#

. /etc/rc.subr

kmod="/usr/local/modules/fuse.ko"
name="fusefs"
rcvar=${name}_enable

start_cmd="fusefs_start"
stop_cmd="fusefs_stop"

fusefs_start()
{
	if kldstat | grep -q fuse\\.ko; then
		echo "${name} is already running."
		return 0
	fi
	echo "Starting ${name}."
	kldload $kmod
}

fusefs_stop()
{
	if ! kldstat | grep -q fuse\\.ko; then
		echo "${name} is not running."
		return 1
	fi
	echo "Stopping ${name}."
# Unmount FUSE filesystems in reverse order (in case they are nested) to
# allow recent FUSE implementation to synchronize disks before shutdown.
	mount | sed -e '1!G;h;$!d' | while read dev d1 mountpoint d2; do
		case "$dev" in
		/dev/fuse[0-9]*)
			echo "fusefs: unmounting ${mountpoint}."
			umount $mountpoint
			;;
		esac
	done
	kldunload $kmod
}
load_rc_config $name

: ${fusefs_enable="NO"}

run_rc_command "$1"
