#!/bin/sh

bindir=/usr/bin
tmpdir=~/.pinepgp
PINERC=$HOME/.pinerc

case $0 in
	*pinegpg-install)	PGPVER="gpg"	;;
	*pinepgp26x-install)	PGPVER="pgp26x"	;;
	*pinepgp5x-install)	PGPVER="pgp5x"	;;
	*pinepgp65x-install)	PGPVER="pgp65x"	;;
	*)
	  echo "Install Pine in/out filters for PGP 2.6.x/5.x/6.5.x / GPG"
	  echo "Use:"
	  echo "	pinegpg-install [your e-mail]"
	  echo "	pinepgp26x-install [your e-mail]"
	  echo "	pinepgp5x-install [your e-mail]"
	  echo "	pinepgp65x-install [your e-mail]"
          echo "If you specify your e-mail address, you will be able to read"
          echo "encrypted messages you sent (which are stored in sent-mail)."
	  exit
	;;
esac

if [ ! -f $PINERC ]; then
	echo -e "You have no .pinerc file in your home directory."
else

if [ ! -e $tmpdir ]; then
	mkdir $tmpdir
	if [ ! -d $tmpdir ]; then
		echo "WARNING! $tmpdir is not a directory!"
	fi
	chmod 700 $tmpdir
fi

ETY=""
if [ ! -z $1 ]; then
        echo "Enabling 'encrypt to yourself' feature ($1) ..."
        ETY=" $1"
fi

cp $PINERC $PINERC.old

/usr/bin/sed -e "
s%^display-filters=\(.\)%display-filters=\"_LEADING(-----BEGIN PGP MESSAGE-----\)_\" $bindir/$PGPVER-check,\
\"_LEADING(-----BEGIN PGP SIGNED MESSAGE-----\)_\" $bindir/$PGPVER-check,\
\1%
s%^sending-filters=\(.\)%sending-filters=$bindir/$PGPVER-sign,\
$bindir/$PGPVER-encrypt _RECIPIENTS_$ETY,\
$bindir/$PGPVER-sign+encrypt _RECIPIENTS_$ETY,\
\1%
s%^display-filters=$%display-filters=\"_LEADING(-----BEGIN PGP MESSAGE-----\)_\" $bindir/$PGPVER-check,\
\"_LEADING(-----BEGIN PGP SIGNED MESSAGE-----\)_\" $bindir/$PGPVER-check\
%
s%^sending-filters=$%sending-filters=$bindir/$PGPVER-sign,\
$bindir/$PGPVER-encrypt _RECIPIENTS_$ETY,\
$bindir/$PGPVER-sign+encrypt _RECIPIENTS_$ETY\
%
" $PINERC.old | uniq > $PINERC

fi
