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