#!/bin/sh
# Prepare a printable list of keysigning participants.
# Useful for the party organiser.
#
# $Id$
#
# License: GPLv2 or later
# Copyright Philippe Teuwen <phil a teuwen o org> 2008

usage() {
    cat <<- EOF
		Usage: $0 [-a HASHES|--algorithm HASHES] input output datestring organizer title

		Use a single hyphen-minus (-) in place of input to read from STDIN
		(resp. of output to write to STDOUT).
		Example:
		    echo 9AD7E3DB 54C12701 |\\
		    $0 - ksp-file.txt "20080222 1100" "My Name <my.name@my.mail>" "my party 08"
	EOF
    exit ${1:-0}
}


# Handle options and arguments
##############################

# Use getopt to validate and normalize options and arguments,
# then reinject them as main arguments
OPTS=$(getopt -o a:h -l algorithm:,help -n $0 -- "$@") || usage 1
eval set -- "$OPTS"

# Default options
algos="SHA256,RIPEMD160"

# Parse options
while [ "$1" != -- ]; do
    case "$1" in
        -a|--algorithm) algos="$2"; shift 2;;
        -h|--help) usage 0; shift;;
        *) usage 1;;
    esac
done

# Get rid of the '--' left before the arguments
shift

# Five arguments should remain
[ $# -eq 5 ] || usage 1
algos=$(echo "$algos" | tr '[:lower:]' '[:upper:]')
halgos="$algos"

algos=$(echo "$algos" | tr ',' ' ')
for algo in $algos; do
	# Ensure this is a valid algo
	${GNUPGBIN:-gpg} --print-md "$algo" /dev/null >/dev/null || exit $?
done

input="$1"
[ "$input" = "-" ] && input="";
output="$2"
date="$3"
org="$4"
title=$(echo "$5"|tr a-z A-Z|sed 's/\(.\)/\1 /g')

[ "$output" = - ] && output=/path/to/ksp-file.txt || { exec > "$output"; }

# Date of event
LC_ALL=C date --date="$date" +"%A, %B %e, %Y;  %H:%M"
# Organiser contact
printf "%80s\n\n\n" "$org"
# Title
printf "%*s\n\n" $(((72+$(echo "$title"|wc -c))/2)) "$title"
# Header
cat <<EOF
                     List of Participants  (v 1.0)


Here's what you have to do with this file:

(1) Print this UTF-8 encoded file to paper.

(2) Compute this file's $(echo "$halgos" | sed -re 's/,([^,]+)$/ and \1/; s/,/& /g') checksum$([ "${algos#* }" = "$algos" ] || echo s).

EOF
for algo in $algos; do
	printf "      ${GNUPGBIN:-gpg} --print-md %s %s\n" "$algo" "$output"
done
cat <<EOF

(3) Fill in the hash values on the printout.

(4) Bring the printout, a pen, and proof of identity to the key signing party
    (and be on time!).

EOF
for algo in $algos; do
	echo
	case "$algo" in
		MD5) cat <<- EOF
			MD5 Checksum:  __ __ __ __ __ __ __ __    __ __ __ __ __ __ __ __           [ ]
		EOF
		;;
		SHA1) cat <<- EOF
			SHA1 Checksum: ____ ____ ____ ____ ____    ____ ____ ____ ____ ____         [ ]
		EOF
		;;
		RIPEMD160) cat <<- EOF
			RIPEMD160 Checksum: ____ ____ ____ ____ ____    ____ ____ ____ ____ ____    [ ]
		EOF
		;;
		SHA256) cat <<- EOF
			SHA256 Checksum: ____ ____   ____ ____   ____ ____   ____ ____

			                 ____ ____   ____ ____   ____ ____   ____ ____              [ ]
		EOF
		;;
		SHA384) cat <<- EOF
			SHA384 Checksum: ____ ____   ____ ____   ____ ____   ____ ____

			                 ____ ____   ____ ____   ____ ____   ____ ____

			                 ____ ____   ____ ____   ____ ____   ____ ____              [ ]
		EOF
		;;
		SHA512) cat <<- EOF
			SHA512 Checksum: ____ ____   ____ ____   ____ ____   ____ ____

			                 ____ ____   ____ ____   ____ ____   ____ ____

			                 ____ ____   ____ ____   ____ ____   ____ ____

			                 ____ ____   ____ ____   ____ ____   ____ ____              [ ]
		EOF
		;;
		SHA224) cat <<- EOF
			SHA224 Checksum: ____ ____   ____ ____   ____ ____   ____ ____

			                 ____ ____   ____ ____   ____ ____                          [ ]
		EOF
		;;
	esac
done
printf '\n\n'


k=0;
options='--list-options no-show-photos,no-show-uid-validity,no-show-keyring'
cat $input | \
while read i; do
    k=$(($k+1));
    printf "\n%03d  [ ] Fingerprint OK        [ ] ID OK\n" $k;
	LANGUAGE=en ${GNUPGBIN:-gpg} $options --display-charset utf-8 --fingerprint --list-keys -- $i \
		| grep -ve "^sub" -e '^uid *\[jpeg image of size ';
	echo "_______________________________________________________________________________"
done


