#!/bin/sh
# <pre>
# 
# $Id: pgpencrypt,v 1.10 1997/03/20 18:51:12 roland Exp $
#
# 21.03.96 Roland Rosenfeld <roland@spinnaker.rhein.de> 
# 17.05.96 exit added.
# 19.09.96 Peter Jaeckel <atmpj@ermine.ox.ac.uk>
#          -metoo option included.
#          Set up your .pinerc with:
#             sending-filters=/home/pjaeck/bin/pgpencrypt -metoo _RECIPIENTS_,
#                       /home/pjaeck/bin/pgpsign
#          [Obviously, you substitute /home/pjaeck/bin/pgpencrypt
#           with your path to this file]
#          As pine stores emails that you send out in the filtered
#          form, without the -metoo switch you as the sender
#          can never decode your own messages.
# 19.03.97 Check return value of pgp.
#          Locate TMPFILE in a secure directory.
# 20.03.97 New bug introduced yesterday fixed.

umask 077

# find a secure place for the tmp files:
if [ -d "$PGPPATH" ]
then 
        TMPDIR=$PGPPATH
else
        TMPDIR=$HOME
fi
TMPFILE="$TMPDIR/pgpencrypt"

trap "rm -f $TMPFILE.???.$$; exit 1" 1 2 15

METOO=
case "$1" in
        "-metoo"|"--metoo"|"--me-too") 
        shift
        METOO='+EncryptToSelf=on'
        ;;
esac

(pgpe +verbose=1 $METOO -fast -r $* >$TMPFILE.txt.$$; \
 echo $? > $TMPFILE.exi.$$) 2>&1 \
 | tee $TMPFILE.pgp.$$ 1>&2

if [ `cat $TMPFILE.exi.$$` != "0" ] 
then 
        rm -f $TMPFILE.???.$$
        exit 1
fi

if grep 'Cannot find the public key' $TMPFILE.pgp.$$ >/dev/null
then 
        rm -f $TMPFILE.???.$$
        exit 1
fi

cat $TMPFILE.txt.$$
rm -f $TMPFILE.???.$$

