#!/usr/bin/sh
#
. /etc/blackPanther-default-apps.conf
# 	Part of servicemenu-encfs Version 0.1.1
# 	Copyright (C) 2009 Giuseppe Benigno <giuseppe.benigno(at)egregorion.net>
#
# 	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 3 of the License, or
# 	(at your option) any later version.
#
# 	This program is distributed in the hope that it will be useful,
# 	but WITHOUT ANY WARRANTY; without even the implied warranty of
# 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 	GNU General Public License for more details.
#
# 	You should have received a copy of the GNU General Public License
# 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

lang=`echo "${1}" | sed -e "s/@//g"`
shift
action="${1}"
shift
encryptedDir="${1}"
encryptedDirName="${encryptedDir##*/}"
destDir="$HOME/_Decrypted_${encryptedDirName}"
destDir_alt="$HOME/Decrypted_${encryptedDirName}"
destDir_chk=$(cat /etc/mtab | grep $(echo "$destDir"| sed 's|.*/_||') | awk '{print $2}')
if [ -n "$destDir_chk" ];then
    destDir="${destDir_chk}"
fi
destDirName="${destDir##*/}"

xmsg=$(which kdialog 2>/dev/null || which pydialog 2>/dev/null)
#### languages strings messages #################
# Sintax for strings name is: msg_[$action]_$window_[$section]
# For languages as sr@Latn use srLatn

load_language_en () {
	msg_no_passwd_title="encFS: enter passphase ..."
	msg_no_passwd_text="Create crypted \"${encryptedDirName}\" directory. Now enter a password for this"
	msg_mount_passwd_title="encFS: enter passphase ..."
	msg_mount_passwd_text="Enter passphrase for \"${encryptedDirName}\""
	msg_mount_destDirError_title="Warning!"
	msg_mount_destDirError_text="Sorry, destination directory \"${destDir}\" already exist!."
	msg_mount_mkdirError_title="Creating directory error!"
	msg_mount_mkdirError_text="Can't create the destination directory \"${destDir}\""
	msg_mount_mountError_title="Mount directory error!"
	msg_mount_mountError_text="Can't mount the destination directory \"${destDir}\""
	msg_mount_finish_title="Directory \"${encryptedDirName}\" mounted in \"${destDir}\""
	msg_mount_finish_text="make"
        volume_make="Volume make"
        volume_make_msg="Create crypted volume needed for you. Please press enter for default settings and add a password pair for volume. This dialog and function show only first time."
        QUESTIONTXT="Has to install the EncFS application to the usage of Encryption service. You want install EncFs now?"
	msg_umount_finish_title="Directory \"${destDir}\" umounted and removed"
	msg_umount_finish_text="Done"
}
load_language_hu () {
	msg_no_passwd_title="encFS:Jelszó megadása ..."
	msg_no_passwd_text="Titkosított \"${encryptedDirName}\" mappa létrehozása, Ehhez most adjon meg egy jelszót"
	msg_mount_passwd_title="encFS: jelszó megadása ..."
	msg_mount_passwd_text="Add meg a <b>jelszót</b> a <u>\"${encryptedDirName}\"</u> feloldásához!"
	msg_mount_destDirError_title="Figyelmeztetés!"
	msg_mount_destDirError_text="Elnézést de a \"${destDir}\" célkönyvtár már létezik!."
	msg_mount_mkdirError_title="Hiba a könyvtár létrehozásakor!"
	msg_mount_mkdirError_text="A \"${destDir}\" célkönyvtárat nem lehetett létrehozi."
	msg_mount_mountError_title="A könyvtár csatolása sikertelen!"
	msg_mount_mountError_text="<big>Hiba!</big><br><br>A titkosított \"${encryptedDirName}\" kötetet nem lehetett becsatolni<br>a \"${destDir}\" mappába!"
	msg_mount_finish_title="A \"${encryptedDirName}\" könytár csatolva lett itt: \"${destDir}\""
	msg_mount_finish_text="Kész"
        volume_make="Kötetlétrehozás"
        volume_make_msg="Most lérte kell hoznia a titkosított kötetet. Az alapértelmezett beállításokhoz először nyomja meg az enter-t majd adjon meg egy jelszavat. Ez a funkció és az üzenet
        csak első alkalommal látható..."
        QUESTIONTXT="A titkosítási szolgáltatáshoz telepítenie kell az EncFS programot. Akarja most telepíteni?"

	msg_umount_finish_title="A \"${destDir}\" könyvtár lecsatolása és eltávolítása kész."
	msg_umount_finish_text="Kész"
}

load_language_it () {
	msg_no_passwd_title="encFS: digita la password ..."
	msg_no_passwd_text="Digita la password per \"${encryptedDirName}\""
	msg_mount_passwd_title="encFS: digita la password ..."
	msg_mount_passwd_text="Digita la password per \"${encryptedDirName}\""
	msg_mount_destDirError_title="Atttenzione!"
	msg_mount_destDirError_text="Spiacente, la directory di destinazione \"${destDir}\" esiste già!."
	msg_mount_mkdirError_title="Errore nella creazione della directory!"
	msg_mount_mkdirError_text="Non è possibile creare la directory di destinazione \"${destDir}\"."
	msg_mount_mountError_title="Errore di montaggio directory!"
	msg_mount_mountError_text="Si è verificato un errore durante il montaggio della directory \"${destDir}\""
	msg_mount_finish_title="Directory \"${encryptedDirName}\" montata in \"${destDir}\""
	msg_mount_finish_text="Finito"
        volume_make="Volume make"
        volume_make_msg="Create crypted volume needed for you. Please press enter for default settings and add a password pair for volume. This dialog and function show only first time."
        QUESTIONTXT="Has to install the EncFS application to the usage of Encryption service. You want install EncFs now?"
	msg_umount_finish_title="Directory \"${destDir}\" smontata e rimossa"
	msg_umount_finish_text="Finito"
}

################################################
instcrypt () {
    $xmsg --warningyesnocancel "$QUESTIONTXT"
    QUESTION=$?
    #echo "Select $QUESTION"
    [[ "$QUESTION" = 0 ]] && $INSTALLGUI encfs
    exit
}

checkscanner () {
if [ ! `which encfs 2>/dev/null` ];then
    instcrypt
fi
}


mount () {
	if [ -e "${destDir}" ]; then
    	    if [ -e "${destDir_alt}" ]; then
		echo "${msg_mount_destDirError_text}" #DEBUG
		$xmsg --title "${msg_mount_destDirError_title}" --icon document-decrypt --sorry "${msg_mount_destDirError_text}"
		exit
	    else
		destDir="${destDir_alt}"
	    fi
	fi

	#encfs_passwd=`kdialog --title "${msg_mount_passwd_title}" --caption "${msg_mount_passwd_title}" --icon document-decrypt --password "${msg_mount_passwd_text}"`
	#if [ $? != 0 ]; then
	#	echo "Aborted!" #DEBUG
	#	rm -r "${destDir}"
	#	exit
	#fi
        mkdir "${destDir}"
	if [ $? != 0 ]; then
    	    mkdir "${destDir_alt}"
	    if [ $? != 0 ]; then
		echo "${msg_mount_mkdirError_text}" #DEBUG
		$xmsg --title "${msg_mount_mkdirError_title}" --icon document-decrypt --error "${msg_mount_mkdirError_text}"
		exit
	    else
		destDir="${destDir_alt}"
	    fi
	fi

	#echo "${encfs_passwd}" | encfs "${encryptedDir}" "${destDir}" -S
	if [ ! -f "${encryptedDir}"/.encfs6.xml ];then
	    echo "No passw for this directory.. "
	    $xmsg --title "$volume_make" --msgbox "$volume_make_msg" 
	    konsole -e encfs ${encryptedDir} ${destDir}
	    #encfs_passwd=`kdialog --title "${msg_no_passwd_title}" --caption "${msg_no_passwd_title}" --icon document-decrypt --password "${msg_no_passwd_text}"`
	    if [ $? != 0 ]; then
		echo "Aborted!" #DEBUG
		rm -r "${destDir}"
		exit
	    fi
	    echo "Add passwd"
	    rm -r "${destDir}"
	    exit
	    #encfs "${encryptedDir}" "${destDir}" -S --extpass="kdialog --title \"${msg_mount_passwd_title}\" \
	    #--caption \"${msg_mount_passwd_title}\" --icon document-decrypt --password \"${msg_mount_passwd_text}\""
	 else
	    encfs "${encryptedDir}" "${destDir}" -S --extpass="$xmsg --title \"${msg_mount_passwd_title}\" --icon document-decrypt --password \"${msg_mount_passwd_text}\""
	    if [ $? != 0 ]; then
		echo "${msg_mount_mountError_text}" #DEBUG
		$xmsg --title "${msg_mount_mountError_title}" --icon document-decrypt --error "${msg_mount_mountError_text}"
		rm -r "${destDir}"
		exit
	    fi
	fi 
	if [ ! -f "${destDir}/.directory" ];then 
	echo "[Desktop Entry]"> "${destDir}/.directory"
	echo "Icon=folder-red">> "${destDir}/.directory"
        fi
	bubblemsg "${msg_mount_finish_text}" "${msg_mount_finish_title}"  && dolphin "${destDir}"
}

umount () {
	#destDir=$(cat /etc/mtab | grep $(echo "$destDir"| sed 's|.*/_||') | awk '{print $2}')
	#echo "fusermount -u \"${destDir}\" && rm -r \"${destDir}\" && $xmsg --title \"${msg_umount_finish_title}\" --passivepopup \"${msg_umount_finish_text}\" 5"
	fusermount -u "${destDir}" && rm -r "${destDir}" && bubblemsg "${msg_umount_finish_text}" "${msg_umount_finish_title}"
}

################################################ main


"load_language_${lang}" || load_language_en
checkscanner
"${action}" "${@}"
