#!/usr/bin/perl -w

use strict;

my $htmlroot = "/home/dirson/public_html/buildinfo";

chdir "data/buildinfo";

mkdir $htmlroot unless -d $htmlroot;
open (SUMMARY, ">$htmlroot/index.html");

print SUMMARY <<EOH
<html>
<head>
<title>Build informations</title>
</head>

<body>
<h1>Build informations</h1>

<p>Build informations are currently generated using the
<tt>dh_buildinfo</tt> script from the <a
href="http://packages.debian.org/dh-buildinfo">dh-buildinfo</a>
package.  This is a temporary measure, as doing so for the whole
archive would add much too much size to it.  The best known idea till
now would be to have them uploaded together with .changes file, and
archived on buildd.debian.org.

<p>dh_buildinfo also has a <a
href="http://ydirson.free.fr/en/software/buildinfo.html" >homepage</a>.

<h2>some graphs</h2>

<ul>
<li><a
    href="npkg.eps" >number
    of packages</a> using the this tool in sid
<li><a
    href="nfiles.eps" >number
    of buildinfo files in the database</a>
</ul>

<p>Note that there were a number of bugs in the fetching script, which
caused a number of buildinfo files to be missed, and that those
bugfixes are reflected in the graphs.

<h2>extracted data</h2>

<ul>
EOH
  ;

my %categs = ('arch' => 'Architecture-dependant build-deps',
	      'arch-closure' => 'Architecture-dependant closure',
	      'indep' => 'Architecture-independant build-deps',
	      'indep-closure' => 'Architecture-independant build-deps closure',
	      'b-ess' => 'Build-essential packages',
	      'b-ess-closure' => 'Build-essential closure',
	      'ess' => 'Essential packages',
	      'ess-closure' => 'Essential closure');

foreach my $package (<*>) {
  my $packagedir="$htmlroot/$package";
 
  my %data = ();
  foreach my $datafile (<$package/*>) {
    $datafile =~ m|^\Q$package/${package}\E_(.*)_(.*)$| or next;
    my ($vers,$arch) = ($1,$2);
    $data{$vers} = { archs => {} } unless defined $data{$vers};
    $data{$vers}->{archs}->{$arch} = {};

    open DATA, $datafile;
    my $categ = 'ess';
    foreach my $dataline (<DATA>) {
      if ($dataline =~ '^ Essential packages closure:') { $categ = 'ess-closure'; next; }
      if ($dataline =~ '^ Build-Essential packages:') { $categ = 'b-ess'; next; }
      if ($dataline =~ '^ Build-Essential closure:') { $categ = 'b-ess-closure'; next; }
      if ($dataline =~ '^ Declared Arch-indep Build-Dependencies:') { $categ = 'indep'; next; }
      if ($dataline =~ '^ Arch-indep Build-Dependencies closure:') { $categ = 'indep-closure'; next; }
      if ($dataline =~ '^ Declared Arch-dependent Build-Dependencies:') { $categ = 'arch'; next; }
      if ($dataline =~ '^ Arch-dependent Build-Dependencies closure:') { $categ = 'arch-closure'; next; }
      next if $dataline =~ /^ /;
      my ($deppkg,$depvers) = split (/\s+/, $dataline);
      $data{$vers}->{archs}->{$arch}->{$deppkg} = $depvers;

      push (@{$data{$vers}->{packages}->{$categ}}, $deppkg)
	unless grep { $_ eq $deppkg } @{$data{$vers}->{packages}->{$categ}};
    }
    close DATA;
  }

  my $datasize = keys %data;
  if ($datasize == 0) {
    print SUMMARY "<li>$package";
    if (-r "$package/ERROR") {
      open (ERROR, "<$package/ERROR");
      print SUMMARY ' (', <ERROR>, ')';
      close ERROR;
    }
    print SUMMARY "\n";
  } else {
    print SUMMARY "<li><a href=\"$package/\">$package</a> ($datasize)";

    if (-r "$package/WARNING") {
      open (WARNING, "<$package/WARNING");
      print SUMMARY ' (<span style="color:red">', <WARNING>, '</span>)';
      close WARNING;
    }

    mkdir $packagedir unless -d $packagedir;
    unlink "$packagedir/data";
    symlink "../../../data/buildinfo/$package", "$packagedir/data";
    open (PKGSUMMARY, ">$packagedir/index.html");

    print PKGSUMMARY <<EOH
<html>
<head>
<title>Build informations for $package</title>
</head>

<body>
<h1>Build informations for $package</h1>
<ul>
EOH
  ;

    foreach my $vers (keys %data) {
      print PKGSUMMARY "<h1><a href=\"data/\">$package $vers</a></h1>\n";
      # list is hardcoded to force ordering
      foreach my $categ ('arch', 'arch-closure', 'indep', 'indep-closure',
			 'b-ess', 'b-ess-closure', 'ess', 'ess-closure') {
	print PKGSUMMARY '<h2>', $categs{$categ}, "</h2>\n";
	print PKGSUMMARY "<table border>\n<tr><th>";
	foreach my $deppkg (@{$data{$vers}->{packages}->{$categ}}) {
	  print PKGSUMMARY "<th>$deppkg";
	}
	print PKGSUMMARY "</tr>\n";
	foreach my $arch (keys %{$data{$vers}->{archs}}) {
	  print PKGSUMMARY "<tr><th>$arch";
	  foreach my $deppkg (@{$data{$vers}->{packages}->{$categ}}) {
	    if (defined $data{$vers}->{archs}->{$arch}->{$deppkg}) {
	      print PKGSUMMARY "<td>$data{$vers}->{archs}->{$arch}->{$deppkg}";
	    } else {
	      print PKGSUMMARY "<td>N/A";
	    }
	  }
	  print PKGSUMMARY "</tr>\n";
	}
	print PKGSUMMARY "</table>\n";
      }
    }
  }
}

print SUMMARY "</ul>\n";
close SUMMARY;
