#!/bin/bash
#
# [Type]
# post_unmount_command
#
# [Description]
# This script kills all processes accessing the stash
# and dispatches another unmount-request when done

# Is the stash still mounted ?
if gnome-encfs-manager is_mounted "$MOUNT_DIR" >/dev/null; then

	# Set the message we want to display in the manager while we are busy
	# and save the message ID so we can properly remove it later
	busy_id=$(gnome-encfs-manager indicate "(kill-unmount) `basename "$MOUNT_DIR"`: Killing processes...")
	
	# Search for processes and kill them
	pids=$(lsof | grep "$MOUNT_DIR" | awk '{print $2}' | sort -u)
	for pid in $pids; do
	    kill -9 $pid
	done
	
	# remove the message from the message stack
	gnome-encfs-manager indicate $busy_id
	
	# Call unmount again, it should now succeed
	gnome-encfs-manager unmount "$MOUNT_DIR"
fi

