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

. /etc/rc.status

collect_funifs () {
  fstab=${1:-/etc/fstab}
  funifs=()
  while read  where what type options rest  ; do
    [[ $where == funifs#* && $type = fuse && $options != *noauto* ]] || continue
    funifs+=( $where )
  done < $fstab
  test -n "$funifs"
}

rc_reset
case "$1" in
    start|reload)
	echo -n "Mounting Funifs userland file systems"
	collect_funifs || rc_failed 3
	for n in ${funifs[@]} ; do
	  mount $n || { rc_failed ; break ; }
	done
	rc_status -v
	;;
    stop)
	echo -n "Unmounting Funifs userland file systems"
	collect_funifs /proc/mounts || rc_failed 3
	for n in ${funifs[@]} ; do
	  umount $n || rc_failed
	done
	rc_status -v
	;;
    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 Funifs nodes:"
	collect_funifs || collect_funifs /proc/mounts || rc_failed 3
	for n in ${funifs[@]} ; do
	  grep -q "$n " /proc/mounts || { echo -n " $n"; rc_failed ; continue ; }
	done
        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

