require 5.008;
use strict;
use warnings;
use File::Basename;
use Config;
use Cwd;
use File::Spec;
use Getopt::Long;
use ExtUtils::MakeMaker;

my $PREREQ_DIR = '000prereqs';

my $devnull = File::Spec->devnull();
my $prefix;
GetOptions( 'prefix=s' => \$prefix );
my $lib = "$prefix/lib";

my $make_pl_prefix = 
"INSTALLSITEMAN3DIR=$prefix/share/man/man3 " .
"INSTALLSITEMAN1DIR=$prefix/share/man/man1 " .
"INSTALLSCRIPT=$prefix/bin " .
"INSTALLVENDORBIN=$prefix/bin " .
"INSTALLSITEBIN=$prefix/bin " .
"INSTALLBIN=$prefix/bin " .
"INSTALLVENDORLIB=$lib " .
"INSTALLARCHLIB=$lib "   .
"INSTALLSITELIB=$lib "   .
"INSTALLPRIVLIB=$lib "   .
"INSTALLDIRS=site ";

my %args = (
	'XML-Parser-2.34' => " EXPATLIBPATH=$lib/ext/lib EXPATINCPATH=$lib/ext/include "
);

my $wd = getcwd;
my $file = __FILE__;
my ( $name, $dir, $suffix ) = File::Basename::fileparse( $file );
$dir .= "/$PREREQ_DIR/";

# install Module::Build
chdir $dir . 'Module-Build-0.2805';
if ( -f 'Makefile.PL' ) {
	system "$Config{'perlpath'} Makefile.PL $make_pl_prefix > $devnull";
	system "$Config{'make'}                                 > $devnull";
	system "$Config{'make'} install                         > $devnull";	
	system "$Config{'make'} clean                           > $devnull";
}
else {
	die "this shouldn't happen";
}
chdir $wd; 

# install expath
if ( $^O ne 'MSWin32' ) {
	print "Installing prerequisite expat-2.0.0\n";
    chdir $dir . '/../expat-2.0.0';
    system './configure', '--prefix', $lib . '/ext';
    system $Config::Config{'make'};
    system $Config::Config{'make'}, 'install';
    chdir $wd;
}

opendir( my $dirhandle, $dir ) || die $!;
my @distros  = grep { -d "$dir/$_" && $_ !~ /^\./  } readdir( $dirhandle );
closedir $dirhandle;
for my $distro ( @distros ) {
	print "Installing prerequisite $distro\n";
	$args{$distro} = " " if not $args{$distro} ;
	$args{$distro} = $args{$distro} . $make_pl_prefix;
	chdir $dir . $distro;
	if ( -f 'Makefile.PL' ) {
		system "$Config{'perlpath'} Makefile.PL $args{$distro} > $devnull";
		system "$Config{'make'}                                > $devnull";
		system "$Config{'make'} install                        > $devnull";		
		system "$Config{'make'} clean                          > $devnull";
	}
	elsif ( -f 'Build.PL' ) {
		system "$Config{'perlpath'} Build.PL                   > $devnull";
		system "$Config{'perlpath'} Build                      > $devnull";
		system "$Config{'perlpath'} Build install              > $devnull";		
		system "$Config{'perlpath'} Build clean                > $devnull";
	}
	else {
		die "this shouldn't happen";
	}
	chdir $wd; 
}

WriteMakefile(
	NAME               => 'Cipres::Everything',
	VERSION            => '0.1',
	INSTALLSITEMAN3DIR => "$prefix/share/man/man3",
	INSTALLSITEMAN1DIR => "$prefix/share/man/man1",
	INSTALLVENDORBIN   => "$prefix/bin",
	INSTALLSITEBIN     => "$prefix/bin",
	INSTALLSCRIPT      => "$prefix/bin",
	INSTALLBIN         => "$prefix/bin",
	INSTALLVENDORLIB   => $lib,
	INSTALLARCHLIB     => $lib,
	INSTALLSITELIB     => $lib,
	INSTALLPRIVLIB     => $lib,
	INSTALLDIRS        => "site",	
);

sub MY::postamble {
    my $hash = shift;
    my $postamble = $hash->{'postamble'};
    my $source    = $postamble->{'source'};
    return "
installcheck :
	\$(NOECHO) \$(NOOP)

check :
	\$(NOECHO) \$(NOOP)
";

}
