initial release
[RTx-MandatoryCustomFields.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.25';
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 directory containing your RT.pm:") or exit;
54             $_ =~ s/\/RT\.pm$//;
55             push @INC, $_, "$_/rt3/lib", "$_/lib/rt3", "$_/lib";
56         }
57     }
58
59     my $lib_path = File::Basename::dirname( $INC{'RT.pm'} );
60     my $local_lib_path = "$RT::LocalPath/lib";
61     print "Using RT configuration from $INC{'RT.pm'}:\n";
62     unshift @INC, "$RT::LocalPath/lib" if $RT::LocalPath;
63
64     $RT::LocalVarPath  ||= $RT::VarPath;
65     $RT::LocalPoPath   ||= $RT::LocalLexiconPath;
66     $RT::LocalHtmlPath ||= $RT::MasonComponentRoot;
67     $RT::LocalLibPath  ||= "$RT::LocalPath/lib";
68
69     my $with_subdirs = $ENV{WITH_SUBDIRS};
70     @ARGV = grep { /WITH_SUBDIRS=(.*)/ ? ( ( $with_subdirs = $1 ), 0 ) : 1 }
71         @ARGV;
72
73     my %subdirs;
74     %subdirs = map { $_ => 1 } split( /\s*,\s*/, $with_subdirs )
75         if defined $with_subdirs;
76     unless ( keys %subdirs ) {
77         $subdirs{$_} = 1 foreach grep -d "$FindBin::Bin/$_", @DIRS;
78     }
79
80     # If we're running on RT 3.8 with plugin support, we really wany
81     # to install libs, mason templates and po files into plugin specific
82     # directories
83     my %path;
84     if ( $RT::LocalPluginPath ) {
85         die "Because of bugs in RT 3.8.0 this extension can not be installed.\n"
86             ."Upgrade to RT 3.8.1 or newer.\n" if $RT::VERSION =~ /^3\.8\.0/;
87         $path{$_} = $RT::LocalPluginPath . "/$original_name/$_"
88             foreach @DIRS;
89     } else {
90         foreach ( @DIRS ) {
91             no strict 'refs';
92             my $varname = "RT::Local" . ucfirst($_) . "Path";
93             $path{$_} = ${$varname} || "$RT::LocalPath/$_";
94         }
95
96         $path{$_} .= "/$name" for grep $path{$_}, qw(etc po var);
97     }
98
99     my %index = map { $_ => 1 } @INDEX_DIRS;
100     $self->no_index( directory => $_ ) foreach grep !$index{$_}, @DIRS;
101
102     my $args = join ', ', map "q($_)", map { ($_, $path{$_}) }
103         grep $subdirs{$_}, keys %path;
104
105     print "./$_\t=> $path{$_}\n" for sort keys %subdirs;
106
107     if ( my @dirs = map { ( -D => $_ ) } grep $subdirs{$_}, qw(bin html sbin) ) {
108         my @po = map { ( -o => $_ ) }
109             grep -f,
110             File::Glob::bsd_glob("po/*.po");
111         $self->postamble(<< ".") if @po;
112 lexicons ::
113 \t\$(NOECHO) \$(PERL) -MLocale::Maketext::Extract::Run=xgettext -e \"xgettext(qw(@dirs @po))\"
114 .
115     }
116
117     my $postamble = << ".";
118 install ::
119 \t\$(NOECHO) \$(PERL) -MExtUtils::Install -e \"install({$args})\"
120 .
121
122     if ( $subdirs{var} and -d $RT::MasonDataDir ) {
123         my ( $uid, $gid ) = ( stat($RT::MasonDataDir) )[ 4, 5 ];
124         $postamble .= << ".";
125 \t\$(NOECHO) chown -R $uid:$gid $path{var}
126 .
127     }
128
129     my %has_etc;
130     if ( File::Glob::bsd_glob("$FindBin::Bin/etc/schema.*") ) {
131
132         # got schema, load factory module
133         $has_etc{schema}++;
134         $self->load('RTxFactory');
135         $self->postamble(<< ".");
136 factory ::
137 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxFactory(qw($RTx $name))"
138
139 dropdb ::
140 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxFactory(qw($RTx $name drop))"
141
142 .
143     }
144     if ( File::Glob::bsd_glob("$FindBin::Bin/etc/acl.*") ) {
145         $has_etc{acl}++;
146     }
147     if ( -e 'etc/initialdata' ) { $has_etc{initialdata}++; }
148
149     $self->postamble("$postamble\n");
150     unless ( $subdirs{'lib'} ) {
151         $self->makemaker_args( PM => { "" => "" }, );
152     } else {
153         $self->makemaker_args( INSTALLSITELIB => $path{'lib'} );
154         $self->makemaker_args( INSTALLARCHLIB => $path{'lib'} );
155     }
156
157     $self->makemaker_args( INSTALLSITEMAN1DIR => "$RT::LocalPath/man/man1" );
158     $self->makemaker_args( INSTALLSITEMAN3DIR => "$RT::LocalPath/man/man3" );
159     $self->makemaker_args( INSTALLSITEARCH => "$RT::LocalPath/man" );
160
161     if (%has_etc) {
162         $self->load('RTxInitDB');
163         print "For first-time installation, type 'make initdb'.\n";
164         my $initdb = '';
165         $initdb .= <<"." if $has_etc{schema};
166 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(schema))"
167 .
168         $initdb .= <<"." if $has_etc{acl};
169 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(acl))"
170 .
171         $initdb .= <<"." if $has_etc{initialdata};
172 \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(insert))"
173 .
174         $self->postamble("initdb ::\n$initdb\n");
175         $self->postamble("initialize-database ::\n$initdb\n");
176     }
177 }
178
179 sub RTxInit {
180     unshift @INC, substr( delete( $INC{'RT.pm'} ), 0, -5 ) if $INC{'RT.pm'};
181     require RT;
182     RT::LoadConfig();
183     RT::ConnectToDatabase();
184
185     die "Cannot load RT" unless $RT::Handle and $RT::DatabaseType;
186 }
187
188 1;
189
190 __END__
191
192 #line 303