#!/bin/bash

# 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

set -e
set -u
#set -x

shopt -s nocasematch

TOP_RANK=12

declare data
declare from
declare key
declare keys
declare msd
declare rank
declare group
declare msg_id
declare name
declare tr_class
declare regex
declare eof

declare -i count=0

declare -r regex_1="^(.*);(.*);(.*);(.*)$"

cat > names.html << FIN
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>DebConf14 Keysigning Party Names</title>
  <link type="text/css" rel="stylesheet" href="style.css">
  <link rel="shortcut icon" href="http://www.debian.org/favicon.ico">
 </head>

 <body>

  <div align="center">
   <a href="http://www.debian.org/"><img src="http://www.debian.org/logos/openlogo-nd-50.png" border="0" hspace="0" vspace="0" alt=""></a>
   <a href="http://www.debian.org/"><img src="http://www.debian.org/Pics/debian.png" border="0" hspace="0" vspace="0" alt="Debian Project"></a>
  </div>

  <br />

  <table class="reddy" width="100%">
   <tr>
    <td class="reddy"><img src="http://www.debian.org/Pics/red-upperleft.png" align="left" border="0" hspace="0" vspace="0" alt="" width="15" height="16"></td>

    <td rowspan="2" class="reddy">DebConf14 Keysigning Party</td>
    <td class="reddy"><img src="http://www.debian.org/Pics/red-upperright.png" align="right" border="0" hspace="0" vspace="0" alt="" width="16" height="16"></td>
   </tr>

   <tr>
    <td class="reddy"><img src="http://www.debian.org/Pics/red-lowerleft.png" align="left" border="0" hspace="0" vspace="0" alt="" width="16" height="16"></td>
    <td class="reddy"><img src="http://www.debian.org/Pics/red-lowerright.png" align="right" border="0" hspace="0" vspace="0" alt="" width="15" height="16"></td>
   </tr>
  </table>

  <h1>DebConf14 Keysigning Party Names</h1>

  <center>
   <table border="0">
    <tr>
     <th>#</th>
     <th>Name</th>
     <th>Key IDs</th>
     <th>MSD</th>
     <th>Rank</th>
    </tr>

FIN

data=$( < names.txt )
msd_data=$( < msd.txt )

while read line
do
    if [[ "$line" =~ $regex_1 ]]
    then
        from="${BASH_REMATCH[1]}"
        keys="${BASH_REMATCH[2]}"
        msg_id="${BASH_REMATCH[3]}"
        url="${BASH_REMATCH[4]}"
    else
        echo error: \"$line\"
        exit 1
    fi

    if [ $(( ++count % 2 )) -eq 0 ]
    then
        tr_class="even"
    else
        tr_class="odd"
    fi

    name=${from%% <*}
    if [ -n "$url" ]
    then
        name="<a href=\"$url\">$name</a>"
    fi

    msd=""
    msd=$( 
        for key in $keys
        do
            count2=0
            regex="^ *[0-9A-F]{8} $key *(.*)$"
            while :
            do
                count2=$(( count2 + 1 ))
                if ! read msd_line
                then
                    echo "&nbsp;"
                    break
                fi

                if [[ "$msd_line" =~ $regex ]]
                then
                    if [ "$count2" -le $TOP_RANK ]
                    then
                        echo "<font color=red>${BASH_REMATCH[1]}</font>"
                    else
                        echo ${BASH_REMATCH[1]}
                    fi
                    break
                fi
            done <<< "$msd_data"
        done
    )
    msd=${msd//$'\n'/<br \/>}

    rank=""
    rank=$(
        for key in $keys
        do
            count2=0
            regex="^ *[0-9A-F]{8} $key *(.*)$"
            while :
            do
                count2=$(( count2 + 1 ))
                if ! read msd_line
                then
                    echo "&nbsp;"
                    break
                fi

                if [[ "$msd_line" =~ $regex ]]
                then
                    if [ "$count2" -le $TOP_RANK ]
                    then
                        echo "<font color=red>$count2</font>"
                    else
                        echo $count2
                    fi
                    break
                fi
            done <<< "$msd_data"
        done
    )
    rank=${rank//$'\n'/<br \/>}

    keys=$( 
        for key in $keys
        do
            echo "<a href=\"http://pgp.cs.uu.nl/stats/$key.html\">$key</a><br />"
        done
    )
    keys=${keys//$'\n'/$'\n'       }

    echo "    <tr class=\"$tr_class\">
     <td class=\"sid\">$count</td>
     <td class=\"sid\">$name</td>
     <td class=\"sid\">
       $keys
     </td>
     <td align=right>$msd</td>
     <td align=right>$rank</td>
    </tr>
"
done <<< "$data" >> names.html

cat >> names.html << FIN
   </table>
  </center>

  <div class="footer">
   <br />
   Names last updated: $( date -u )
   <br />
   MSD last updated: $( date -ur msd.txt )
   <br />
   The MSD and rank values were calculated with keyanalyze of the <a href="http://packages.qa.debian.org/s/signing-party.html">signing-party</a> package<br />
   Copyright (C) 2014 <a href="http://www.spi-inc.org/">Software in the Public Interest</a> and others;
   see <a href="http://www.debian.org/license">license terms</a>
  </div>

 </body>
</html>
FIN

exit 0
