#!/bin/bash
# -*- mode: sh; tab-width: 8; encoding: utf-8-unix -*-

prog=`basename $0`
PREFIX=/usr/local
ROLE=gpgkey
PYVER=3
EXE=/var/local/bin/tomb.bash

. /usr/local/bin/usr_local_tput.bash || exit 2
. /usr/local/bin/gridfire.rc

# python3 -c "import keyring.util.platform_; print(keyring.util.platform_.config_root())"
# ~/.local/share/python_keyring

# what goes on stdin - the passwd to the keyfile with the keyfile as an arg?
# or open the keyfile?
# passwd from gridfire or from keepass

usage() {
	echo "Syntax: tomb [options] command [arguments]"
	echo
	echo " // Creation:"
	echo " dig          create a new empty TOMB file of size -s in MiB"
	echo " forge        create a new KEY file and set its password"
	echo " lock         installs a lock on a TOMB to use it with KEY"
	echo
	echo " // Operations on tombs:"
	echo " open         open an existing TOMB (-k KEY file or - for stdin)"
	echo " index        update the search indexes of tombs"
	echo " search       looks for filenames matching text patterns"
	echo " list         list of open TOMBs and information on them"
	echo " ps           list of running processes inside open TOMBs"
	echo " close        close a specific TOMB (or 'all')"
	echo " slam         slam a TOMB killing all programs using it"
	[[ $RESIZER == 1 ]] && {
		echo " resize       resize a TOMB to a new size -s (can only grow)"
	}
	echo
	echo " // Operations on keys:"
	echo " passwd       change the password of a KEY (needs old pass)"
	echo " setkey       change the KEY locking a TOMB (needs old key and pass)"
	echo
	[[ $QRENCODE == 1 ]] && {
		echo " // Backup on paper:"
		echo " engrave makes a QR code of a KEY to be saved on paper"
		echo
	}
	[[ $STEGHIDE == 1 || $CLOAKIFY == 1 || $DECLOAKIFY == 1 ]] && {
		echo " // Steganography:"
		[[ $STEGHIDE == 1 ]] && {
			echo " bury         hide a KEY inside a JPEG image (for use with -k)"
			echo " exhume       extract a KEY from a JPEG image (prints to stdout)"
		}
		[[ $CLOAKIFY == 1 ]] && {
			echo " cloak        transform a KEY into TEXT using CIPHER (for use with -k)"
		}
		[[ $DECLOAKIFY == 1 ]] && {
			echo " uncloak      extract a KEY from a TEXT using CIPHER (prints to stdout)"
		}
		echo
	}
	echo "Options:"
	echo
	echo " -s           size of the tomb file when creating/resizing one (in MiB)"
	echo " -k           path to the key to be used ('-k -' to read from stdin)"
	echo " -n           don't launch the execution hooks found in tomb"
	echo " -p           preserve the ownership of all files in tomb"
	echo " -o           options passed to commands: open, lock, forge (see man)"
	echo " -f           force operation (i.e. even if swap is active)"
	echo " -g           use a GnuPG key to encrypt a tomb key"
	echo " -r           provide GnuPG recipients (separated by comma)"
	echo " -R           provide GnuPG hidden recipients (separated by comma)"

	[[ $SPHINX == 1 ]] && {
		echo " --sphx-user  user associated with the key (for use with pitchforkedsphinx)"
		echo " --sphx-host  host associated with the key (for use with pitchforkedsphinx)"
	}

	[[ $KDF == 1 ]] && {
		echo " --kdf        forge keys armored against dictionary attacks"
	}

	echo
	echo " -q           run quietly without printing informations"
	echo " -D           print debugging information at runtime"
}

# FixMe: make sure command is first
if [ "$#" -eq 0 ] || [ "$1" = '--help' -o  "$1" = 'help' ] ; then
#    usage
#    exit 0
:
fi

LARGS="-H \"tomb $tail\""
tail=`echo $@ | sed -e 's/.* \([^ ]*\) \([^ ]*\)/\1 \2/'`
if [[ "$*" =~ "-- " ]];then
    RARGS=`echo $*|sed -e "s/-- /-- $EXE/"`
    exec $PREFIX/bin/gridfire $LARGS $RARGS
else
    exec $PREFIX/bin/gridfiremake: *** No rule to make target 'up'.  Stop.
 $LARGS -- $EXE  "$@"
fi

