#! /bin/bash
### BEGIN INIT INFO
# Provides:          umountfs
# Required-Start:
# Required-Stop:     umountroot
# Default-Start:
# Default-Stop:      0 6
# Short-Description: Turn off swap and unmount all local file systems.
# Description:
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/init/vars.sh

. /lib/lsb/init-functions

umask 022

do_stop () {
    LIVE_DIR="/live";
	exec 9<&0 </proc/mounts

	PROTECTED_MOUNTS="$(sed -n '0,/^\/[^ ]* \/ /p' /proc/mounts)"
	WEAK_MTPTS="" # be gentle, don't use force
	REG_MTPTS=""
	TMPFS_MTPTS=""
    READ_ONLY_MTPTS=""

	while read -r DEV MTPT FSTYPE REST
	do
		echo "$PROTECTED_MOUNTS" | grep -qs "^$DEV $MTPT " && continue

        # Skip bind mounted top level dirs  -jbb
        case "$DEV" in 
        $LIVE_DIR/aufs)
            LIVE_BIND_MTPTS="$MTPT $LIVE_BIND_MTPTS"
            continue;
            ;;
        esac

		case "$MTPT" in
		  /|/proc|/dev|/.dev|/dev/pts|/dev/shm|/dev/.static/dev|/proc/*|/sys|/sys/*|/lib/init/rw)
			continue
			;;

        # Completely skip ram portion of aufs
        $LIVE_DIR/aufs-ram)
            continue;
            ;;
        # Skip all liveCD/USB mount points
        $LIVE_DIR/*)
            LIVE_OTHER_MTPTS="$MTPT $LIVE_OTHER_MTPTS"
            continue;
            ;;

		  /var/run)
			if [ yes = "$RAMRUN" ] ; then
				continue
			fi
			;;
		  /var/lock)
			if [ yes = "$RAMLOCK" ] ; then
				continue
			fi
			;;
		esac
		case "$FSTYPE" in 
		  proc|procfs|linprocfs|sysfs|usbfs|usbdevfs|devpts)
			continue
			;;
		  tmpfs)
			TMPFS_MTPTS="$MTPT $TMPFS_MTPTS"
			;;
		  *)
			if echo "$PROTECTED_MOUNTS" | grep -qs "^$DEV "; then
				WEAK_MTPTS="$MTPT $WEAK_MTPTS"
			else
				REG_MTPTS="$MTPT $REG_MTPTS"
			fi
			;;
		esac
	done

	exec 0<&9 9<&-
	
	#
	# Make sure tmpfs file systems are umounted before turning off
	# swap, to avoid running out of memory if the tmpfs filesystems
	# use a lot of space.
	#

	if [ "$TMPFS_MTPTS" ]
	then

		if [ "$VERBOSE" = no ]
		then
			log_action_begin_msg "Unmounting temporary filesystems"
			fstab-decode umount $TMPFS_MTPTS
			log_action_end_msg $?
		else
			log_daemon_msg "Will now unmount temporary filesystems"
			fstab-decode umount -v $TMPFS_MTPTS
			log_end_msg $?
		fi
	fi

	#
	# Deactivate swap
	#
	if [ "$VERBOSE" = no ]
	then
		log_action_begin_msg "Deactivating swap"
		swapoff -a >/dev/null
		log_action_end_msg $?
	else
		log_daemon_msg "Will now deactivate swap"
		swapoff -a -v
		log_end_msg $?
	fi

	#
	# Unmount local filesystems
	#
    echo "Starting to umount local file systems"
	if [ "$WEAK_MTPTS" ]; then
        echo "UMOUNTING WEAK: $WEAK_MTPTS"
		# Do not use -f umount option for WEAK_MTPTS
		if [ "$VERBOSE" = no ]
		then
			log_action_begin_msg "Unmounting weak filesystems"
			fstab-decode umount -r -d $WEAK_MTPTS
			log_action_end_msg $?
		else
			log_daemon_msg "Will now unmount weak filesystems"
			fstab-decode umount -v -r -d $WEAK_MTPTS
			log_end_msg $?
		fi
	fi
	if [ "$REG_MTPTS" ]
	then
        echo "UMOUNTING REG: $REG_MTPTS"
		if [ "$VERBOSE" = no ]
		then
			log_action_begin_msg "Unmounting local filesystems"
			fstab-decode umount -f -r -d $REG_MTPTS
			log_action_end_msg $?
		else
			log_daemon_msg "Will now unmount local filesystems"
			fstab-decode umount -f -v -r -d $REG_MTPTS
			log_end_msg $?
		fi
	fi

	if [ "$LIVE_BIND_MTPTS" ]
	then
	    echo "Mounting LIVE bind mountpoints read-only"
        for MTPT in $LIVE_BIND_MTPTS; do
            mount -o remount,ro $MTPT
        done
	fi

	if [ "$LIVE_OTHER_MTPTS" ]
	then
	    echo "Mounting remaining LIVE filesystems read-only"
	    for MTPT in $LIVE_OTHER_MTPTS; do
            mount -o remount,ro $MTPT
        done
	fi

    echo "Done umounting local file systems"
}

case "$1" in
  start)
	# No-op
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop)
	do_stop
	;;
  *)
	echo "Usage: $0 start|stop" >&2
	exit 3
	;;
esac

:
