Description: Add include directive
Forwarded: http://wiki.powerdns.com/trac/ticket/466
Origin: Debian

--- a/pdns/arguments.cc
+++ b/pdns/arguments.cc
@@ -312,9 +312,12 @@
     if(pos && pos!=string::npos) 
       val=val.substr(pos);
 
-    if(parmIsset(var))
+    if(parmIsset(var)) {
       params[var]=val;
-    else
+	  if (var == "include") { // include directory
+		  preParseDir(val, arg, lax);
+	  }
+	} else
       if(!lax)
         throw ArgException("Trying to set unexisting parameter '"+var+"'");
   }
@@ -341,6 +344,35 @@
   }
 }
 
+bool ArgvMap::preParseDir(const string &dir, const string &arg, bool lax)
+{
+	DIR *dir_p;
+	string filename, name;
+	struct dirent *dir_entry_p;
+
+	if (dir_p = opendir(dir.c_str())) {
+		while((dir_entry_p = readdir(dir_p)))
+		{
+			name = dir_entry_p->d_name;
+			if (name == "." || name == "..")
+				continue;
+			if (name.find("ucf-dist") != std::string::npos)
+				continue;
+			if (name.find("ucf-old") != std::string::npos)
+				continue;
+
+			filename = dir + "/" + name;
+			file(filename.c_str(), lax);
+		}
+		closedir(dir_p);
+	} else {
+		// Could be a file.
+		file(dir.c_str(), lax);
+	}
+
+	return true;
+}
+
 bool ArgvMap::preParseFile(const char *fname, const string &arg, const string& theDefault)
 {
   params[arg]=theDefault;
--- a/pdns/arguments.hh
+++ b/pdns/arguments.hh
@@ -24,6 +24,7 @@
 #include <vector>
 #include <fstream>
 #include <iostream>
+#include <dirent.h>
 #include "misc.hh"
 #include "ahuexception.hh"
 #ifndef WIN32
@@ -83,6 +84,7 @@
   }
   void preParse(int &argc, char **argv, const string &arg); //!< use this to preparse a single var
   bool preParseFile(const char *fname, const string &arg, const string& theDefault=""); //!< use this to preparse a single var in configuration
+  bool preParseDir(const string &dir, const string &arg, bool lax); //!< use this to include a directory
 
   bool file(const char *fname, bool lax=false); //!< Parses a file with parameters
   bool laxFile(const char *fname) 
--- a/pdns/common_startup.cc
+++ b/pdns/common_startup.cc
@@ -64,6 +64,7 @@
   ::arg().set("version-string","PowerDNS version in packets - full, anonymous, powerdns or custom")="full"; 
   ::arg().set("control-console","Debugging switch - don't use")="no"; // but I know you will!
   ::arg().set("fancy-records","Process URL and MBOXFW records")="no";
+  ::arg().set("include", "Directory with config files to include")="/etc/powerdns/pdns.d"; // Add directory with config files
   ::arg().set("wildcard-url","Process URL and MBOXFW records")="no";
   ::arg().set("loglevel","Amount of logging. Higher is more. Do not set below 3")="4";
   ::arg().set("default-soa-name","name to insert in the SOA record if none set in the backend")="a.misconfigured.powerdns.server";
--- a/pdns/pdnssec.cc
+++ b/pdns/pdnssec.cc
@@ -50,6 +50,7 @@
   ::arg().set("launch","Which backends to launch");
   ::arg().set("dnssec","if we should do dnssec")="true";
   ::arg().set("config-name","Name of this virtual configuration - will rename the binary image")=g_vm["config-name"].as<string>();
+  ::arg().set("include", "Directory with config files to include")="/etc/powerdns/pdns.d"; // Add directory with config files
   ::arg().setCmd("help","Provide a helpful message");
   //::arg().laxParse(argc,argv);
 
