#!/usr/bin/perl

use lib './lib';
use lib '../lib';
use lib "$ENV{HOME}/.cpan";
use Getopt::Std;

getopts('cfd:h');

require 5.008_005;

use vars qw/$opt_c $opt_d $opt_h $VERSION/;

$VERSION = q{$Id};

my $prog = $0;
$prog =~ s:.*/::;

$::USAGE = <<EOF;
usage: $prog [-d interchange_root] [module1 module2 module_n]

[module] defaults to Bundle::Interchange.

OPTIONS:

    -d    Set interchange root directory (default current)

EOF

if($opt_h) {
	warn $USAGE;
	exit 2;
}

use Cwd;
use File::Spec;

use strict;

my $libdir = $opt_d || '';

my @mods_to_get = @ARGV;

if(! @mods_to_get) {
	push @mods_to_get, 'Bundle::Interchange';
}

if(! $libdir) {
	my @possible = grep -f $_, qw/minivend.cfg interchange.cfg interchange.cfg.dist/;
	if(@possible) {
		$libdir = cwd() if -d 'lib';
	}
}

$libdir =~ s:(^|/)lib$::;

if(! File::Spec->file_name_is_absolute($libdir) ) {
	$libdir = File::Spec->catfile(cwd(), $libdir);
}

unshift @INC, $libdir, "$libdir/lib";

$ENV{PERL5LIB} = join ":", @INC;


use CPAN;

eval {
    require CPAN::MyConfig
};
if($@) {
    eval {
        require CPAN::Config;
    };
};

if($@) {
	print <<EOF if ! $opt_c;

We can go and get optional modules that help Interchange work a
bit better and faster. At least we can if you are connected
to the Internet and have one of the following on your machine:

		Perl LWP libraries
		Perl Net::FTP library
		ncftp (a nice FTP program)
		lynx  (the text-based web browser)

CPAN is a worldwide network of over 100 FTP sites which maintain
the latest Perl software.

You will be asked quite a few questions during the process. It is
almost always right to accept the default by hitting ENTER.
(There may be a couple of module tests that require you to hit 'q'
to terminate them.)

If you have errors during the process, don't worry. We will try
real, real, hard to get all of the modules installed. If
all don't continue to install, then try rerunning:

	$0

EOF
	my_prompt('Press [ENTER] to continue....');
	if(ref $CPAN::Config) {
		$CPAN::Config->{makepl_arg} = "INSTALLPRIVLIB=$libdir/lib INSTALLARCHLIB=$libdir/lib INSTALLSITELIB=$libdir/lib INSTALLMAN1DIR=none INSTALLMAN3DIR=none INSTALLSITEARCH=$libdir/lib INSTALLDIRS=perl";
		$CPAN::Config->{keep_source_where} = "$libdir/src"
			unless -w $CPAN::Config->{keep_source_where};
		$CPAN::Config->{cpan_home} = "$libdir/src"
			unless -w $CPAN::Config->{cpan_home};
		$CPAN::Config->{build_dir} = "$libdir/src"
			unless -w $CPAN::Config->{build_dir};
	}
	CPAN::get 'Bundle::Interchange';
}

# See if we have the CPAN module
eval { 	
	die "Don't try this at home with Windows.\n" if $^O =~ /win32/i;
};

if($@) {
	die "Can't do cpan_local_install: $@\n";
}

sub my_prompt {
    my($pr) = shift || '? ';
    my($def) = shift;
    my($ans);

    print $pr;
    print "[$def] " if $def;
    chomp($ans = <STDIN>);
    $ans ? $ans : $def;
}

for my $module (@mods_to_get) {
	my $prompt = "Get $module? [yes] ";
	my $ask = 'y';
	$ask = my_prompt($prompt)
		unless $opt_c;
	exit 2 if $ask =~ /^\s*n/i;
	
	$CPAN::Config->{makepl_arg} = "INSTALLPRIVLIB=$libdir/lib INSTALLARCHLIB=$libdir/lib INSTALLSITELIB=$libdir/lib INSTALLMAN1DIR=none INSTALLMAN3DIR=none INSTALLSITEARCH=$libdir/lib INSTALLDIRS=perl";
	$CPAN::Config->{keep_source_where} = "$libdir/src"
		unless -w $CPAN::Config->{keep_source_where};
	$CPAN::Config->{cpan_home} = "$libdir/src"
		unless -w $CPAN::Config->{cpan_home};
	$CPAN::Config->{build_dir} = "$libdir/src"
		unless -w $CPAN::Config->{build_dir};
	my $incstring = join " ", @INC;
	print <<EOF;
	INSTALLPRIVLIB=$libdir/lib
	INSTALLARCHLIB=$libdir/lib
	INSTALLSITELIB=$libdir/lib
	INSTALLMAN1DIR=none
	INSTALLMAN3DIR=none
	INSTALLSITEARCH=$libdir/lib
	INSTALLDIRS=perl
	LIBDIRS=$incstring
EOF
	CPAN::install($module);
}

