#! /bin/sh
#
# fuse		Init script for Filesystem in Userspace
#
# Author:	Miklos Szeredi <miklos@szeredi.hu>
# chkconfig: - 50 50
# description: load fuse module

prog="FUSE"
MOUNTPOINT=/sys/fs/fuse/connections

# source function library
. /etc/init.d/functions

# Gracefully exit if the package has been removed.
which fusermount &>/dev/null || exit 0

error()
{
	echo "Error $1" >&2
	failure
	exit 1
}

start () {
	echo -n $"Starting $prog: "
	if grep -qw "^fuse" /proc/modules && grep -qw $MOUNTPOINT /proc/mounts ; then
		error "already $prog was loaded"
	fi

	if ! grep -qw fuse /proc/filesystems; then
		/sbin/modprobe fuse >/dev/null 2>&1 || error "loading fuse module"
	fi

	if grep -qw fusectl /proc/filesystems && \
	   ! grep -qw $MOUNTPOINT /proc/mounts; then
		mount -t fusectl fusectl $MOUNTPOINT >/dev/null 2>&1 || \
			error "mounting control filesystem"
	fi

	RETVAL=$?
	success
	echo
	return $RETVAL
}

stop () {
	echo -n $"Stopping $prog: "
	if ! grep -qw "^fuse" /proc/modules && ! grep -qw $MOUNTPOINT /proc/mounts ; then
		error "already $prog was unloaded"
	fi

	if grep -qw $MOUNTPOINT /proc/mounts; then
		umount $MOUNTPOINT >/dev/null 2>&1 || \
			error "unmounting control filesystem"
	fi

	if grep -qw "^fuse" /proc/modules; then
		rmmod fuse >/dev/null 2>&1 || error "unloading fuse module"
	fi

	RETVAL=$?
	success
	echo
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
   stop
	start
	;;
  *)
	echo $"Usage: $0 {start|stop|restart}"
	exit 1
	;;
esac

exit $RETVAL