#!/bin/sh
#
# Usage: ishencryptmime [s|e|se|es] input-file output-file boundary
#			[recipient-list]
#
# Options:
#	s: generate digital signature using multipart/signature
#	e: encrypt using multipart/encrypted (requires recipient-list)
#	se|es: sign and encrypt using multipart/encrypted (requires recip-list)
#
# Description:
#	Passes the input file through pgp.  Creates a multipart/signed or
#	multipart/encrypted MIME body part in "output-file" with content-
#	headers in "{output-file}.head" using the specified boundary.
#

usage()
{
   $ECHO "$0 $*"
   $ECHO ""
   $ECHO "Usage: ishencryptmime [s|e|se|es] input-file output-file boundary"
   $ECHO "                      [recipient-list]"
   $ECHO "   s    : generate digital signature using multipart/signed"
   $ECHO "   e    : encrypt using multipart/encrypted (requires recipient-list)"
   $ECHO "   se|es: sign and encrypt using multipart/encrypted"
   $ECHO "          (requires recipient-list)"
   $ECHO ""
   $ECHO "*** Press return to continue ***"
   read ANS
}

#
# Check arguments
#

ARGS=""
TYPE=""
case $1 in
   s)     ARGS="-sbat";
          TYPE="signing";
          shift;;
   e)     ARGS="-eat";
          TYPE="encrypting";
          shift;;
   se|es) ARGS="-seat";
	  TYPE="signing and encryption";
	  shift;;
esac

#
# Make sure all arguments are present
#

if [ "$TYPE" = "signing" -a $# -lt 3 ]
then
   usage $*
   exit 1
elif [ "$TYPE" != "signing" -a $# -lt 4 ]
then
   usage $*
   exit 1
fi

INPUT=$1
OUTPUT=$2
BOUNDARY=$3
shift
shift
shift

#
# These variables are used to echo a string without a newline
#

if [ $OS_TYPE = sol23 -o $OS_TYPE = sol24 ]
then
   ECHO=/usr/bin/echo
else
   ECHO=echo
fi

if [ $OS_TYPE = linux -o $OS_TYPE = sun ]
then
   DASHN="-n"
   BACKC=
else
   DASHN=
   BACKC="\c"
fi

#
# Run the command and save the status
#

$ECHO "pgp $ARGS $INPUT $*"
pgp $ARGS $INPUT $*
PGPSTAT=$?

#
# Allow user to confirm
#

$ECHO ""
$ECHO ""
ANS=""

while [ "$ANS" != "y" -a "$ANS" != "Y" -a "$ANS" != "n" -a "$ANS" != "N" ]
do
   $ECHO $DASHN "Was the $TYPE successful (Y/n)? $BACKC"
   read ANS
   test -z "$ANS" && ANS=y
done

if [ "$ANS" = "n" -o "$ANS" = "N" ]
then
   test $PGPSTAT -eq 0 && PGPSTAT=1
fi

if [ $PGPSTAT -ne 0 ]
then
   exit $PGPSTAT
fi

rm -f $OUTPUT $OUTPUT.head

#
# See if we're just signing
#

if [ "$TYPE" = "signing" ]
then

#
# Add content-type header
#
   $ECHO "Content-Type: multipart/signed;" >> $OUTPUT.head
   $ECHO " boundary=\"$BOUNDARY\";" >> $OUTPUT.head
   $ECHO " micalg=pgp-md5;" >> $OUTPUT.head
   $ECHO " protocol=application/pgp-signature" >> $OUTPUT.head

   $ECHO "--$BOUNDARY" >> $OUTPUT

#
# Add the original file
#

   cat $INPUT >> $OUTPUT
   $ECHO "" >> $OUTPUT
   $ECHO "--$BOUNDARY" >> $OUTPUT

#
# Add signature
#

   $ECHO "Content-Type: application/pgp-signature" >> $OUTPUT
   $ECHO "" >> $OUTPUT
   cat $INPUT.asc >> $OUTPUT
   $ECHO "" >> $OUTPUT
   $ECHO "--$BOUNDARY--" >> $OUTPUT

#
# We're encrypting
#

else

#
# Add content-type header
#

   $ECHO "Content-Type: multipart/encrypted;" >> $OUTPUT.head
   $ECHO " boundary=\"$BOUNDARY\";" >> $OUTPUT.head
   $ECHO " protocol=application/pgp-encrypted" >> $OUTPUT.head

   $ECHO "--$BOUNDARY" >> $OUTPUT

#
# Add the blank part
#

   $ECHO "Content-Type: application/pgp-encrypted" >> $OUTPUT
   $ECHO "" >> $OUTPUT
   $ECHO "--$BOUNDARY" >> $OUTPUT

#
# Add the encrypted file
#

   $ECHO "Content-Type: application/octet-stream" >> $OUTPUT
   $ECHO "" >> $OUTPUT
   cat $INPUT.asc >> $OUTPUT
   $ECHO "" >> $OUTPUT
   $ECHO "--$BOUNDARY--" >> $OUTPUT

fi

rm -f $INPUT.asc
