]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - configure
Store generated SSL certificates in the .configure directory.
[user/henk/code/inspircd.git] / configure
1 #!/usr/bin/env perl
2 #
3 # InspIRCd -- Internet Relay Chat Daemon
4 #
5 #   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
6 #   Copyright (C) 2019 Anatole Denis <natolumin@rezel.net>
7 #   Copyright (C) 2017 emerson <github@emersonveenstra.net>
8 #   Copyright (C) 2013-2020 Sadie Powell <sadie@witchery.services>
9 #   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
10 #   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
11 #   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
12 #   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
13 #   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
14 #   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
15 #   Copyright (C) 2006-2008 Craig Edwards <brain@inspircd.org>
16 #
17 # This file is part of InspIRCd.  InspIRCd is free software: you can
18 # redistribute it and/or modify it under the terms of the GNU General Public
19 # License as published by the Free Software Foundation, version 2.
20 #
21 # This program is distributed in the hope that it will be useful, but WITHOUT
22 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
24 # details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 #
29
30
31 BEGIN {
32         require 5.10.0;
33 }
34
35 use feature ':5.10';
36 use strict;
37 use warnings FATAL => qw(all);
38
39 use Cwd                   qw(getcwd);
40 use File::Basename        qw(basename);
41 use File::Copy            ();
42 use File::Spec::Functions qw(abs2rel catfile catdir rel2abs);
43 use FindBin               qw($RealDir);
44 use Getopt::Long          qw(GetOptions);
45 use POSIX                 qw(getgid getuid);
46
47 use lib $RealDir;
48 use make::common;
49 use make::configure;
50 use make::console;
51 use make::directive;
52
53 my ($opt_binary_dir,
54     $opt_config_dir,
55     $opt_data_dir,
56     $opt_development,
57     $opt_disable_auto_extras,
58     $opt_disable_interactive,
59     $opt_distribution_label,
60     $opt_example_dir,
61     $opt_gid,
62     $opt_log_dir,
63     $opt_manual_dir,
64     $opt_module_dir,
65     $opt_portable,
66     $opt_prefix,
67     $opt_script_dir,
68     $opt_socketengine,
69     $opt_system,
70     $opt_uid);
71
72 sub list_extras ();
73
74 sub enable_extras (@);
75
76 sub disable_extras (@);
77
78 my @opt_enableextras;
79 my @opt_disableextras;
80
81 exit 1 unless GetOptions(
82         'clean'  => \&cmd_clean,
83         'help'   => \&cmd_help,
84         'update' => \&cmd_update,
85
86         'binary-dir=s'         => \$opt_binary_dir,
87         'config-dir=s'         => \$opt_config_dir,
88         'data-dir=s'           => \$opt_data_dir,
89         'development'          => \$opt_development,
90         'disable-auto-extras'  => \$opt_disable_auto_extras,
91         'disable-interactive'  => \$opt_disable_interactive,
92         'distribution-label=s' => \$opt_distribution_label,
93         'example-dir=s'        => \$opt_example_dir,
94         'gid=s'                => \$opt_gid,
95         'log-dir=s'            => \$opt_log_dir,
96         'manual-dir=s'         => \$opt_manual_dir,
97         'module-dir=s'         => \$opt_module_dir,
98         'portable'             => \$opt_portable,
99         'prefix=s'             => \$opt_prefix,
100         'script-dir=s'         => \$opt_script_dir,
101         'socketengine=s'       => \$opt_socketengine,
102         'system'               => \$opt_system,
103         'uid=s'                => \$opt_uid,
104
105         # TODO: when the modulemanager rewrite is done these should be removed.
106         'disable-extras=s@' => \@opt_disableextras,
107         'enable-extras=s@'  => \@opt_enableextras,
108         'list-extras'       => sub { list_extras; exit 0; },
109 );
110
111 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
112         @opt_enableextras = split /[, ]+/, join(',', @opt_enableextras);
113         @opt_disableextras = split /[, ]+/, join(',', @opt_disableextras);
114         enable_extras(@opt_enableextras);
115         disable_extras(@opt_disableextras);
116         list_extras;
117         print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
118         exit 0;
119 }
120
121 our $interactive = !(
122         !-t STDIN ||
123         !-t STDOUT ||
124         defined $opt_binary_dir ||
125         defined $opt_config_dir ||
126         defined $opt_data_dir ||
127         defined $opt_development ||
128         defined $opt_disable_auto_extras ||
129         defined $opt_disable_interactive ||
130         defined $opt_distribution_label ||
131         defined $opt_example_dir ||
132         defined $opt_gid ||
133         defined $opt_log_dir ||
134         defined $opt_manual_dir ||
135         defined $opt_module_dir ||
136         defined $opt_portable ||
137         defined $opt_prefix ||
138         defined $opt_script_dir ||
139         defined $opt_socketengine ||
140         defined $opt_system ||
141         defined $opt_uid
142 );
143
144 my %version = get_version $opt_distribution_label;
145 print_format "<|BOLD Configuring InspIRCd $version{FULL} on $^O.|>\n";
146
147 my %config;
148 if ($interactive) {
149         %config = read_config_file(CONFIGURE_CACHE_FILE);
150         run_test abs2rel(CONFIGURE_CACHE_FILE, $RealDir), %config;
151         if (!defined $config{VERSION}) {
152                 $config{VERSION} = CONFIGURE_CACHE_VERSION;
153         } elsif ($config{VERSION} != CONFIGURE_CACHE_VERSION) {
154                 print_warning "ignoring contents of ${\CONFIGURE_CACHE_FILE} as it was generated by an incompatible version of $0!";
155                 %config = ('VERSION', CONFIGURE_CACHE_VERSION);
156         }
157 }
158
159 $config{CXX} = find_compiler($config{CXX} // $ENV{CXX});
160 unless ($config{CXX}) {
161         say 'A suitable C++ compiler could not be detected on your system!';
162         unless ($interactive) {
163                 say 'Set the CXX environment variable to the path to a C++ compiler binary if this is incorrect.';
164                 exit 1;
165         }
166         until ($config{CXX}) {
167                 my $compiler_path = prompt_string 1, 'Please enter the path to a C++ compiler binary:', 'c++';
168                 $config{CXX} = find_compiler $compiler_path;
169         }
170 }
171 my %compiler = get_compiler_info($config{CXX});
172
173 $config{HAS_ARC4RANDOM_BUF} = run_test 'arc4random_buf()', test_file($config{CXX}, 'arc4random_buf.cpp');
174 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt');
175 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
176
177 my @socketengines;
178 push @socketengines, 'epoll'  if run_test 'epoll', test_header $config{CXX}, 'sys/epoll.h';
179 push @socketengines, 'kqueue' if run_test 'kqueue', test_file $config{CXX}, 'kqueue.cpp';
180 push @socketengines, 'poll'   if run_test 'poll', test_header $config{CXX}, 'poll.h';
181 push @socketengines, 'select';
182
183 if (defined $opt_socketengine) {
184         unless (grep { $_ eq $opt_socketengine } @socketengines) {
185                 my $reason = -f "src/socketengines/socketengine_$opt_socketengine.cpp" ? 'is not available on this platform' : 'does not exist';
186                 print_error "The socket engine you requested ($opt_socketengine) $reason!",
187                         'Available socket engines are:',
188                         map { "  * $_" } @socketengines;
189         }
190 }
191 $config{SOCKETENGINE} = $opt_socketengine // $socketengines[0];
192
193 if (defined $opt_portable) {
194         print_error '--portable and --system can not be used together!' if defined $opt_system;
195         $config{DESTDIR}     = catfile $RealDir, 'run', '';
196         $config{BASE_DIR}    = $opt_prefix      // '';
197         $config{BINARY_DIR}  = $opt_binary_dir  // 'bin';
198         $config{CONFIG_DIR}  = $opt_config_dir  // 'conf';
199         $config{DATA_DIR}    = $opt_data_dir    // 'data';
200         $config{EXAMPLE_DIR} = $opt_example_dir // catdir $config{CONFIG_DIR}, 'examples';
201         $config{LOG_DIR}     = $opt_log_dir     // 'logs';
202         $config{MANUAL_DIR}  = $opt_manual_dir  // 'manuals';
203         $config{MODULE_DIR}  = $opt_module_dir  // 'modules';
204         $config{SCRIPT_DIR}  = $opt_script_dir  // $config{BASE_DIR};
205 } elsif (defined $opt_system) {
206         $config{BASE_DIR}    = $opt_prefix      // '/var/lib/inspircd';
207         $config{BINARY_DIR}  = $opt_binary_dir  // '/usr/sbin';
208         $config{CONFIG_DIR}  = $opt_config_dir  // '/etc/inspircd';
209         $config{DATA_DIR}    = $opt_data_dir    // '/var/inspircd';
210         $config{EXAMPLE_DIR} = $opt_example_dir // '/usr/share/doc/inspircd';
211         $config{LOG_DIR}     = $opt_log_dir     // '/var/log/inspircd';
212         $config{MANUAL_DIR}  = $opt_manual_dir  // '/usr/share/man/man1';
213         $config{MODULE_DIR}  = $opt_module_dir  // '/usr/lib/inspircd';
214         $config{SCRIPT_DIR}  = $opt_script_dir  // '/usr/share/inspircd'
215 } else {
216         $config{BASE_DIR}    = rel2abs $opt_prefix // $config{BASE_DIR}    // catdir $RealDir,            'run';
217         $config{BINARY_DIR}  = $opt_binary_dir     // $config{BINARY_DIR}  // catdir $config{BASE_DIR},   'bin';
218         $config{CONFIG_DIR}  = $opt_config_dir     // $config{CONFIG_DIR}  // catdir $config{BASE_DIR},   'conf';
219         $config{DATA_DIR}    = $opt_data_dir       // $config{DATA_DIR}    // catdir $config{BASE_DIR},   'data';
220         $config{EXAMPLE_DIR} = $opt_example_dir    // $config{EXAMPLE_DIR} // catdir $config{CONFIG_DIR}, 'examples';
221         $config{LOG_DIR}     = $opt_log_dir        // $config{LOG_DIR}     // catdir $config{BASE_DIR},   'logs';
222         $config{MANUAL_DIR}  = $opt_manual_dir     // $config{MANUAL_DIR}  // catdir $config{BASE_DIR},   'manuals';
223         $config{MODULE_DIR}  = $opt_module_dir     // $config{MODULE_DIR}  // catdir $config{BASE_DIR},   'modules';
224         $config{SCRIPT_DIR}  = $opt_script_dir     // $config{SCRIPT_DIR}  // $config{BASE_DIR};
225 }
226
227 # Parse --gid=123 or --gid=foo and extract the group id.
228 my @group;
229 if (defined $opt_gid) {
230         @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
231         print_error "there is no '$opt_gid' group on this system!" unless @group;
232 } else {
233         @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} // getgid());
234         print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
235         unless ($group[2]) {
236                 print_warning <<"EOW";
237 You are building as the privileged $group[0] group and have not specified
238 an unprivileged group to run InspIRCd as.
239
240 This is almost never what you should do. You should probably either create a new
241 unprivileged user/group to build and run as or pass the '--gid [id|name]' flag
242 to specify an unprivileged group to run as.
243 EOW
244                 if (!prompt_bool $interactive, "Are you sure you want to build as the $group[0] group?", 0) {
245                         # PACKAGERS: You do not need to delete this check. Use `--gid $(id -g)` or `--gid 0` instead.
246                         say STDERR "If you are sure you want to build as the $group[0] group pass the --gid $group[2] flag." unless $interactive;
247                         exit 1;
248                 }
249         }
250 }
251 $config{GROUP} = $group[0];
252 $config{GID}   = $group[2];
253
254 # Parse --uid=123 or --uid=foo and extract the user id.
255 my @user;
256 if (defined $opt_uid) {
257         @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
258         print_error "there is no '$opt_uid' user on this system!" unless @user;
259 } else {
260         @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} // getuid());
261         print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
262         unless ($user[2]) {
263                 print_warning <<"EOW";
264 You are building as the privileged $user[0] user and have not specified
265 an unprivileged user to run InspIRCd as.
266
267 This is almost never what you should do. You should probably either create a new
268 unprivileged user/group to build and run as or pass the '--uid [id|name]' flag
269 to specify an unprivileged user to run as.
270 EOW
271                 if (!prompt_bool $interactive, "Are you sure you want to build as the $user[0] user?", 0) {
272                         # PACKAGERS: You do not need to delete this check. Use `--uid $(id -u)` or `--uid 0` instead.
273                         say STDERR "If you are sure you want to build as the $user[0] user pass the --uid $user[2] flag." unless $interactive;
274                         exit 1;
275                 }
276         }
277 }
278 $config{USER} = $user[0];
279 $config{UID}  = $user[2];
280
281 # Warn the user about clock drifting when running on OpenVZ.
282 if (-e '/proc/user_beancounters' || -e '/proc/vz/vzaquota') {
283         print_warning <<'EOW';
284 You are building InspIRCd inside of an OpenVZ container. If you
285 plan to use InspIRCd in this container then you should make sure that NTP is
286 configured on the Hardware Node. Failure to do so may result in clock drifting!
287 EOW
288 }
289
290 # Warn the user about OpenBSD shipping incredibly broken compilers/linkers.
291 if ($^O eq 'openbsd') {
292         print_warning <<'EOW';
293 You are building InspIRCd on OpenBSD. The C++ compilers and linkers
294 that OpenBSD ship are incredibly broken. You may have strange linker errors
295 and crashes. Please consider using a different OS like FreeBSD/NetBSD instead.
296 EOW
297 }
298
299 # Check that the user actually wants this version.
300 if (defined $version{REAL_LABEL}) {
301         print_warning <<'EOW';
302 You are building a development version. This contains code which has
303 not been tested as heavily and may contain various faults which could seriously
304 affect the running of your server. It is recommended that you use a stable
305 version instead.
306
307 You can obtain the latest stable version from https://www.inspircd.org or by
308 running `<|GREEN git checkout $(git describe --abbrev=0 --tags insp3)|>` if you are
309 installing from Git.
310 EOW
311         if (!prompt_bool $interactive, 'I understand this warning and want to continue anyway.', $opt_development // 0) {
312                 say STDERR 'If you understand this warning and still want to continue pass the --development flag.' unless $interactive;
313                 exit 1;
314         }
315 }
316
317 # Configure directory settings.
318 my $question = <<"EOQ";
319 Currently, InspIRCd is configured with the following paths:
320
321 <|BOLD Base:|>   $config{BASE_DIR}
322 <|BOLD Binary:|> $config{BINARY_DIR}
323 <|BOLD Config:|> $config{CONFIG_DIR}
324 <|BOLD Data:|>   $config{DATA_DIR}
325 <|BOLD Log:|>    $config{LOG_DIR}
326 <|BOLD Manual:|> $config{MANUAL_DIR}
327 <|BOLD Module:|> $config{MODULE_DIR}
328 <|BOLD Script:|> $config{SCRIPT_DIR}
329
330 Do you want to change these settings?
331 EOQ
332 if (prompt_bool $interactive, $question, 0) {
333         my $original_base_dir = $config{BASE_DIR};
334         $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
335         foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR SCRIPT_DIR)) {
336                 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
337         }
338         $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
339         $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
340         $config{DATA_DIR}   = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
341         $config{LOG_DIR}    = prompt_dir $interactive, 'In what directory are log files to be stored?',           $config{LOG_DIR};
342         $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?',        $config{MANUAL_DIR};
343         $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?',             $config{MODULE_DIR};
344         $config{SCRIPT_DIR} = prompt_dir $interactive, 'In what directory are scripts to be placed?',             $config{SCRIPT_DIR};
345         $config{EXAMPLE_DIR} = $config{CONFIG_DIR} . '/examples';
346 }
347
348 # Configure module settings.
349 $question = <<'EOQ';
350 Currently, InspIRCd is configured to automatically enable all available extra modules.
351
352 Would you like to enable extra modules manually?
353 EOQ
354 if (prompt_bool $interactive, $question, 0) {
355         foreach my $extra (<$RealDir/src/modules/extra/m_*.cpp>) {
356                 my $module_name = basename $extra, '.cpp';
357                 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
358                         enable_extras $module_name;
359                 }
360         }
361 } elsif (!defined $opt_disable_auto_extras) {
362         # TODO: finish modulemanager rewrite and replace this code with:
363         # system './modulemanager', 'enable', '--auto';
364         my %modules = (
365                 # Missing: m_ldap, m_regex_stdlib, m_ssl_mbedtls
366                 'm_geo_maxmind.cpp'     => 'pkg-config --exists libmaxminddb',
367                 'm_mysql.cpp'           => 'mysql_config --version',
368                 'm_pgsql.cpp'           => 'pg_config --version',
369                 'm_regex_pcre.cpp'      => 'pcre-config --version',
370                 'm_regex_posix.cpp'     => undef,
371                 'm_regex_re2.cpp'       => 'pkg-config --exists re2',
372                 'm_regex_tre.cpp'       => 'pkg-config --exists tre',
373                 'm_sqlite3.cpp'         => 'pkg-config --exists sqlite3',
374                 'm_ssl_gnutls.cpp'      => 'pkg-config --exists gnutls',
375                 'm_ssl_openssl.cpp'     => 'pkg-config --exists openssl',
376                 'm_sslrehashsignal.cpp' => undef,
377         );
378         while (my ($module, $command) = each %modules) {
379                 unless (defined $command && system "$command 1>/dev/null 2>/dev/null") {
380                         enable_extras $module;
381                 }
382         }
383 }
384
385 # Generate SSL certificates.
386 $question = <<EOQ;
387 Would you like to generate a self-signed SSL certificate now? This certificate
388 can be used for testing but <|BOLD should not|> be used on a production network.
389
390 Note: you can get a <|BOLD free|> CA-signed certificate from Let's Encrypt. See
391 https://letsencrypt.org/getting-started/ for more details.
392 EOQ
393
394 if (<$RealDir/src/modules/m_ssl_*.cpp>) {
395         if (prompt_bool $interactive, $question, $interactive) {
396                 create_directory CONFIGURE_DIRECTORY, 0750 or print_error "unable to create ${\CONFIGURE_DIRECTORY}: $!";
397                 system './tools/genssl', 'auto', CONFIGURE_DIRECTORY;
398         } else {
399                 my @pems = <${\CONFIGURE_DIRECTORY}/{cert,csr,dhparams,key}.pem>;
400                 $question = <<EOQ;
401 The following self-signed files were previously generated and will be installed
402 when you run Make. Do you want to delete them?
403
404   * ${\join "\n  * ", @pems}
405 EOQ
406                 if (@pems && prompt_bool $interactive, $question, 0) {
407                         unlink @pems;
408                 }
409         }
410 } elsif (!defined $opt_disable_auto_extras) {
411         print_warning <<"EOM";
412 You are building without enabling any SSL modules. This is not
413 recommended as SSL greatly enhances the security and privacy of your IRC server
414 and in a future version will be <|BOLD required|> for linking servers.
415
416 Please read the following documentation pages on how to enable SSL support:
417
418 GnuTLS (recommended): https://docs.inspircd.org/3/modules/ssl_gnutls
419 mbedTLS:              https://docs.inspircd.org/3/modules/ssl_mbedtls
420 OpenSSL:              https://docs.inspircd.org/3/modules/ssl_openssl
421 EOM
422 }
423
424 # Cache the distribution label so that its not lost when --update is run.
425 $config{DISTRIBUTION} = $opt_distribution_label if $opt_distribution_label;
426
427 write_configure_cache %config;
428 parse_templates \%config, \%compiler, \%version;
429
430 print_format <<"EOM";
431
432 Configuration is complete! You have chosen to build with the following settings:
433
434 <|GREEN Compiler:|>
435   <|GREEN Binary:|>  $config{CXX}
436   <|GREEN Name:|>    $compiler{NAME}
437   <|GREEN Version:|> $compiler{VERSION}
438
439 <|GREEN Extra Modules:|>
440 EOM
441
442 for my $file (<$RealDir/src/modules/m_*>) {
443         my $module = basename $file, '.cpp';
444         say "  * $module" if -l $file;
445 }
446
447 my @makeargs;
448 push @makeargs, "-C${\abs2rel $RealDir}" unless getcwd eq $RealDir;
449 push @makeargs, "-j${\(get_cpu_count() + 1)}";
450
451 print_format <<"EOM";
452
453 <|GREEN Paths:|>
454   <|GREEN Base:|>    $config{BASE_DIR}
455   <|GREEN Binary:|>  $config{BINARY_DIR}
456   <|GREEN Config:|>  $config{CONFIG_DIR}
457   <|GREEN Data:|>    $config{DATA_DIR}
458   <|GREEN Example:|> $config{EXAMPLE_DIR}
459   <|GREEN Log:|>     $config{LOG_DIR}
460   <|GREEN Manual:|>  $config{MANUAL_DIR}
461   <|GREEN Module:|>  $config{MODULE_DIR}
462   <|GREEN Script:|>  $config{SCRIPT_DIR}
463
464 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
465 <|GREEN Execution User:|>  $config{USER} ($config{UID})
466 <|GREEN Socket Engine:|>   $config{SOCKETENGINE}
467
468 To build with these settings run '<|GREEN make ${\join ' ', @makeargs} install|>' now.
469
470 EOM
471
472 # Routine to list out the extra/ modules that have been enabled.
473 # Note: when getting any filenames out and comparing, it's important to lc it if the
474 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
475 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
476 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
477 sub list_extras () {
478         use File::Spec;
479         # @_ not used
480         my $srcdir = File::Spec->catdir("src", "modules");
481         my $abs_srcdir = File::Spec->rel2abs($srcdir);
482         local $_;
483         my $dd;
484         opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
485         my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
486         closedir $dd;
487         undef $dd;
488         opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
489         my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
490         closedir $dd;
491         undef $dd;
492         my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
493         my %extras = ();
494 EXTRA:  for my $extra (@extras) {
495                 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
496                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
497                 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
498                 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
499                 if (-l $abs_source) {
500                         # Symlink, is it in the right place?
501                         my $targ = readlink($abs_source);
502                         my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
503                         if ($abs_targ eq $abs_extra) {
504                                 $extras{$extra} = "\e[32;1menabled\e[0m";
505                         } else {
506                                 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
507                         }
508                 } elsif (-e $abs_source) {
509                         my ($devext, $inoext) = stat($abs_extra);
510                         my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
511                         if ($lnksrc > 1) {
512                                 if ($devsrc == $devext && $inosrc == $inoext) {
513                                         $extras{$extra} = "\e[32;1menabled\e[0m";
514                                 } else {
515                                         $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
516                                 }
517                         } else {
518                                 open my $extfd, "<", $abs_extra;
519                                 open my $srcfd, "<", $abs_source;
520                                 local $/ = undef;
521                                 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
522                                         $extras{$extra} = "\e[32;1menabled\e[0m";
523                                 } else {
524                                         $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
525                                 }
526                         }
527                 } else {
528                         $extras{$extra} = "\e[33;1mdisabled\e[0m";
529                 }
530         }
531         # Now let's add dependency info
532         for my $extra (keys(%extras)) {
533                 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
534                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
535                 my @deps = split /\s+/, get_directive($abs_extra, 'ModDep', '');
536                 for my $dep (@deps) {
537                         if (exists($extras{$dep})) {
538                                 my $ref = \$extras{$dep}; # Take reference.
539                                 if ($$ref !~ m/needed by/) {
540                                         # First dependency found.
541                                         if ($$ref =~ m/enabled/) {
542                                                 $$ref .= " (needed by \e[32;1m$extra\e[0m";
543                                         } else {
544                                                 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
545                                                 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
546                                         }
547                                 } else {
548                                         if ($$ref =~ m/enabled/) {
549                                                 $$ref .= ", \e[32;1m$extra\e[0m";
550                                         } else {
551                                                 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
552                                         }
553                                 }
554                         }
555                 }
556         }
557         for my $extra (sort {$a cmp $b} keys(%extras)) {
558                 my $text = $extras{$extra};
559                 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
560                         printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
561                 } else {
562                         printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
563                 }
564         }
565         return keys(%extras) if wantarray; # Can be used by manage_extras.
566 }
567
568 sub enable_extras(@) {
569         my $moduledir = catdir $RealDir, 'src', 'modules';
570         my $extradir = catdir $moduledir, 'extra';
571
572         for my $extra (@_) {
573                 my $shortname = $extra =~ s/(?:^m_|\.cpp$)//gr;
574                 my $extrafile = "m_$shortname.cpp";
575
576                 my $extrapath = catfile $extradir, $extrafile;
577                 if (!-f $extrapath) {
578                         print_error "<|GREEN $extra|> is not an extra module!";
579                 }
580
581                 my $modulepath = catfile $moduledir, $extrafile;
582                 if (-l $modulepath) {
583                         if (readlink($modulepath) ne $extrapath) {
584                                 unlink $modulepath; # Remove the dead symlink.
585                         } else {
586                                 next; # Module is already enabled.
587                         }
588                 }
589
590                 if (-e $modulepath) {
591                         print_error "unable to symlink <|GREEN ${\abs2rel $modulepath}|> to <|GREEN ${\abs2rel $extrapath}|>: the target exists and is not a symlink.";
592                 } else {
593                         print_format "Enabling the <|GREEN $shortname|> module ...\n";
594                         symlink $extrapath, $modulepath or print_error "unable to symlink <|GREEN ${\abs2rel $modulepath}|> to <|GREEN ${\abs2rel $extrapath}|>: $!";
595                 }
596         }
597 }
598
599 sub disable_extras(@) {
600         my $moduledir = catdir $RealDir, 'src', 'modules';
601         my $extradir = catdir $moduledir, 'extra';
602
603         for my $extra (@_) {
604                 my $shortname = $extra =~ s/(?:^m_|\.cpp$)//gr;
605                 my $extrafile = "m_$shortname.cpp";
606
607                 my $modulepath = catfile $moduledir, $extrafile;
608                 my $extrapath = catfile $extradir, $extrafile;
609                 if (!-e $modulepath && !-e $extrapath) {
610                         print_error "the <|GREEN $shortname|> module does not exist!";
611                 } elsif (!-e $modulepath && -e $extrapath) {
612                         print_error "the <|GREEN $shortname|> module is not currently enabled!";
613                 } elsif ((-e $modulepath && !-e $extrapath) || !-l $modulepath) {
614                         print_error "the <|GREEN $shortname|> module is not an extra module!";
615                 } else {
616                         print_format "Disabling the <|GREEN $shortname|> module ...\n";
617                         unlink $modulepath or print_error "unable to unlink <|GREEN $extrapath|>: $!";
618                 }
619         }
620 }