#!/bin/perl
#
# Generate a script to download release artifacts. 
# Usage: perl download_release_artifacts.pl [target version] [filters ...]
# If no arguments are specified, the whole Aries section of the Apache
# repository is included. Any number of regular expression can be specified to narrow down what's included. 
# If no version is 
# specified, the highest version is used; otherwise only the specified version is transferred. 
# Example: perl download_release_artifacts.pl 1.0.0 application modeller | tee download.sh

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

@filters = ();

# Arguments are assumed to be regexps unless they're numeric versions
foreach my $arg (@ARGV) {
	if ($arg =~ /^[0-9\.]+$/) {   
		$target_version="$arg\/";
		print "# Targetting version $target_version\n"
	} else {
		push(@filters, $arg);
	}
 
}

$from_repository = "https://repository.apache.org/content/repositories/releases/org/apache/aries";

crawl($from_repository);

sub crawl {
	my $url = @_[0];
	print "# Crawling $url\n";
	my $data = get($url);
	my $html = HTML::TokeParser->new(\$data);
	 
	my @hrefs = ();
	my @versions = ();

	while (my $token = $html->get_tag("a")) {
	    my $href = $token->[1]{href} || "-";
	    my $text = $html->get_trimmed_text("/a");
	    

	# If this is a directory link ... 
	if ($href =~ /http.*\/$/) {
		# If this is a version of this module ...
		if ($text =~ /^[0-9\.]+\/$/) {
			if ((! defined $target_version) || $text eq $target_version) {
				push(@versions, $href);
			}
			} else {
				push(@hrefs, $href);
			}
		}
	}

	# If no version is specified, choose the maximum 
	@versions = sort(@versions);
	my $len = scalar @versions;
	 
	if ($len > 0) {
		# Check we meet all the filters 
		my $matches = true;
		foreach $filter (@filters) {
			$matches = $matches && (@versions[$#versions] =~ $filter);
		}
		if ($matches) {
			do_downloads(@versions[$#versions]);
		}
	}

	# Recurse
	for my $link (@hrefs) {
	    crawl($link)
	}
}


sub do_downloads {
	my $url = @_[0];
       	my @folders = split(/\//, $url);
        my $bundle = @folders[$#folders - 1];
        my $version = @folders[$#folders];
	@file_types = (
              "",
              ".md5",
              ".asc",
              ".sha1"
              );
	foreach $ft (@file_types) {
		print "wget -N --no-check-certificate $url/$bundle-$version.jar$ft\n";
		print "wget -N --no-check-certificate $url/$bundle-$version-sources.jar$ft\n";
		print "wget -N --no-check-certificate $url/$bundle-$version-source-release.zip$ft\n";
		print "wget -N --no-check-certificate $url/$bundle-$version-javadoc.jar$ft\n";
	}
}





