anchor.diff:      validate_rel_apps(Server, sys_tuple(ReltoolConfig)),
   
       %% Finally, run reltool
  -    case catch(run_reltool(Config, ReltoolConfig)) of
  +    case catch(run_reltool(Server, Config, ReltoolConfig)) of
           ok ->
               ok;
           {error, failed} ->
  
positive.code: %% Internal functions
  %% ===================================================================
  
  sys_tuple(ReltoolConfig) ->
      case lists:keysearch(sys, 1, ReltoolConfig) of
          {value, {sys, Data}} ->
              {sys, Data};
          false ->
              ?ERROR("Failed to find {sys, ...} tuple in reltool.config.", []),
              ?FAIL
      end.
  
  load_config(ReltoolFile) ->
      %% Load the reltool configuration from the file
      case file:consult(ReltoolFile) of
          {ok, Terms} ->
              Terms;
          Other ->
              ?ERROR("Failed to load expected config from ~s: ~p\n", [ReltoolFile, Other]),
              ?FAIL
      end.
  
  
  %%
  %% Determine the name of the target directory; try the user provided name
  %% first, or fall back to the release name if that's available. If neither
  %% is available, just use "target"
  %%
  target_dir(Config, {sys, ReltoolConfig}) ->
    case rebar_config:get(Config, target_name, undefined) of
        undefined ->
            case lists:keysearch(rel, 1, ReltoolConfig) of
                {value, {rel, Name, _Vsn, _Apps}} ->
                    Name;
                false ->
                    "target"
            end;
        Name ->
            Name
    end.
  
  validate_rel_apps(ReltoolServer, {sys, ReltoolConfig}) ->
      case lists:keysearch(rel, 1, ReltoolConfig) of
  
negative.code:             ok
      end.
  
  
  execute_overlay([], _Vars, _BaseDir, _TargetDir) ->
      ok;
  execute_overlay([{mkdir, Out} | Rest], Vars, BaseDir, TargetDir) ->
      OutFile = render(filename:join([TargetDir, Out, "dummy"]), Vars),
      filelib:ensure_dir(OutFile),
      ?DEBUG("Created dir ~s\n", [filename:dirname(OutFile)]),
      execute_overlay(Rest, Vars, BaseDir, TargetDir);
  execute_overlay([{copy, In} | Rest], _Vars, BaseDir, TargetDir) ->
      execute_overlay([{copy, In, ""} | Rest], _Vars, BaseDir, TargetDir);
  execute_overlay([{copy, In, Out} | Rest], Vars, BaseDir, TargetDir) ->
      InFile = render(filename:join(BaseDir, In), Vars),
      OutFile = render(filename:join(TargetDir, Out), Vars),
      case filelib:is_dir(InFile) of
          true ->
              ok;
          false ->
              ok = filelib:ensure_dir(OutFile)
      end,
      rebar_utils:sh(?FMT("cp -R ~p ~p", [InFile, OutFile])),
      execute_overlay(Rest, Vars, BaseDir, TargetDir);
  execute_overlay([{template, In, Out} | Rest], Vars, BaseDir, TargetDir) ->
      {ok, InFileData} = file:read_file(render(filename:join(BaseDir, In), Vars)),
      OutFile = render(filename:join(TargetDir, Out), Vars),
      ok = filelib:ensure_dir(OutFile),
      case file:write_file(OutFile, render(InFileData, Vars)) of
          ok ->
              ?DEBUG("Templated ~p\n", [OutFile]),
              execute_overlay(Rest, Vars, BaseDir, TargetDir);
          {error, Reason} ->
              ?ABORT("Failed to template ~p: ~p\n", [OutFile, Reason])
      end;
  execute_overlay([{create, Out, Contents} | Rest], Vars, BaseDir, TargetDir) ->
      OutFile = render(filename:join(TargetDir, Out), Vars),
      ok = filelib:ensure_dir(OutFile),
      case file:write_file(OutFile, Contents) of
          ok ->
              ?DEBUG("Created ~p\n", [OutFile]),
              execute_overlay(Rest, Vars, BaseDir, TargetDir);
          {error, Reason} ->
              ?ABORT("Failed to create ~p: ~p\n", [OutFile, Reason])
      end;
  execute_overlay([{replace, Out, Regex, Replacement} | Rest],
                  Vars, BaseDir, TargetDir) ->
      execute_overlay([{replace, Out, Regex, Replacement, []} | Rest], Vars, BaseDir, TargetDir);
  execute_overlay([{replace, Out, Regex, Replacement, Opts} | Rest],
                  Vars, BaseDir, TargetDir) ->
      Filename = render(filename:join(TargetDir, Out), Vars),
      {ok, OrigData} = file:read_file(Filename),
      Data = re:replace(OrigData, Regex, Replacement, [global, {return, binary}] ++ Opts),
      case file:write_file(Filename, Data) of
          ok ->
              ?DEBUG("Edited ~s: s/~s/~s/\n", [Filename, Regex, Replacement]),
              execute_overlay(Rest, Vars, BaseDir, TargetDir);
          {error, Reason} ->
              ?ABORT("Failed to edit ~p: ~p\n", [Filename, Reason])
      end;
  execute_overlay([Other | _Rest], _Vars, _BaseDir, _TargetDir) ->
      {error, {unsupported_operation, Other}}.
  
  
  
  %%
  %% Render a binary to a string, using mustache and the specified context
  %%
  render(Bin, Context) ->
      %% Be sure to escape any double-quotes before rendering...
      Str = re:replace(Bin, "\"", "\\\\\"", [global, {return,list}]),
      mustache:render(Str, Context).
  