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