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

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=gpgkey

. /usr/local/bin/gridfire.rc

COMMANDS=(
	asn1parse ca ciphers cms crl crl2pkcs7 dgst dhparam dsa dsaparam ec
	ecparam enc engine errstr gendsa genpkey genrsa help list nseq ocsp
	passwd pkcs12 pkcs7 pkcs8 pkey pkeyparam pkeyutl prime rand rehash req
	rsa rsautl s_client s_server s_time sess_id smime speed spkac srp
	storeutl ts verify version x509 dgst enc
)
# for elt in ${COMMANDS[*]}; do echo INFO: openssl $elt;openssl $elt -help;done

usage () {
    echo "USAGE: recognized commands are - ${PASSIN_COMMANDS[*]} ${PASSOUT_COMMANDS[*]} ${PASS_COMMANDS[*]}"
    return 0
}


if [ "$#" -eq 0 ] || [ "$1" = '--help' ] || [ "$1" = '-h' ] ; then
    echo USAGE: $0 command [options]
    cat << EOF
Recognized commands:

-passin commands: -passin pass:stdin
    ca \
      -passin val	Input file pass phrase source
    cms
      -pwri_password val	(No additional info)
      -passin val	Input file pass phrase source
    dgst
      -passin val	Input file pass phrase source
    pkeyutl
      -passin val	Input file pass phrase source
    rsautl
      -passin val	Input file pass phrase source
    smime
      -passin val	Input file pass phrase source
    spkac
      -passin val	Input file pass phrase source
    storeutl
      -passin val	Input file pass phrase source
    ts
      -passin val	Input file pass phrase source
    x509
      -passin val	Private key password/pass-phrase source
    dgst
      -passin val	Input file pass phrase source

-passout commands: -passout pass:stdin
    gendsa
      -passout val	Output file pass phrase source
    genrsa
      -passout val	Output file pass phrase source

-pass commands: -pass pass:stdin
    enc
      -pass val	Passphrase source
    genpkey
      -pass val	Output file pass phrase source
	
Options:
	pass:stdin
	pass:fd0
EOF
    exit 1
  fi
COMMAND=$1

# FixMe: make sure command is first
if [ $COMMAND = '-help' ] || [ $COMMAND = '--help' ] ; then
    usage
    echo "USAGE: all openssl commands are - ${COMMANDS[*]}"
    exit 0
fi
if [ "${COMMAND:0:1}" = "-" ] ; then
    echo "USAGE: command args - command must precede args"
    usage
    exit 1
  fi

case $COMMAND in \
# PASSIN_COMMANDS=(
    ca \
    | cms \
    | dgst \
    | pkeyutl \
    | rsautl \
    | smime \
    | spkac \
    | storeutl \
    | ts \
    | x509 \
    | dgst \
)   # FixMe: check if already there
    LARGS="-passin pass:stdin"
    $PREFIX/bin/gridfire -H "openssl $LARGS" -- openssl $LARGS "$@" || exit $?
    ;;
    
# PASSOUT_COMMANDS=(
    gendsa \
    | genrsa \
)   # FixMe: check if already there
    LARGS="-passout pass:stdin"
    $PREFIX/bin/gridfire -H "openssl $LARGS" -- openssl $LARGS "$@" || exit $?
    ;;

# PASS_COMMANDS=( \
    enc \
    | genpkey \
)   # FixMe: check if already there
    LARGS="-pass pass:stdin"
    $PREFIX/bin/gridfire -H "openssl $LARGS" -- openssl $LARGS "$@" || exit $?    
    ;;

# PASSNOV_COMMANDS=( \
    passwd \
    | '-in infile	Read passwords from file' \
    | '-noverify	Never verify when reading password from terminal' \
    | '-stdin	Read passwords from stdin' \
)    # FixMe: check if already there
    #? conflicts with -in?
    LARGS=" -noverify -stdin"
    bash $PREFIX/bin/gridfire -H "openssl $LARGS" -- openssl $LARGS "$@" || exit $?
    ;;

# PASSINOUT_COMMANDS=( \
    pkcs8 \
    | '-passin val	Input file pass phrase source' \
    | '-passout val	Output file pass phrase source' \
    | pkey \
    | '-passin val	Input file pass phrase source' \
    | '-passout val	Output file pass phrase source' \
    | rsa \
    | '-passout val	Output file pass phrase source' \
    | '-passin val	Input file pass phrase source' \
    | srp \
    | '-passin val	Input file pass phrase source' \
    | '-passout val	Output file pass phrase source' \
)   # FixMe: check if already there
    # FixMe: fd:
    LARGS="--passin"
    passin=`sh $PREFIX/bin/gridfire -H "openssl $LARGS" `
    LARGS="-passin pass:$passin -passout pass:stdin"
    bash $PREFIX/bin/gridfire -H "openssl -passout pass:stdin" -- openssl $LARGS "$@" || exit $?

esac
exit 0

# PASSDPASS_COMMANDS=( \
	s_server \
    # -pass val	Private key file pass phrase source \
    # -dpass val	Second private key file pass phrase source \
)   # FixMe: check if already there
    # FixMe: fd:
    pass=`sh $PREFIX/bin/gridfire.sh`
    LARGS="-pass pass:$pass -dpass pass:stdin"
    bash $PREFIX/bin/gridfire -- openssl $LARGS "$@" || exit $?

;; # PASSKPASS_COMMANDS=( \
	enc \
    # -pass val	Passphrase source \
    # -kfile infile	Read passphrase from file \
)   # FixMe: check if already there
    # FixMe: fd:
    #?pass=`sh $PREFIX/bin/gridfire.sh`
    #?LARGS="-pass pass:$pass -dpass pass:stdin"
    LARGS="-pass pass:stdin"
    $PREFIX/bin/gridfire -H "openssl $LARGS" -- openssl $LARGS "$@" || exit $?

;; # PASSINOUTWORD_COMMANDS=( \ \
	pkcs12 \
    # -twopass	Separate MAC, encryption passwords \
    # -passin val	Input file pass phrase source \
    # -passout val	Output file pass phrase source \
    # -password val	Set import/export password source \
)   # FixMe: check if already there


    # FixMe: pass: prefix
$PREFIX/bin/gridfire -H "-passin pass:" --single "passin" -- sh $PREFIX/bin/gridfire -H "-passout stdin" -- openssl "$@" || exit $?
esac
