#!/usr/bin/env perl

=head1 NAME

setup_dkbench - DKbench Perl CPU Benchmark utility setup script

=head1 DESCRIPTION

This is a setup script, part of the L<Benchmark::DKbench> distribution.

Refer to the distribution POD for more information on the DKbench suite:

 perldoc Benchmark::DKbench

=head1 SYNOPSIS

Simple setup script to check/get the reference versions of CPAN modules and download
the Genbank data file required for the BioPerl benchmarks of the DKbench suite.

 setup_dkbench [options]

 Options:
 --sudo   : Will use sudo for cpanm calls.
 --force  : Will install reference CPAN module versions and re-download the genbank data.
 --test   : Will run the test suites for the CPAN module (default behavior is to skip).
 --data=s : Data dir path to copy files from. Should not need if you installed DKbench.
 --help   : Print this help text and exit.

If you don't match the recommended Perl and CPAN module versions, the benchmarks
may show 'Fail' under Pass/Fail and scores may not be comparable to the baseline.
This might be perfectly fine for your purposes - in fact it might be intended if
you are benchmarking to compare different software instead of hardware.

This installer assumes you have at least cpanm (L<App::cpanminus>) already installed.
If not, on must systems you can do that with:

 # System-wide with sudo:
 curl -L https://cpanmin.us | perl - --sudo App::cpanminus

 # Locally or system-wide when root
 curl -L https://cpanmin.us | perl - App::cpanminus

You will also need to have a build enviroment set up (compiler, make etc). If you
want to add a couple of optional BioPerl bencharks you'd want an XML library in
addition. Example commands to have everything you might need installed with the
most popular pqckage managers (run as root or with sudo):

 # Debian/Ubuntu etc
 apt-get update
 apt-get install wget build-essential perl cpanminus libxml-simple-perl

 # CentOS/Redhat
 yum update
 yum install wget gcc make patch perl perl-App-cpanminus perl-XML-LibXML perl-XML-Parser

The above will have you ready for using the default (system) perl. If you want to
specifically install the reference perl version for this suite (currently 5.36.0),
the simplest way is using perlbrew (no need for root/sudo):

 \curl -L https://install.perlbrew.pl | bash
 source ~/perl5/perlbrew/etc/bashrc
 perlbrew install perl-5.36.0 -n -j 4
 perlbrew use perl-5.36.0
 perlbrew install-cpanm

Note that the suite can be installed to as low as perl 5.12, however if you have a perl
version lower than 5.14 make sure you install L<IO::Socket::IP> 0.41 or lower first:

 cpanm -n http://cpan.metacpan.org/authors/id/P/PE/PEVANS/IO-Socket-IP-0.41.tar.gz

Some benchmarks will report failure on "ancient" Perls.

=cut

use strict;
use warnings;

use Pod::Usage;

pod2usage({ -exitval => 'NOEXIT', -verbose => 1, -output => \*STDOUT, -noperldoc => 1});

use lib 'lib';
use Benchmark::DKbench::Setup;
use Getopt::Long;

GetOptions (
    sudo     => \my $use_sudo,
    force    => \my $force,
    test     => \my $test,
    'data=s' => \my $data,
    'help|h' => \my $help,
);

exit(1) if $help;
my $sudo = $use_sudo ? 'sudo' : '';
my $t    = $test ? '' : '-n';

Benchmark::DKbench::Setup::fetch_genbank();

system "cp -r $data/* ".Benchmark::DKbench::Setup::datadir() if $data;

my @packages = Benchmark::DKbench::Setup::cpan_packages();
my %mod_ver  = Benchmark::DKbench::Setup::cpan_versions();

print $force ? "Installing reference cpan versions ...\n" : "Checking cpan modules ...\n";
foreach (@packages) {
    m#/([a-z]+)(?:-([a-z]+))?(?:-([a-z]+))?(?:-([a-z]+))?[^/]+$#i;
    my $mod = $1;
    $mod .= "::$2" if $2;
    $mod .= "::$3" if $3;
    $mod .= "::$4" if $4;
    my $ver = eval "use $mod; return \$${mod}::VERSION" || '';
    if (!$force && !$@) {
        print "Skipping $mod (found version $ver)\n";
    } else {
        system "$sudo cpanm $t http://cpan.metacpan.org/authors/id/$_"
            unless $ver eq $mod_ver{$mod};
    }
}
system "$sudo cpanm $t System::Info MCE::Loop Test::Harness Test::Output Test::Requires";
