#!/bin/sh
clear
sudo modprobe fuse
rep="/home/$USER/"
rep1=$rep".encrypted"
rep2=$rep"decrypted"
file="/media/$USER/SECURE/passphrase.bin"

#Close Coffre fort numérique
if [ "$1" = "close" ] && [ ! -f "$rep2" ]
then
	fusermount -u "$rep2"
	echo "Closed"
	exit 0
fi
#Installation du coffre fort numérique
if [ "$1" = "install" ] && [ ! -f "$rep1" ]
then
	mkdir "$rep1" "$rep2"
	sudo apt install encfs
	echo "Usage coffre open or close"
	exit 0

fi

#Aide
if [ "$1" = "" ]
then
	echo "Usage: $0 |open|close|install"
	exit 0
fi

#Open Coffre fort numérique
if [ "$1" = "open" ] && [ ! -f "$rep2" ]
then

echo "Trying to get the key from USB keychain ..."

if [ -f "$file" ]
then
	base64 -w 0 "$file" | encfs -S $rep1 $rep2
else
	echo "FAILED to find suitable USB keychain ..."
        exit 1
fi

fi 
exit 0