#!/bin/bash
#
# [Type]
# mount_added_command
#
# [Description]
# This script automatically imports stashes on inserted devices.
# Note that MOUNT_DIR refers to the directory of the mounted device in the
# "mount_*_command" commands.

case "$SCRIPT_TYPE" in

	# make sure the script only runs in the right slot
	mount_added_command)
	
		# contains the mounted drive a valid stash?
		if gnome-encfs-manager --checkstash "$MOUNT_DIR" >/dev/null; then
		
			# Is the stash already registered in GEncfsM?
			if ! gnome-encfs-manager is_configured "$MOUNT_DIR" >/dev/null; then
			
				# Import the stash
				gnome-encfs-manager import_stash "$MOUNT_DIR" ":/`basename "$MOUNT_DIR"`"
				
				# Is it now in GEncfsM?
				if gnome-encfs-manager is_configured "$MOUNT_DIR" >/dev/null; then
				
				    # adjust the configuration
				    gnome-encfs-manager set use_keyring=true "$MOUNT_DIR"
				    gnome-encfs-manager set mount_on_startup=true "$MOUNT_DIR"
				    gnome-encfs-manager set mount_when_available=true "$MOUNT_DIR"

                    # mount it
                    gnome-encfs-manager mount "$MOUNT_DIR"
				fi
			fi
		fi
		;;
esac
