body classes if_failed(x)
{
      repair_failed => { "$(x)" };
}

body contain run_under_shell
{
      useshell => "useshell";
}

body action in_background
{
      background => "true";
}


bundle agent start_server(server_config)
{
  classes:
      "debug_server" expression => "debug_mode";

  vars:
      "config_basename" string => filestat("$(server_config)", basename);
      "dlog"            string => "$(sys.workdir)/server-debug.$(config_basename).log";

  commands:
    !debug_server.!windows::
      "$(sys.cf_serverd) -Kf $(server_config)"
        classes => if_failed("server_failed");
    !debug_server.windows::
      # Windows cannot daemonize and needs to start in background.
      "$(sys.cf_serverd) -Kf $(server_config)"
        action => in_background,
        classes => if_failed("server_failed");

    debug_server::
      "$(sys.cf_serverd) -Kldf $(server_config) > $(dlog) 2>&1"
        contain => run_under_shell,
        action => in_background,
        classes => if_failed("server_failed");

    debug_server|windows::
      # Sleep 3 seconds since cf-serverd takes some time to start in background
      # mode. We need to add a handle with $(server_config) in it so that the
      # promise is not skipped if this bundle is used for multiple configs.
      "$(G.sleep) 3"
        contain => run_under_shell,
        handle => "sleep_for_$(server_config)";

  reports:
    debug_server::
      "$(sys.cf_serverd) was run in debug mode, the logs will be in $(dlog)";
}


bundle agent run_test(test_name)
{
  classes:
      "debug_client" expression => "debug_mode";

  vars:
      "dlog" string => "$(sys.workdir)/agent-debug.log";

  commands:
    !debug_client::
      "$(sys.cf_agent) -KIf $(test_name) -D DEBUG,AUTO 2>&1 | $(G.tee) $(dlog)"
        contain => run_under_shell;
    debug_client::
      "$(sys.cf_agent) -Kldf $(test_name) -D DEBUG,EXTRA,AUTO 2>&1 | $(G.tee) $(dlog)"
        contain => run_under_shell;

  reports:
    debug_client::
      "$(sys.cf_agent) was run in debug mode, the logs will be in $(dlog)";
}


bundle agent run_runagent(runagent_params)
{
  classes:
      "debug_client" expression => "debug_mode";

  vars:
      "dlog" string => "$(sys.workdir)/runagent-debug.log";

  commands:
    !debug_client::
      "$(sys.cf_runagent) -I $(runagent_params) 2>&1 | $(G.tee) $(dlog)"
        contain => run_under_shell;
    debug_client::
      "$(sys.cf_runagent) -d $(runagent_params) 2>&1 | $(G.tee) $(dlog)"
        contain => run_under_shell;

  reports:
    debug_client::
      "$(sys.cf_runagent) was run in debug mode, the logs will be in $(dlog)";
}


bundle agent stop_server(server_config)
{
  # On some old platforms, "ps" truncates its output, which CFEngine depends on. This can lead to
  # the test servers not being killed.
  # On HP-UX you can set the DEFAULT_CMD_LINE_WIDTH inside /etc/default/ps to a higher value, which
  # controls the maximum line length of "ps". Unfortunately it is not overridable from the
  # environment.
  processes:
      "$(server_config)"
        signals => { "term", "kill" };
}
