]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - configure
m_spanningtree Remove SpanningTreeUtilities* fields and parameters
[user/henk/code/inspircd.git] / configure
1 #!/usr/bin/env perl
2
3 #
4 # InspIRCd -- Internet Relay Chat Daemon
5 #
6 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
7 #   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
8 #   Copyright (C) 2003, 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
9 #   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
10 #   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
11 #   Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
12 #   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
13 #   Copyright (C) 2003-2006 Craig McLure <craig@chatspike.net>
14 #
15 # This file is part of InspIRCd.  InspIRCd is free software: you can
16 # redistribute it and/or modify it under the terms of the GNU General Public
17 # License as published by the Free Software Foundation, version 2.
18 #
19 # This program is distributed in the hope that it will be useful, but WITHOUT
20 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
22 # details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
26 #
27
28
29 BEGIN {
30         require 5.8.0;
31 }
32
33 use strict;
34 use warnings FATAL => qw(all);
35
36 use File::Copy ();
37 use File::Spec::Functions qw(rel2abs);
38 use Cwd;
39 use Getopt::Long;
40
41 use make::configure;
42 use make::utilities;
43
44 our ($opt_use_gnutls, $opt_use_openssl, $opt_nointeractive, $opt_socketengine,
45      $opt_system, $opt_uid, $opt_base_dir, $opt_config_dir, $opt_module_dir, $opt_binary_dir,
46      $opt_data_dir, $opt_log_dir);
47
48 sub list_extras ();
49
50 sub enable_extras (@);
51
52 sub disable_extras (@);
53
54 my @opt_enableextras;
55 my @opt_disableextras;
56
57 GetOptions (
58         'enable-gnutls' => \$opt_use_gnutls,
59         'system' => \$opt_system,
60         'uid=s' => \$opt_uid,
61         'enable-openssl' => \$opt_use_openssl,
62         'disable-interactive' => \$opt_nointeractive,
63         'socketengine=s' => \$opt_socketengine,
64         'prefix=s' => \$opt_base_dir,
65         'config-dir=s' => \$opt_config_dir,
66         'module-dir=s' => \$opt_module_dir,
67         'binary-dir=s' => \$opt_binary_dir,
68         'data-dir=s' => \$opt_data_dir,
69         'log-dir=s' => \$opt_log_dir,
70         'help'  => sub { showhelp(); },
71         'update' => sub { update(); },
72         'clean' => sub { clean(); },
73         'list-extras' => sub { list_extras; exit 0; }, # This, --enable-extras, and --disable-extras are for non-interactive managing.
74         'enable-extras=s@' => \@opt_enableextras, # ^
75         'disable-extras=s@' => \@opt_disableextras, # ^
76 );
77
78 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
79         @opt_enableextras = split /,/, join(',', @opt_enableextras);
80         @opt_disableextras = split /,/, join(',', @opt_disableextras);
81         enable_extras(@opt_enableextras);
82         disable_extras(@opt_disableextras);
83         list_extras;
84         print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
85         exit 0;
86 }
87
88 our $interactive = !(
89         (defined $opt_base_dir) ||
90         (defined $opt_config_dir) ||
91         (defined $opt_module_dir) ||
92         (defined $opt_base_dir) ||
93         (defined $opt_binary_dir) ||
94         (defined $opt_data_dir) ||
95         (defined $opt_log_dir) ||
96         (defined $opt_nointeractive) ||
97         (defined $opt_socketengine) ||
98         (defined $opt_use_openssl) ||
99         (defined $opt_system) ||
100         (defined $opt_uid) ||
101         (defined $opt_use_gnutls)
102 );
103
104 our $topdir = getcwd();
105 our @modlist = ();
106 our %config = ();
107 our $cache_loaded = getcache();
108
109 $config{BASE_DIR} = $topdir."/run";
110
111 if (defined $opt_base_dir) {
112         $config{BASE_DIR} = $opt_base_dir;
113 } elsif (defined $opt_system) {
114         $config{BASE_DIR} = '/var/lib/inspircd';
115 }
116
117 if (defined $opt_system) {
118         $config{UID} = $opt_uid || 'ircd';
119         $config{CONFIG_DIR} = '/etc/inspircd';
120         $config{MODULE_DIR} = '/usr/lib/inspircd';
121         $config{BINARY_DIR} = '/usr/sbin/';
122         $config{BUILD_DIR} = $topdir."/build";
123         $config{DATA_DIR} = '/var/inspircd';
124         $config{LOG_DIR} = '/var/log/inspircd';
125 } else {
126         $config{UID} = $opt_uid || $<;
127         $config{CONFIG_DIR} = rel2abs($config{BASE_DIR}."/conf");
128         $config{MODULE_DIR} = rel2abs($config{BASE_DIR}."/modules");
129         $config{BINARY_DIR} = rel2abs($config{BASE_DIR}."/bin");
130         $config{BUILD_DIR} = rel2abs($topdir."/build");
131         $config{DATA_DIR} = rel2abs($config{BASE_DIR}."/data");
132         $config{LOG_DIR} = rel2abs($config{BASE_DIR}."/logs");  
133 }
134
135 if (defined $opt_config_dir) {
136         $config{CONFIG_DIR} = $opt_config_dir;
137 }
138 if (defined $opt_module_dir) {
139         $config{MODULE_DIR} = $opt_module_dir;
140 }
141 if (defined $opt_binary_dir) {
142         $config{BINARY_DIR} = $opt_binary_dir;
143 }
144 if (defined $opt_data_dir) {
145         $config{DATA_DIR} = $opt_data_dir;
146 }
147 if (defined $opt_log_dir) {
148         $config{LOG_DIR} = $opt_log_dir;
149 }
150 chomp($config{HAS_GNUTLS}   = `pkg-config --modversion gnutls 2>/dev/null`);
151 chomp($config{HAS_OPENSSL}  = `pkg-config --modversion openssl 2>/dev/null`);
152
153 chomp(our $gnutls_ver = $config{HAS_GNUTLS});
154 chomp(our $openssl_ver = $config{HAS_OPENSSL});
155 $config{USE_GNUTLS}         = "n";
156 if (defined $opt_use_gnutls)
157 {
158         $config{USE_GNUTLS} = "y";                                      # Use gnutls.
159 }
160 $config{USE_OPENSSL}    = "n";                                          # Use openssl.
161 if (defined $opt_use_openssl)
162 {
163         $config{USE_OPENSSL} = "y";
164 }
165
166 $config{STARTSCRIPT} = $^O eq 'darwin' ? 'org.inspircd.plist' : 'inspircd';
167
168 $config{CXX} = defined $ENV{CXX} && !system("$ENV{CXX} -v > /dev/null 2>&1") ? $ENV{CXX} : find_compiler();
169 if ($config{CXX} eq "") {
170         print "A C++ compiler could not be detected on your system!\n";
171         print "Set the CXX environment variable to the full path if this is incorrect.\n";
172         exit 1; 
173 }
174
175 our %cxx = get_compiler_info($config{CXX});
176 if ($cxx{UNSUPPORTED}) {
177         print "Your C++ compiler is too old to build InspIRCd!\n";
178         print "Reason: $cxx{REASON}\n";
179         exit 1;
180 }
181
182 if ($config{HAS_OPENSSL} =~ /^([-[:digit:].]+)(?:[a-z])?(?:\-[a-z][0-9])?/) {
183         $config{HAS_OPENSSL} = $1;
184 } else {
185         $config{HAS_OPENSSL} = "";
186 }
187
188 # Get and Set some important vars..
189 getmodules();
190
191 sub clean
192 {
193         unlink(".config.cache");
194 }
195
196 sub update
197 {
198         eval {
199                 # Does the cache file exist?
200                 if (!getcache()) {
201                         # No, No it doesn't.. *BASH*
202                         print "You have not run ./configure before. Please do this before trying to run the update script.\n";
203                         exit 1;
204                 } else {
205                         # We've Loaded the cache file and all our variables..
206                         $topdir = getcwd();
207                         getmodules();
208                         print "Updating files...\n";
209                         %cxx = get_compiler_info($config{CXX});
210                         writefiles();
211                         makecache();
212                         print "Complete.\n";
213                         exit;
214                 }
215         };
216         if ($@)
217         {
218                 print "Configure update failed: $@\n";
219         }
220         exit;
221 }
222
223 print "Running non-interactive configure...\n" unless $interactive;
224 print "Checking for cache from previous configure... ";
225 print ($cache_loaded ? "found\n" : "not found\n");
226
227 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', '-lrt');
228 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
229
230 if ($config{HAS_EPOLL} = run_test 'epoll', test_header($config{CXX}, 'sys/epoll.h')) {
231         $config{SOCKETENGINE} ||= 'epoll';
232 }
233
234 if ($config{HAS_KQUEUE} = run_test 'kqueue', test_file($config{CXX}, 'kqueue.cpp')) {
235         $config{SOCKETENGINE} ||= 'kqueue';
236 }
237
238 if ($config{HAS_PORTS} = run_test 'Solaris IOCP', test_header($config{CXX}, 'port.h')) {
239         $config{SOCKETENGINE} ||= 'ports';
240 }
241
242 if ($config{HAS_POLL} = run_test 'poll', test_header($config{CXX}, 'poll.h')) {
243         $config{SOCKETENGINE} ||= 'poll';
244 }
245
246 # Select is available on all platforms
247 $config{HAS_SELECT} = 1;
248 $config{SOCKETENGINE} ||= "select";
249
250 if (defined $opt_socketengine) {
251         my $cfgkey = "HAS_" . uc $opt_socketengine;
252         if ($config{$cfgkey} && -f "src/socketengines/socketengine_$opt_socketengine.cpp") {
253                 $config{SOCKETENGINE} = $opt_socketengine;
254         } else {
255                 print "Unable to use a socket engine which is not supported on this platform ($opt_socketengine)!\n";
256                 print "Available socket engines are:";
257                 foreach (<src/socketengines/socketengine_*.cpp>) {
258                         s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
259                         print " $1" if $config{"HAS_" . uc $1};
260                 }
261                 print "\n";     
262                 exit 1;
263         }
264 }
265
266 print "Checking for libgnutls... ";
267 if (defined($config{HAS_GNUTLS}) && (($config{HAS_GNUTLS}) || ($config{HAS_GNUTLS} eq "y"))) {
268         if (defined($gnutls_ver) && ($gnutls_ver ne "")) {
269                 print "yes\n";
270                 $config{HAS_GNUTLS} = "y";
271         } else {
272                 print "no\n";
273                 $config{HAS_GNUTLS} = "n";
274         }
275 } else {
276         print "no\n";
277         $config{HAS_GNUTLS} = "n";
278 }
279
280 print "Checking for openssl... ";
281 if (defined($config{HAS_OPENSSL}) && (($config{HAS_OPENSSL}) || ($config{HAS_OPENSSL} eq "y"))) {
282         if (defined($openssl_ver) && ($openssl_ver ne "")) {
283                 print "yes\n";
284                 $config{HAS_OPENSSL} = "y";
285         } else {
286                 print "no\n";
287                 $config{HAS_OPENSSL} = "n";
288         }
289 } else {
290         print "no\n";
291         $config{HAS_OPENSSL} = "n";
292 }
293
294 if ($interactive)
295 {
296         # Clear the screen.
297         system 'tput', 'clear';
298
299         my $revision = getrevision();
300         chomp(my $version = `sh src/version.sh`);
301
302         # Display Introduction Message..
303         print <<"STOP" ;
304 Welcome to the \e[1mInspIRCd\e[0m Configuration program! (\e[1minteractive mode\e[0m)
305 \e[1mPackage maintainers: Type ./configure --help for non-interactive help\e[0m
306
307 *** If you are unsure of any of these values, leave it blank for    ***
308 *** standard settings that will work, and your server will run      ***
309 *** using them. Please consult your IRC network admin if in doubt.  ***
310
311 Press \e[1m<RETURN>\e[0m to accept the default for any option, or enter
312 a new value. Please note: You will \e[1mHAVE\e[0m to read the docs
313 dir, otherwise you won't have a config file!
314
315 Your operating system is: \e[1;32m$^O\e[0m 
316 STOP
317         print "Your InspIRCd version is: \e[1;32m";
318         print $revision eq 'release' ? substr($version, 9) : substr($revision, 1);
319         print "\e[0m\n\n";
320         print "The following compiler has been detected: \e[1;32m$cxx{NAME} $cxx{VERSION}\e[0m ($config{CXX})\n\n";
321
322         # Directory Settings..
323         my $tmpbase = $config{BASE_DIR};
324         dir_check("do you wish to install the InspIRCd base", "BASE_DIR");
325         if ($tmpbase ne $config{BASE_DIR}) {
326                 $config{CONFIG_DIR} = rel2abs($config{BASE_DIR}."/conf");
327                 $config{MODULE_DIR} = rel2abs($config{BASE_DIR}."/modules");
328                 $config{DATA_DIR} = rel2abs($config{BASE_DIR}."/data");
329                 $config{LOG_DIR} = rel2abs($config{BASE_DIR}."/logs");
330                 $config{BINARY_DIR} = rel2abs($config{BASE_DIR}."/bin");
331         }
332
333         dir_check("are the configuration files", "CONFIG_DIR");
334         dir_check("are the modules to be compiled to", "MODULE_DIR");
335         dir_check("is the IRCd binary to be placed", "BINARY_DIR");
336         dir_check("are variable data files to be located in", "DATA_DIR");
337         dir_check("are the logs to be stored in", "LOG_DIR");
338         dir_check("do you want the build to take place", "BUILD_DIR");
339                 
340         my $chose_hiperf = 0;
341         if ($config{HAS_KQUEUE}) {
342                 $config{USE_KQUEUE} = "y";
343                 yesno('USE_KQUEUE',"You are running a BSD operating system, and kqueue\nwas detected. Would you like to enable kqueue support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable kqueue?");
344                 print "\n";
345                 if ($config{USE_KQUEUE} eq "y") {
346                         $config{SOCKETENGINE} = "kqueue";
347                         $chose_hiperf = 1;
348                 }
349         }
350         if ($config{HAS_EPOLL}) {
351                 $config{USE_EPOLL} = "y";
352                 yesno('USE_EPOLL',"You are running a Linux 2.6+ operating system, and epoll\nwas detected. Would you like to enable epoll support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable epoll?");
353                 print "\n";
354                 if ($config{USE_EPOLL} eq "y") {
355                         $config{SOCKETENGINE} = "epoll";
356                         $chose_hiperf = 1;
357                 }
358         }
359         if ($config{HAS_PORTS}) {
360                 $config{USE_PORTS} = "y";
361                 yesno('USE_PORTS',"You are running Solaris 10.\nWould you like to enable I/O completion ports support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable support for I/O completion ports?");
362                 print "\n";
363                 if ($config{USE_PORTS} eq "y") {
364                         $config{SOCKETENGINE} = "ports";
365                         $chose_hiperf = 1;
366                 }
367         }
368
369         if (!$chose_hiperf && $config{HAS_POLL}) {
370                 $config{USE_POLL} = "y";
371                 yesno('USE_POLL', "Would you like to use poll?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable poll?");
372                 if ($config{USE_POLL} eq "y") {
373                         $config{SOCKETENGINE} = "poll";
374                 }
375         }
376         if (!$chose_hiperf && $config{USE_POLL} ne "y")
377         {
378                 print "No high-performance socket engines are available, or you chose\n";
379                 print "not to enable one. Defaulting to select() engine.\n\n";
380                 $config{SOCKETENGINE} = "select";
381         }
382
383         $config{USE_SSL} = "n";
384
385         if ($config{HAS_GNUTLS} eq "y" || $config{HAS_OPENSSL} eq "y")
386         {
387                 print "Detected GnuTLS version: \e[1;32m" . $gnutls_ver . "\e[0m\n";
388                 print "Detected OpenSSL version: \e[1;32m" . $openssl_ver . "\e[0m\n\n";
389
390                 yesno('USE_SSL', "One or more SSL libraries detected. Would you like to enable SSL support?");
391                 if ($config{USE_SSL} eq "y")
392                 {
393                         if ($config{HAS_GNUTLS} eq "y")
394                         {
395                                 yesno('USE_GNUTLS',"Would you like to enable SSL with m_ssl_gnutls? (recommended)");
396                                 if ($config{USE_GNUTLS} eq "y")
397                                 {
398                                         print "\nUsing GnuTLS SSL module.\n";
399                                 }
400                         }
401
402                         if ($config{HAS_OPENSSL} eq "y")
403                         {
404                                 yesno('USE_OPENSSL', "Would you like to enable SSL with m_ssl_openssl?");
405                                 if ($config{USE_OPENSSL} eq "y")
406                                 {
407                                         print "\nUsing OpenSSL SSL module.\n";
408                                 }
409                         }
410                 }
411         }
412         else
413         {
414                 print "\nCould not detect OpenSSL or GnuTLS. Make sure pkg-config is installed and\n";
415                 print "is in your path.\n\n";
416         }
417 }
418
419 # We are on a POSIX system, we can enable POSIX extras without asking
420 symlink "extra/m_regex_posix.cpp", "src/modules/m_regex_posix.cpp";
421
422 if (($config{USE_GNUTLS} eq "y") && ($config{HAS_GNUTLS} ne "y"))
423 {
424         print "Sorry, but i couldn't detect gnutls. Make sure pkg-config is in your path.\n";
425         exit 1;
426 }
427 if (($config{USE_OPENSSL} eq "y") && ($config{HAS_OPENSSL} ne "y"))
428 {
429         print "Sorry, but i couldn't detect openssl. Make sure pkg-config is in your path.\n";
430         exit 1;
431 }
432
433 $config{CERTGEN} ||= 'y';
434 yesno('CERTGEN',"Would you like generate SSL certificates now?") if ($interactive && ($config{USE_GNUTLS} eq "y" || $config{USE_OPENSSL} eq "y"));
435
436 if ($config{USE_GNUTLS} eq "y") {
437         unless (-r "src/modules/m_ssl_gnutls.cpp") {
438                 print "Symlinking src/modules/m_ssl_gnutls.cpp from extra/\n";
439                 symlink "extra/m_ssl_gnutls.cpp", "src/modules/m_ssl_gnutls.cpp" or print STDERR "Symlink failed: $!";
440         }
441         if ($interactive && $config{CERTGEN} eq 'y')
442         {
443                 unless (-r "$config{CONFIG_DIR}/key.pem" && -r "$config{CONFIG_DIR}/cert.pem" && -r "$config{CONFIG_DIR}/dhparams.pem") {
444                         unless (system './tools/genssl gnutls') {
445                                 print "\nCertificate generation complete, copying to config directory... ";
446                                 File::Copy::move("key.pem", "$config{CONFIG_DIR}/key.pem") or print STDERR "Could not copy key.pem!\n";
447                                 File::Copy::move("cert.pem", "$config{CONFIG_DIR}/cert.pem") or print STDERR "Could not copy cert.pem!\n";
448                                 File::Copy::move("dhparams.pem", "$config{CONFIG_DIR}/dhparams.pem") or print STDERR "Could not copy dhparams.pem!\n";
449                                 print "Done.\n\n";
450                         }
451                 }
452                 else {
453                         print "SSL Certificates found, skipping.\n\n";
454                 }
455         }
456         else
457         {
458                 print "Skipping SSL certificate generation\nin non-interactive mode.\n\n";
459         }
460 }
461
462 if ($config{USE_OPENSSL} eq "y") {
463         unless (-r "src/modules/m_ssl_openssl.cpp") {
464                 print "Symlinking src/modules/m_ssl_openssl.cpp from extra/\n";
465                 symlink "extra/m_ssl_openssl.cpp", "src/modules/m_ssl_openssl.cpp" or print STDERR "Symlink failed: $!";
466         }
467         if ($interactive && $config{CERTGEN} eq 'y')
468         {
469                 unless (-r "$config{CONFIG_DIR}/key.pem" && -r "$config{CONFIG_DIR}/cert.pem" && -r "$config{CONFIG_DIR}/dhparams.pem") {
470                         unless (system './tools/genssl openssl') {
471                                 print "\nCertificate generation complete, copying to config directory... ";
472                                 File::Copy::move("key.pem", "$config{CONFIG_DIR}/key.pem") or print STDERR "Could not copy key.pem!\n";
473                                 File::Copy::move("cert.pem", "$config{CONFIG_DIR}/cert.pem") or print STDERR "Could not copy cert.pem!\n";
474                                 File::Copy::move("dhparams.pem", "$config{CONFIG_DIR}/dhparams.pem") or print STDERR "Could not copy dhparams.pem!\n";
475                                 print "Done.\n\n";
476                         }
477                 } else {
478                         print "SSL Certificates found, skipping.\n\n"
479                 }
480         }
481         else
482         {
483                 print "Skipping SSL certificate generation\nin non-interactive mode.\n\n";
484         }
485 }
486 if (($config{USE_GNUTLS} eq "n") && ($config{USE_OPENSSL} eq "n")) {
487         print "Skipping SSL Certificate generation, SSL support is not available.\n\n";
488 }
489
490 getmodules();
491 writefiles();
492 makecache();
493 dumphash();
494
495 print "\n";
496 print "To build your server with these settings, please run '\e[1;32mmake\e[0m' now.\n";
497 if (($config{USE_GNUTLS} eq "y") || ($config{USE_OPENSSL} eq "y")) {
498         print "Please note: for \e[1;32mSSL support\e[0m you will need to load required\n";
499         print "modules in your config. This configure script has added those modules to the\n";
500         print "build process. For more info please refer to:\n";
501         print "\e[1;32mhttp://wiki.inspircd.org/Installation_From_Tarball\e[0m\n";
502 }
503 print "*** \e[1;32mRemember to edit your configuration files!!!\e[0m ***\n\n";
504
505 sub getcache {
506         # Retrieves the .config.cache file, and loads values into the main config hash.
507         open(CACHE, ".config.cache") or return 0;
508         while (<CACHE>) {
509                 chomp;
510                 # Ignore Blank lines, and comments..
511                 next if /^\s*$/;
512                 next if /^\s*#/;
513                 my ($key, $value) = split("=", $_, 2);
514                 $value =~ /^\"(.*)\"$/;
515                 # Do something with data here!
516                 $config{$key} = $1;
517         }
518         close(CACHE);
519         return 1;
520 }
521
522 sub makecache {
523         # Dump the contents of %config
524         print "Writing \e[1;32mcache file\e[0m for future ./configures ...\n";
525         open(FILEHANDLE, ">.config.cache");
526         foreach my $key (keys %config) {
527                 print FILEHANDLE "$key=\"$config{$key}\"\n";
528         }
529         close(FILEHANDLE);
530 }
531
532 sub dir_check {
533         my ($desc, $hash_key) = @_;
534         my $complete = 0;
535         while (!$complete) {
536                 print "In what directory $desc?\n";
537                 print "[\e[1;32m$config{$hash_key}\e[0m] -> ";
538                 chomp(my $var = <STDIN>);
539                 if ($var eq "") {
540                         $var = $config{$hash_key};
541                 }
542                 if ($var =~ /^\~\/(.+)$/) {
543                         # Convert it to a full path..
544                         $var = rel2abs($ENV{HOME} . "/" . $1);
545                 }
546                 $var = rel2abs($var);
547                 if (! -e $var) {
548                         print "$var does not exist. Create it?\n[\e[1;32my\e[0m] ";
549                         chomp(my $tmp = <STDIN>);
550                         if (($tmp eq "") || ($tmp =~ /^y/i)) {
551                                 # Attempt to Create the Dir..
552                                 my $chk = eval {
553                                         use File::Path ();
554                                         File::Path::mkpath($var, 0, 0777);
555                                         1;
556                                 };
557                                 unless (defined($chk) && -d $var) {
558                                         print "Unable to create directory. ($var)\n\n";
559                                         # Restart Loop..
560                                         next;
561                                 }
562                         } else {
563                                 # They said they don't want to create, and we can't install there.
564                                 print "\n\n";
565                                 next;
566                         }
567                 } else {
568                         if (! -d $var) {
569                                 # Target exists, but is not a directory.
570                                 print "File $var exists, but is not a directory.\n\n";
571                                 next;
572                         }
573                 }
574                 # Either Dir Exists, or was created fine.
575                 $config{$hash_key} = $var;
576                 $complete = 1;
577                 print "\n";
578         }
579 }
580
581 sub writefiles {
582         chomp(my $incos = `uname -n -s -r`);
583         chomp(my $version = `sh src/version.sh`);
584         my $revision = getrevision();
585         my $branch = "InspIRCd-0.0";
586         if ($version =~ /^(InspIRCd-[0-9]+\.[0-9]+)\.[0-9]+/)
587         {
588                 $branch = $1;
589         }
590         print "Writing \e[1;32mconfig.h\e[0m\n";
591         open(FILEHANDLE, ">include/config.h.tmp");
592         print FILEHANDLE <<EOF;
593 /* Auto generated by configure, do not modify! */
594 #pragma once
595
596 #define BRANCH "$branch"
597 #define VERSION "$version"
598 #define REVISION "$revision"
599 #define SYSTEM "$incos"
600
601 #define CONFIG_PATH "$config{CONFIG_DIR}"
602 #define DATA_PATH "$config{DATA_DIR}"
603 #define LOG_PATH "$config{LOG_DIR}"
604 #define MOD_PATH "$config{MODULE_DIR}"
605
606 EOF
607
608         if ($config{HAS_EVENTFD}) {
609                 print FILEHANDLE "#define HAS_EVENTFD\n";
610         }
611         if ($config{HAS_CLOCK_GETTIME}) {
612                 print FILEHANDLE "#define HAS_CLOCK_GETTIME\n";
613         }
614
615         print FILEHANDLE "\n#include \"threadengines/threadengine_pthread.h\"\n";
616         close(FILEHANDLE);
617
618         unlink 'include/config.h';
619         rename 'include/config.h.tmp', 'include/config.h';
620
621         # Do this once here, and cache it in the .*.inc files,
622         # rather than attempting to read src/version.sh from
623         # compiled code -- we might not have the source to hand.
624
625         my @dotfiles = qw(main.mk inspircd);
626         push @dotfiles, 'org.inspircd.plist' if $^O eq 'darwin';
627
628         foreach my $file (@dotfiles) {
629                 open(FILEHANDLE, "make/template/$file") or die "Can't open make/template/$file: $!";
630                 $_ = join '', <FILEHANDLE>;
631                 close(FILEHANDLE);
632
633                 $config{BUILD_DIR} ||= rel2abs($topdir."/build");
634                 $config{COMPILER} = lc $cxx{NAME};
635                 $config{SYSTEM} = lc $^O;
636
637                 for my $var (qw(
638                         CXX COMPILER SYSTEM BASE_DIR CONFIG_DIR MODULE_DIR BINARY_DIR BUILD_DIR DATA_DIR UID
639                         STARTSCRIPT SOCKETENGINE
640                 )) {
641                         s/\@$var\@/$config{$var}/g;
642                 }
643
644                 s/\@VERSION\@/$version/ if defined $version;
645
646                 if ($file eq 'main.mk') {
647                         print "Writing \e[1;32mGNUmakefile\e[0m ...\n";
648
649                         my $mk_tmp = $_;
650                         s/\@IFDEF (\S+)/ifdef $1/g;
651                         s/\@IFNDEF (\S+)/ifndef $1/g;
652                         s/\@IFEQ (\S+) (\S+)/ifeq ($1,$2)/g;
653                         s/\@IFNEQ (\S+) (\S+)/ifneq ($1,$2)/g;
654                         s/\@ELSIFEQ (\S+) (\S+)/else ifeq ($1,$2)/g;
655                         s/\@ELSE/else/g;
656                         s/\@ENDIF/endif/g;
657                         s/ *\@BSD_ONLY .*\n//g;
658                         s/\@GNU_ONLY //g;
659                         s/\@DO_EXPORT (.*)/export $1/g;
660                         open MKF, '>GNUmakefile' or die "Can't write to GNUmakefile: $!";
661                         print MKF $_;
662                         close MKF;
663
664                         print "Writing \e[1;32mBSDmakefile\e[0m ...\n";
665                         $_ = $mk_tmp;
666                         s/\@IFDEF (\S+)/.if defined($1)/g;
667                         s/\@IFNDEF (\S+)/.if !defined($1)/g;
668                         s/\@IFEQ (\S+) (\S+)/.if $1 == $2/g;
669                         s/\@IFNEQ (\S+) (\S+)/.if $1 != $2/g;
670                         s/\@ELSIFEQ (\S+) (\S+)/.elif $1 == $2/g;
671                         s/\@ELSE/.else/g;
672                         s/\@ENDIF/.endif/g;
673                         s/\@BSD_ONLY //g;
674                         s/ *\@GNU_ONLY .*\n//g;
675                         $mk_tmp = $_;
676                         $mk_tmp =~ s#\@DO_EXPORT (.*)#"MAKEENV += ".join ' ', map "$_='\${$_}'", split /\s/, $1#eg;
677                         open MKF, '>BSDmakefile' or die "Can't write to BSDmakefile: $!";
678                         print MKF $mk_tmp;
679                         close MKF;
680                 } else {
681                         print "Writing \e[1;32m$file\e[0m ...\n";
682                         open(FILEHANDLE, ">$file") or die("Can't write to $file: $!\n");
683                         print FILEHANDLE $_;
684                         close(FILEHANDLE);
685                 }
686         }
687
688         chmod 0755, 'inspircd';
689 }
690
691 # Routine to list out the extra/ modules that have been enabled.
692 # Note: when getting any filenames out and comparing, it's important to lc it if the
693 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
694 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
695 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
696 sub list_extras () {
697         use File::Spec;
698         # @_ not used
699         my $srcdir = File::Spec->catdir("src", "modules");
700         my $abs_srcdir = File::Spec->rel2abs($srcdir);
701         local $_;
702         my $dd;
703         opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
704         my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
705         closedir $dd;
706         undef $dd;
707         opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
708         my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
709         closedir $dd;
710         undef $dd;
711         my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
712         my %extras = ();
713 EXTRA:  for my $extra (@extras) {
714                 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
715                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
716                 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
717                 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
718                 if (-l $abs_source) {
719                         # Symlink, is it in the right place?
720                         my $targ = readlink($abs_source);
721                         my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
722                         if ($abs_targ eq $abs_extra) {
723                                 $extras{$extra} = "\e[32;1menabled\e[0m";
724                         } else {
725                                 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
726                         }
727                 } elsif (-e $abs_source) {
728                         my ($devext, $inoext) = stat($abs_extra);
729                         my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
730                         if ($lnksrc > 1) {
731                                 if ($devsrc == $devext && $inosrc == $inoext) {
732                                         $extras{$extra} = "\e[32;1menabled\e[0m";
733                                 } else {
734                                         $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
735                                 }
736                         } else {
737                                 open my $extfd, "<", $abs_extra;
738                                 open my $srcfd, "<", $abs_source;
739                                 local $/ = undef;
740                                 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
741                                         $extras{$extra} = "\e[32;1menabled\e[0m";
742                                 } else {
743                                         $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
744                                 }
745                         }
746                 } else {
747                         $extras{$extra} = "\e[33;1mdisabled\e[0m";
748                 }
749         }
750         # Now let's add dependency info
751         for my $extra (keys(%extras)) {
752                 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
753                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
754                 my @deps = split /\s+/, get_property($abs_extra, 'ModDep');
755                 for my $dep (@deps) {
756                         if (exists($extras{$dep})) {
757                                 my $ref = \$extras{$dep}; # Take reference.
758                                 if ($$ref !~ m/needed by/) {
759                                         # First dependency found.
760                                         if ($$ref =~ m/enabled/) {
761                                                 $$ref .= " (needed by \e[32;1m$extra\e[0m";
762                                         } else {
763                                                 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
764                                                 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
765                                         }
766                                 } else {
767                                         if ($$ref =~ m/enabled/) {
768                                                 $$ref .= ", \e[32;1m$extra\e[0m";
769                                         } else {
770                                                 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
771                                         }
772                                 }
773                         }
774                 }
775         }
776         for my $extra (sort {$a cmp $b} keys(%extras)) {
777                 my $text = $extras{$extra};
778                 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
779                         printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
780                 } else {
781                         printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
782                 }
783         }
784         return keys(%extras) if wantarray; # Can be used by manage_extras.
785 }
786
787 sub enable_extras (@) {
788         my (@extras) = @_;
789         for my $extra (@extras) {
790                 my $extrapath = "src/modules/extra/$extra";
791                 if (!-e $extrapath) {
792                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
793                         next;
794                 }
795                 my $source = "src/modules/$extra";
796                 if (-e $source) {
797                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
798                         next;
799                 }
800                 # Get dependencies, and add them to be processed.
801                 my @deps = split /\s+/, get_property($extrapath, 'ModDep');
802                 for my $dep (@deps) {
803                         next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
804                         if (!-e "src/modules/$dep" && !-e "include/$dep") {
805                                 if (-e "src/modules/extra/$dep") {
806                                         print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
807                                         push @extras, $dep;
808                                 } else {
809                                         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";
810                                 }
811                         }
812                 }
813                 print "Enabling $extra ... \n";
814                 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
815         }
816 }
817
818 sub disable_extras (@)
819 {
820         opendir my $dd, "src/modules/extra/";
821         my @files = readdir($dd);
822         closedir $dd;
823         my (@extras) = @_;
824 EXTRA:  for my $extra (@extras) {
825                 my $extrapath = "src/modules/extra/$extra";
826                 my $source = "src/modules/$extra";
827                 if (!-e $extrapath) {
828                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
829                         next;
830                 }
831                 if ((! -l $source) || readlink($source) ne "extra/$extra") {
832                         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";
833                         next;
834                 }
835                 # Check if anything needs this.
836                 for my $file (@files) {
837                         my @deps = split /\s+/, get_property("src/modules/extra/$file", 'ModDep');
838                         # File depends on this extra...
839                         if (scalar(grep { $_ eq $extra } @deps) > 0) {
840                                 # And is both enabled and not about to be disabled.
841                                 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
842                                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
843                                         next EXTRA;
844                                 }
845                         }
846                 }
847                 # Now remove.
848                 print "Disabling $extra ... \n";
849                 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";
850         }
851 }