--- zenencfs-0.2.2/zenencfs	2016-08-27 12:24:26.000000000 +0200
+++ zenencfs-0.9/zenencfs	2016-09-10 14:39:55.359110584 +0200
@@ -28,54 +28,67 @@
 export TEXTDOMAINDIR="/usr/share/locale"
 . gettext.sh
 
-
-if [ ! "$@" ]; then
-	$dialog --title "$(eval_gettext 'Bad folder name')" --msgbox "$(eval_gettext 'You must provide an ENCFS Folder')" 8 65 
-	exit 1
-fi
-
-arg="$(dirname "$@"/.)"
+# We deal with any situation : normal folder, folder with encfs extention, no folder, relative or absolute path ...
+[ "$@" ] && arg="$(dirname "$@"/.)" || arg="$HOME/Private Folder"
+[ ! -e "$arg" ] && mkdir -p "$arg"
+[ ! -d "$arg" ] && $dialog --title "$(eval_gettext 'Not a folder')" --msgbox "$(eval_gettext 'You must provide a folder to process')" 8 65 &&	exit 1
 
 if [ "$(echo "$arg" | grep "\.encfs$")" ]; then
-	# $dialog --title "$(eval_gettext 'Bad folder name')" --msgbox "$(eval_gettext 'ENCFS Folder must have ".encfs" suffix')" 8 65 
 	foldernoext="$(basename -s .encfs "$arg")"
 	folderext="$(basename "$arg")"
-	pathtofolder="$(dirname "$arg")"
+	[ -d "$folderext" ] && pathtofolder="$(pwd)" || pathtofolder="$(dirname "$arg")"
 else
 	foldernoext="$(basename "$arg")"
 	folderext="$(basename "$arg").encfs"
-	pathtofolder="$(dirname "$arg")"
+	[ -d "$foldernoext" ] && pathtofolder="$(pwd)" || pathtofolder="$(dirname "$arg")"
 fi
 
+cd "$pathtofolder" 
 
+# case 1 : folder already opened : we may close it
 if [ "$(/bin/mount | grep "^encfs.*$pathtofolder/$foldernoext.*" )" ] ; then
 	$dialog --title "$(eval_gettext 'Closing')" --ok-label "$(eval_gettext 'OK')" --cancel-label "$(eval_gettext 'Cancel')" --yesno "$(eval_gettext 'Closing ENCFS Folder') \"$foldernoext\"" 8 65 
 	[ $? != 0 ] && exit 1
 	mountpoint="$(/bin/mount | grep "^encfs.*$pathtofolder/$foldernoext.*" | sed -n 's|^encfs on \(.*\) type .*|\1|p')"
-	fusermount -u "$mountpoint" && rmdir "$mountpoint"
+	ERROR="$(fusermount -u "$mountpoint" 2>&1)"
+	[ "$ERROR" ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$ERROR" 8 65 && exit 1
+	rmdir "$mountpoint"
+
 else
-	if [ -d "$pathtofolder/$folderext" ]; then
+	if [ -d "$folderext" ]; then
+	# case 2 : folder not opened, but already encrypted : we may open it
 		password="$(${securedialog} "$(eval_gettext 'Opening ENCFS Folder') \"$folderext\"" 12 80)"
 		[ $? != 0 ] && exit 1
-		mkdir -p "$pathtofolder/$foldernoext"
+		[ ! "$password" ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$(eval_gettext 'Empty password')" 8 65 && exit 1
+		mkdir -p "$foldernoext"
 		echo -n $password |  encfs --standard --stdinpass "$pathtofolder/$folderext" "$pathtofolder/$foldernoext"
+		[ $? != 0 ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$(eval_gettext 'Error opening folder')" 8 65 && rmdir "$foldernoext"
 	else
+	# case 3 : folder not opened, and not already encrypted : we may create it
 		password="$(${securedialog} "$(eval_gettext 'Creating ENCFS Folder') \"$folderext\"" 12 80)"
 		[ $? != 0 ] && exit 1
-		mkdir -p "$pathtofolder/$folderext"
-		mkdir -p "$pathtofolder/$foldernoext.TMP"
+		[ ! "$password" ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$(eval_gettext 'Empty password')" 8 65 && exit 1
+		mkdir -p "$folderext"
+		mkdir -p "$foldernoext.TMP"
 		echo -n $password |  encfs --standard --stdinpass "$pathtofolder/$folderext" "$pathtofolder/$foldernoext.TMP"
-		if [ -d "$pathtofolder/$foldernoext" ]; then
-			cp -rf "$pathtofolder/$foldernoext"/* "$pathtofolder/$foldernoext.TMP"
-			mountpoint="$(/bin/mount | grep "^encfs.*$pathtofolder/$foldernoext.*" | sed -n 's|^encfs on \(.*\) type .*|\1|p')"
-			fusermount -u "$mountpoint" && rmdir "$mountpoint"
+		[ $? != 0 ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$(eval_gettext 'Error creating folder')" 8 65 && rmdir "$foldernoext.TMP" && rmdir "$folderext" && exit 1
+		if [ -e "$foldernoext"/* ]; then
+			# $dialog --title "$(eval_gettext 'Moving data')" --timeout 5 --msgbox "$(eval_gettext 'Moving data to ENCFS Folder,this may take a while depending on the size of data') \"$folderext\"" 8 65 &
+			cp -rf "$foldernoext"/* "$foldernoext.TMP"
 			$dialog --title "$(eval_gettext 'Closing')" --msgbox "$(eval_gettext 'Done moving data to ENCFS Folder') \"$folderext\"" 8 65 
 		fi
+		mountpoint="$(/bin/mount | grep "^encfs.*$pathtofolder/$foldernoext.*" | sed -n 's|^encfs on \(.*\) type .*|\1|p')"
+		ERROR="$(fusermount -u "$mountpoint" 2>&1)"
+		[ "$ERROR" ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$ERROR" 8 65 && exit 1
+		rmdir "$mountpoint"
+		rm -rf "$foldernoext"
 	fi
 fi
 
 exit 0
 
 
-
+# too slow on big folders...
+# [ "$(/usr/bin/file "$folderext" | grep "POSIX tar archive" )" ] && tar -xf "$folderext"
+# tar -cf "$folderext.tar" "$folderext" ; rm -rf "$folderext" ; mv "$folderext.tar" "$folderext" 
 
