#!/usr/bin/env perl

# by Matt Simerson & Phil Nadeau
# circa 2008, but based in installer in Mail::Toaster dating back to the 20th century

use strict;
use warnings;

use English qw( -no_match_vars );

use lib 'lib';
use Provision::Unix;
use Provision::Unix::Utility;
my $prov = Provision::Unix->new();
my $util = Provision::Unix::Utility->new( prov => $prov );

my $deps = {
    'apps' => [
#        { app => 'perl'              , info => {  } },
    ],
    'modules' => [
        { module => 'Test::More'        , info => { } },
        { module => 'Getopt::Long'      , info => { version => 2.37 } },
        { module => 'Config::Std'       , info => { } },
        { module => 'Params::Validate'  , info => { } },
        { module => 'Apache::Admin::Config', info => { } },
        { module => 'JSON::XS'          , info => { } },
        { module => 'Proc::ProcessTable', info => { } },
    ],
};

$EUID == 0 or die( "You will have better luck if you run me as root.\n");

# this causes problems when CPAN is not configured.
#$ENV{PERL_MM_USE_DEFAULT} = 1;       # supress CPAN prompts

$ENV{FTP_PASSIVE} = 1;        # for FTP behind NAT/firewalls

my $apps = $deps->{apps};
foreach ( @$apps ) {
    print "checking $_->{app}\n";
    $util->install_package( $_->{app} => $_->{info} );
}

my $modules = $deps->{modules};
my @failed  = ();
foreach ( @$modules ) {
    my $module = $_->{module} or die 'missing module name';
    my $info   = $_->{info};
    my $version = $info->{version} || '';
    print "checking for $module $version\n";
    eval "use $module $version";
    if ($EVAL_ERROR) {
        next if $info->{ships_with} && $info->{ships_with} eq 'perl';
        $util->install_module( $module, $info );
        eval "use $module $version";
        if ($EVAL_ERROR) {
            push @failed, $module;
        }
    }
}

if ( scalar @failed > 0 ) {
    print "The following modules failed installation:\n";
    print join( "\n", @failed );
    print "\n";
}

