]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - configure
Don't send 421 to unregistered clients, per RFC (thanks nenolod)
[user/henk/code/inspircd.git] / configure
index 101b89dfe544df3123ce98ef5f1029e93ce75e71..a13f59c8135372c9a246135c0b989e4ff5ca4353 100755 (executable)
--- a/configure
+++ b/configure
@@ -12,7 +12,7 @@
 #
 ###################################################
 
-require 5.6.0;
+require 5.8.0;
 use Socket;
 use Cwd;
 use Getopt::Long;
@@ -23,6 +23,67 @@ use make::configure;
 use make::gnutlscert;
 use make::opensslcert;
 
+###############################################################################################
+#
+#                                   EDITABLE VARIABLES
+#
+###############################################################################################
+
+# If you wish to ignore a dependency throughout the entire core, add it here.
+
+my @ignoredeps = (
+       "inspircd_win32wrapper.h",      # windows has its own configure program
+);
+
+# If you wish for all files in the entire core to have a given dependency, insert it here.
+# You should keep this to an absolute minimum to avoid rebuilds that are not neccessary.
+
+my @immutabledeps = (
+       "inspircd_config.h",            # auto re-generated by configure
+       "inspircd.h",
+);
+
+###############################################################################################
+#
+#                                 NON-EDITABLE VARIABLES
+#
+###############################################################################################
+
+# List of commands that make up 'make install' and 'make deinstall'
+
+my $install_list = "";
+my $uninstall_list = "";
+
+# This is a list of all files in the core. Each cpp file is mapped to a shared object file,
+# whos file extension is omitted (these can vary from system to system). Auto detected by
+# scanning the src/*.cpp files for files containing /* $Core: */ identifiers.
+
+my %filelist = ();
+
+# If you wish for a file to have special dependencies in the makefile, add an entry here.
+# Auto populated by /* $ExtraDeps: */ instruction
+
+my %specialdeps = ();
+
+# If you wish for a file to have extra make lines (in between the compile and link steps)
+# then insert them here.
+# Auto populated by /* $ExtraBuild: */ instruction
+        
+my %extrabuildlines = ();
+
+# If you wish for a file to be linked against extra objects or arctives, insert them here.
+# Auto populated by /* $ExtraObjects: */ instruction
+
+my %extraobjects = ();
+
+# If you wish to compile extra cpp sources into an object, define them here.
+# NOTE: Certain cpp files such as the socket engines have a value auto calculated
+# for this table so that their derived class is built.
+# Auto populated by /* $ExtraSources: */ instruction
+
+my %extrasources = ();
+
+
 GetOptions (
        'enable-gnutls' => \$opt_use_gnutls,
        'rebuild' => \$opt_rebuild,
@@ -37,6 +98,7 @@ GetOptions (
        'disable-ports' => \$opt_noports,
        'disable-epoll' => \$opt_noepoll,
        'disable-kqueue' => \$opt_nokqueue,
+       'disable-rpath' => \$opt_disablerpath,
        'enable-ipv6' => \$opt_ipv6,
        'enable-remote-ipv6' => \$opt_ipv6links,
        'disable-remote-ipv6' => \$opt_noipv6links,
@@ -217,22 +279,21 @@ if (defined $opt_noipv6links)
 {
        $config{SUPPORT_IP6LINKS} = "n";
 }
-$config{STATIC_LINK}       = "no";                                     # are doing static modules?
 chomp($config{MAX_CLIENT_T} = `sh -c \"ulimit -n\"`);                  # FD Limit
 chomp($config{MAX_DESCRIPTORS} = `sh -c \"ulimit -n\"`);               # Hard FD Limit
 chomp($config{GCCVER}       = `g++ -dumpversion | cut -c 1`);          # Major GCC Version
 $config{_SOMAXCONN} = SOMAXCONN;                                       # Max connections in accept queue
 $config{OSNAME}            = $^O;                                      # Operating System Name
-$config{IS_DARWIN}          = "NO";                                    # Is OSX?
-$config{STARTSCRIPT}          = "inspircd";                    # start script?
-$config{DESTINATION}          = "BASE";                                # Is target path.
-$config{EXTRA_DIR}          = "";                                              # Is empty.
+$config{IS_DARWIN}       = "NO";                                       # Is OSX?
+$config{STARTSCRIPT}     = "inspircd";                 # start script?
+$config{DESTINATION}     = "BASE";                             # Is target path.
+$config{EXTRA_DIR}       = "";                                         # Is empty.
 if ($config{OSNAME} =~ /darwin/i)
 {
        $config{IS_DARWIN} = "YES";
-       $config{STARTSCRIPT}          = "org.inspircd.plist";           # start script for OSX.
-       $config{DESTINATION}          = "LAUNCHDPATH";                          # Is OSX target.
-       $config{EXTRA_DIR}            = " launchd_dir";                         # Is OSX specific path.
+       $config{STARTSCRIPT}      = "org.inspircd.plist";               # start script for OSX.
+       $config{DESTINATION}      = "LAUNCHDPATH";                              # Is OSX target.
+       $config{EXTRA_DIR}          = " launchd_dir";                           # Is OSX specific path.
 }
 $config{CC}                = "g++";                                            # C++ compiler
 if (defined $opt_cc)
@@ -242,7 +303,6 @@ if (defined $opt_cc)
 $exec = $config{CC} . " -dumpversion | cut -c 1";
 chomp($config{GCCVER}          = `$exec`);                             # Major GCC Version
 $config{MAKEORDER}             = "ircd mods";                          # build order
-$config{STATICLIBS}            = "";                                   # library archive path
 $config{MAX_IDENT}             = "12";                                 # max ident size
 $config{MAX_QUIT}              = "255";                                # max quit message size
 $config{MAX_TOPIC}             = "307";                                # max topic size
@@ -283,15 +343,6 @@ if ($config{GCCVER} eq "") {
        exit;
 }
 
-# Minihack! Convert Cygwin to 'Cyg-Static' so i can
-# Keep my dynamic module experiments here for later
-# consideration!
-
-if ($config{OSNAME} =~ /CYGWIN/i)
-{
-       $config{OSNAME} = "CYG-STATIC";
-}
-
 if (!$config{MAX_CLIENT_T}) { 
        $config{MAX_CLIENT_T} = 1024;                            # Set a reasonable 'Default'
        $fd_scan_fail = "true";                                # Used Later
@@ -302,7 +353,7 @@ getmodules();
 
 sub clean
 {
-       system("rm -rf .config.cache");
+       unlink(".config.cache");
 }
 
 sub update
@@ -318,7 +369,7 @@ sub update
                        exit 0;
                } else {
                        # We've Loaded the cache file and all our variables..
-                       print "Updating Files..\n";
+                       print "Updating files...\n";
                        getosflags();
                        if ($opt_disable_debug == 1)
                        {
@@ -355,7 +406,7 @@ sub modupdate
                        exit 0;
                } else {
                        # We've Loaded the cache file and all our variables..
-                       print "Updating Files..\n";
+                       print "Updating files...\n";
                        getosflags();
                        $has_epoll = $config{HAS_EPOLL};
                        $has_ports = $config{HAS_PORTS};
@@ -397,7 +448,7 @@ sub svnupdate
 
 print "Running non-interactive configure...\n" unless $interactive;
 print "Checking for cache from previous configure... ";
-print ((getcache() eq "true") ? "found\n" : "not found\n");
+print ((!getcache()) ? "not found\n" : "found\n");
 print "Checking operating system version... ";
 print getosflags() . "\n";
 
@@ -474,6 +525,51 @@ if ($has_epoll) {
        if (($kernel =~ /^2\.0\./) || ($kernel =~ /^2\.2\./) || ($kernel =~ /^2\.4\./)) {
                $has_epoll = 0;
        }
+       else
+       {
+               # Suggestion from nenolod, weed out odd systems which have glibc built
+               # against 2.4 kernels (ick)
+               my $kernel_arch = `uname -p`;
+               chomp($kernel_arch);
+               $libcv = 0.0;
+               $kernelv = 0.0;
+               if ($kernel_arch =~ /x86_64/) {
+                       open (FH,"/lib64/libc.so.6|") or $has_epoll = 0;
+               }
+               else {
+                       open (FH,"/lib/libc.so.6|") or $has_epoll = 0;
+               }
+               if ($has_epoll)
+               {
+                       while (chomp($line = <FH>))
+                       {
+                               if ($line =~ /GNU C Library .* version (.*?) /)
+                               {
+                                       $libcv = $1;
+                                       $libcv =~  /(\d+\.\d+)/;
+                                       $libcv = $1;
+                               }
+                               elsif ($line =~ /Compiled on a Linux (.*?\..*?)\.* system/)
+                               {
+                                       $kernelv = $1;
+                                       # Fix for some retarded libc builds, strip off >> and << etc.
+                                       $kernelv =~ /(\d+\.\d+)/;
+                                       $kernelv = $1;
+                               }
+                       }
+                       close FH;
+                       if ($libcv < 2.3)
+                       {
+                               $has_epoll = 0;
+                               printf "libc too old: $libcv... ";
+                       }
+                       if ($kernelv < 2.6)
+                       {
+                               $has_epoll = 0;
+                               printf "libc built against older kernel $kernelv... ";
+                       }
+               }
+       }
 }
 print "yes\n" if $has_epoll == 1;
 print "no\n" if $has_epoll == 0;
@@ -494,15 +590,11 @@ if ($has_ports) {
 print "yes\n" if $has_ports == 1;
 print "no\n" if $has_ports == 0;
 
-if (($config{OSNAME} =~ /CYGWIN/) || ($config{OSNAME} eq "CYG-STATIC")) {
-       $config{HAS_STRLCPY} = "true";
-}
-
 $config{HAS_EPOLL} = $has_epoll;
 $config{HAS_KQUEUE} = $has_kqueue; 
 
 printf "Checking for libgnutls... ";
-if (($config{HAS_GNUTLS}) && (($config{HAS_GNUTLS} >= 1.2) || ($config{HAS_GNUTLS} eq "y"))) {
+if (($config{HAS_GNUTLS}) || ($config{HAS_GNUTLS} eq "y")) {
        print "yes\n";
        $config{HAS_GNUTLS} = "y";
 } else {
@@ -511,7 +603,7 @@ if (($config{HAS_GNUTLS}) && (($config{HAS_GNUTLS} >= 1.2) || ($config{HAS_GNUTL
 }
 
 printf "Checking for openssl... ";
-if (($config{HAS_OPENSSL}) && (($config{HAS_OPENSSL} >= 0.8) || ($config{HAS_OPENSSL} eq "y"))) {
+if (($config{HAS_OPENSSL}) || ($config{HAS_OPENSSL} eq "y")) {
        print "yes\n";
        $config{HAS_OPENSSL} = "y";
 } else {
@@ -519,6 +611,36 @@ if (($config{HAS_OPENSSL}) && (($config{HAS_OPENSSL} >= 0.8) || ($config{HAS_OPE
        $config{HAS_OPENSSL} = "n";
 }
 
+printf "Checking if you are running an ancient, unsupported OS... ";
+if ($config{OSNAME} =~ /FreeBSD/i)
+{
+       $version = `uname -r`;
+       if ($version =~ /^4\./)
+       {
+               $foundit = `ls -l /usr/local/lib/libgnugetopt* | wc -l`;
+               if ($foundit > 0)
+               {
+                       # ICKY ICKY ICK, FREEBSD 4.x! GET AN UPGRADE!
+                       $config{CRAQ} = "-L/usr/local/lib -lgnugetopt -DHAVE_DECL_GETOPT=1";
+                       print "yes\n";
+               }
+               else
+               {
+                       print "\n\nERROR: You require libgnugetopt (from ports or packages) to build InspIRCd on FreeBSD 4.11.\n";
+               }
+       }
+       else
+       {
+               $config{CRAQ} = " ";
+               print "no ($version)\n";
+       }
+}
+else
+{
+       $config{CRAQ} = " ";
+       print "no ($config{OSNAME})\n";
+}
+
 ################################################################################
 #                        BEGIN INTERACTIVE PART                              #
 ################################################################################
@@ -713,14 +835,14 @@ if ($config{USE_GNUTLS} eq "y") {
 * few times and get that HD going :) Then answer the        *
 * Questions which follow. If you are unsure, just hit enter *
 *************************************************************\n\n";
-                       make_gnutls_cert() or $failed = 1;
-                       if (!$failed) {
+                       $failed = make_gnutls_cert();
+                       if ($failed) {
+                               print "\n\033[1;32mCertificate generation failed!\033[0m\n\n";
+                       } else {
                                print "\nCertificate generation complete, copying to config directory... ";
                                system("mv key.pem $config{CONFIG_DIR}/key.pem");
                                system("mv cert.pem $config{CONFIG_DIR}/cert.pem");
                                print "Done.\n\n";
-                       } else {
-                               print "\n\033[1;32mCertificate generation failed!\033[0m\n\n";
                        }
                }
                else {
@@ -753,7 +875,7 @@ if ($config{USE_GNUTLS} eq "y") {
                        print "SSL Certificates Not found, Generating.. \n\n
 *************************************************************
 * Generating the certificates may take some time, go grab a *
-* coffee, or something.                                     *
+* coffee, or something.                                            *
 *************************************************************\n\n";
                        make_openssl_cert();
                        print "\nCertificate generation complete, copying to config directory... ";
@@ -806,7 +928,7 @@ FOO2
 ################################################################################
 sub getcache {
        # Retrieves the .config.cache file, and loads values into the main config hash.
-       open(CACHE, ".config.cache") or return undef;
+       open(CACHE, ".config.cache") or return 0;
        while (<CACHE>) {
                chomp;
                # Ignore Blank lines, and comments..
@@ -818,7 +940,7 @@ sub getcache {
                $config{$key} = $1;
        }
        close(CONFIG);
-       return "true";
+       return 1;
 }
 
 sub makecache {
@@ -887,19 +1009,20 @@ sub dir_check {
 sub getosflags {
 
        $config{LDLIBS} = "-lstdc++";
-       $config{FLAGS}  = "-fno-strict-aliasing -fPIC -Wall -Woverloaded-virtual -Wno-deprecated $config{OPTIMISATI}";
-       $config{DEVELOPER} = "-fno-strict-aliasing -fPIC -Wall -Woverloaded-virtual -Wno-deprecated -g";
-       $SHARED = "-Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared";
+       $config{FLAGS}  = "-fPIC -Wall -pedantic $config{OPTIMISATI}";
+       $config{DEVELOPER} = "-fPIC -Wall -pedantic -g";
+       $SHARED = "-Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared" unless defined $opt_disablerpath;
        $config{MAKEPROG} = "make";
 
        if ($config{OSNAME} =~ /darwin/i) {
-               $config{FLAGS}  = "-DDARWIN -frtti -fPIC -Wall -Woverloaded-virtual -Wno-deprecated $config{OPTIMISATI}";
+               $config{FLAGS}  = "-DDARWIN -frtti -fPIC -Wall -pedantic $config{OPTIMISATI}";
                $SHARED = "-bundle -twolevel_namespace -undefined dynamic_lookup";
                $config{LDLIBS} = "-ldl -lstdc++";
        }
 
        if ($config{OSNAME} =~ /OpenBSD/i) {
                $config{MAKEPROG} = "gmake";
+               $config{LDLIBS} = $config{LDLIBS} . " -lunwind";
                chomp($foo = `eg++ -dumpversion | cut -c 1`);
                # theyre running the package version of gcc (eg++)... detect it and set up its version numbers.
                # if theyre not running this, configure lets the build continue but they probably wont manage to
@@ -913,25 +1036,10 @@ sub getosflags {
 
        if ($config{OSNAME} =~ /Linux/i) {
                $config{LDLIBS} = "-ldl -lstdc++";
-               $config{FLAGS}  = "-fno-strict-aliasing -fPIC -Wall -Woverloaded-virtual -Wno-deprecated $config{OPTIMISATI}";
+               $config{FLAGS}  = "-fPIC -Wall -pedantic $config{OPTIMISATI}";
                $config{FLAGS}  .= " " . $ENV{CXXFLAGS} if exists($ENV{CXXFLAGS});
                $config{LDLIBS} .= " " . $ENV{LDLIBS} if exists($ENV{LDLIBS});
                $config{MAKEPROG} = "make";
-               if ($config{OSNAME} =~ /CYGWIN/) {
-                       $config{FLAGS}  = "-fno-strict-aliasing -Wall -Woverloaded-virtual -Wno-deprecated $config{OPTIMISATI}";
-                       $config{LDLIBS} = "";
-                       $config{MAKEPROG} = "/usr/bin/make";
-                       $config{MAKEORDER} = "ircd mods";
-                       return "Cygwin";
-               } elsif ($config{OSNAME} eq "CYG-STATIC") {
-                       $config{FLAGS} = "-fno-strict-aliasing -Wall -Woverloaded-virtual -Wno-deprecated $config{OPTIMISATI}";
-                       $config{LDLIBS} = "";
-                       $config{MAKEPROG} = "/usr/bin/make";
-                       $config{MAKEORDER} = "mods ircd";
-                       $config{STATICLIBS} = "modules/mods.a";
-                       $config{STATIC_LINK} = "yes";
-                       return "Cygwin-Static";
-               }
        }
 
        if ($config{OSNAME} =~ /FreeBSD/i) {
@@ -1008,19 +1116,9 @@ print FILEHANDLE "#define MAXBUF " . ($config{MAXBUF}+2) . "\n";
                if ($config{OSNAME} =~ /SunOS/i) {
                        print FILEHANDLE "#define IS_SOLARIS\n";
                }
-               if ($config{OSNAME} =~ /CYGWIN/i) {
-                       print FILEHANDLE "#define IS_CYGWIN\n";
-                       print FILEHANDLE "#ifndef FD_SETSIZE\n#define FD_SETSIZE        1024\n#endif\n";
-               }
                if ($config{OSNAME} =~ /MINGW32/i) {
                        print FILEHANDLE "#define IS_MINGW\n";
                }
-               if ($config{OSNAME} =~ /CYG-STATIC/i) {
-                       print FILEHANDLE "#ifndef FD_SETSIZE\n#define FD_SETSIZE    1024\n#endif\n";
-               }
-               if ($config{STATIC_LINK} eq "yes") {
-                       print FILEHANDLE "#define STATIC_LINK\n";
-               }
                if ($config{GCCVER} >= 3) {
                        print FILEHANDLE "#define GCC3\n";
                }
@@ -1070,7 +1168,7 @@ print FILEHANDLE "#define MAXBUF " . ($config{MAXBUF}+2) . "\n";
 #ifndef __CONFIGURATION_SOCKETENGINE__
 #define __CONFIGURATION_SOCKETENGINE__
 
-#include "$se.h"
+#include "socketengines/$se.h"
 
 #endif
 EOF
@@ -1082,12 +1180,7 @@ EOF
        my $modules = "";
        foreach $i (@modlist)
        {
-               if ($config{STATIC_LINK} eq "yes") {
-                       $modules .= "m_".$i.".o ";
-               }
-               else {
-                       $modules .= "m_".$i.".so ";
-               }
+               $modules .= "m_".$i.".so ";
        }
        chomp($modules);   # Remove Redundant whitespace..
 
@@ -1097,6 +1190,7 @@ EOF
                        if (opendir(MDIRHANDLE, "src/modules/$name") != 0) {
                                closedir(MDIRHANDLE);
                                $modules .= "$name.so ";
+                               $uninstall_list = $uninstall_list . "   -rm \$(MODULES)/$name.so\n";
                        }
                }
        }
@@ -1108,12 +1202,6 @@ EOF
        my $file = "";
        my $exe = "inspircd";
 
-       if ($config{OSNAME} =~ /CYGWIN/i) {
-               $exe = "inspircd.exe";
-       }
-
-       opendir(DIRHANDLE, $this);
-
        # Do this once here, and cache it in the .*.inc files,
        # rather than attempting to read src/version.sh from
        # compiled code -- we might not have the source to hand.
@@ -1123,25 +1211,31 @@ EOF
        chomp(my $revision = getrevision());
        $version = "$version(r$revision)";
 
-       my $LIBEXT = "so";
-       if ($config{IS_DARWIN} eq "YES")
-       {
-               $LIBEXT = "dylib";
-       }
        # We can actually parse any file starting with . and ending with .inc,
        # but right now we only parse .inspircd.inc to form './inspircd'
 
+        print "Writing \033[1;32mMakefiles\033[0m\n";
+       write_dynamic_modules_makefile();
+       write_dynamic_makefile();
+
+       opendir(DIRHANDLE, $this);
+
        foreach $name (sort readdir(DIRHANDLE)) {
                if ($name =~ /^\.(.+)\.inc$/) {
                        $file = $1;
+
+                       # Bug #353, omit this on non-darwin
+                       next if (($config{OSNAME} !~ /darwin/) && ($file eq "org.inspircd.plist"));
+
                        # All .name.inc files need parsing!
                        $tmp = "";
-                       open(FILEHANDLE, ".$file.inc");
+                       open(FILEHANDLE, ".$file.inc") or die ("Can't open .$file.inc");
                        while (<FILEHANDLE>) {
                                $tmp .= $_;
                        }
                        close(FILEHANDLE);
 
+                       print "Writing \033[1;32m$file\033[0m ...\n";
                        $tmp =~ s/\@CC\@/$config{CC}/;
                        $tmp =~ s/\@MAKEPROG\@/$config{MAKEPROG}/;
                        $tmp =~ s/\@FLAGS\@/$config{FLAGS}/;
@@ -1152,17 +1246,16 @@ EOF
                        $tmp =~ s/\@MODULE_DIR\@/$config{MODULE_DIR}/;
                        $tmp =~ s/\@BINARY_DIR\@/$config{BINARY_DIR}/;
                        $tmp =~ s/\@LIBRARY_DIR\@/$config{LIBRARY_DIR}/;
-                       $tmp =~ s/\@LIBRARY_EXT\@/$LIBEXT/;
                        $tmp =~ s/\@MODULES\@/$modules/;
                        $tmp =~ s/\@STARTSCRIPT\@/$config{STARTSCRIPT}/;
                        $tmp =~ s/\@DESTINATION\@/$config{DESTINATION}/;
                        $tmp =~ s/\@EXTRA_DIR\@/$config{EXTRA_DIR}/;
                        $tmp =~ s/\@EXECUTABLE\@/$exe/;
                        $tmp =~ s/\@MAKEORDER\@/$config{MAKEORDER}/;
-                       $tmp =~ s/\@STATICLIBS\@/$config{STATICLIBS}/;
                        $tmp =~ s/\@VERSION\@/$version/;
+                       $tmp =~ s/\@INSTALL_LIST\@/$install_list/;
+                       $tmp =~ s/\@UNINSTALL_LIST\@/$uninstall_list/;
 
-                       print "Writing \033[1;32m$file\033[0m\n";
                        open(FILEHANDLE, ">$file");
                        print FILEHANDLE $tmp;
                }
@@ -1171,144 +1264,46 @@ EOF
 
        # Make inspircd executable!
        chmod 0744, 'inspircd';
-
-       if ($config{STATIC_LINK} eq "yes") {
-               print "Writing static-build \033[1;32msrc/Makefile\033[0m\n";
-               write_static_makefile();
-               write_static_modules_makefile();
-       } elsif ($config{OSNAME} =~ /CYGWIN/i) {
-               print "Writing cygwin-build \033[1;32msrc/Makefile\033[0m\n";
-               write_static_makefile();
-               write_dynamic_modules_makefile();
-       } else {
-               print "Writing dynamic-build \033[1;32msrc/Makefile\033[0m\n";
-               write_dynamic_makefile();
-               write_dynamic_modules_makefile();
-       }
 }
 
-sub write_static_modules_makefile {
+sub write_dynamic_modules_makefile {
        # Modules Makefile..
        print "Writing \033[1;32msrc/modules/Makefile\033[0m\n";
        open(FILEHANDLE, ">src/modules/Makefile");
 
-       ###
-       # Module Makefile Header
-       ###
+###
+# Module Makefile Header
+###
        print FILEHANDLE <<EOF;
-# (C) ChatSpike development team
-# Makefile by <Craig\@ChatSpike.net>
-# Many Thanks to Andrew Church <achurch\@achurch.org>
-#    for assisting with making this work right.
+###################################################
+# Copyright 2002-2007 The InspIRCd Development Team
+#  http://www.inspircd.org/wiki/index.php/Credits
+#
+# Thanks to Andrew Church <achurch\@achurch.org>
+#   for assisting with making this work right.
 #
-# Automatically Generated by ./configure to add a modules
-# please run ./configure --update
+# Automatically Generated by ./configure to add a
+#  modules please run ./configure -modupdate
+###################################################
 
 all: \$(MODULES)
 
 EOF
-       ###
-       # End Module Makefile Header
-       ###
-
-       # Create a Modules List..
-       my $modules = "";
-       my $cmflags = "";
-       my $liflags = "";
-
-       open(MODLIST,">include/modlist.h");
-
-       ###
-       # Include File Header
-       ###
-       print MODLIST <<HEADER;
-// Generated automatically by configure. DO NOT EDIT!
 
-#ifndef __SYMBOLS__H_CONFIGURED__
-#define __SYMBOLS__H_CONFIGURED__
+if ($config{OSNAME} =~ /darwin/) {
+               print FILEHANDLE <<EOCHEESE;
 
-HEADER
-       ###
-       # End Include File Header
-       ###
+PICLDFLAGS = -twolevel_namespace -undefined dynamic_lookup -bundle
 
-       # Place Module List into Include
-       foreach $i (@modlist) {
-               if ($i !~ /_static$/) {
-                       print MODLIST "extern \"C\" void * $i\_init (void);\n";
-               }
-       }
-       print MODLIST "\nstruct {const char *name; initfunc *value; } modsyms[] = {\n";
-
-       ###
-       # Build Module Crap
-       ###
-       foreach $i (@modlist)
-       {
-               if ($i !~ /_static$/) {
-                       $cmflags = getcompilerflags("src/modules/m_".$i.".cpp");
-                       $liflags = getlinkerflags("src/modules/m_".$i.".cpp");
-                       $deps = getdependencies("src/modules/m_".$i.".cpp");
-
-                       #print "file: $i: cmflags=$cmflags; liflags=$liflags; deps=$deps\n";
+EOCHEESE
+} else {
+               print FILEHANDLE <<EOCHEESE;
 
-                       ###
-                       # Write Entry to the Makefile
-                       ###
-                       print FILEHANDLE <<EOCHEESE;
-m_$i.o: .m_$i\_static.cpp ../../include/modules.h ../../include/users.h ../../include/channels.h ../../include/base.h $deps
-       \$(CC) -pipe -I../../include \$(FLAGS) $flags -export-dynamic -c .m_$i\_static.cpp
-       mv .m_$i\_static.o ../m_$i.o
+PICLDFLAGS = -fPIC -DPIC -shared
 
 EOCHEESE
-                       ###
-                       # End Write Entry to the MakeFile
-                       ###
-                       print "Configuring module [\033[1;32mm_$i.so\033[0m] for static linking... ";
-                       open(MODULE,"<src/modules/m_".$i.".cpp") or die("Could not open m_".$i.".cpp");
-                       open(MUNGED,">src/modules/.m_".$i."_static.cpp") or die("Could not create .m_".$i."_static.cpp");
-                       while (chomp($a = <MODULE>)) { 
-                               $a =~ s/init_module/$i\_init/g;
-                               print MUNGED "$a\n";
-                       }
-                       close(MODULE);
-                       close(MUNGED);
-                       print MODLIST <<EOENT;
-{"m_$i.so",\&$i\_init},
-EOENT
-                       print "done\n";
-               }
-       }
-
-       print MODLIST "{0}};\n\n#endif\n";
-       close(MODLIST);
 }
 
-sub write_dynamic_modules_makefile {
-       # Modules Makefile..
-       print "Writing \033[1;32msrc/modules/Makefile\033[0m\n";
-       open(FILEHANDLE, ">src/modules/Makefile");
-       my $extra = "";
-
-       if ($config{OSNAME} =~ /CYGWIN/i) {
-               $extra = "../inspircd.dll.a";
-       }
-
-###
-# Module Makefile Header
-###
-       print FILEHANDLE <<EOF;
-# (C) ChatSpike development team
-# Makefile by <Craig\@ChatSpike.net>
-# Many Thanks to Andrew Church <achurch\@achurch.org>
-#    for assisting with making this work right.
-#
-# Automatically Generated by ./configure to add a modules
-# please run ./configure -update or ./configure -modupdate
-
-all: \$(MODULES)
-
-EOF
        ###
        # End Module Makefile Header
        ###
@@ -1317,8 +1312,6 @@ EOF
        my $modules = "";
        my $cmflags = "";
        my $liflags = "";
-       my $crud = "";
-
        foreach $i (@modlist) {
                ###
                # Write Entry to the MakeFile
@@ -1329,23 +1322,26 @@ EOF
        
                #print "file: $i: cmflags=$cmflags; liflags=$liflags; deps=$deps\n";
        
-               print FILEHANDLE <<EOCHEESE;
-m_$i.so: m_$i.cpp ../../include/modules.h ../../include/users.h ../../include/channels.h ../../include/base.h ../../include/inspircd_config.h ../../include/inspircd.h ../../include/configreader.h $deps
-       \$(CC) -pipe -I../../include \$(FLAGS) $cmflags -export-dynamic -c m_$i.cpp
-EOCHEESE
 
-if ($config{OSNAME} =~ /darwin/) {
-               print FILEHANDLE <<EOCHEESE;
-       \$(CC) -pipe -twolevel_namespace -undefined dynamic_lookup \$(FLAGS) -bundle $liflags -o m_$i.so m_$i.o $extra
+               $nicerflags = $config{FLAGS};
+               $nicerflags =~ s/-pedantic//g;
 
-EOCHEESE
-} else {
-               print FILEHANDLE <<EOCHEESE;
-       \$(CC) -pipe \$(FLAGS) -shared $liflags -o m_$i.so m_$i.o $extra
-
-EOCHEESE
-}
-               $crud = $crud . "       install -m \$(INSTMODE) m_$i.so \$(MODPATH)\n";
+               if (nopedantic("src/modules/m_".$i.".cpp"))
+               {
+                       print FILEHANDLE "
+m_$i.so: m_$i.cpp ../../include/modules.h ../../include/users.h ../../include/channels.h ../../include/base.h ../../include/inspircd_config.h ../../include/inspircd.h ../../include/configreader.h $deps
+       \$(CC) -pipe -I../../include $nicerflags $cmflags \$(PICLDFLAGS) $liflags -export-dynamic -o m_$i.so m_$i.cpp
+"
+               }
+               else
+               {
+                       print FILEHANDLE "
+m_$i.so: m_$i.cpp ../../include/modules.h ../../include/users.h ../../include/channels.h ../../include/base.h ../../include/inspircd_config.h ../../include/inspircd.h ../../include/configreader.h $deps
+       \$(CC) -pipe -I../../include \$(FLAGS) $cmflags \$(PICLDFLAGS) $liflags -export-dynamic -o m_$i.so m_$i.cpp
+";
+               }
+               $install_list = $install_list . "       install -m \$(INSTMODE) src/modules/m_$i.so \$(MODPATH)\n";
+               $uninstall_list = $uninstall_list . "   -rm \$(MODULES)/m_$i.so\n";
 ###
                # End Write Entry to the MakeFile
                ###
@@ -1354,450 +1350,328 @@ EOCHEESE
        opendir(DIRHANDLE, "src/modules");
        foreach $name (sort readdir(DIRHANDLE)) {
                if ($name =~ /^m_(.+?)$/) {
-                       $crapola = "";
-                       $crap3 = "";
+                       $mfrules = "";
+                       $mobjs = "";
                        $mliflags = "";
+                       $mfcount = 0;
                        # A module made of multiple files, in a dir, e.g. src/modules/m_spanningtree/
                        if (opendir(MDIRHANDLE, "src/modules/$name") != 0) {
-                               my $i = 0;
-                               print FILEHANDLE "$name.so: ../../include/modules.h ../../include/users.h ../../include/channels.h ../../include/base.h ../../include/inspircd_config.h ../../include/inspircd.h ../../include/configreader.h"; 
-                               foreach $fname (sort readdir(MDIRHANDLE)) {
-                                       if ($fname =~ /\.cpp$/) {
-                                               $cmflags = getcompilerflags("src/modules/$name/$fname");
-                                               $mliflags = $mliflags . " " . getlinkerflags("src/modules/$name/$fname");
-                                               $deps = getdependencies("src/modules/$name/$fname");
-                                               $oname = $fname;
-                                               $oname =~ s/\.cpp$/.o/g;
-                                               print FILEHANDLE " $name/$oname";
-                                               $crapola = $crapola .  "$name/$oname: $name/$fname ../../include/modules.h ../../include/users.h ../../include/channels.h ../../include/base.h ../../include/inspircd_config.h ../../include/inspircd.h ../../include/configreader.h $deps\n";
-                                               $crapola = $crapola .  "        \$(CC) -pipe -I../../include -I. \$(FLAGS) $cmflags -export-dynamic -o $name/$oname -c $name/$fname\n\n";
-                                               $crap3 = $crap3 . " $name/$oname";
-                                               $i++;
-                                       }
-                               }
-                               print "Composing Makefile rules for directory \033[1;32m$name\033[0m... (\033[1;32m$i files found\033[0m)\n";
+                               read_module_directory("src/modules/$name", $name);
+                               print "Composing Makefile rules for directory \033[1;32m$name\033[0m... (\033[1;32m$mfcount files found\033[0m)\n";
+                               print FILEHANDLE "$name.so: ../../include/modules.h ../../include/users.h ../../include/channels.h ../../include/base.h ../../include/inspircd_config.h ../../include/inspircd.h ../../include/configreader.h $mobjs\n"; 
                                if ($config{IS_DARWIN} eq "YES") {
-                                       print FILEHANDLE "\n    \$(CC) -pipe -twolevel_namespace -undefined dynamic_lookup \$(FLAGS) -bundle -o $name.so $crap3\n"; 
+                                       print FILEHANDLE "      \$(CC) -pipe -twolevel_namespace -undefined dynamic_lookup \$(FLAGS) $mliflags -bundle -o $name.so $mobjs\n"; 
                                } else {
-                                       print FILEHANDLE "\n    \$(CC) -pipe \$(FLAGS) -shared $mliflags -o $name.so $crap3\n";
+                                       print FILEHANDLE "      \$(CC) -pipe \$(FLAGS) -shared $mliflags -o $name.so $mobjs\n";
                                }
-                               print FILEHANDLE "\n$crapola\n";
+                               print FILEHANDLE "\n$mfrules\n";
                                closedir(MDIRHANDLE);
-                               $crud = $crud . "       install -m \$(INSTMODE) $name.so \$(MODPATH)\n";
+                               $install_list = $install_list . "       install -m \$(INSTMODE) src/modules/$name.so \$(MODPATH)\n";
                        }
                }
        }
        closedir(DIRHANDLE);
-
-       print FILEHANDLE "modinst:\n    \@echo \"Installing modules...\"\n" . $crud;
 }
 
-
-sub write_static_makefile {
-       open(FH,">src/Makefile") or die("Could not write src/Makefile!");
-       my $i = 0;
-       my @cmdlist = ();
-       opendir(DIRHANDLE, "src");
-       foreach $name (sort readdir(DIRHANDLE)) {
-               if ($name =~ /^cmd_(.+)\.cpp$/) {
-                       $cmdlist[$i++] = $1;
-               }
+sub read_module_directory {
+       my ($dpath, $reldpath) = @_;
+       
+       if (opendir(MDIRHANDLE, $dpath) == 0) {
+               return;
        }
-       closedir(DIRHANDLE);
-       my $cmdobjs = "";
-       my $srcobjs = "";
-       foreach my $cmd (@cmdlist) {
-               $cmdobjs = $cmdobjs . "cmd_$cmd.o ";
-               $srcobjs = $srcobjs . "cmd_$cmd.cpp ";
+       
+       foreach $fname (sort readdir(MDIRHANDLE)) {
+               if ($fname =~ /\.cpp$/) {
+                       $cmflags = getcompilerflags("$dpath/$fname");
+                       $mliflags = $mliflags . " " . getlinkerflags("$dpath/$fname");
+                       $deps = getdependencies("$dpath/$fname");
+                       $oname = $fname;
+                       $oname =~ s/\.cpp$/.o/g;
+                       $mfrules = $mfrules .  "$reldpath/$oname: $reldpath/$fname ../../include/modules.h ../../include/users.h ../../include/channels.h ../../include/base.h ../../include/inspircd_config.h ../../include/inspircd.h ../../include/configreader.h $deps\n";
+                       $mfrules = $mfrules .  "        \$(CC) -pipe -I../../include -I. \$(FLAGS) $cmflags -export-dynamic -o $reldpath/$oname -c $reldpath/$fname\n\n";
+                       $mobjs = $mobjs . " $reldpath/$oname";
+                       $mfcount++;
+               }
+               elsif ((-d "$dpath/$fname") && !($fname eq ".") && !($fname eq "..")) {
+                       read_module_directory($dpath."/".$fname, $reldpath."/".$fname);
+               }
        }
-       print FH <<EOM;
-# Insp Makefile :p
-#
-# (C) ChatSpike development team
-# Makefile by <Craig\@ChatSpike.net>
-# Makefile version 2 (statically linked core) by <brain\@inspircd.org>
-#
-
-CC = im a cheezeball
-
-CXXFLAGS = -I../include \${FLAGS}
-CPPFILES = \$(shell /bin/ls -l modes/ | grep '\\.cpp' | sed 's/^.* //' | grep -v svn)
-RELCPPFILES = \$(shell /bin/ls -l modes/ | grep '\\.cpp' | sed 's/^.* /modes\\//' | grep -v svn)
-
-EOM
-
-$se = "socketengine_select";
-if (($has_kqueue) && ($config{USE_KQUEUE} eq "y")) {
-       $se = "socketengine_kqueue";
-}       
-elsif (($has_epoll) && ($config{USE_EPOLL} eq "y")) {
-       $se = "socketengine_epoll";
-}
-elsif (($has_ports) && ($config{USE_PORTS} eq "y")) {
-       $se = "socketengine_ports";
 }
 
-       ###
-       # This next section is for cygwin dynamic module builds.
-       # Basically, what we do, is build the inspircd core as a library
-       # then the main executable uses that. the library is capable of
-       # loading / unloading the modules dynamically :)
-       # Massive thanks to the guys on #cygwin @ irc.freenode.net for helping
-       # make this work :)
-       ###
+sub calcdeps($)
+{
+       # Yes i know we could use gcc -M but it seems to ideneify a lot of 'deep'
+       # dependencies which are not relevent in C++.
 
-       if ($config{OSNAME} =~ /CYGWIN/i) {
-               print FH <<EOM;
-all: timer.o command_parse.o cull_list.o userprocess.o socketengine.o socket.o hashcomp.o channels.o mode.o xline.o inspstring.o dns.o base.o configreader.o inspsocket.o $cmdobjs commands.o dynamic.o users.o modules.o wildcard.o helperfuncs.o snomasks.o inspircd.exe
+       my $file = $_[0];
 
-inspircd.exe: inspircd.dll.a
-       \$(CC) -o \$@ \$^
+       open (CPP, "<$file") or die("Can't open $file for reading!");
 
-inspircd.dll inspircd.dll.a: inspircd.o channels.o mode.o xline.o inspstring.o dns.o base.o configreader.o inspsocket.o $cmdobjs  commands.o dynamic.o users.o modules.o wildcard.o helperfuncs.o hashcomp.o socket.o socketengine.o userprocess.o cull_list.o command_parse.o timer.o snomasks.o
-       \$(CC) -shared -Wl,--out-implib=inspircd.dll.a -o inspircd.dll \$^
-EOM
-       } else {
-               print FH <<EOM;
-all: timer.o command_parse.o cull_list.o userprocess.o socketengine.o socket.o hashcomp.o channels.o mode.o xline.o inspstring.o dns.o base.o configreader.o inspsocket.o $cmdobjs commands.o dynamic.o users.o modules.o wildcard.o helperfuncs.o snomasks.o \$(MODULES) inspircd.exe
+       my %dupe = ();
+       my $retlist = "";
 
-inspircd.exe: inspircd.cpp ../include/base.h ../include/channels.h ../include/inspircd.h ../include/channels.h ../include/globals.h ../include/inspircd_config.h ../include/base.h
-       \$(CC) -I../include \$(FLAGS) inspircd.cpp -o inspircd.exe \$(LDLIBS) channels.o mode.o xline.o inspstring.o dns.o base.o inspsocket.o configreader.o $cmdobjs commands.o dynamic.o users.o modules.o wildcard.o helperfuncs.o hashcomp.o socket.o socketengine.o userprocess.o cull_list.o command_parse.o timer.o snomasks.o modes/modeclasses.a \$(MODULES)
-EOM
+       foreach my $d (@ignoredeps)
+       {
+               $dupe{$d} = 1;
        }
 
-       print FH <<EOM;
-
-cull_list.o: cull_list.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h ../include/users.h ../include/channels.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c cull_list.cpp
-
-snomasks.o: snomasks.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/channels.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c snomasks.cpp
-
-command_parse.o: command_parse.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c command_parse.cpp
-
-userprocess.o: userprocess.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c userprocess.cpp
-
-socketengine.o: $se.cpp socketengine.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h ../include/$se.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socketengine.cpp $se.cpp
-
-hashcomp.o: hashcomp.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c hashcomp.cpp
-
-helperfuncs.o: helperfuncs.cpp ../include/base.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c helperfuncs.cpp
-
-channels.o: channels.cpp ../include/base.h ../include/channels.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c channels.cpp
-
-mode.o: mode.cpp ../include/base.h ../include/mode.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h \$(RELCPPFILES) modes/modeclasses.a
-       \${MAKE} -C "modes" DIRNAME="src/modes" CC="\$(CC)" \$(MAKEARGS)
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c mode.cpp
-
-xline.o: xline.cpp ../include/base.h ../include/xline.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c xline.cpp
-
-inspstring.o: inspstring.cpp ../include/base.h ../include/inspstring.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c inspstring.cpp
-
-dns.o: dns.cpp ../include/base.h ../include/dns.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c dns.cpp
-
-base.o: base.cpp ../include/base.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c base.cpp
-
-configreader.o: configreader.cpp ../include/base.h ../include/configreader.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c configreader.cpp
-
-commands.o: commands.cpp ../include/base.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h $srcobjs
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c commands.cpp $cmdobjs
-
-dynamic.o: dynamic.cpp ../include/base.h ../include/dynamic.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c dynamic.cpp
-
-users.o: users.cpp ../include/base.h ../include/users.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c users.cpp
-
-modules.o: modules.cpp ../include/base.h ../include/modules.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c modules.cpp
-
-wildcard.o: wildcard.cpp ../include/base.h ../include/wildcard.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c wildcard.cpp
-
-socket.o: socket.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socket.cpp
-       
-inspsocket.o: inspsocket.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c inspsocket.cpp
-
-timer.o: timer.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c timer.cpp
+       my $immutable = "";
+       foreach my $dep (@immutabledeps)
+       {
+               $immutable = $immutable . "../include/$dep ";
+       }
+       $immutable =~ s/ $//g;
 
-EOM
-       foreach my $cmd (@cmdlist) {
-               print FH <<ITEM;
-cmd_$cmd.o: cmd_$cmd.cpp ../include/base.h ../include/modules.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/commands/cmd_$cmd.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c cmd_$cmd.cpp
-ITEM
+       while (chomp($line = <CPP>))
+       {
+               if ($line =~ /#include "(.+\.h)"/)
+               {
+                       if (!exists($dupe{$1}))
+                       {
+                               $retlist = $retlist . "../include/$1 ";
+                               $dupe{$1} = 1;
+                       }
+               }
        }
-       close(FH);
+       close CPP;
+       return length($immutable) ? $immutable . " " . $retlist : $retlist;
 }
 
-sub write_dynamic_makefile {
-
+sub write_dynamic_makefile
+{
        my $i = 0;
        my @cmdlist = ();
-       opendir(DIRHANDLE, "src");
-       foreach $name (sort readdir(DIRHANDLE)) {
-               if ($name =~ /^cmd_(.+)\.cpp$/) {
+       my %existing_install_list = ();
+       opendir(DIRHANDLE, "src/commands");
+       foreach $name (sort readdir(DIRHANDLE))
+       {
+               if ($name =~ /^cmd_(.+)\.cpp$/)
+               {
                        $cmdlist[$i++] = $1;
+                       $install_list = $install_list . "       -install -m \$(INSTMODE) src/commands/cmd_" . $1 . ".so \$(LIBPATH)\n";
+                       $uninstall_list = $uninstall_list . "   -rm \$(LIBPATH)/cmd_$1.so\n";
                }
        }
        closedir(DIRHANDLE);
 
-       my $cmdobjs = "";
-       my $srcobjs = "";
-       foreach my $cmd (@cmdlist) {
-               $cmdobjs = $cmdobjs . "cmd_$cmd.so ";
-               $srcobjs = $srcobjs . "cmd_$cmd.cpp ";
+       if (!$has_epoll)
+       {
+               $config{USE_EPOLL} = 0;
+       }
+       if (!$has_kqueue)
+       {
+               $config{USE_KQUEUE} = 0;
+       }
+       if (!$has_ports)
+       {
+               $config{USE_PORTS} = 0;
        }
 
-       $se = "socketengine_select";
-       if (($has_kqueue) && ($config{USE_KQUEUE} eq "y")) {
-               $se = "socketengine_kqueue";
+       foreach my $dir (("src","src/commands","src/modes","src/socketengines","src/modules"))
+       {
+               print "Scanning \033[1;32m$dir\033[0m for core files ";
+               opendir(DIRHANDLE, $dir);
+               foreach $name (sort readdir(DIRHANDLE))
+               {
+                       if ($name =~ /\.cpp$/)
+                       {
+                               open (CPP, "<$dir/$name") or die("Can't open $dir/$name to scan it! oh bugger");
+                               print ".";
+                               while (chomp($line = <CPP>))
+                               {
+                                       if ($line =~ /\/\* \$Core: (\w+) \*\//i)
+                                       {
+                                               $filelist{$name} = $1;
+                                       }
+                                       elsif ($line =~ /\/\* \$ExtraDeps: (.*?) \*\//i)
+                                       {
+                                               $specialdeps{$name} = $1;
+                                       }
+                                       elsif ($line =~ /\/\* \$ExtraObjects: (.*?) \*\//i)
+                                       {
+                                               $extraobjects{$name} = $1;
+                                       }
+                                       elsif ($line =~ /\/\* \$ExtraBuild: (.*?) \*\//i)
+                                       {
+                                               $extrabuildlines{$name} = $1;
+                                       }
+                                       elsif ($line =~ /\/\* \$ExtraSources: (.*?) \*\//i)
+                                       {
+                                               $extrasources{$name} = $1;
+                                               }
+                                       elsif ($line =~ /\/\* \$If: (\w+) \*\//i)
+                                       {
+                                               if (($config{$1} !~ /y/i) and ($config{$1} ne "1"))
+                                               {
+                                                       # Skip to 'endif'
+                                                       while (chomp($line = <CPP>))
+                                                       {
+                                                               die ("\$If buildsystem instruction within another \$If in file $dir/$name") if ($line =~ /\/\* \$If: (\w+) \*\//i);
+                                                               last if ($line =~ /\/\* \$EndIf \*\//i);
+                                                       }
+                                               }
+                                       }
+                                       elsif ($line =~ /\/\* \$Install: (.*?) \*\//i)
+                                       {
+                                               if (!exists($existing_install_list{$1}))
+                                               {
+                                                       $existing_install_list{$1} = 1;
+                                                       $idir = (split(' ',$1))[1];
+                                                       $ifile = (split(' ',$1))[0];
+                                                       $install_list = $install_list . "       -install -m \$(INSTMODE) $1\n";
+                                                       $ifile =~ s/.*\///g;
+                                                       $uninstall_list = $uninstall_list . "   -rm $idir/$ifile\n";
+                                               }
+                                       }
+                                       elsif ($line =~ /\/\* \$CopyInstall: (.*?) \*\//i)
+                                       {
+                                               if (!exists($existing_install_list{$1}))
+                                               {
+                                                       $existing_install_list{$1} = 1;
+                                                       $idir = (split(' ',$1))[1];
+                                                       $ifile = (split(' ',$1))[0];
+                                                       $install_list = $install_list . "       -cp $1\n";
+                                                       $ifile =~ s/.*\///g;
+                                                       $uninstall_list = $uninstall_list . "   -rm $idir/$ifile\n";
+                                               }
+                                       }
+                               }
+                               close CPP;
+                       }
+               }
+               closedir(DIRHANDLE);
+               print " done!\n";
        }
-       elsif (($has_epoll) && ($config{USE_EPOLL} eq "y")) {
-               $se = "socketengine_epoll";
+
+       $freebsd4libs = $config{CRAQ};
+
+       my $all = "all: ";
+       $all_libsonly = "";
+
+       if ($config{IS_DARWIN} eq "YES")
+       {
+               $libraryext = "dylib";
+               $othercrap = "  \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c inspircd.cpp\n   \$(CC) -pipe -dynamic -bind_at_load -L. -o inspircd \$(LDLIBS) inspircd.o "
        }
-       elsif (($has_ports) && ($config{USE_PORTS} eq "y")) {
-               $se = "socketengine_ports";
+       else
+       {
+               $libraryext = "so";
+               $RPATH = "-Wl,--rpath -Wl,$config{LIBRARY_DIR}" unless defined $opt_disablerpath;
+               $othercrap = "  \$(CC) -pipe -I../include $RPATH \$(FLAGS) $freebsd4libs -rdynamic -L. inspircd.cpp -o inspircd \$(LDLIBS) ";
        }
 
+       foreach my $cpp (sort keys %filelist)
+       {
+               $all = $all . $filelist{$cpp} . "." . $libraryext . " ";
+               $all_libsonly = $all_libsonly . $filelist{$cpp} . "." . $libraryext . " ";
+               $install_list = $install_list . "       -install -m \$(INSTMODE) src/" . $filelist{$cpp} . "." . $libraryext . " \$(LIBPATH)\n";
+               $uninstall_list = $uninstall_list . "   -rm \$(LIBPATH)/" . $filelist{$cpp} . "." . $libraryext . "\n";
+       }
+       $all = $all . "moo inspircd\n";
+
+       $othercrap = $othercrap . " $all_libsonly\n\n";
+
        open(FH,">src/Makefile") or die("Could not write src/Makefile");
        print FH <<EOM;
-# Insp Makefile :p
-#
-# (C) ChatSpike development team
-# Makefile by <Craig\@ChatSpike.net>
-# Makefile version 2 (dynamically linked core) by <brain\@inspircd.org>
-#
 
 CC = im a cheezeball
-
 CXXFLAGS = -I../include \${FLAGS}
 CPPFILES = \$(shell /bin/ls -l modes/ | grep '\\.cpp' | sed 's/^.* //' | grep -v svn)
 RELCPPFILES = \$(shell /bin/ls -l modes/ | grep '\\.cpp' | sed 's/^.* /modes\\//' | grep -v svn)
 
 EOM
 
-if ($config{IS_DARWIN} eq "YES") {
-       print FH <<EOM;
-all: libIRCDtimer.dylib libIRCDcull_list.dylib libIRCDuserprocess.dylib libIRCDsocketengine.dylib libIRCDsocket.dylib libIRCDhash.dylib libIRCDchannels.dylib libIRCDmode.dylib libIRCDxline.dylib libIRCDstring.dylib libIRCDasyncdns.dylib libIRCDbase.dylib libIRCDconfigreader.dylib libIRCDinspsocket.dylib libIRCDcommands.dylib libIRCDdynamic.dylib libIRCDusers.dylib libIRCDmodules.dylib libIRCDwildcard.dylib libIRCDhelper.dylib libIRCDcommand_parse.dylib libIRCDsnomasks.dylib inspircd
-
-inspircd: inspircd.cpp ../include/base.h ../include/channels.h ../include/inspircd.h ../include/channels.h ../include/globals.h ../include/inspircd_config.h ../include/socket.h $cmdobjs libIRCDtimer.dylib libIRCDcull_list.dylib libIRCDuserprocess.dylib libIRCDsocketengine.dylib libIRCDsocket.dylib libIRCDhash.dylib libIRCDchannels.dylib libIRCDmode.dylib libIRCDxline.dylib libIRCDstring.dylib libIRCDasyncdns.dylib libIRCDbase.dylib libIRCDconfigreader.dylib libIRCDinspsocket.dylib libIRCDsnomasks.dylib libIRCDcommands.dylib libIRCDdynamic.dylib libIRCDusers.dylib libIRCDmodules.dylib libIRCDwildcard.dylib libIRCDhelper.dylib libIRCDcommand_parse.dylib
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c inspircd.cpp
-       \$(CC) -pipe -dynamic -bind_at_load -L. -o inspircd \$(LDLIBS) inspircd.o libIRCDchannels.dylib libIRCDmode.dylib libIRCDxline.dylib libIRCDstring.dylib libIRCDasyncdns.dylib libIRCDbase.dylib libIRCDconfigreader.dylib libIRCDinspsocket.dylib libIRCDcommands.dylib libIRCDdynamic.dylib libIRCDusers.dylib libIRCDmodules.dylib libIRCDwildcard.dylib libIRCDhelper.dylib libIRCDhash.dylib libIRCDsocket.dylib libIRCDsocketengine.dylib libIRCDuserprocess.dylib libIRCDcull_list.dylib libIRCDcommand_parse.dylib libIRCDtimer.dylib libIRCDsnomasks.dylib
-
-libIRCDsocketengine.dylib: $se.cpp socketengine.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h ../include/$se.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socketengine.cpp
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c $se.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDsocketengine.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDsocketengine.dylib socketengine.o $se.o
-
-libIRCDsnomasks.dylib: snomasks.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/channels.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c snomasks.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDsnomasks.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDsnomasks.dylib snomasks.o
-
-libIRCDcommand_parse.dylib: command_parse.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c command_parse.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDcommand_parse.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDcommand_parse.dylib command_parse.o
-
-libIRCDcull_list.dylib: cull_list.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h ../include/users.h ../include/channels.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c cull_list.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDcull_list.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDcull_list.dylib cull_list.o
-
-libIRCDuserprocess.dylib: userprocess.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c userprocess.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDuserprocess.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDuserprocess.dylib userprocess.o
-
-libIRCDhash.dylib: hashcomp.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c hashcomp.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDhash.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDhash.dylib hashcomp.o
-
-libIRCDhelper.dylib: helperfuncs.cpp ../include/base.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c helperfuncs.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDhelper.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDhelper.dylib helperfuncs.o
-
-libIRCDchannels.dylib: channels.cpp ../include/base.h ../include/channels.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c channels.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDchannels.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDchannels.dylib channels.o
-
-libIRCDmode.dylib: mode.cpp ../include/base.h ../include/mode.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h \$(RELCPPFILES)
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c mode.cpp
-       \${MAKE} -C "modes" DIRNAME="src/modes" CC="\$(CC)" \$(MAKEARGS) CPPFILES="\$(CPPFILES)"
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDmode.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDmode.dylib mode.o modes/modeclasses.a
-
-libIRCDxline.dylib: xline.cpp ../include/base.h ../include/xline.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c xline.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDxline.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDxline.dylib xline.o
-
-libIRCDstring.dylib: inspstring.cpp ../include/base.h ../include/inspstring.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c inspstring.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDstring.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDstring.dylib inspstring.o
-
-libIRCDasyncdns.dylib: dns.cpp ../include/base.h ../include/dns.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c dns.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDasyncdns.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDasyncdns.dylib dns.o
-
-libIRCDbase.dylib: base.cpp ../include/base.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c base.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDbase.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDbase.dylib base.o
-
-libIRCDconfigreader.dylib: configreader.cpp ../include/base.h ../include/configreader.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c configreader.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDconfigreader.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDconfigreader.dylib configreader.o
-
-libIRCDcommands.dylib: commands.cpp ../include/base.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c commands.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDcommands.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDcommands.dylib commands.o
-
-libIRCDdynamic.dylib: dynamic.cpp ../include/base.h ../include/dynamic.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c dynamic.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDdynamic.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDdynamic.dylib dynamic.o
-
-libIRCDusers.dylib: users.cpp ../include/base.h ../include/users.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c users.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDusers.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDusers.dylib users.o
-
-libIRCDmodules.dylib: modules.cpp ../include/base.h ../include/modules.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c modules.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDmodules.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDmodules.dylib modules.o
-
-libIRCDwildcard.dylib: wildcard.cpp ../include/base.h ../include/wildcard.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c wildcard.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDwildcard.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDwildcard.dylib wildcard.o
-
-libIRCDsocket.dylib: socket.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socket.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDsocket.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDsocket.dylib socket.o
-
-libIRCDinspsocket.dylib: inspsocket.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c inspsocket.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDinspsocket.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDinspsocket.dylib inspsocket.o
-
-libIRCDtimer.dylib: timer.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c timer.cpp
-       \$(CC) -pipe -install_name $config{LIBRARY_DIR}/libIRCDtimer.dylib -dynamiclib -twolevel_namespace -undefined dynamic_lookup -o libIRCDtimer.dylib timer.o
-
-EOM
-
-} else {
-
-       print FH <<EOM;
-all: libIRCDtimer.so libIRCDcull_list.so libIRCDuserprocess.so libIRCDsocketengine.so libIRCDsocket.so libIRCDhash.so libIRCDchannels.so libIRCDmode.so libIRCDxline.so libIRCDstring.so libIRCDasyncdns.so libIRCDbase.so libIRCDconfigreader.so libIRCDinspsocket.so $cmdobjs libIRCDcommands.so libIRCDdynamic.so libIRCDusers.so libIRCDmodules.so libIRCDwildcard.so libIRCDhelper.so libIRCDcommand_parse.so libIRCDsnomasks.so inspircd
-
-inspircd: inspircd.cpp ../include/base.h ../include/channels.h ../include/inspircd.h ../include/channels.h ../include/globals.h ../include/inspircd_config.h ../include/socket.h libIRCDtimer.so libIRCDcull_list.so libIRCDuserprocess.so libIRCDsocketengine.so libIRCDsocket.so libIRCDhash.so libIRCDchannels.so libIRCDmode.so libIRCDxline.so libIRCDstring.so libIRCDasyncdns.so libIRCDbase.so libIRCDconfigreader.so libIRCDinspsocket.so $cmdobjs libIRCDsnomasks.so libIRCDcommands.so libIRCDdynamic.so libIRCDusers.so libIRCDmodules.so libIRCDwildcard.so libIRCDhelper.so libIRCDcommand_parse.so
-       \$(CC) -pipe -I../include $extra -Wl,--rpath -Wl,$config{LIBRARY_DIR} \$(FLAGS) -rdynamic -L. inspircd.cpp -o inspircd \$(LDLIBS) libIRCDchannels.so libIRCDmode.so libIRCDxline.so libIRCDstring.so libIRCDasyncdns.so libIRCDbase.so libIRCDconfigreader.so libIRCDinspsocket.so libIRCDcommands.so libIRCDdynamic.so libIRCDusers.so libIRCDmodules.so libIRCDwildcard.so libIRCDhelper.so libIRCDhash.so libIRCDsocket.so libIRCDsocketengine.so libIRCDuserprocess.so libIRCDcull_list.so libIRCDcommand_parse.so libIRCDtimer.so libIRCDsnomasks.so
+       print FH "$all\n\n";
 
-libIRCDsocketengine.so: $se.cpp socketengine.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h ../include/$se.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socketengine.cpp $se.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDsocketengine.so socketengine.o $se.o
+       $deps = calcdeps("src/inspircd.cpp");
+       print FH "inspircd: inspircd.cpp $deps $all_libsonly\n";
+       print FH "$othercrap\n";
 
-libIRCDsnomasks.so: snomasks.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/channels.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c snomasks.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDsnomasks.so snomasks.o
-
-libIRCDcommand_parse.so: command_parse.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c command_parse.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDcommand_parse.so command_parse.o
-
-libIRCDcull_list.so: cull_list.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h ../include/users.h ../include/channels.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c cull_list.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDcull_list.so cull_list.o
-
-libIRCDuserprocess.so: userprocess.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c userprocess.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDuserprocess.so userprocess.o
-
-libIRCDhash.so: hashcomp.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c hashcomp.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDhash.so hashcomp.o
-
-libIRCDhelper.so: helperfuncs.cpp ../include/base.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c helperfuncs.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDhelper.so helperfuncs.o
-
-libIRCDchannels.so: channels.cpp ../include/base.h ../include/channels.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c channels.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDchannels.so channels.o
-
-libIRCDmode.so: mode.cpp ../include/base.h ../include/mode.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h \$(RELCPPFILES)
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c mode.cpp
-       \${MAKE} -C "modes" DIRNAME="src/modes" CC="\$(CC)" \$(MAKEARGS) CPPFILES="\$(CPPFILES)"
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDmode.so mode.o modes/modeclasses.a
-
-libIRCDxline.so: xline.cpp ../include/base.h ../include/xline.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c xline.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDxline.so xline.o
-
-libIRCDstring.so: inspstring.cpp ../include/base.h ../include/inspstring.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c inspstring.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDstring.so inspstring.o
-
-libIRCDasyncdns.so: dns.cpp ../include/base.h ../include/dns.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c dns.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDasyncdns.so dns.o
-
-libIRCDbase.so: base.cpp ../include/base.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c base.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDbase.so base.o
-
-libIRCDconfigreader.so: configreader.cpp ../include/base.h ../include/configreader.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c configreader.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDconfigreader.so configreader.o
-
-libIRCDcommands.so: commands.cpp ../include/base.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c commands.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDcommands.so commands.o
+       foreach my $cpp (sort keys %filelist)
+       {
+               my $thislib = $filelist{$cpp} . "." . $libraryext; 
+               my $objs = $cpp;
+               my $rawcpp = $cpp;
+               $objs =~ s/\.cpp$/.o/;
+               if (exists($extraobjects{$cpp}))
+               {
+                       $objs = $objs . " " . $extraobjects{$cpp};
+               }
+               if (exists($extrasources{$cpp}))
+               {
+                       $rawcpp = $rawcpp . " " . $extrasources{$cpp};
+               }
+               if ($config{IS_DARWIN} eq "YES")
+               {
+                       $libcrap = "-install_name " . $config{LIBRARY_DIR} . "/" . $thislib . " -dynamiclib -twolevel_namespace -undefined dynamic_lookup";
+               }
+               else
+               {
+                       if (defined $opt_disablerpath)
+                       {
+                               $libcrap = " -shared";
+                       }
+                       else
+                       {
+                               $libcrap = "-Wl,--rpath -Wl," . $config{LIBRARY_DIR} . " -shared";
+                       }
+               }
+               $deps = calcdeps("src/$cpp");
+               if (exists($extrasources{$cpp}))
+               {
+                       foreach my $seperate (sort split(' ',$extrasources{$cpp}))
+                       {
+                               $d = calcdeps("src/$extrasources{$cpp}") . " ";
+                               if ($d ne "")
+                               {
+                                       $deps = $deps . $d . " ";
+                               }
+                       }
+               }
+               print FH $thislib . ": $cpp $deps ". $specialdeps{$cpp} . "\n";
+               print FH "      \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c $rawcpp\n";
+               if (exists($extrabuildlines{$cpp}))
+               {
+                       print FH "      " . $extrabuildlines{$cpp} . "\n";
+               }
+               print FH "      \$(CC) -pipe $libcrap -o " . $thislib . " " . $objs . "\n\n";
+       }
 
-libIRCDdynamic.so: dynamic.cpp ../include/base.h ../include/dynamic.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c dynamic.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDdynamic.so dynamic.o
+       print FH "moo:\n        \${MAKE} -C \"commands\" DIRNAME=\"src/commands\" CC=\"\$(CC)\" \$(MAKEARGS)\n\n";
 
-libIRCDusers.so: users.cpp ../include/base.h ../include/users.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c users.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDusers.so users.o
+       # close main makefile
+       close(FH);
 
-libIRCDmodules.so: modules.cpp ../include/base.h ../include/modules.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c modules.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDmodules.so modules.o
+       # generate a list of .so
+       foreach my $cmd (@cmdlist) {
+               $cmdobjs = $cmdobjs . "cmd_$cmd.so ";
+       }
 
-libIRCDwildcard.so: wildcard.cpp ../include/base.h ../include/wildcard.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c wildcard.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDwildcard.so wildcard.o
+       # and now reopen the commands makefile
+       open(FH,">src/commands/Makefile") or die("Could not write src/commands/Makefile");
+       print FH <<ITEM;
+CC = i am cornholio
+CXXFLAGS = -I../../include ${FLAGS}
 
-libIRCDsocket.so: socket.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socket.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDsocket.so socket.o
+all: $cmdobjs
 
-libIRCDinspsocket.so: inspsocket.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c inspsocket.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDinspsocket.so inspsocket.o
 
-libIRCDtimer.so: timer.cpp ../include/base.h ../include/inspircd.h ../include/globals.h ../include/inspircd_config.h ../include/timer.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c timer.cpp
-       \$(CC) -pipe -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDtimer.so timer.o
+ITEM
 
-EOM
-}
+       # now print the command file details.
        foreach my $cmd (@cmdlist) {
                print FH <<ITEM;
-cmd_$cmd.so: cmd_$cmd.cpp ../include/base.h ../include/modules.h ../include/inspircd.h ../include/channels.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/commands/cmd_$cmd.h
-       \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c cmd_$cmd.cpp
+cmd_$cmd.so: cmd_$cmd.cpp ../../include/base.h ../../include/modules.h ../../include/inspircd.h ../../include/channels.h ../../include/users.h ../../include/globals.h ../../include/inspircd_config.h ../../include/commands/cmd_$cmd.h
+       \$(CC) -pipe -I../../include \$(FLAGS) -export-dynamic -c cmd_$cmd.cpp
        \$(CC) -pipe $SHARED -o cmd_$cmd.so cmd_$cmd.o
 
 ITEM
        }
-       close(FH);
+#try build a .so, no intermediate .o
+#      \$(CC) -pipe -I../../include \$(FLAGS) -export-dynamic $SHARED -o cmd_$cmd.so
+
+#this works for sure
+#      \$(CC) -pipe -I../../include \$(FLAGS) -export-dynamic -c cmd_$cmd.cpp
+#      \$(CC) -pipe $SHARED -o cmd_$cmd.so cmd_$cmd.o
 }