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