#!/bin/sh

set -e

TOMB=/usr/bin/tomb

# tomb writes non-error output to stderr, we redirect it to stdout by 2>&1.
# Create the file to hold the encrypted file system. With ext4 20MiB suffice,
# btrfs requires at least 115MiB. 
zsh -e $TOMB dig   -s 20 test.tomb 2>&1
# Create the key file making sure the kdf binaries are used during key
# generation by applying --kdf.
zsh -e $TOMB forge --unsafe -f --tomb-pwd somepw --kdf 1 test.key 2>&1

# Stop test here if the environment has no loop device available
if ! losetup -f >/dev/null 2>&1; then
	echo No loop device available, cutting test short.
	exit 0
fi

# Continue test with commands requiring a loop device
# Create the encrypted file system.
zsh -e $TOMB lock  --unsafe -f --tomb-pwd somepw -k test.key --filesystem ext4 test.tomb 2>&1
# Open and use the created tomb.
zsh -e $TOMB open  --unsafe -f --tomb-pwd somepw -k test.key test.tomb 2>&1
echo Copying some data into the opened tomb ...
cp -a test.key /media/test
echo Opened tomb\'s content:
ls -la /media/test
zsh -e $TOMB close test 2>&1

# Repeat test with btrfs
rm -rf test.tomb
zsh -e $TOMB dig   -s 115 test.tomb 2>&1
zsh -e $TOMB lock  --unsafe -f --tomb-pwd somepw -k test.key --filesystem btrfs test.tomb 2>&1
zsh -e $TOMB open  --unsafe -f --tomb-pwd somepw -k test.key test.tomb 2>&1
echo Copying some data into the opened tomb ...
cp -a test.key /media/test
echo Opened tomb\'s content:
ls -la /media/test
zsh -e $TOMB close test 2>&1
