#!/usr/bin/perl

$|++;

use strict;
use LWP::Simple qw/get $ua/;
use Net::SSLeay;

my $url = 'https://readonly:readonly@hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/';
my $r = get($url);
my $package_list_html = $r;
my %package_urls = reverse $package_list_html =~ m|<li><a href="(.*)">(.*)/</a></li>|g;
#print keys(%package_urls),"\n";
#print "\n",'*' x50,"\n\n";
#exit;

#my %package_urls = {'affy'=> 'affy', 'affydata' => 'affydata'};
#print keys(%package_urls),"\n";

$_ = $url . $_ for values %package_urls;
printf "/* Found %d packages */\n", scalar keys %package_urls;

my %package_dependencies;

while( my($package,$url) = each %package_urls ){
  
  my $description = get( $url . 'DESCRIPTION' );
  my @deps = split /, */, ($description =~ /Depends: (.*)/)[0];
  s/\(.+\)// for @deps;
  s/\s//g for @deps;
  @deps = grep { $_ ne "R" } @deps;
  $package_dependencies{ $package } = [ @deps ];
  print "Depends: \n";
  printf qq{\t"%s" -> "%s";\n}, $package,$_ for( @deps );
    
#  my @sugs = split /, */, ($description =~ /Suggests: (.*)\w+: /s)[0];
   if ( $description =~ /Suggests: (.*\w+:).*/s ) {
      my $tmp=$1;
#      print "**** ", $tmp,"\n";
      s/\w*:.*//sg for $tmp ;
#      print "**** ", $tmp,"\n";
      my @sugs = split /, */, $tmp;
      s/\(.+\)// for @sugs;
      s/\s//g for @sugs;
      @sugs = grep { $_ ne "R" } @sugs;
      $package_dependencies{ $package } = [ @sugs ];
      print "Suggests: \n";
      printf qq{\t"%s" -> "%s";\n}, $package,$_ for( @sugs );
   }
}

