#!/usr/bin/perl
#
# Generate a release table row for each bundle to be released.
# We assume we want the most recent version of anything in the dist directory on minotaur.
# Run from the aries/site/trunk/content/downloads directory
# Usage: perl $this_dir/create_modules_table.pl > modules_table.txt; cat currentrelease.mdtext_body modules_table.txt > currentrelease.mdtext
#

use LWP::Simple;
use HTML::TokeParser;

# Distribution directories
$cgi = "http://www.apache.org/dyn/closer.cgi/aries/";
$dist = "http://www.apache.org/dist/aries/";

#HTML fragments
$open_td = "<td class=\"confluenceTd\"\>";
$close_td = "</td>";
$open_th = "<th class=\"confluenceTh\"\>";
$close_th = "</th>";
$open_tr = "<tr>";
$close_tr = "</tr>";
$ar = "<A href=\"";
$c_jar = "\">jar</A>";
$c_zip = "\">zip</A>";
$c_asc = "\">asc</A>";
$c_md5 = "\">md5</A>";
$c_sha1 = "\">sha1</A>";

my $data = get($dist);
my $html = HTML::TokeParser->new(\$data);

my @hrefs = ();
my @versions = ();

my %links = ();
my @archives = ();

while (my $token = $html->get_tag("a")) {
	my $href = $token->[1]{href} || "-";
# TODO eba-maven plugin and samples 
# This pattern doesn't catch blueprint-parser but it's not an independent bundle so that's a good thing
# Special case util-r42 and util-parent so we don't list them - assume anything with dashes isn't a real bundle	
 	if ($href =~ /(org.apache.aries.([^\.-]*)(\..*)*)-([0-9\.]+)(\.jar|-sources.jar|-source-release.zip)$/) {
		my($component) = $2;
		my($bundle) = $1;
		my($version) = $4;
		my($extension) = $5;
		# Only take the highest version 
		if ($version > $links{$component}{$bundle}) {
			 $links{$component}{$bundle}=$version;
		}
		# Make a note of the link so we know what sort of archives we have
		push(@archives,$href);
	}
}

print "<!-- This page can be automatically generated from the dist directory using the create_modules_table.pl script -->\n"; 
print '<table class="confluenceTable">';
print $open_tr.$open_th." Sub project ".$close_th.$open_th." Module ".$close_th.$open_th." Version ".$close_th.$open_th." Binary ".$close_th.$open_th." Source ".$close_th.$open_th." Buildable src ".$close_th.$open_th." Comments ".$close_th.$close_tr."\n"; 

for $component ( sort keys %links ) {
# Don't list all the sample bundles here
    if (!($component =~ /samples/)) {
	print_component_header();
	for $bundle ( sort keys %{ $links{$component} } ) {
		$version = $links{$component}{$bundle};
		print_table_row();
	    }
        print "\n";
    }
}

print "</table>";

sub print_component_header {
    print $open_tr.$open_td." ".$component." ".$close_td.$open_td.$close_td.$open_td." - ".$close_td.$open_td." - " .$close_td.$open_td." - ".$close_td.$open_td." - ".$close_td.$open_td.$close_td.$close_tr."\n";
}


sub print_table_row {

    $jar = $bundle."-".$version.".jar";
    $asc = $bundle."-".$version.".jar.asc";
    $md5 = $bundle."-".$version.".jar.md5";
    $sha1 = $bundle."-".$version.".jar.sha1";

    $s_jar = $bundle."-".$version."-sources.jar";
    $s_asc = $bundle."-".$version."-sources.jar.asc";
    $s_md5 = $bundle."-".$version."-sources.jar.md5";
    $s_sha1 = $bundle."-".$version."-sources.jar.sha1";

    $bs_zip = $bundle."-".$version."-source-release.zip";
    $bs_asc = $bundle."-".$version."-source-release.zip.asc";
    $bs_md5 = $bundle."-".$version."-source-release.zip.md5";
    $bs_sha1 = $bundle."-".$version."-source-release.zip.sha1";

	if ($jar ~~ @archives) {
    $line1 = $ar.$cgi.$jar.$c_jar.
             " (".
             $ar.$dist.$asc.$c_asc.
             ", ".
             $ar.$dist.$md5.$c_md5.
             ", ".
             $ar.$dist.$sha1.$c_sha1.
             ")";
    } else {
      $line1 = " - ";
    }

	if ($s_jar ~~ @archives) {
    $line2 = $ar.$cgi.$s_jar.$c_jar.
             " (".
             $ar.$dist.$s_asc.$c_asc.
             ", ".
             $ar.$dist.$s_md5.$c_md5.
             ", ".
             $ar.$dist.$s_sha1.$c_sha1.
             ")";
    } else {
      $line2 = " - ";
    }
            
	if ($bs_zip ~~ @archives) {
      $line3 = $ar.$cgi.$bs_zip.$c_zip.
               " (".
               $ar.$dist.$bs_asc.$c_asc.
               ", ".
               $ar.$dist.$bs_md5.$c_md5.
               ", ".
               $ar.$dist.$bs_sha1.$c_sha1.
               ")";
    } else {
      $line3 = " - ";
    }

    print $open_tr.$open_td.$close_td.$open_td." ".$bundle." ".$close_td.$open_td." ".$version." ".$close_td.$open_td."\n";
    print $line1."\n";
    print $close_td.$open_td."\n";
    print $line2."\n";
    print $close_td.$open_td."\n";
    print $line3."\n";
    print $close_td.$open_td.$close_td.$close_tr."\n";
}
