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

prog=`basename $0 .bash`
base=TrueC
ROLE=gpgkey
TMPDIR=/tmp
EXE=/q/Pg64/TrueCrypt-7.1a/TrueCrypt.exe
[ -x "$EXE" -a -n "$MSYSTEM" ] || EXE=truecrypt.bash

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

RARGS="--non-interactive -v"
if [[ "$*" =~ "--list" ]];  then
    cat<<EOF
Usage: truecrypt [--auto-mount <str>] [--backup-headers] [--background-task] [-C] [-c] [--create-keyfile] [--delete-token-keyfiles] [-d] [--display-password] [--encryption <str>] [--explore] [--export-token-keyfile] [--filesystem <str>] [-f] [--fs-options <str>] [--hash <str>] [-h] [--import-token-keyfiles] [-k <str>] [-l] [--list-token-keyfiles] [--load-preferences] [--mount] [-m <str>] [--new-keyfiles <str>] [--new-password <str>] [--non-interactive] [-p <str>] [--protect-hidden <str>] [--protection-keyfiles <str>] [--protection-password <str>] [--random-source <str>] [--restore-headers] [--save-preferences] [--quick] [--size <str>] [--slot <str>] [--test] [-t] [--token-lib <str>] [-v] [--version] [--volume-properties] [--volume-type <str>] [Volume path] [Mount point]
  --auto-mount=<str>         	Auto mount device-hosted/favorite volumes
  --backup-headers           	Backup volume headers
  --background-task          	Start Background Task
  -C, --change               	Change password or keyfiles
  -c, --create               	Create new volume
  --create-keyfile           	Create new keyfile
  --delete-token-keyfiles    	Delete security token keyfiles
  -d, --dismount             	Dismount volume
  --display-password         	Display password while typing
  --encryption=<str>         	Encryption algorithm
  --explore                  	Open explorer window for mounted volume
  --export-token-keyfile     	Export keyfile from security token
  --filesystem=<str>         	Filesystem type
  -f, --force                	Force mount/dismount/overwrite
  --fs-options=<str>         	Filesystem mount options
  --hash=<str>               	Hash algorithm
  -h, --help                 	Display detailed command line help
  --import-token-keyfiles    	Import keyfiles to security token
  -k, --keyfiles=<str>       	Keyfiles
  -l, --list                 	List mounted volumes
  --list-token-keyfiles      	List security token keyfiles
  --load-preferences         	Load user preferences
  --mount                    	Mount volume interactively
  -m, --mount-options=<str>  	TrueCrypt volume mount options
  --new-keyfiles=<str>       	New keyfiles
  --new-password=<str>       	New password
  --non-interactive          	Do not interact with user
  -p, --password=<str>       	Password
  --protect-hidden=<str>     	Protect hidden volume
  --protection-keyfiles=<str>	Keyfiles for protected hidden volume
  --protection-password=<str>	Password for protected hidden volume
  --random-source=<str>      	Use file as source of random data
  --restore-headers          	Restore volume headers
  --save-preferences         	Save user preferences
  --quick                    	Enable quick format
  --size=<str>               	Size in bytes
  --slot=<str>               	Volume slot number
  --test                     	Test internal algorithms
  -t, --text                 	Use text user interface
  --token-lib=<str>          	Security token library
  -v, --verbose              	Enable verbose output
  --version                  	Display version information
  --volume-properties        	Display volume properties
  --volume-type=<str>        	Volume type

EOF
    exit 0
elif [[ "$*" =~ "--list" ]];  then
    :
else
    [[ "$*" =~ "--password" ]] || [[ $RARGS =~ "--password" ]] || {
	ERROR --password is required
	exit 3
    }
    [[ "$*" =~ "--keyfiles" ]] || [[ $RARGS =~ "--keyfiles" ]] || RARGS="--keyfiles='' $RARGS"
fi

DBUG truecrypt.sh $RARGS "$@" >> $TMPDIR/$base$$.tmp
$EXE $RARGS "$@" 2>$TMPDIR/${base}$$.err >/tmp/${base}$$.log
retval=$?
if [ $retval  -eq 0 ] ; then
    INFO see $TMPDIR/${base}$$.log
else
    WARN see $TMPDIR/${base}$$.err
    cat $TMPDIR/${base}$$.err
fi
exit $retval
