#!/usr/bin/perl -w

use strict;
use Getopt::Std;

my %options;
getopts('k:', \%options);
my $keyring = $options{k} ? "--no-default-keyring --keyring=$options{k}" : "";
my %UID;

sub get_uid {
	my $key = shift;
	return $UID{$key} if $UID{$key};
	open G, '-|', qw/gpg --list-key --fixed-list-mode --with-colon/, $keyring, $key or die "gpg: $!";
	while(<G>) {
		next unless /^uid:[-qmfue]::::\d*::[\dA-F]*::(.+):$/;
		my $name = $1;
		$name =~ s/</</g;
		$name =~ s/>/>/g;
		$name =~ s/\@/@/g;
		close G;
		return $UID{$key} = $name;
	}
	close G;
}

sub uid_link {
	my $key = shift;
	#$key =~ /^([\dA-F]{2})/;
	#return "$key";
	return "$key";
}

for my $file (@ARGV) {
	#print STDERR "$file...\n";
	open F, "$file" or die "$file: $!";
	open H, ">$file.html" or die "$file.html: $!";
	print H <<EOF;

$file

EOF
	while(<F>) {
		next if /^(This individual|report at)/;
		s/([\dA-F]{8})$/uid_link($1)." ".get_uid($1);/e;
		print H;
	}
	print H "
\n\n";
}
