#!/bin/sh

TO="$2"

#Check if we got the public key 
function check_key()
{
  RES=`pgp -kv $TO 2>&1 | grep "0 matching keys found."`
  if [ ! -z "$RES" ]; then
    echo
    echo "Can't find public key for $TO"
    echo -n "Try a diffrent pgp user-id or press enter to abort: "
    TO=`head -1`
    if [ ! -z "$TO" ]; then
      check_key
    fi
  fi
}

TMP="$1"
TOS=""
shift
for i in $*; do
  TO="$i"
  check_key
  if [ -z "$TO" ]; then
    exit -1
  fi
  TOS="$TOS $TO"
done



if [ ! -z "$TOS" ]; then
  pgp -sea $TMP -o ~/.pgp/tmp.crypt $TOS
  mv ~/.pgp/tmp.crypt $TMP
else
  exit -1
fi

