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