#!/usr/bin/perl -w

use strict;
use warnings;

use FindBin;
use File::Spec;
use Config;
use lib File::Spec->catdir( $FindBin::Bin, qw[.. lib] ),
        File::Spec->catdir( $FindBin::Bin, qw[.. inc bundle] );


use CPANPLUS::Error;
use CPANPLUS::Configure;
use CPANPLUS::Internals::Constants;
use CPANPLUS::Internals::Utils;

### put the name in the dir, so other users can use it if they want
my $Who         = getlogin || getpwuid($<) || $<;
my $Base        = File::Spec->catfile($FindBin::Bin, '..', DOT_CPANPLUS, $Who);
my $Libdir      = File::Spec->catfile( $Base, 'lib' );
my $ConfObj     = CPANPLUS::Configure->new;
my $Config      = 'CPANPLUS::Config::Boxed';
my $Util        = 'CPANPLUS::Internals::Utils';
my $ConfigFile  = $ConfObj->_config_pm_to_file( $Config => $Libdir );

### setup the environment if needed 
{   ### no base dir even, set it up
    unless( IS_DIR->( $Base ) ) {
        $Util->_mkdir( dir => $Base ) or die CPANPLUS::Error->stack_as_string;
    }
 
    ### no config file exists yet, so we create it
    unless( -e $ConfigFile ) {
        ### alter what needs changing
        $ConfObj->set_conf( base    => $Base );     # new base dir
        $ConfObj->set_conf( verbose => 1     );     # be verbose
        $ConfObj->set_conf( prereqs => 1     );     # install prereqs
        $ConfObj->save(     $Config => $Libdir );   # save the pm in that dir
    }
}

print qq[
===

Your boxed CPANPLUS install is setup to use:
  Metadir:  $Base
  Config:   $ConfigFile

===
    \n    
];

$ENV{'PERL5LIB'} = join $Config{'path_sep'}, grep { defined } 
                    $Libdir,                # to find the boxed config
                    $ENV{'PERL5LIB'},
                    File::Spec->catdir( $FindBin::Bin, qw[.. lib] ),
                    File::Spec->catdir( $FindBin::Bin, qw[.. inc bundle] );

system( 
    $^X, 
    File::Spec->catfile( $FindBin::Bin, 'cpanp' ),
    @ARGV,
);
