#!/usr/bin/perl -w
#
# $Id: pgp2schnippsel.pl,v 1.3 2001/11/12 21:50:32 cord Exp $
#
# DESCRIPTION: pgp2schnippsel - convert a pgp-fingerprint to a
#   key-signing-party-format
#
# Copyright (C) 2001,2005 Cord Beermann
#
# URL: http://cord.de/tools/
#
# AUTHOR: Cord Beermann (Cord@Wunder-Nett.org)
#
# Syntax-for PostScript-Output taken from 'gpg-key2ps' by Simon Richter
#   (Simon.Richter@phobos.fachschaften.tu-muenchen.de)
# Bugfix-patch for new gnupg by Holger van Lengerich (holger@van-lengerich.de)

# Todo./Shortcomings/Bugs
# Sanity-Checks for User-Input (does font exist? is size numeric?)
# Switching between letter and A4
# Switching between portrait and landscape
# Check for long lines.

#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.

# (If you modify and want to publish it under the name 'Calamaris', please ask
# me. I don't want to confuse the 'audience' with many different versions of
# the same name and/or Version number. (This is not part of the license, it
# is only a favour i asked of you.))

# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.

# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.

# A Perl script is "correct" if it gets the job done before your boss fires
# you.
#   -- 'Programming Perl Second Edition'
#       by Larry Wall, Tom Christiansen & Randal L. Schwartz

require 5.002;
use vars qw($opt_C $opt_f $opt_h $opt_s $opt_t $opt_V);
use Getopt::Std;

getopts('Cf:hs:t:V');

$VERSION='pgp2schnippsel.pl $Revision: 1.3 $';

$COPYRIGHT='Copyright (C) 2001 Cord Beermann.
pgp2schnippsel comes with ABSOLUTELY NO WARRANTY. It is free software, and you
are welcome to redistribute it under certain conditions. See source for details.
Homepage: http://cord.de/tools/';

# Usage: pgp -kvc or gpg --fingerprint |pgp2schnippsel > print.ps
# (tested with pgp 2.6.3ia and gnupg 1.0.6 and 1.4.1)

$USAGE='Usage: (pgp -kvc|gpg --fingerprint) | ' . $0 . ' [switches]

-f text    font (sets Font-Type. Defaults to Times-Roman.)
-s n	   size (sets Font-Size. Defaults to 6)

-t text	   title (The Title for the Output)

-C         copyright (prints the copyright)
-h         help    (prints out this message)
-V         Version (prints version-info)
';

print("$USAGE\n\n$COPYRIGHT\n\n") if ($opt_h);
print("$COPYRIGHT\n\n") if ($opt_C);
print("$VERSION\n\n$COPYRIGHT\n\n") if ($opt_V);

exit 0 if (($opt_h) or ($opt_C) or ($opt_V));

$opt_f = 'Times-Roman' unless ($opt_f);
$opt_s = '6' unless ($opt_s);
$opt_t = 'Fingerprint' unless ($opt_t);

print "%!PS-Adobe-3.0\n";
print "%%BoundingBox: 0 0 594 842\n";
print "%%Title: $opt_t\n";
print "%%Creator: $0 " . '$Version$' . "\n";
print "%%CreationDate: " . localtime() . "\n";
print "%%Pages: 1\n";
print "%%EndComments\n";

print "\n";

print "%%Page: 1 1\n";

print "\n";

print "/$opt_f findfont $opt_s scalefont setfont\n";

print "\n";

print "/newline {\n";
print "\t/y y $opt_s sub def\n";
print "} def\n";

print "\n";

print "/pub {\n";
print "\t2 y moveto (pub) show\n";
printf "\t%d y moveto show show (/) show show\n", $opt_s*2+2;
printf "\t%d y moveto show\n", $opt_s*10+5;
printf "\t%d y moveto show\n", $opt_s*15+6;
print "\tnewline\n";
print "} def\n";

print "\n";

print "/fpr {\n";
printf "\t%d y moveto (Key fingerprint = ) show show\n", $opt_s*2+2;
print "\tnewline\n";
print "} def\n";

print "\n";

print "/uid {\n";
print "\t2 y moveto (uid) show\n";
printf "\t%d y moveto show\n", $opt_s*15+6;
print "\tnewline\n";
print "} def\n";

print "\n";

print "/sbk {\n";
print "\t2 y moveto (sub) show\n";
printf "\t%d y moveto show show (/) show show\n", $opt_s*2+2;
printf "\t%d y moveto show\n", $opt_s*10+5;
print "\tnewline\n";
print "} def\n";

print "\n";

print "/sig {\n";
print "\t2 y moveto (sig) show\n";
printf "\t%d y moveto show\n", $opt_s*5+2;
printf "\t%d y moveto show\n", $opt_s*10+5;
printf "\t%d y moveto show\n", $opt_s*16+6;
print "\tnewline\n";
print "} def\n";

print "\n";

$count=0;
print "/key {\n";
while (<>) {
  chomp;
  if (m#^pub\s+(\d+)(\w)?/([\dA-F]+)\s+(\d+[\/\-]\d+[\/\-]\d+)\s+(.+)$#) {
    print "\t($5) ($4) ($3) ($2) ($1) pub\n";
    $count++;
  } elsif (m#^pub\s+(\d+)(\w)?/([\dA-F]+)\s+(\d+[\/\-]\d+[\/\-]\d+)$#) {
    print "\t( ) ($4) ($3) ($2) ($1) pub\n";
    $count++;
  } elsif (m#^\s+Key\s+fingerprint\s+=\s+([\dA-F\s]+)$#) {
    print "\t($1) fpr\n";
    $count++;
  } elsif (m#^sub\s+(\d+)(\w)/([\dA-F]+)\s+(\d+[\/\-]\d+[\/\-]\d+)$#) {
    print "\t($4) ($3) ($2) ($1) sbk\n";
    $count++;
  } elsif (m#^(uid)?\s+(.+)$#) {
    print "\t($2) uid\n";
    $count++;
  } elsif (m#^sig\s+([\dA-F\s]+)\s+(\d+[\/\-]\d+[\/\-]\d+)\s+(.+)$#) {
    print "\t($3) ($2) ($1) sig\n";
    $count++;
  } elsif (m#^$#) {
    print "\tnewline\n";
    $count++;
  } else {
    print STDERR "$0: ignored input line: $_\n";
  }
}
print "\tnewline\n";
$count++;
print "} def\n";

print "\n";

print "/numlines $count def\n";
print "/num 802 $opt_s div numlines div def\n";

print "\n";

print "/column {\n";
print "\t/y 802 def\n";
print "\t1 1 num {\n";
printf "\t\t/upper y %d add def\n", $opt_s*2 ;
print "\t\tkey\n";
printf "\t\t/lower y %d add def\n", $opt_s*2 ;
print "\t\t0 upper 277 upper 277 lower 0 lower 0 upper moveto lineto lineto lineto lineto stroke\n";
print "\t} for\n";
print "} def\n";

print "\n";

print "20 0 translate\n";
print "column\n";
print "277 0 translate\n";
print "column\n";

print "\n";

print "showpage\n";

print "\n";

print "%%Trailer\n";
print "%%EOF\n";
