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