#!/bin/sh

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

xtra=
RES=`grep "Good signature" ~/.pgp/tmp.res`

if [ -z  "$RES" ]; then
  if [ ! "$3" = "NOBEEP" ]; then
    echo ''
  fi
  RES=`grep "Bad signature" ~/.pgp/tmp.res`
  if [ -z  "$RES" ]; then
    if grep -q "Can't find the right public key" ~/.pgp/tmp.res; then
      RES="Cannot find matching public key, signature not checked."
    else
      RES="No signature could be found.";
    fi
  fi
else
  adj='Good'
  if grep -q WARNING ~/.pgp/tmp.res; then
    if grep -q "public key is not certified with a trusted" ~/.pgp/tmp.res; then
      adj=Good
      xtra="Key is not certified with a trusted signature."
    elif grep -q "public key is not certified with enough trusted" ~/.pgp/tmp.res; then
      adj=Good
      xtra="Key is not certified with enough trusted signatures."
    elif grep -q "public key is not trusted" ~/.pgp/tmp.res; then
      adj=Untrusted
      xtra="Key is not trusted."
    elif grep -q "revoked" ~/.pgp/tmp.res; then
      adj=Revoked
      xtra="Key has been revoked by its owner."
    fi
    RES=`echo $RES | sed -e "s/Good/$adj/"`
  fi
fi
# if it's greater than a given width, Pine puts it in a new window.
echo "$RES" | sed -e 's/user //' -e 's/signature/sig/' | cut -c 1-58 > $2
echo >> $1
echo -- >> $1
echo $RES >> $1
if [ -n "$xtra" ]; then echo $xtra >> $1; fi
rm ~/.pgp/tmp.res


