cfa74933fea5a262c3a5155476eaccbec2eedf52
[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 use File::Spec ();
34
35 # Look up to see find Makefile.PL aka the base of the local::lib install.
36 my $lookup; $lookup = sub {
37     my $dir = $_[0] || '.';
38
39     my(undef, $dirbit) = File::Spec->splitpath($FindBin::Bin);
40     my $trydir         = File::Spec->catdir($dirbit, $dir);
41
42     return '' unless -d $trydir;
43
44     my $tryfile = File::Spec->catfile($trydir, "Makefile.PL");
45
46     return $trydir if -r $tryfile;
47
48     return $lookup->( File::Spec->catdir($dir, File::Spec->updir) );
49 };
50
51 my $basedir = $lookup->();
52
53 $basedir ||= '';
54 my $target = "$basedir/local-lib5";
55
56 my $on = -d $target;
57 $on = ! ! $ENV{CATALYST_LOCAL_LIB}
58     if (exists $ENV{CATALYST_LOCAL_LIB} and defined $ENV{CATALYST_LOCAL_LIB});
59
60 Carp::confess("Could not find local-lib5 from '$FindBin::Bin'")
61     if ($on && ! length $basedir);
62
63 if ( $on ) {
64     # So we can find local::lib when fully self contained
65     lib->import("$target/lib/perl5");
66
67     # . for CPAN + app dir
68     my @include = ('.', "$basedir/lib");
69
70     $ENV{PERL5LIB} = join ':', @include;
71
72     # Sorry kane ;)
73     $ENV{PERL_AUTOINSTALL_PREFER_CPAN}=1;
74
75     $ENV{PERL_MM_OPT} .= " INSTALLMAN1DIR=none INSTALLMAN3DIR=none";
76
77     require lib::core::only;
78     require local::lib;
79     lib::core::only->import();
80     local::lib->import( $target );
81 }
82
83 unless ( caller ) {
84     if ( @ARGV ) {
85         exec @ARGV;
86     }
87 }
88
89 1;
90