#!/bin/bash
#
# /etc/init.d/fuse
#
# Copyright (C) Vladimir V. Kolpakov 2006-2009
# All rights reserved.
#
### BEGIN INIT INFO
# Provides:         fuse
# Required-Start:   $local_fs $remote_fs $network
# Required-Stop:
# Default-Start:    3 5
# Default-Stop:     0 1 2 6
# Short-Description:    FUSE filesystems
# Description:      Mount FUSE filesystems
### END INIT INFO

. /etc/rc.status

fuse=
while read  where what type options rest  ; do
    case "$where" in
	\#*|"") ;;
	*) case "$options" in
	    *noauto*) ;;
	    *)  if test "$type" = "fuse" ; then
		    fuse=yes
		    break
		fi ;;
	    esac
    esac
done < /etc/fstab

rc_reset
case "$1" in
    start|reload)
	echo -n "Mounting Userland (FUSE) File Systems"
	if test -n "$fuse" ; then
	  mount -at fuse
	  rc_status -v
	else
	  rc_status -u
	fi
	;;
    stop)
	echo -n "Unmounting Userland (FUSE) File Systems"
	if test -n "$fuse" ; then
	  umount -at fuse
	  rc_status -v
	else
	  rc_status -u
	fi
	;;
    restart|force-reload)
        ## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start
	rc_status
	;;
    status)
	echo -n "Checking for mounted FUSE filesystems (from /etc/fstab):"
	if test -n "$fuse" ; then
	  while read  where what type options rest  ; do
	    case "$where" in
	      \#*|"") ;;
	      *) case "$options" in
		   *noauto*) ;;
		   *) if test "$type" = "fuse" ; then
			grep -q "$where $what fuse" /proc/mounts || rc_failed 3
		      fi ;;
		 esac
	    esac
          done < /etc/fstab
        else
	  rc_failed 3
	fi
        rc_status -v
	;;
    try-restart|condrestart)
	$0 status
	if test $? = 0; then
	    $0 restart
	else
	    rc_reset
	fi
	rc_status
	;;
    *)
	echo "Usage: $0 {start|stop|status|reload|force-reload|restart|try-restart|condrestart}"
	exit 1
esac
rc_exit

