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

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=gpgkey
base=VerC
TMPDIR=/tmp
if [ -z "$EXE" ] ; then
EXE='/q/Pg64/VeraCrypt_1.24-Update7/veracrypt.exe'
[ -x "$EXE" -a -n "$MSYSTEM" ] || EXE=veracrypt
fi

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

[[ "$*" =~ --stdin ]] && \
    LARGS="--text --non-interactive"

if [[ "$*" =~ --create ]] ; then
    . /usr/local/bin/veracrypt-create.sh
elif [[ "$*" =~ --change ]] ; then
    :
#    [[ "$*" =~ --pim ]]         || RARGS="--pim=100 $RARGS"
    [[ "$*" =~ --volume-type ]] || RARGS="--volume-type=normal $RARGS"
    [[ "$*" =~ --hash ]]        || RARGS="--hash=sha512 $RARGS"
    [[ "$*" =~ --encryption ]]  || RARGS="--encryption=Twofish $RARGS"
elif [[ "$*" =~ --list ]] ; then
    :
else
    [[ "$*" =~ --pim ]] || RARGS="--pim=100 $RARGS"
    [[ "$*" =~ --filesystem ]] || [[ $RARGS =~ "--filesystem" ]] || RARGS="--filesystem=none $RARGS"
    [[ "$*" =~ --encryption ]]  || RARGS="--encryption=Twofish $RARGS"
    [[ "$*" =~ --volume-type ]] || RARGS="--volume-type=normal $RARGS"
    [[ "$*" =~ --hash ]]        || RARGS="--hash=sha512 $RARGS"
# not =''
    [[ "$*" =~ --keyfiles ]] || RARGS="--keyfiles '' $RARGS"
fi

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