#!/usr/bin/perl

# Copyright 2008-2014 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.iso-8859-1") or die "couldn't open rank.txt.iso-8859-1: $!\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_unsorted = split(/ /,$rank);
        @msds_unsorted = split(/ /,$msd);
        @keys_unsorted = split(/ /,$key);

        for ($i = 0; $i <= $#ranks_unsorted; $i++)
        {
            $pos{$i} = $ranks_unsorted[$i];
        }

        $i = 0;
        foreach $pos ( sort { $pos{$a} <=> $pos{$b} } keys %pos )
        {
            $ranks[$i] = $ranks_unsorted[$pos];
            $msds[$i] = $msds_unsorted[$pos];
            $keys[$i] = $keys_unsorted[$pos];
            $i++;
        }

        $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 1 4    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-dc14.txt
3. fill in the hash values on the printout.
4. Bring the printout, a pen, and proof of identity to the keysigning.

For each participant:

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: _________________________________________________________ [ ]


FIN

#print "\n* * * * * * * * * * * * * * *    G R O U P    A    * * * * * * * * * * * * * * *\n";
foreach $number ( sort { $group_a{$a} <=> $group_a{$b} } keys %group_a)
{
    $rank{$number} =~ s/999//g;
    if ( $rank{$number} != "" )
    {
        print "\n#$number   $name{$number} (rank: $rank{$number})\n\n";
    }
    else
    {
        print "\n#$number   $name{$number}\n\n";
    }
    print "      [ ] Fingerprint(s) OK        [ ] ID OK\n\n";
    system ("gpg --homedir gnupg-dc14 --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//g;
#    print "\n#$number   $name{$number} (rank: $rank{$number})\n\n";
#    print "      [ ] Fingerprint(s) OK        [ ] ID OK\n\n";
#    system ("gpg --homedir gnupg-dc14 --fingerprint $key{$number} | grep -Ev '^sub|jpeg image'");
#}

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

exit(0);
