#!/bin/sh #
# 
# $ Id: pgpencrypt,v 1.10 1997/03/20 18:51:12 roland Exp $
#
#   Copyright (C) 1996-1997  Roland Rosenfeld 
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# 21.03.96 Roland Rosenfeld  
# 17.05.96 exit added.
# 19.09.96 Peter Jaeckel 
#          -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

(pgp +verbose=1 $METOO -feast $* >$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.???.$$