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

my $PREREQ_DIR = '000prereqs';

my $devnull  = File::Spec->devnull();
my $redirect = "> $devnull";
my $prefix;
GetOptions( 
	'prefix=s' => \$prefix,
	'verbose'  => sub { $redirect = "" },
	'cpan-local-mirror=s' => sub { print "Ignoring old build system flag \"cpan-local-mirror\" (please remove it from the invocation, if you have nothing else to do)\n" },
	'config-cpan'         => sub { print "Ignoring old build system flag \"config-cpan\" (please remove it from the invocation, if you have nothing else to do)\n" },	
);
my $lib = "$prefix/lib";

my $make_pl_prefix = 

"INSTALLMAN3DIR=$prefix/share/man/man3 "       .
"INSTALLSITEMAN3DIR=$prefix/share/man/man3 "   .
"INSTALLVENDORMAN3DIR=$prefix/share/man/man3 " .

"INSTALLMAN1DIR=$prefix/share/man/man1 "       .
"INSTALLSITEMAN1DIR=$prefix/share/man/man1 "   .
"INSTALLVENDORMAN1DIR=$prefix/share/man/man1 " .

"INSTALLSCRIPT=$prefix/bin "       .
"INSTALLSITESCRIPT=$prefix/bin "   .
"INSTALLVENDORSCRIPT=$prefix/bin " .

"INSTALLBIN=$prefix/bin "       .
"INSTALLSITEBIN=$prefix/bin "   .
"INSTALLVENDORBIN=$prefix/bin " .

"INSTALLPRIVLIB=$lib "  .
"INSTALLSITELIB=$lib "  .
"INSTALLVENDORLIB=$lib ".

"INSTALLARCHLIB=$lib "   .
"INSTALLSITEARCH=$lib "  .
"INSTALLVENDORARCH=$lib ";



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 $redirect";
	system "$Config{'make'}                                 $redirect";
	system "$Config{'make'} install                         $redirect";	
	system "$Config{'make'} clean                           $redirect";
}
else {
	die "this shouldn't happen";
}
chdir $wd; 

opendir( my $dirhandle, $dir ) || die $!;
my @distros  = grep { -d "$dir/$_" && $_ !~ /^\./  } readdir( $dirhandle );
closedir $dirhandle;
for my $distro ( @distros ) {
	next if $distro eq 'Module-Build-0.2805';
	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'} -I$lib Makefile.PL $args{$distro} $redirect";
		system "$Config{'make'}                                $redirect";
		system "$Config{'make'} install                        $redirect";		
		system "$Config{'make'} clean                          $redirect";
	}
	elsif ( -f 'Build.PL' ) {
		system "$Config{'perlpath'} -I$lib Build.PL            $redirect";
		system "$Config{'perlpath'} -I$lib  Build              $redirect";
		system "$Config{'perlpath'} -I$lib  Build install      $redirect";		
		system "$Config{'perlpath'} -I$lib  Build clean        $redirect";
	}
	else {
		die "this shouldn't happen";
	}
	chdir $wd; 
}

WriteMakefile(
	'NAME'                 => 'Cipres::Everything',
	'VERSION'              => '0.1',
	'INSTALLMAN3DIR'       => "$prefix/share/man/man3",
	'INSTALLSITEMAN3DIR'   => "$prefix/share/man/man3",
	'INSTALLVENDORMAN3DIR' => "$prefix/share/man/man3",	
	'INSTALLMAN1DIR'       => "$prefix/share/man/man1",
	'INSTALLSITEMAN1DIR'   => "$prefix/share/man/man1",
	'INSTALLVENDORMAN1DIR' => "$prefix/share/man/man1",	
	'INSTALLSCRIPT'        => "$prefix/bin",
	'INSTALLSITESCRIPT'    => "$prefix/bin",
	'INSTALLVENDORSCRIPT'  => "$prefix/bin",	
	'INSTALLBIN'           => "$prefix/bin",
	'INSTALLSITEBIN'       => "$prefix/bin",
	'INSTALLVENDORBIN'     => "$prefix/bin",	
	'INSTALLPRIVLIB'       => "$lib",
	'INSTALLSITELIB'       => "$lib",
	'INSTALLVENDORLIB'     => "$lib",	
	'INSTALLARCHLIB'       => "$lib",
	'INSTALLSITEARCH'      => "$lib",
	'INSTALLVENDORARCH'    => "$lib",
);

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

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

}

mkdir "$prefix/etc";
open my $fh, '>', "$prefix/etc/perlvars.bashrc" or die $!;
print $fh <<"!GROK!THIS!";
#!/bin/sh
if test -z "\$CIPRES_ROOT" 
then
	echo " CIPRES_ROOT must be defined"
else
	export PATH=\$CIPRES_ROOT/lib/perl/bin$Config{'path_sep'}\$PATH
	export PERL5LIB=\$CIPRES_ROOT/lib/perl/lib$Config{'path_sep'}\$PERL5LIB
	export $Config{'ldlibpthname'}=\$CIPRES_ROOT/lib/perl/lib/ext/lib$Config{'path_sep'}\$$Config{'ldlibpthname'}
	export LOGPERLCONF=\$CIPRES_ROOT/share/cipres/resources/log4perl.properties
fi
!GROK!THIS!
close $fh;

if ( not -d "$prefix/../../share/cipres/" ) {
	mkpath(["$prefix/../../share/cipres/"]);
}

open my $share_dir_bashrc_fh, '>', "$prefix/../../share/cipres/perl.bashrc" or die $!;
print $share_dir_bashrc_fh <<"!SHARE!DIR!";
#!/bin/sh
if test -z "\$CIPRES_ROOT" 
then
	echo "The CIPRES_ROOT variable must be defined to set up your environment"
else 
	TOADDTO_PERL5LIB="\$CIPRES_ROOT/lib/perl/lib"
	TOADDTO_$Config{'ldlibpthname'}="\$CIPRES_ROOT/lib/perl/lib/ext"

	if test -n "\$PERL5LIB" 
	then
		PERL5LIB="\$PERL5LIB/:\$TOADDTO_PERL5LIB"
	else
		PERL5LIB="\$TOADDTO_PERL5LIB"
	fi
	export PERL5LIB

	if test -n "\$$Config{'ldlibpthname'}" 
	then
		$Config{'ldlibpthname'}="\$$Config{'ldlibpthname'}/$Config{'path_sep'}\$TOADDTO_DYLD_LIBRARY_PATH"
	else
		$Config{'ldlibpthname'}="\$TOADDTO_DYLD_LIBRARY_PATH"
	fi
	export $Config{'ldlibpthname'}
	export LOGPERLCONF=\$CIPRES_ROOT/share/cipres/resources/log4perl.properties
	export PATH="\$PATH$Config{'path_sep'}\$CIPRES_ROOT/lib/perl/bin"

fi
!SHARE!DIR!
close $share_dir_bashrc_fh;

open my $share_dir_cshrc_fh, '>', "$prefix/../../share/cipres/perl.cshrc" or die $!;
print $share_dir_cshrc_fh <<"!SHARE!DIR!CSHRC!";
#!/bin/sh
if (\$?CIPRES_ROOT == 0) then
	echo "The CIPRES_ROOT variable must be defined to set up your environment"
else 
	set TOADDTO_PERL5LIB="\$CIPRES_ROOT/lib/perl/lib"
	set TOADDTO_$Config{'ldlibpthname'}="\$CIPRES_ROOT/lib/perl/lib/ext"

	if (\$?PERL5LIB != 0) then
		setenv PERL5LIB "\$PERL5LIB/$Config{'path_sep'}\$TOADDTO_PERL5LIB"
	else
		setenv PERL5LIB "\$TOADDTO_PERL5LIB"
	endif

	if ("\$?$Config{'ldlibpthname'}" != 0) then
		setenv $Config{'ldlibpthname'} "\$DYLD_LIBRARY_PATH/$Config{'path_sep'}\$TOADDTO_DYLD_LIBRARY_PATH"
	else
		setenv $Config{'ldlibpthname'} "\$TOADDTO_$Config{'ldlibpthname'}"
	endif
	setenv LOGPERLCONF \$CIPRES_ROOT/share/cipres/resources/log4perl.properties
	setenv PATH "\$PATH$Config{'path_sep'}\$CIPRES_ROOT/lib/perl/bin"

endif
!SHARE!DIR!CSHRC!
close $share_dir_cshrc_fh;
