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