Consult default.d/local.d for ini files

COUCHDB-3089
diff --git a/src/config_app.erl b/src/config_app.erl
index 5c5515a..8ba58e6 100644
--- a/src/config_app.erl
+++ b/src/config_app.erl
@@ -28,7 +28,8 @@
     ok.
 
 get_ini_files() ->
-    hd([L || L <- [command_line(), env(), default()], L =/= skip]).
+    IniFiles = hd([L || L <- [command_line(), env(), default()], L =/= skip]),
+    lists:flatmap(fun expand_dirs/1, IniFiles).
 
 env() ->
     case application:get_env(config, ini_files) of
@@ -48,5 +49,18 @@
 
 default() ->
     Etc = filename:join(code:root_dir(), "etc"),
-    Default = [filename:join(Etc,"default.ini"), filename:join(Etc,"local.ini")],
+    Default = [
+        filename:join(Etc, "default.ini"),
+        filename:join(Etc, "default.d"),
+        filename:join(Etc, "local.ini"),
+        filename:join(Etc, "local.d")
+    ],
     lists:filter(fun filelib:is_file/1, Default).
+
+expand_dirs(File) ->
+    case filelib:is_dir(File) of
+        true ->
+            lists:sort(filelib:wildcard(File ++ "/*.ini"));
+        false ->
+            [File]
+    end.
diff --git a/test/config_tests.erl b/test/config_tests.erl
index 50972d4..802e0e3 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -28,6 +28,10 @@
         filename:join([?CONFIG_FIXTURESDIR, "config_tests_1.ini"])).
 -define(CONFIG_FIXTURE_2,
         filename:join([?CONFIG_FIXTURESDIR, "config_tests_2.ini"])).
+-define(CONFIG_DEFAULT_D,
+        filename:join([?CONFIG_FIXTURESDIR, "default.d"])).
+-define(CONFIG_LOCAL_D,
+        filename:join([?CONFIG_FIXTURESDIR, "local.d"])).
 -define(CONFIG_FIXTURE_TEMP,
     begin
         FileName = filename:join([?TEMPDIR, "config_temp.ini"]),
@@ -159,6 +163,14 @@
                  fun should_ensure_in_defaults/2},
                 {{temporary, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_1]},
                  fun should_override_options/2},
+                {{temporary, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_1]},
+                 fun should_override_options/2},
+                {{temporary, [?CONFIG_DEFAULT, ?CONFIG_DEFAULT_D]},
+                 fun(_, _) -> ?_assertEqual("11", config:get("couchdb", "max_dbs_open")) end},
+                {{temporary, [?CONFIG_DEFAULT, ?CONFIG_LOCAL_D]},
+                 fun(_, _) -> ?_assertEqual("12", config:get("couchdb", "max_dbs_open")) end},
+                {{temporary, [?CONFIG_DEFAULT, ?CONFIG_DEFAULT_D, ?CONFIG_LOCAL_D]},
+                 fun(_, _) -> ?_assertEqual("12", config:get("couchdb", "max_dbs_open")) end},
                 {{temporary, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_2]},
                  fun should_create_new_sections_on_override/2},
                 {{temporary, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_1,
diff --git a/test/fixtures/default.d/extra.ini b/test/fixtures/default.d/extra.ini
new file mode 100644
index 0000000..fda68b3
--- /dev/null
+++ b/test/fixtures/default.d/extra.ini
@@ -0,0 +1,19 @@
+; Licensed to the Apache Software Foundation (ASF) under one
+; or more contributor license agreements.  See the NOTICE file
+; distributed with this work for additional information
+; regarding copyright ownership.  The ASF licenses this file
+; to you under the Apache License, Version 2.0 (the
+; "License"); you may not use this file except in compliance
+; with the License.  You may obtain a copy of the License at
+; 
+;   http://www.apache.org/licenses/LICENSE-2.0
+;
+; Unless required by applicable law or agreed to in writing,
+; software distributed under the License is distributed on an
+; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+; KIND, either express or implied.  See the License for the
+; specific language governing permissions and limitations
+; under the License.
+
+[couchdb]
+max_dbs_open=11
diff --git a/test/fixtures/local.d/extra.ini b/test/fixtures/local.d/extra.ini
new file mode 100644
index 0000000..d6a6d46
--- /dev/null
+++ b/test/fixtures/local.d/extra.ini
@@ -0,0 +1,19 @@
+; Licensed to the Apache Software Foundation (ASF) under one
+; or more contributor license agreements.  See the NOTICE file
+; distributed with this work for additional information
+; regarding copyright ownership.  The ASF licenses this file
+; to you under the Apache License, Version 2.0 (the
+; "License"); you may not use this file except in compliance
+; with the License.  You may obtain a copy of the License at
+; 
+;   http://www.apache.org/licenses/LICENSE-2.0
+;
+; Unless required by applicable law or agreed to in writing,
+; software distributed under the License is distributed on an
+; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+; KIND, either express or implied.  See the License for the
+; specific language governing permissions and limitations
+; under the License.
+
+[couchdb]
+max_dbs_open=12