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