anchor.diff:      io_lib:format("-~2..0w:~2..0w", [abs(Hr), abs(Min)]).
   
   
  -ensure_rep_db_exists() ->
  -    DbName = ?l2b(couch_config:get("replicator", "db", "_replicator")),
  -    UserCtx = #user_ctx{roles = [<<"_admin">>, <<"_replicator">>]},
  -    case couch_db:open_int(DbName, [sys_db, {user_ctx, UserCtx}, nologifmissing]) of
  -    {ok, Db} ->
  -        Db;
  -    _Error ->
  -        {ok, Db} = couch_db:create(DbName, [sys_db, {user_ctx, UserCtx}])
  +ensure_rep_db_exists(DbName) ->
  +    Db = case couch_db:open_int(DbName, [?CTX, sys_db, nologifmissing]) of
  +        {ok, Db0} ->
  +            Db0;
  +        _Error ->
  +            {ok, Db0} = couch_db:create(DbName, [?CTX, sys_db]),
  +            Db0
       end,
  -    ensure_rep_ddoc_exists(Db, <<"_design/_replicator">>),
  +    ensure_rep_ddoc_exists(DbName),
       {ok, Db}.
   
   
  -ensure_rep_ddoc_exists(RepDb, DDocID) ->
  -    case couch_db:open_doc(RepDb, DDocID, []) of
  -    {ok, _Doc} ->
  -        ok;
  -    _ ->
  -        DDoc = couch_doc:from_json_obj({[
  -            {<<"_id">>, DDocID},
  -            {<<"language">>, <<"javascript">>},
  -            {<<"validate_doc_update">>, ?REP_DB_DOC_VALIDATE_FUN}
  -        ]}),
  -        {ok, _Rev} = couch_db:update_doc(RepDb, DDoc, [])
  -     end.
  +ensure_rep_ddoc_exists(RepDb) ->
  +    DDocId = <<"_design/_replicator">>,
  +    case open_rep_doc(RepDb, DDocId) of
  +        {ok, _Doc} ->
  +            ok;
  +        _ ->
  +            DDoc = couch_doc:from_json_obj({[
  +                {<<"_id">>, DDocId},
  +                {<<"language">>, <<"javascript">>},
  +                {<<"validate_doc_update">>, ?REP_DB_DOC_VALIDATE_FUN}
  +            ]}),
  +            try
  +                {ok, _} = save_rep_doc(RepDb, DDoc)
  +            catch
  +                throw:conflict ->
  +                    % NFC what to do about this other than
  +                    % not kill the process.
  +                    ok
  +            end
  +    end.
   
   
   % pretty-print replication id
  
positive.code: 
  
  update_rep_doc(RepDbName, RepDocId, KVs) when is_binary(RepDocId) ->
      {Pid, Ref} =
      spawn_monitor(fun() ->
          try
              case fabric:open_doc(mem3:dbname(RepDbName), RepDocId, []) of
                  {ok, LatestRepDoc} ->
                      update_rep_doc(RepDbName, LatestRepDoc, KVs);
                  _ ->
                      ok
              end
          catch
          throw:conflict ->
              % a race condition may cause an update conflict,
              % in which cae update_rep_doc is called again to refetch
              twig:log(error, "Conflict error when updating replication document `~s`."
                           " Retrying.", [RepDocId]),
              ok = timer:sleep(5),
              update_rep_doc(RepDbName, RepDocId, KVs);
          Type:Error ->
              exit({Type, Error})
          end
      end),
      receive
      {'DOWN', Ref, process, Pid, normal} ->
          ok;
      {'DOWN', Ref, process, Pid, {throw, Error}} ->
          throw(Error);
      {'DOWN', Ref, process, Pid, {error, Error}} ->
          erlang:error(Error)
      end;
  
  update_rep_doc(RepDbName, #doc{body = {RepDocBody}} = RepDoc, KVs) ->
      NewRepDocBody = lists:foldl(
          fun({K, undefined}, Body) ->
                  lists:keydelete(K, 1, Body);
             ({<<"_replication_state">> = K, State} = KV, Body) ->
                  case get_value(K, Body) of
                  State ->
                      Body;
                  _ ->
  
negative.code: 
  
  maybe_tag_rep_doc(DbName, DocId, {RepProps}, RepId) ->
      case get_json_value(<<"_replication_id">>, RepProps) of
      RepId ->
          ok;
      _ ->
  