]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - configure
Allow customising the example config directory at build time.
[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 }
327
328 # Configure module settings.
329 $question = <<'EOQ';
330 Currently, InspIRCd is configured to automatically enable all available extra modules.
331
332 Would you like to enable extra modules manually?
333 EOQ
334 if (prompt_bool $interactive, $question, 0) {
335         foreach my $extra (<src/modules/extra/m_*.cpp>) {
336                 my $module_name = basename $extra, '.cpp';
337                 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
338                         enable_extras "$module_name.cpp";
339                 }
340         }
341 } elsif (!defined $opt_disable_auto_extras) {
342         # TODO: finish modulemanager rewrite and replace this code with:
343         # system './modulemanager', 'enable', '--auto';
344         my %modules = (
345                 # Missing: m_ldap, m_regex_stdlib, m_ssl_mbedtls
346                 'm_geo_maxmind.cpp'     => 'pkg-config --exists libmaxminddb',
347                 'm_mysql.cpp'           => 'mysql_config --version',
348                 'm_pgsql.cpp'           => 'pg_config --version',
349                 'm_regex_pcre.cpp'      => 'pcre-config --version',
350                 'm_regex_posix.cpp'     => undef,
351                 'm_regex_re2.cpp'       => 'pkg-config --exists re2',
352                 'm_regex_tre.cpp'       => 'pkg-config --exists tre',
353                 'm_sqlite3.cpp'         => 'pkg-config --exists sqlite3',
354                 'm_ssl_gnutls.cpp'      => 'pkg-config --exists gnutls',
355                 'm_ssl_openssl.cpp'     => 'pkg-config --exists openssl',
356                 'm_sslrehashsignal.cpp' => undef,
357         );
358         while (my ($module, $command) = each %modules) {
359                 unless (defined $command && system "$command 1>/dev/null 2>/dev/null") {
360                         enable_extras $module;
361                 }
362         }
363 }
364
365 # Generate SSL certificates.
366 $question = <<EOQ;
367 Would you like to generate a self-signed SSL certificate now? This certificate
368 can be used for testing but <|BOLD should not|> be used on a production network.
369
370 Note: you can get a <|BOLD free|> CA-signed certificate from Let's Encrypt. See
371 https://letsencrypt.org/getting-started/ for more details.
372 EOQ
373
374 if (<src/modules/m_ssl_*.cpp>) {
375         if (prompt_bool $interactive, $question, $interactive) {
376                 system './tools/genssl', 'auto';
377         }
378 } else {
379         print_warning <<"EOM";
380 You are building without enabling any SSL modules. This is not
381 recommended as SSL greatly enhances the security and privacy of your IRC server
382 and in a future version will be <|BOLD required|> for linking servers.
383
384 Please read the following documentation pages on how to enable SSL support:
385
386 GnuTLS (recommended): https://docs.inspircd.org/3/modules/ssl_gnutls
387 mbedTLS:              https://docs.inspircd.org/3/modules/ssl_mbedtls
388 OpenSSL:              https://docs.inspircd.org/3/modules/ssl_openssl
389 EOM
390 }
391
392 # Cache the distribution label so that its not lost when --update is run.
393 $config{DISTRIBUTION} = $opt_distribution_label if $opt_distribution_label;
394
395 write_configure_cache %config;
396 parse_templates \%config, \%compiler, \%version;
397
398 print_format <<"EOM";
399
400 Configuration is complete! You have chosen to build with the following settings:
401
402 <|GREEN Compiler:|>
403   <|GREEN Binary:|>  $config{CXX}
404   <|GREEN Name:|>    $compiler{NAME}
405   <|GREEN Version:|> $compiler{VERSION}
406
407 <|GREEN Extra Modules:|>
408 EOM
409
410 for my $file (<src/modules/m_*>) {
411         my $module = basename $file, '.cpp';
412         say "  * $module" if -l $file;
413 }
414
415 print_format <<"EOM";
416
417 <|GREEN Paths:|>
418   <|GREEN Base:|>    $config{BASE_DIR}
419   <|GREEN Binary:|>  $config{BINARY_DIR}
420   <|GREEN Config:|>  $config{CONFIG_DIR}
421   <|GREEN Data:|>    $config{DATA_DIR}
422   <|GREEN Example:|> $config{EXAMPLE_DIR}
423   <|GREEN Log:|>     $config{LOG_DIR}
424   <|GREEN Manual:|>  $config{MANUAL_DIR}
425   <|GREEN Module:|>  $config{MODULE_DIR}
426   <|GREEN Script:|>  $config{SCRIPT_DIR}
427
428 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
429 <|GREEN Execution User:|>  $config{USER} ($config{UID})
430 <|GREEN Socket Engine:|>   $config{SOCKETENGINE}
431
432 To build with these settings run '<|GREEN make -j${\get_cpu_count} install|>' now.
433
434 EOM
435
436 # Routine to list out the extra/ modules that have been enabled.
437 # Note: when getting any filenames out and comparing, it's important to lc it if the
438 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
439 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
440 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
441 sub list_extras () {
442         use File::Spec;
443         # @_ not used
444         my $srcdir = File::Spec->catdir("src", "modules");
445         my $abs_srcdir = File::Spec->rel2abs($srcdir);
446         local $_;
447         my $dd;
448         opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
449         my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
450         closedir $dd;
451         undef $dd;
452         opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
453         my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
454         closedir $dd;
455         undef $dd;
456         my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
457         my %extras = ();
458 EXTRA:  for my $extra (@extras) {
459                 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
460                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
461                 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
462                 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
463                 if (-l $abs_source) {
464                         # Symlink, is it in the right place?
465                         my $targ = readlink($abs_source);
466                         my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
467                         if ($abs_targ eq $abs_extra) {
468                                 $extras{$extra} = "\e[32;1menabled\e[0m";
469                         } else {
470                                 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
471                         }
472                 } elsif (-e $abs_source) {
473                         my ($devext, $inoext) = stat($abs_extra);
474                         my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
475                         if ($lnksrc > 1) {
476                                 if ($devsrc == $devext && $inosrc == $inoext) {
477                                         $extras{$extra} = "\e[32;1menabled\e[0m";
478                                 } else {
479                                         $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
480                                 }
481                         } else {
482                                 open my $extfd, "<", $abs_extra;
483                                 open my $srcfd, "<", $abs_source;
484                                 local $/ = undef;
485                                 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
486                                         $extras{$extra} = "\e[32;1menabled\e[0m";
487                                 } else {
488                                         $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
489                                 }
490                         }
491                 } else {
492                         $extras{$extra} = "\e[33;1mdisabled\e[0m";
493                 }
494         }
495         # Now let's add dependency info
496         for my $extra (keys(%extras)) {
497                 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
498                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
499                 my @deps = split /\s+/, get_directive($abs_extra, 'ModDep', '');
500                 for my $dep (@deps) {
501                         if (exists($extras{$dep})) {
502                                 my $ref = \$extras{$dep}; # Take reference.
503                                 if ($$ref !~ m/needed by/) {
504                                         # First dependency found.
505                                         if ($$ref =~ m/enabled/) {
506                                                 $$ref .= " (needed by \e[32;1m$extra\e[0m";
507                                         } else {
508                                                 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
509                                                 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
510                                         }
511                                 } else {
512                                         if ($$ref =~ m/enabled/) {
513                                                 $$ref .= ", \e[32;1m$extra\e[0m";
514                                         } else {
515                                                 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
516                                         }
517                                 }
518                         }
519                 }
520         }
521         for my $extra (sort {$a cmp $b} keys(%extras)) {
522                 my $text = $extras{$extra};
523                 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
524                         printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
525                 } else {
526                         printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
527                 }
528         }
529         return keys(%extras) if wantarray; # Can be used by manage_extras.
530 }
531
532 sub enable_extras (@) {
533         my (@extras) = @_;
534         for my $extra (@extras) {
535                 my $extrapath = "src/modules/extra/$extra";
536                 if (!-e $extrapath) {
537                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
538                         next;
539                 }
540                 my $source = "src/modules/$extra";
541                 if (-e $source) {
542                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
543                         next;
544                 }
545                 # Get dependencies, and add them to be processed.
546                 my @deps = split /\s+/, get_directive($extrapath, 'ModDep', '');
547                 for my $dep (@deps) {
548                         next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
549                         if (!-e "src/modules/$dep" && !-e "include/$dep") {
550                                 if (-e "src/modules/extra/$dep") {
551                                         print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
552                                         push @extras, $dep;
553                                 } else {
554                                         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";
555                                 }
556                         }
557                 }
558                 print "Enabling $extra ... \n";
559                 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
560         }
561 }
562
563 sub disable_extras (@)
564 {
565         opendir my $dd, "src/modules/extra/";
566         my @files = readdir($dd);
567         closedir $dd;
568         my (@extras) = @_;
569 EXTRA:  for my $extra (@extras) {
570                 my $extrapath = "src/modules/extra/$extra";
571                 my $source = "src/modules/$extra";
572                 if (!-e $extrapath) {
573                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
574                         next;
575                 }
576                 if ((! -l $source) || readlink($source) ne "extra/$extra") {
577                         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";
578                         next;
579                 }
580                 # Check if anything needs this.
581                 for my $file (@files) {
582                         my @deps = split /\s+/, get_directive("src/modules/extra/$file", 'ModDep', '');
583                         # File depends on this extra...
584                         if (scalar(grep { $_ eq $extra } @deps) > 0) {
585                                 # And is both enabled and not about to be disabled.
586                                 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
587                                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
588                                         next EXTRA;
589                                 }
590                         }
591                 }
592                 # Now remove.
593                 print "Disabling $extra ... \n";
594                 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";
595         }
596 }