#!/bin/bash
#
#    Copyright © 2016 Zetok Zalbavar <zetok@openmailbox.org>
#
#    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/>.


# GUI for `epaste`.
#
# Depends on:
#   * bash
#   * kdialog
#   * `epaste` in $PATH
#
# Usage:
#   ./epaste-gui


set -eu -o pipefail


# if there is no `epaste` in PATH, error & exit
if ! which epaste &> /dev/null
then
    kdialog --title 'Epaste not found!' \
            --error 'There is no `epaste` in PATH!'
    exit 1
fi

# pick mode of operation
ENC_DEC=$(kdialog --title 'Epaste – mode' \
                  --radiolist 'Do you want to encrypt, or decrypt?' \
                  1 Decrypt on \
                  2 Encrypt off)

# get password
get_passwd() {
    PASSWD=$(kdialog --title 'Epaste – file password' \
                     --password 'Enter password to encrypt / decrypt file:' )
}
get_passwd

# make sure that password is set, or exit if it's not
until [[ -n "${PASSWD}" ]]
do
    kdialog --title 'Epaste – no password' \
            --yesno 'Looks like there was no password provided.

Do you want to try again?'

    get_passwd
done


# get file
get_enc_dec_file() {
    FILE_INPUT=$(kdialog --title 'Epaste – pick file to encrypt / decrypt' \
                         --getopenfilename .)
}
get_enc_dec_file

# make sure that file exists
until [[ -e "${FILE_INPUT}" ]]
do
    kdialog --title 'Epaste – no file' \
            --yesno "There is no file named:

$FILE_INPUT

Mind selecting a real™ file?"

    get_enc_dec_file
done


# get output file
get_output_file() {
    FILE_OUTPUT=$(kdialog --title 'Epaste – output file' \
                          --getsavefilename "$PWD")

    # make sure that existing file should be used
    if [[ -e "${FILE_OUTPUT}" ]]
    then
        kdialog --title 'Epaste – file already exists' \
                --warningcontinuecancel "Are you sure that you want to overwrite file

$FILE_OUTPUT

?" || get_output_file

    fi

    # try to write to the file
    > "${FILE_OUTPUT}"
}
get_output_file

# make sure that picked file is writable
until [[ -w "${FILE_OUTPUT}" ]]
do
    kdialog --title "Epaste – can\'t write" \
            --error "Picked file is not writable, please pick other one than:

$FILE_OUTPUT"

    get_output_file
done

failed_decrypt() {
    kdialog --title 'Epaste – failed to decrypt' \
            --passivepopup "Failed to decrypt:

$FILE_INPUT

Are you sure that supplied password was correct?" 10

    exit 1
}

decrypt() {
    epaste -d "${PASSWD}" < "${FILE_INPUT}" > "${FILE_OUTPUT}" \
        || failed_decrypt

    kdialog --title 'Epaste – successful decryption' \
            --passivepopup "Successfully decrypted

$FILE_INPUT

↓ to ↓

$FILE_OUTPUT" 10
}

encrypt() {
    epaste "${PASSWD}" < "${FILE_INPUT}" > "${FILE_OUTPUT}"
    kdialog --title 'Epaste – successful encryption' \
            --passivepopup "Successfully encrypted

$FILE_INPUT

↓ to ↓

$FILE_OUTPUT" 10
}


if [[ $ENC_DEC == "1" ]]
then
    decrypt
else
    encrypt
fi
