#! /bin/sh
### BEGIN INIT INFO
# Provides:          tmfs
# Required-Start:    
# Should-Start:      udev
# Required-Stop:     
# Default-Start:     S
# Default-Stop:
# Short-Description: Start and stop tmfs.
# Description:       Load the tmfs module and mount the tmfs control
#	filesystem.
### END INIT INFO

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MOUNTPOINT=/sys/fs/tmfs/connections

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

case "$1" in
    start|restart|force-reload)
	if ! grep -qw tmfs /proc/filesystems; then
		echo -n "Loading tmfs module"
		if ! modprobe tmfs >/dev/null 2>&1; then
			echo " failed!"
			exit 1
		else
			echo "."
		fi
	else
		echo "Fuse filesystem already available."
	fi
	if grep -qw tmfsctl /proc/filesystems && \
	   ! grep -qw $MOUNTPOINT /proc/mounts; then
		echo -n "Mounting tmfs control filesystem"
		if ! mount -t tmfsctl tmfsctl $MOUNTPOINT >/dev/null 2>&1; then
			echo " failed!"
			exit 1
		else
			echo "."
		fi
	else
		echo "Fuse control filesystem already available."
	fi
	;;
    stop)
	if ! grep -qw tmfs /proc/filesystems; then
		echo "Fuse filesystem not loaded."
		exit 7
	fi
	if grep -qw $MOUNTPOINT /proc/mounts; then
		echo -n "Unmounting tmfs control filesystem"
		if ! umount $MOUNTPOINT >/dev/null 2>&1; then
			echo " failed!"
		else
			echo "."
		fi
	else
		echo "Fuse control filesystem not mounted."
	fi
	if grep -qw "^tmfs" /proc/modules; then
		echo -n "Unloading tmfs module"
		if ! rmmod tmfs >/dev/null 2>&1; then
			echo " failed!"
		else
			echo "."
		fi
	else
		echo "Fuse module not loaded."
	fi
	;;
    status)
	echo -n "Checking tmfs filesystem"
	if ! grep -qw tmfs /proc/filesystems; then
		echo " not available."
		exit 3
	else
		echo " ok."
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|restart|force-reload|status}"
	exit 1
	;;
esac

exit 0
