Add support for new ensure_dbs_exist option to GET, POST/finish_cluster

Addresses apache/couchdb:593
diff --git a/.gitignore b/.gitignore
index 1dbfa4b..f84f14c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
 ebin
 .rebar
+*~
+*.swp
diff --git a/src/setup.erl b/src/setup.erl
index 5a71004..dd7410a 100644
--- a/src/setup.erl
+++ b/src/setup.erl
@@ -12,8 +12,8 @@
 
 -module(setup).
 
--export([enable_cluster/1, finish_cluster/0, add_node/1, receive_cookie/1]).
--export([is_cluster_enabled/0, has_cluster_system_dbs/0]).
+-export([enable_cluster/1, finish_cluster/1, add_node/1, receive_cookie/1]).
+-export([is_cluster_enabled/0, has_cluster_system_dbs/1, cluster_system_dbs/0]).
 
 -include_lib("../couch/include/couch_db.hrl").
 
@@ -54,9 +54,6 @@
     ["_users", "_replicator", "_global_changes"].
 
 
-has_cluster_system_dbs() ->
-    has_cluster_system_dbs(cluster_system_dbs()).
-
 has_cluster_system_dbs([]) ->
     ok;
 has_cluster_system_dbs([Db|Dbs]) ->
@@ -172,12 +169,19 @@
   config:set("admins", binary_to_list(Username), binary_to_list(Password)).
 
 
-finish_cluster() ->
-    finish_cluster_int(has_cluster_system_dbs()).
-finish_cluster_int(ok) ->
+finish_cluster(Options) ->
+    Dbs = proplists:get_value(ensure_dbs_exist, Options),
+    case Dbs of
+        undefined ->
+            finish_cluster_int(cluster_system_dbs(), has_cluster_system_dbs(cluster_system_dbs()));
+        Dbs ->
+            finish_cluster_int(Dbs, has_cluster_system_dbs(Dbs))
+    end.
+
+finish_cluster_int(_Dbs, ok) ->
     {error, cluster_finished};
-finish_cluster_int(no) ->
-    lists:foreach(fun fabric:create_db/1, cluster_system_dbs()).
+finish_cluster_int(Dbs, no) ->
+    lists:foreach(fun fabric:create_db/1, Dbs).
 
 
 add_node(Options) ->
diff --git a/src/setup_httpd.erl b/src/setup_httpd.erl
index a23a3e2..59ed5c7 100644
--- a/src/setup_httpd.erl
+++ b/src/setup_httpd.erl
@@ -29,11 +29,12 @@
     end;
 handle_setup_req(#httpd{method='GET'}=Req) ->
     ok = chttpd:verify_is_server_admin(Req),
+    Dbs = chttpd:qs_json_value(Req, "ensure_dbs_exist", setup:cluster_system_dbs()),
     case setup:is_cluster_enabled() of
         no ->
             chttpd:send_json(Req, 200, {[{state, cluster_disabled}]});
         ok ->
-            case setup:has_cluster_system_dbs() of
+            case setup:has_cluster_system_dbs(Dbs) of
                 no ->
                     chttpd:send_json(Req, 200, {[{state, cluster_enabled}]});
                 ok ->
@@ -74,7 +75,11 @@
 
 handle_action("finish_cluster", Setup) ->
     couch_log:notice("finish_cluster: ~p~n", [Setup]),
-    case setup:finish_cluster() of
+
+    Options = get_options([
+        {ensure_dbs_exist, <<"ensure_dbs_exist">>}
+    ], Setup),
+    case setup:finish_cluster(Options) of
         {error, cluster_finished} ->
             {error, <<"Cluster is already finished">>};
         Else ->