20a354b32d15bb16923ec7b52be0b84155cbe337
[manu/RT-Extension-SearchResults-XLS.git] / inc / Module / Install / RTx.pm
1 #line 1
2 package Module::Install::RTx;
3
4 use 5.008;
5 use strict;
6 use warnings;
7 no warnings 'once';
8
9 use Module::Install::Base;
10 use base 'Module::Install::Base';
11 our $VERSION = '0.24';
12
13 use FindBin;
14 use File::Glob     ();
15 use File::Basename ();
16
17 my @DIRS = qw(etc lib html bin sbin po var);
18 my @INDEX_DIRS = qw(lib bin sbin);
19
20 sub RTx {
21     my ( $self, $name ) = @_;
22
23     my $original_name = $name;
24     my $RTx = 'RTx';
25     $RTx = $1 if $name =~ s/^(\w+)-//;
26     my $fname = $name;
27     $fname =~ s!-!/!g;
28
29     $self->name("$RTx-$name")
30         unless $self->name;
31     $self->all_from( -e "$name.pm" ? "$name.pm" : "lib/$RTx/$fname.pm" )
32         unless $self->version;
33     $self->abstract("RT $name Extension")
34         unless $self->abstract;
35
36     my @prefixes = (qw(/opt /usr/local /home /usr /sw ));
37     my $prefix   = $ENV{PREFIX};
38     @ARGV = grep { /PREFIX=(.*)/ ? ( ( $prefix = $1 ), 0 ) : 1 } @ARGV;
39
40     if ($prefix) {
41         $RT::LocalPath = $prefix;
42         $INC{'RT.pm'} = "$RT::LocalPath/lib/RT.pm";
43     } else {
44         local @INC = (
45             @INC,
46             $ENV{RTHOME} ? ( $ENV{RTHOME}, "$ENV{RTHOME}/lib" ) : (),
47             map { ( "$_/rt3/lib", "$_/lib/rt3", "$_/lib" ) } grep $_,
48             @prefixes
49         );
50         until ( eval { require RT; $RT::LocalPath } ) {
51             warn
52                 "Cannot find the location of RT.pm that defines \$RT::LocalPath in: @INC\n";
53             $_ = $self->prompt("Path to your RT.pm:") or exit;
54             push @INC, $_, "$_/rt3/lib", "$_/lib/rt3", "$_/lib";
55         }
56     }
57
58     my $lib_path = File::Basename::dirname( $INC{'RT.pm'} );
59     my $local_lib_path = "$RT::LocalPath/lib";
60     print "Using RT configuration from $INC{'RT.pm'}:\n";
61     unshift @INC, "$RT::LocalPath/lib" if $RT::LocalPath;
62
63     $RT::LocalVarPath  ||= $RT::VarPath;
64     $RT::LocalPoPath   ||= $RT::LocalLexiconPath;
65     $RT::LocalHtmlPath ||= $RT::MasonComponentRoot;
66     $RT::LocalLibPath  ||= "$RT::LocalPath/lib";
67
68     my $with_subdirs = $ENV{WITH_SUBDIRS};
69     @ARGV = grep { /WITH_SUBDIRS=(.*)/ ? ( ( $with_subdirs = $1 ), 0 ) : 1 }
70         @ARGV;
71
72     my %subdirs;
73     %subdirs = map { $_ => 1 } split( /\s*,\s*/, $with_subdirs )
74         if defined $with_subdirs;
75     unless ( keys %subdirs ) {
76         $subdirs{$_} = 1 foreach grep -d "$FindBin::Bin/$_", @DIRS;
77     }
78
79     # If we're running on RT 3.8 with plugin support, we really wany
80     # to install libs, mason templates and po files into plugin specific
81     # directories
82     my %path;
83     if ( $RT::LocalPluginPath ) {
84         die "Because of bugs in RT 3.8.0 this extension can not be installed.\n"
85             ."Upgrade to RT 3.8.1 or newer.\n" if $RT::VERSION =~ /^3\.8\.0/;
86         $path{$_} = $RT::LocalPluginPath . "/$original_name/$_"
87             foreach @DIRS;
88     } else {
89         foreach ( @DIRS ) {
90             no strict 'refs';
91             my $varname = "RT::Local" . ucfirst($_) . "Path";
92             $path{$_} = ${$varname} || "$RT::LocalPath/$_";
93         }
94
95         $path{$_} .= "/$name" for grep $path{$_}, qw(etc po var);
96     }
97
98     my %index = map { $_ => 1 } @INDEX_DIRS;
99     $self->no_index( directory => $_ ) foreach grep !$index{$_}, @DIRS;
100
101     my $args = join ', ', map "q($_)", map { ($_, $path{$_}) }
102         grep $subdirs{$_}, keys %path;
103
104     print "./$_\t=> $path{$_}\n" for sort keys %subdirs;
105
106     if ( my @dirs = map { ( -D => $_ ) } grep $subdirs{$_}, qw(bin html sbin) ) {
107         my @po = map { ( -o => $_ ) }
108             grep -f,
109             File::Glob::bsd_glob("po/*.po");
110         $self->postamble(<< ".") if @po;
111 lexicons ::
112 \t\$(NOECHO) \$(PERL) -MLocale::Maketext::Extract::Run=xgettext -e \"xgettext(qw(@dirs @po))\"
113 .
114     }
115
116     my $postamble = << ".";
117 install ::
118 \t\$(NOECHO) \$(PERL) -MExtUtils::Install -e \"install({$args})\"
119 .
120
121     if ( $subdirs{var} and -d $RT::MasonDataDir ) {
122         my ( $uid, $gid ) = ( stat($RT::MasonDataDir) )[ 4, 5 ];
123         $postamble .= << ".";
124 \t\$(NOECHO) chown -R $uid:$gid $path{var}
125 .
126     }
127
128     my %has_etc;
129     if ( File::Glob::bsd_glob("$FindBin::Bin/etc/schema.*") ) {
130
131         # got schema, load factory module
132         $has_etc{schema}++;
133         $self->load('RTxFactory');
134         $self->postamble(<< ".");
135 factory ::
136 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxFactory(qw($RTx $name))"
137
138 dropdb ::
139 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxFactory(qw($RTx $name drop))"
140
141 .
142     }
143     if ( File::Glob::bsd_glob("$FindBin::Bin/etc/acl.*") ) {
144         $has_etc{acl}++;
145     }
146     if ( -e 'etc/initialdata' ) { $has_etc{initialdata}++; }
147
148     $self->postamble("$postamble\n");
149     unless ( $subdirs{'lib'} ) {
150         $self->makemaker_args( PM => { "" => "" }, );
151     } else {
152         $self->makemaker_args( INSTALLSITELIB => $path{'lib'} );
153         $self->makemaker_args( INSTALLARCHLIB => $path{'lib'} );
154     }
155
156     $self->makemaker_args( INSTALLSITEMAN1DIR => "$RT::LocalPath/man/man1" );
157     $self->makemaker_args( INSTALLSITEMAN3DIR => "$RT::LocalPath/man/man3" );
158     $self->makemaker_args( INSTALLSITEARCH => "$RT::LocalPath/man" );
159
160     if (%has_etc) {
161         $self->load('RTxInitDB');
162         print "For first-time installation, type 'make initdb'.\n";
163         my $initdb = '';
164         $initdb .= <<"." if $has_etc{schema};
165 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(schema))"
166 .
167         $initdb .= <<"." if $has_etc{acl};
168 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(acl))"
169 .
170         $initdb .= <<"." if $has_etc{initialdata};
171 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(insert))"
172 .
173         $self->postamble("initdb ::\n$initdb\n");
174         $self->postamble("initialize-database ::\n$initdb\n");
175     }
176 }
177
178 sub RTxInit {
179     unshift @INC, substr( delete( $INC{'RT.pm'} ), 0, -5 ) if $INC{'RT.pm'};
180     require RT;
181     RT::LoadConfig();
182     RT::ConnectToDatabase();
183
184     die "Cannot load RT" unless $RT::Handle and $RT::DatabaseType;
185 }
186
187 1;
188
189 __END__
190
191 #line 302