#!/usr/bin/perl -w
use strict;
use lib 'lib';
use File::Path;
use Haul;
use LWP::Simple;

my $cwd = '/home/acme/Haul/';

open(OUT, "> install.log");
select OUT;
$| = 1;
select STDOUT;

my $h = Haul->new(perl => "/home/acme/Haul/perl583/bin/perl");

my $map = {
    "TimeDate" => "Date::Parse",
    "Time-modules" => "Time::CTime",
    "TermReadKey" => "Term::ReadKey",
    "Scalar-List-Utils" => "Scalar::Util",
    "perl-ldap" => "Net::LDAP",
    "Msql-Mysql-modules" => "DBD::mSQL",
    "libxml-perl" => "XML::LibXML",
    "GDTextUtil" => "GD::Text",
    "GDGraph" => "GD::Graph",
    "DateManip" => "Date::Manip",
    "bioperl" => "Bio::Seq",
    "Template-Toolkit" => "Template",
    "Net_SSLeay.pm" => "Net::SSLeay",
    "MIME-tools" => "MIME::Parser",
    "IO-stringy" => "IO::Wrap",
    "MailTools" => "Mail::Mailer",
    "libwww-perl" => "LWP",
};

my $top = get('http://qa.perl.org/phalanx/distros.html');
foreach my $line (split /\n/, $top) {
  my($module) = $line =~ m{<tr><td>([^<]+)</td>};
  next unless $module;
  $module = $map->{$module} if exists $map->{$module};
  $module =~ s/-/::/g;
  next if $module eq 'PerlMagick'; # nightmare
  next if $module eq 'PAR'; # runs CPAN.pm, sigh
  next if $module eq 'Net::SSH::Perl'; # Math::Pari is interactive
  next if $module eq 'DBD::mSQL'; # don't have mSQL installed
  next if $module eq 'Mail::Box'; # Object::Realize::Later fails, dunno why
  next if $module eq 'Mac::Glue'; # Not a mac
  next if $module eq 'Mac::Carbon'; # Not a mac
  next if $module eq 'libapreq'; # not under apache
  next if $module eq 'HTML::Mason'; # not under apache
  next if $module eq 'DBD::Pg'; # don't have PostgreSQL
  next if $module eq 'DBD::ODBC'; # don't have ODBC
  next if $module eq 'Cache::Cache'; # IPC::ShareLite is interactive
  next if $module eq 'Apache::DBI'; # not under apache
  next if $module eq 'Term::ReadLine::Perl'; # interactive
  next if $module eq 'Template'; # latex test takes too long?
  next if $module eq 'DBD::Oracle'; # don't have oracle
  next if $module eq 'libwin32'; # don't have windows
  next if $module eq 'CPANPLUS'; # interactive
  next if $module eq 'IO::Socket::SSL'; # interactive, infinite loop
  warn "* Trying to install $module\n";

  chdir $cwd;
  rmtree("perl583");
  system("tar fx perl583.tar");
  die unless -d "perl583";
  die unless -f "perl583/bin/perl";
  mkdir "work";
  chdir "work";
  print OUT "Installing $module...\n";
  if ($h->installed($module)) {
    print OUT "... already installed\n";
    next;
  }
  eval {
    local $SIG{ALRM} = sub { die "alarm\n" };
    alarm 60 * 10;
    $h->install($module);
    alarm 0;
  };
  if ($@) {
    die unless $@ eq "alarm\n";   # propagate unexpected errors
    print OUT "... timed out (after 10 mins)!\n";
  } else {
    print OUT "... done\n";
  }



  die "error installing $module" unless $h->installed($module);
}
die "finished";



