#!/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 frontend to encfs (now using yad)

if ! command -v yad >/dev/null 2>&1; then
  echo "yad is required but was not found in PATH." >&2
  exit 1
fi
if [ ! "$DISPLAY" ]; then
  echo "This program requires a running X session." >&2
  exit 1
fi
YAD_COMMON=(--center --window-icon="security")

arg="$1"

# Print Error message
dialog_error(){
  yad "${YAD_COMMON[@]}" --title "Error" --image "dialog-error" --button=OK:0 --text "$1" --width=350
}

# We deal with any situation : normal folder, folder with encfs extension, 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
  yad "${YAD_COMMON[@]}" --title "Closing" --button="Cancel:1" --button="OK:0" --image "dialog-question" \
      --text "Closing ENCFS Folder \"$foldernoext\"" --width=350
  [ $? != 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)"
  if [[ -d "$mountpoint" ]] ; then
    dialog_error "$ERROR"
    exit 0
  else
    gio set "$folderext" -t stringv metadata::emblems emblem-encrypted-locked && touch "$folderext"
    exit 0
  fi
else
  if [ -d "$folderext" ]; then
  # case 2 : folder not opened, but already encrypted : we may open it
    password="$(yad "${YAD_COMMON[@]}" --title "Password" --image "security-high" --entry --hide-text \
                    --text "Opening ENCFS Folder \"$folderext\"" --width=350)"
    [ $? != 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"
    if [[ $? != 0 ]] ; then
      dialog_error "$ERROR"
      rmdir "$foldernoext"
      exit 0
    else
      gio set "$folderext" -t stringv metadata::emblems emblem-encrypted-unlocked && touch "$folderext"
      # /usr/bin/xdg-open "$foldernoext"
      exit 0
    fi
  else
  # case 3 : folder not opened, and not already encrypted : we may create it
    password="$(yad "${YAD_COMMON[@]}" --title "Password" --image "security-high" --entry --hide-text \
                    --text "Creating ENCFS Folder \"$foldernoext.enc\"" --width=350)"
    [ $? != 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
      yad "${YAD_COMMON[@]}" --title "Moving data" --image "dialog-information" --no-buttons \
          --text "Moving data to ENCFS Folder, this may take a while depending on the size of data \"$foldernoext\"" --width=350 &
      pid=$!
      cp -rf "$foldernoext"/* "$foldernoext.TMP"
      kill $pid
      yad "${YAD_COMMON[@]}" --title "Closing" --image "dialog-information" --timeout=10 --timeout-indicator=bottom \
          --text "Done moving data to ENCFS Folder \"$foldernoext\"" --width=350
      ERROR="$(mv "$foldernoext" "$foldernoext.backup")"
      [ $? != 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)"
    if [[ -d "$mountpoint" ]] ; then
      dialog_error "$ERROR"
      exit 0
    else
      gio set "$folderext" -t stringv metadata::emblems emblem-encrypted-locked && touch "$folderext"
      exit 0
    fi
  fi
fi

exit 0
