#!/bin/ksh -p

: ${AUTHOR:="Jack Repenning"}
: ${ORGANIZATION:="Silicon Graphics, Inc."}


#
# Debugging boilerplate
#
#	All comments in this file are intended for the customizer.
#	Read them all!
#

flags=?hdnvct
usage="Usage: ${0##*/} [-$flags]"

if [ "$DEBUG" ] ; then
	set -$DEBUG
	: Debug enabled by envariable
fi

Usage () {
        echo "$usage"
        echo "	-?, -h	Help text (this stuff)."
        echo "	-c	Cat request message, rather than mailing."
        echo "	-d	Debug execution (lots of output)."
        echo "	-n	No-execute (echos what it would have done)."
        echo "	-t	Test run (send mail to self)."
        echo "	-v	Verbose (echos before doing things)."
}

Log () {
        [[ ! -z "$verbose" || ! -z "$noexecute" ]] && echo >&2 "$@"
        : always return true
}

Do () {
        Log "$@"
        [[ -z "$noexecute" ]] && "$@"
        typeset evalstatus=$?
        [[ ! -z "$noexecute" || $evalstatus = 0 ]]
}

DO () {
        Log "$@"
        "$@"
        typeset evalstatus=$?
        [[ $evalstatus = 0 ]]
}

# Everyone needs a temp file!  Name it here, so CleanUp can clean it up.
# Don't create yet, so it can be created as a directory if appropriate.
tmpfile=/usr/tmp/${0##*/}.$$

cleanuplist="$tmpfile $tmpfile.asc /usr/tmp/${PWD##*/}.md5"

CleanUp () {
        : Add your own clean-up actions here

        Do /bin/rm -fr $cleanuplist

        Do /sbin/stty sane
}

# Clean up in cases of "normal" aborts, leave really violent aborts
# along (so intermediates, such as they are, can be used for debugging)
SigList="EXIT HUP INT QUIT TERM"
trap "trap - $SigList; CleanUp" $SigList



# Note: Betsi depends on having the word "certify" in the subject line

MAIL=betsifyMail
betsifyMail () {
        Do Mail -s "Please certify ${PWD##*/}" certify@bellcore.com
}
betsifyCheckMail () {
        Do Mail -s "Please certify ${PWD##*/}" $USER
}
show () {
        Do cat
}
verbose=
noexecute=
while getopts $flags flag; do
        case $flag in
        c)
                MAIL=show
                ;;
	d)
		export DEBUG=xv
		set -$DEBUG
		: Debug enabled by command line
	        ;;
	h | \?)
	        Usage >&2
                exit 0
	        ;;
	n)
		noexecute=-n
	        ;;
        t)
                MAIL=betsifyCheckMail
                ;;
	v)
                verbose=-v
                ;;
	esac
done

# Now, flush the args just consumed
while ((OPTIND > 1)) ; do
        shift
        ((OPTIND-=1))
done


#
# Change the limits here to match your parameter list length
# requirements.
#
if (( $# < 0 )) ; then
        Usage >&2
        exit 1
fi

#
# End of Debugging boilerplate
# Your stuff goes here
#

if (( $# == 0 )) ; then
        set -- .
fi

read user?"PGP user ID to use [$USER]? "
[[ -z $user ]] && user=$USER

stty -echo
read pp?"PGP passphrase? "
stty echo

for dir; do
        (
                DO cd $dir

                Do rm -f Betsi.Certificate Betsi.Request \
			*.Betsi *.asc *.md5 *~ ./#*

                if [ ! -f *.key ] ; then

                        Do pgp -kxa "$user" $USER

                        Do mv $USER.asc $USER.key

                fi

#                for f in *; do
#
#                        PGPPASSFD=0 DO pgp +batchmode -bfast -u "$user" +clear > $f.asc <<EOF
#$pp
#$(<$f)
#EOF
#
#		done

                (
                        DO echo Author Name: $AUTHOR
                        DO echo Author Organization: $ORGANIZATION
                        DO echo Hash function: MD5
                        DO echo Date of certificate creation: $(DO date +%D)
                        DO echo
                        DO md5sum *

                ) > $tmpfile

                PGPPASSFD=0 Do pgp +batchmode -fast -u "$user" +clear > $tmpfile.asc <<EOF
$pp
$(<$tmpfile)
EOF

                Do $MAIL < $tmpfile.asc

                DO mv $tmpfile.asc ${PWD##*/}.md5
        )
done



#	If you use Emacs, and if you have sh-mode available, then this
#	will turn it on for this file.

#	Local variables:
#	eval: (if (fboundp 'sh-mode) (sh-mode))
#	compile-command: "betsify -n "
#	end:
