#!/bin/bash

# Don't run as "sudo".

# Edit the script before using it, to insert your username and disk name.

# Use this script if your disk's passphrase is set up properly
# in the Mint files (file .../.ecryptfs/wrapped-passphrase).
# If that's not true, you'll have to get the passphrase
# (via "ecryptfs-unwrap-passphrase") or supply it from your records.

# This script was adapted from one found through
# https://superuser.com/questions/227713/ecryptfs-how-to-mount-a-backup-of-an-encrypted-home-dir

# Maybe there are other ways to do this, using the ecryptfs-mount-private command
# or the mount.ecryptfs command ?

# Works on Linux Mint 19 Cinnamon as of 10/2018.

USER=user0
#    ^^^^^ put your username here
echo Will mount home for user \'$USER\'

TARGET=/mnt/$USER

# ROOT should be the parent of the .ecryptfs and .Private folders
#ROOT=/home/.ecryptfs/$USER
ROOT=/media/mint/b5033879-dead-beef-9605-fa272887e0c4/.ecryptfs/$USER
# put disk name  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ here

sudo mkdir -p $TARGET
cd $ROOT

echo Type your password:
PASS=$(sudo ecryptfs-unwrap-passphrase .ecryptfs/wrapped-passphrase | sed s/Passphrase:\ //)
SIG1=$(sudo head -n1 .ecryptfs/Private.sig)
SIG2=$(sudo tail -n1 .ecryptfs/Private.sig)

echo Passphrase:
echo $PASS
echo Signatures:
echo $SIG1
echo $SIG2

read -p 'OK ? '

echo Should be empty:
sudo keyctl clear @u
sudo keyctl list @u

echo Do not type anything:
echo $PASS | sudo ecryptfs-add-passphrase --fnek

echo Should have signatures:
sudo keyctl list @u

read -p 'OK ? '

echo Mounting \'$ROOT\' on \'$TARGET\' ...
sudo mount -t ecryptfs -o key=passphrase,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_passthrough=no,ecryptfs_enable_filename_crypto=yes,ecryptfs_sig=$SIG1,ecryptfs_fnek_sig=$SIG2,passwd=$(echo $PASS) .Private $TARGET

ls $TARGET

# clear passphrase
unset -v PASS

echo Home for user \'$USER\' has been mounted as \'$TARGET\'

read -p 'OK ? '

