#!/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

arg="$1"

# 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 --left --icon security"
  shortttl='3000'
  longttl='8000'
  xflag='yes'
  securedialog='Xdialog --title Password --stdout --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

# Printe Error message
dialog_error(){
  $dialog --title "$(eval_gettext 'Error')" --msgbox "$(eval_gettext "$1")" 8 65
}

# We deal with any situation : normal folder, folder with encfs extention, no folder, relative or absolute path ...

[ ! -e "$arg" ] && mkdir -p "$arg"
# [ ! -d "$arg" ] && dialog_error "Not a folder" && exit 0

if [ "$(echo "$arg" | grep "\.encfs$")" ]; then
  folderext="$(basename "$arg")"
  [ -d "$folderext" ] && pathtofolder="$(pwd)" || pathtofolder="$(dirname "$arg")"
  foldernoext="$(basename -s .encfs "$arg")"
else
  foldernoext="$(basename "$arg")"
  [ -d "$foldernoext" ] && pathtofolder="$(pwd)" || pathtofolder="$(dirname "$arg")"
  folderext="${foldernoext}.encfs"
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)"
  [ $? != 0 ] && dialog_error "$ERROR"
  ERROR="$(rmdir "$mountpoint" 2>&1)"
  [ -d "$mountpoint" ] && dialog_error "$ERROR" && exit 0
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_error "Empty password" && exit 0
    ERROR="$(rmdir "$foldernoext" 2>&1)"
    [ -d "$foldernoext" ] && dialog_error "$ERROR" && exit 0
    mkdir -p "$foldernoext"
    ERROR="$(echo -n $password |  encfs --standard --stdinpass "$pathtofolder/$folderext" "$pathtofolder/$foldernoext" 2>&1)"
    [ $? != 0 ] && dialog_error "$ERROR" && rmdir "$foldernoext" || /usr/bin/xdg-open "$foldernoext"
  else
  # case 3 : folder not opened, and not already encrypted : we may create it
    password="$(${securedialog} "$(eval_gettext 'Creating ENCFS Folder') \"$foldernoext.enc\"" 12 80)"
    [ $? != 0 ] && exit 0
    [ ! "$password" ] && dialog_error "Empty password" && exit 0
    mkdir -p "$folderext"
    mkdir -p "$foldernoext.TMP"
    echo -n "$password" |  encfs --standard --stdinpass "$pathtofolder/$folderext" "$pathtofolder/$foldernoext.TMP"
    [ $? != 0 ] && dialog_error "Error creating folder"
    rmdir "$foldernoext"
    if [ $? != 0 ]; then
      $dialog --title "$(eval_gettext 'Moving data')" --msgbox "$(eval_gettext 'Moving data to ENCFS Folder,this may take a while depending on the size of data') \"$foldernoext\"" 8 65 &
      pid=$!
      cp -rf "$foldernoext"/* "$foldernoext.TMP"
      kill $pid
      $dialog --title "$(eval_gettext 'Closing')" --timeout 10 --msgbox "$(eval_gettext 'Done moving data to ENCFS Folder') \"$foldernoext\"" 8 65
      ERROR="$(mv "$foldernoext" "$foldernoext.clear")"
      [ $? != 0 ] && dialog_error "$ERROR" && exit 0
    fi
    mountpoint="$(/bin/mount | grep "^encfs.*$pathtofolder/$foldernoext.*" | sed -n 's|^encfs on \(.*\) type .*|\1|p')"
    ERROR="$(fusermount -u "$mountpoint" 2>&1)"
    [ $? != 0 ] && dialog_error "$ERROR" && exit 0
    ERROR="$(rmdir "$mountpoint" 2>&1)"
    [ -d "$mountpoint" ] && dialog_error "$ERROR" && exit 0
  fi
fi

exit 0

