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