## created for use with PGP 2.6.3

if [  ! -z "`grep -- "-----BEGIN PGP PUBLIC KEY BLOCK-----" $1`" ]; then
  pgp -ka $1 2>&1 | tee /dev/tty | tr -d '\a' | tee $PGPPATH/tmp.res
elif [ -z "`grep -- "-----BEGIN PGP MESSAGE-----" $1`" ]; then
  pgp $1 +LANGUAGE=en +batch=on -o $PGPPATH/tmp.check 2>&1 | tr -d '\a' | tee $PGPPATH/tmp.res
else
  pgp $1 +LANGUAGE=en -o $PGPPATH/tmp.check 2>&1 | tee /dev/tty | tr -d '\a' | tee $PGPPATH/tmp.res
fi

# avoid adding unnecessary blank lines
if [ ! -z "$HEAD" ]; then
  echo "$HEAD" > $1
  echo >> $1
  echo "$starttext" >> $1
else
  echo "$starttext" > $1
fi
cat $PGPPATH/tmp.check >> $1
rm $PGPPATH/tmp.check

msg=`grep "Good signature" $PGPPATH/tmp.res`
note=
if [ -z "$msg" ]; then
  if [ -z "`grep "Bad signature" $PGPPATH/tmp.res`" ]; then
    if grep -q "Can't find the right public key" $PGPPATH/tmp.res; then
      msg="Cannot find matching public key, signature not checked."
    elif grep -q "Unsupported packet format" $PGPPATH/tmp.res; then
      msg="Unsupported packet format."
      note="You need a newer version of PGP for this message."
    elif grep -q "message can only be read by" $PGPPATH/tmp.res; then
      msg="Cannot decrypt message."
      note=`grep -A 1 "message can only be read by" $PGPPATH/tmp.res`
    else
      msg="No signature could be found."
    fi
  fi
else
  adj="Good"
  if grep -q "WARNING" $PGPPATH/tmp.res; then
    if grep -q "public key is not certified with a trusted" $PGPPATH/tmp.res; then
      note="Key is not certified with a trusted signature."
    elif grep -q "public key is not certified with enough trusted" $PGPPATH/tmp.res; then
      note="Key is not certified with enough trusted signatures."
    elif grep -q "public key is not trusted" $PGPPATH/tmp.res; then
      adj=Untrusted
      note="Key is not trusted."
    elif grep -q "revoked" $PGPPATH/tmp.res; then
      adj=Revoked
      note="Key has been revoked by its owner."
    fi
    msg=`echo $msg | sed -e "s/Good/$adj/"`
  fi
fi

if [ ! -z "`grep "No new keys or signatures in keyfile." $PGPPATH/tmp.res`" ]; then
  msg="No new keys could be found in this keyblock.";
fi

if [ ! -z "`grep "Keyfile contains:" $PGPPATH/tmp.res`" ]; then
  msg="Keys added to keyring.";
fi

# if it's greater than a given width, Pine puts it in a new window.
echo "$msg" | sed -e 's/user //' -e 's/signature/sig/' | cut -c 1-58 > $2
echo >> $1
echo -- >> $1
echo $msg >> $1
if [ -n "$note" ]; then
  echo $note >> $1
fi
rm $PGPPATH/tmp.res
echo "$endtext" >> $1
if [ ! -z "$TAIL" ]; then
  echo >> $1
  echo "$TAIL" >> $1
fi