#!/usr/bin/perl -w

use strict;
use LWP::Simple;
use File::Path qw(rmtree);
use Cwd;

my $dir = getcwd;

use constant USAGE =><<END;
Usage: $0 <file>
   Argument must be the folder containing the CVS image

/!\ Do not forget to correct the scrip for each release of Bioconductor.org

Example to try:
   perl Update-R-BioC.pl ~/CVS/
or
   ./Update-R-BioC.pl ~/CVS/
END


### My variables
my $folder = $ARGV[0] or die USAGE;
my $dl_BioC = "http://bioconductor.org/packages/2.2/bioc/src/contrib/"; ## To be updated for each release of Bioconductor.org
my $dl_cran = "http://cran.r-project.org/src/contrib/";
################

if (! -d $folder){
        print "Your argument is not a folder\n\n";
        die;
}

# Read the folder
opendir (MYDIR, "$folder")|| die "can not open the folder";
my @contents = grep !/^\.\.?$/, readdir MYDIR;
closedir MYDIR;

# Remove the packages that are not R-package
for(my $i =0; $i<=$#contents;$i++){
	if ($contents[$i] !~ /^R-.*$/){
#		warn($contents[$i]);
		splice (@contents, $i,1);
		$i--;
	}
}
#print "@contents\n";exit;

# Open the spec file on the devel branch
my %currentVersion;
my %myURL;

for (my $k =0; $k<$#contents; $k++){
	open(SPEC, $folder.$contents[$k]."/devel/".$contents[$k].".spec")|| warn "Can not open".$folder.$contents[$k]."/devel/".$contents[$k].".spec" && next;
	my @spec = <SPEC>;
	close(SPEC);
	
	foreach my $line (@spec){
		if ($line =~ /^Version:(.*)/){
			$currentVersion{$contents[$k]} = $1;
			$currentVersion{$contents[$k]} =~ s/\s//g;
		}
		if ($line =~ /^URL:(.*)/){
			$myURL{$contents[$k]} = $1;
			$myURL{$contents[$k]} =~ s/\s//g;
		}
	}
	#  For Debugging purpose only
	# print $contents[$k],"\n";
	# print $currentVersion{$contents[$k]},"\n";
	# print $myURL{$contents[$k]},"\n","\n";
}

# Extract the name of the package
my $o =0;
my @packages;
foreach my $package (@contents){
	$package =~ /R-(.*)/;
	$packages[$o] = $1;
	$o++;
}

# Parse the website to get the current version.
print "="x100, "\n\nRetrieving the version from the website\n\n","="x100,"\n";
my @table;
my $res;
my $dl_url;
for (my $k =1; $k < $#contents; $k++){
	$res ="";
	my $page = get ($myURL{$contents[$k]}) || warn "Can not retrieve the page for $myURL{$contents[$k]}";

	my $package = $contents[$k];
	$package =~ s/R-(.*)/$1/;

	if($page ne 1){
		@table = split(/ /, $page);
		# Base on wether it is a cran or a bioconductor package the regex is different

		if ($myURL{$contents[$k]} =~ /http:\/\/cran.*/){
			$dl_url=$dl_cran;
			for (my $i= 0; $i <$#table; $i++){
			       if($table[$i]=~ /contrib\/$package.*\.tar\.gz/){
					# reference = href="../pls_2.1-0.tar.gz">pls_2.1-0.tar.gz</a></td></tr>
					$res = $table[$i];
					$res =~ s/\n//g;
					$res =~ s/.*($package.*\.tar\.gz).*/$1/g;
			        }
			}
		}
		if ($myURL{$contents[$k]} =~ /http:\/\/(www\.)?bioconductor\.org.*/){
			$dl_url=$dl_BioC;
			for (my $i= 0; $i <$#table; $i++){
				if($table[$i]=~ /.*href=\"..\/src\/contrib\/$package.*.tar.gz\".*/){
					$res = $table[$i];
					$res =~ s/\n//g;
					$res =~ s/.*($package.*\.tar\.gz).*/$1/g;	
				}
			}
		}
		# Extract the version number
		my $version = $res;
		$version =~ s/.*_(.*)\.tar.gz/$1/;
		if($version ne $currentVersion{$contents[$k]}){
			print $contents[$k]."\n";
			print $myURL{$contents[$k]}."\n";
			print "Version from the website = ".$version."\n";
			print "Version on the CVS = ".$currentVersion{$contents[$k]},"\n\n";
			download($dl_url, $res);
		}
	}
}

exit;

### Sub download()
# Get the source and unzip them

sub download {

	my($url, $lib) = @_;
	my $depends;
	my $suggests;
	chdir ($dir);

	# If the folder lib does not exist yet, creates it on the current directory
	if ( ! -d "lib"){
		mkdir "lib";
	}
	chdir "lib";

	if ( ! -e  "$lib"){
	  print "$lib <===\n";
	  `wget $url$lib`;
	}
	`tar -xvf $lib`;

	# Read the DESCRIPTION file and extract the dependancies
	my $title = $lib;
	$title =~ s/(.*)_\d*.*\.tar\.gz/$1/;
	open (LIB, "$title/DESCRIPTION");
	my @description = <LIB>;
	close (LIB);
	for (my $i =0 ; $i <=$#description; $i++){
		if ($description[$i] =~ /Depends:.*$/){
			$depends = $description[$i];
			$depends =~ s/Depends:(.*)/$1/g;
		}
		if ($description[$i] =~ /Suggests:.*$/){
			$suggests = $description[$i];
			$suggests =~ s/Suggests:(.*)/$1/g;
		}
	}

	# Print the output
	if($depends){
		print "Depends --> $depends";
	}else{
		print "Depends --> none \n";
	}
	if($suggests){
		print "Suggests --> $suggests\n";
	}else{
		print "Suggests --> none \n\n";
	}
	print "*"x50,"\n";

	# Remove the folder create while uncompressing the sources
	chdir($dir);
	rmtree "lib/$title/";
}

