#!/bin/bash

# Copyright Jean-Philippe Guillemin <h1p8r10n@gmail.com>. This program is free software;
# you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version. Please take a look at http://www.gnu.org/copyleft/gpl.htm

# zenencfs is a simple GTK/NCURSES frontend to encfs


# Take a look at "Xdialog" and use it instead of "dialog" in case X is running
if [[ "$DISPLAY" && "$(which Xdialog 2>&1 | grep -v "which: no")" ]]; then
	dialog="Xdialog --wrap --left --icon security"
	shortttl='3000'
	longttl='8000'
	xflag='yes'
	securedialog='Xdialog --title Password --stdout --wrap --ignore-eof --password --icon security --inputbox'
else
	dialog="dialog"
	shortttl=''
	longttl=''
	xflag='no'
	securedialog='dialog --title Password --stdout --insecure --fixed-font --smooth --passwordbox'
fi

# Gettext internationalization
export TEXTDOMAIN="zencfs"
export TEXTDOMAINDIR="/usr/share/locale"
. gettext.sh

# 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 0

if [ "$(echo "$arg" | grep "\.encfs$")" ]; then
	foldernoext="$(basename -s .encfs "$arg")"
	folderext="$(basename "$arg")"
	[ -d "$folderext" ] && pathtofolder="$(pwd)" || pathtofolder="$(dirname "$arg")"
else
	foldernoext="$(basename "$arg")"
	folderext="$(basename "$arg").encfs"
	[ -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 0
	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 0
	rmdir "$mountpoint"

else
	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 0
		[ ! "$password" ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$(eval_gettext 'Empty password')" 8 65 && exit 0
		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 0
		[ ! "$password" ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$(eval_gettext 'Empty password')" 8 65 && exit 0
		mkdir -p "$folderext"
		mkdir -p "$foldernoext.TMP"
		echo -n $password |  encfs --standard --stdinpass "$pathtofolder/$folderext" "$pathtofolder/$foldernoext.TMP"
		[ $? != 0 ] && $dialog --title "$(eval_gettext 'Error')" --msgbox "$(eval_gettext 'Error creating folder')" 8 65 && rmdir "$foldernoext.TMP" && rmdir "$folderext" && exit 0
		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 0
		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" 

