#!/usr/bin/perl

# Copyright 2008 Anibal Monsalve Salazar <anibal@debian.org>
#
# 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.
#
# 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

unlink "rank.tmp";
open(IN,"rank.txt") or die "couldn't open rank.txt: $!\n";
open(OUT,"| sort -g > rank.tmp") or die "couldn't run sort: $!\n";
while ($line=<IN>)
{
    if ( $line =~ /^(.*);(.*);(.*);(.*);(.*)$/ )
    {
        $rank = $1;
        $msd = $2;
        $key = $3;
        $number = $4;
        $name = $5;
    }
    else
    {
        print "error: invalid line: $line\n";
        exit(1);
    }

    if ( $rank =~ / / )
    {
        @ranks = split(/ /,$rank);
        @msds = split(/ /,$msd);
        @keys = split(/ /,$key);
        
        if ( $#ranks != 1 )
        {
            print "error: only processes a max of two keys: $line\n";
        }

        if ( $ranks[0] > $ranks[1] )
        {
            $tmp = $ranks[0];
            $ranks[0] = $ranks[1];
            $ranks[1] = $tmp;

            $tmp = $msds[0];
            $msds[0] = $msds[1];
            $msds[1] = $tmp;

            $tmp = $keys[0];
            $keys[0] = $keys[1];
            $keys[1] = $tmp;

            $rank = join(' ', @ranks);
            $msd = join(' ', @msds);
            $key = join(' ', @keys);
        }
    }

    print OUT "$rank;$msd;$key;$number;$name\n";
}
close(OUT);
close(IN);

$count = 0;
open(IN,"rank.tmp") or die "couldn't open rank.tmp: $!\n";
while ($line=<IN>)
{
    $line =~ /^(.*);(.*);(.*);(.*);(.*)$/;
    $number = $4;
    $rank{$number} = $1;
    $msd{$number} = $2;
    $key{$number} = $3;
    $name{$number} = $5;

    if ( ++$count % 2 eq 1 )
    {
        $group_a{$number} = $number;
    }
    else
    {
        $group_b{$number} = $number;
    }

}
close(IN);

print <<FIN;
                    Anibal Monsalve Salazar <anibal\@debian.org>

                      D E B C O N F 8    K E Y S I G N I N G

                           List of Participants  (v 1.0)


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

1. Print this file to paper.
2. Compute this file's SHA256 checksum: sha256sum ksp-dc8.txt
3. fill in the hash values on the printout.
4. Bring the printout, a pen, and proof of identity to the keysigning.
5. Look for participants in your assigned group. 

For each participant in your group: 

1. Compare the hash you computed with the other participant.
2. Ask if the other participant's gpg fingerprint on the hardcopy is correct.
3. Verify each other's identity by checking preferably a passport.
4. If you are satisfied with the identification, mark on your hardcopy that
   the other participant's gpg fingerprint is correct and has been identified. 


SHA256 Checksum: _________________________________________________________ [ ]


* * * * * * * * * * * * * * *    G R O U P    A    * * * * * * * * * * * * * * *
FIN

foreach $number ( sort { $group_a{$a} <=> $group_a{$b} } keys %group_a)
{
    $rank{$number} =~ s/999//;
    print "\n#$number   $name{$number} (rank: $rank{$number})\n\n";
    print "      [ ] Fingerprint(s) OK        [ ] ID OK\n\n";
    system ("gpg --homedir gnupg-dc8 --fingerprint $key{$number} | grep -Ev '^sub|jpeg image'");
}

print "\n* * * * * * * * * * * * * * *    G R O U P    B    * * * * * * * * * * * * * * *\n";
foreach $number ( sort { $group_b{$a} <=> $group_b{$b} } keys %group_b)
{
    $rank{$number} =~ s/999//;
    print "\n#$number   $name{$number} (rank: $rank{$number})\n\n";
    print "      [ ] Fingerprint(s) OK        [ ] ID OK\n\n";
    system ("gpg --homedir gnupg-dc8 --fingerprint $key{$number} | grep -Ev '^sub|jpeg image'");
}

system ("sha256sum ksp-dc8.txt > ksp-dc8.txt.sha256");
system ("bzip2 -9 --stdout gnupg-dc8/pubring.gpg > ksp-dc8.gpg.bz2");
system ("sha256sum ksp-dc8.gpg.bz2 > ksp-dc8.gpg.bz2.sha256");
system ("gpg --default-key 0x1880283c --clearsign ksp-dc8.txt.sha256");
system ("gpg --default-key 0x1880283c --clearsign ksp-dc8.gpg.bz2.sha256");

exit(0);
