7f9699130726cb49534c9963b0f7cdb930096c43
[catagits/Gitalist.git] / script / env
1 #!/usr/bin/env perl
2 # vim: set filetype=perl:
3
4 # env is a perl script similar in concept to /usr/bin/env
5
6 # If you have a local-lib5 directory then this script will set it up for
7 # you as it executes.
8
9 # If used like /usr/bin/env then it will run other commands based on
10 # your current path settings (with a local::lib environment if present)
11 #
12 #  e.g. script/env bash
13 #
14 # NOTE: This environment _IS NOT_ self contained
15
16 # If included inside another perl script, then it will be a no-op if
17 # a local::lib environment is not present, but if one is, it will be
18 # used as a --self-contained environment, expected to contain all non-core
19 # dependencies for your perl
20 #
21 #  e.g.
22 #       use FindBin;
23 #       BEGIN { do "$FindBin::Bin/env" or die $@ }
24
25 # The local::lib behavior can be explicitly enabled or disabled by setting
26 # the CATALYST_LOCAL_LIB enviromnent variable to true or false.
27
28 use strict;
29 use warnings;
30 use Carp;
31 use lib;
32 use FindBin;
33
34 my $basedir;
35 if (-r "$FindBin::Bin/Makefile.PL") {
36     $basedir = $FindBin::Bin;
37 }
38 elsif (-r "$FindBin::Bin/../Makefile.PL") {
39     $basedir = "$FindBin::Bin/..";
40 }
41
42 $basedir ||= '';
43 my $target = "$basedir/local-lib5";
44
45 my $on = -d $target;
46 $on = ! ! $ENV{CATALYST_LOCAL_LIB}
47     if (exists $ENV{CATALYST_LOCAL_LIB} and defined $ENV{CATALYST_LOCAL_LIB});
48
49 Carp::confess("Could not find local-lib5 from '$FindBin::Bin'")
50     if ($on && ! length $basedir);
51
52 if ( $on ) {
53     # So we can find local::lib when fully self contained
54     lib->import("$target/lib/perl5");
55
56     # . for CPAN + app dir
57     my @include = ('.', "$basedir/lib");
58
59     $ENV{PERL5LIB} = join ':', @include;
60
61     # Sorry kane ;)
62     $ENV{PERL_AUTOINSTALL_PREFER_CPAN}=1;
63
64     $ENV{PERL_MM_OPT} .= " INSTALLMAN1DIR=none INSTALLMAN3DIR=none";
65
66     require lib::core::only;
67     require local::lib;
68     lib::core::only->import();
69     local::lib->import( $target );
70 }
71
72 unless ( caller ) {
73     if ( @ARGV ) {
74         exec @ARGV;
75     }
76 }
77
78 1;
79