#!/usr/athena/bin/perl

BEGIN { push(@INC, "/afs/net.mit.edu/project/pks/perl-mysql/perllib"); }

use PGP::Keyring;
use IO::File;
use Mmap;

$ringname = shift(@ARGV);

$file = new IO::File($ringname) || die "Couldn't open keyring $ringname: $!\n";

$mmap = mmap($ringbuf, 0, PROT_READ, MAP_SHARED, $file);

$ring = new PGP::Keyring(\$ringbuf);

while (1) {
    $packet = $ring->NextPacket() || last;
    $ctag = $packet->ctag;

    if ($ctag == 6) {
	print "pubkey\n";
    } elsif ($ctag == 14) {
	print "subkey\n";
    } elsif ($ctag == 13) {
	print "userid\n";
    } elsif ($ctag == 2) {
	$sig = new PGP::Certificate::Signature($packet);
	$sigtype = $sig->sigtype;
	if ($sigtype == 0x20) {
	    print "pubkey revocation signature\n";
	} elsif ($sigtype == 0x1f) {
	    print "direct-key signature\n";
	} elsif ($sigtype >= 0x10 && $sigtype <= 0x13) {
	    print "userid signature\n";
	} elsif ($sigtype == 0x28) {
	    print "subkey revocation signature\n";
	} elsif ($sigtype == 0x18) {
	    print "subkey binding signature\n";
	} else {
	    print "type $sigtype signature\n";
	}
    } else {
	print "ctag $ctag\n";
    }

    pp($ {$packet->packetref});
}

sub pp {
    my ($packet) = @_;

    print "  length = ",length($packet),"\n";

    while ($packet) {
	$line = substr($packet,0,16);
	substr($packet,0,16) = "";
	printf "  %-39s  ", join(" ",unpack("H4"x8, $line));
	$line =~ tr/\000-\037\177-\377/./;
	printf "%s %s\n", unpack("a8a8",$line);
    }
}
