Choose backend at build time

This allows us to set the backend as a dependency of the front end,
ensuring it is started first. This also means we don't need to call
application:load/1.
diff --git a/src/couch_log.app.src b/src/couch_log.app.src
deleted file mode 100644
index b59b1e9..0000000
--- a/src/couch_log.app.src
+++ /dev/null
@@ -1,20 +0,0 @@
-% Licensed 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.
-
-{application, couch_log, [
-    {description, "CouchDB Log API"},
-    {vsn, git},
-    {modules, [couch_log]},
-    {registered, []},
-    {applications, [kernel, stdlib]},
-    {env, [{backend, couch_log_stderr}]}
-]}.
diff --git a/src/couch_log.app.src.script b/src/couch_log.app.src.script
new file mode 100644
index 0000000..8fd7b02
--- /dev/null
+++ b/src/couch_log.app.src.script
@@ -0,0 +1,42 @@
+% Licensed 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.
+
+CouchConfig = case filelib:is_file(os:getenv("COUCHDB_CONFIG")) of
+    true ->
+        {ok, Result} = file:consult(os:getenv("COUCHDB_CONFIG")),
+        Result;
+    false ->
+        []
+end.
+
+Backend = case lists:keyfind(couch_log_backend, 1, CouchConfig) of
+    {couch_log_backend, Backend0} ->
+        Backend0;
+    false ->
+        couch_log_stderr
+end.
+
+BackendApps = case lists:keyfind(couch_log_backend_apps, 1, CouchConfig) of
+    {couch_log_backend_apps, Apps} ->
+        Apps;
+    false ->
+        []
+end.
+
+{application, couch_log, [
+    {description, "CouchDB Log API"},
+    {vsn, git},
+    {modules, [couch_log]},
+    {registered, []},
+    {applications, [kernel, stdlib] ++ BackendApps},
+    {env, [{backend, Backend}]}
+]}.
diff --git a/src/couch_log.erl b/src/couch_log.erl
index 0c10230..e80685a 100644
--- a/src/couch_log.erl
+++ b/src/couch_log.erl
@@ -77,19 +77,12 @@
 
 -spec set_level(atom()) -> ok.
 set_level(Level) ->
-    {ok, Backend} = application:get_env(?MODULE, backend),
+    {ok, Backend} = get_backend(),
     Backend:set_level(Level).
 
 -spec get_backend() -> {ok, atom()}.
 get_backend() ->
-    case application:get_env(?MODULE, backend) of
-        undefined ->
-            ok = application:load(?MODULE),
-            get_backend();
-        {ok, Backend} ->
-            {ok, Backend}
-    end.
-
+    application:get_env(?MODULE, backend).
 
 -ifdef(TEST).