#######################################################
#
# Replace a pattern and use match.0
#
#######################################################

body common control
{
      inputs => { "../../default.cf.sub" };
      bundlesequence  => { default("$(this.promise_filename)") };
      version => "1.0";
}

#######################################################

bundle agent init
{
  vars:
      "states" slist => { "actual", "expected" };

      "actual" string =>
      "BEGIN
kernel /vmlinuz-2.6.18-348.el5 ro noapic nolapic apci=off time process_timing=everything root=LABEL=/foo
kernel /vmlinuz-2.6.18-348.el5 ro root=LABEL=/1
END";

      "parameters" string => "noapic nolapic apci=off time process_timing=everything";

      "expected" string =>
      "BEGIN
kernel /vmlinuz-2.6.18-348.el5 ro root=LABEL=/1 noapic nolapic apci=off time process_timing=everything
kernel /vmlinuz-2.6.18-348.el5 ro  root=LABEL=/foo noapic nolapic apci=off time process_timing=everything
END";

  files:
      "$(G.testfile).$(states)"
      create => "true",
      edit_line => init_insert("$(init.$(states))"),
      edit_defaults => init_empty;
}

bundle edit_line init_insert(str)
{
  insert_lines:
      "$(str)";
}

body edit_defaults init_empty
{
      empty_file_before_editing => "true";
}

######################################################
#
bundle agent test
{
  files:
      "$(G.testfile).actual"
      edit_line => test_replace("$(init.parameters)");
}

bundle edit_line test_replace(parameters)
{
  replace_patterns:

      # replace the parameters with an empty string
      "$(parameters)"
      handle => "test_replace_parameters",
      replace_with => value("");

      # should append $(parameters) to any line that begins with "kernel"
      "^\s*(kernel.+)$"
      depends_on => { "test_replace_parameters" },
      replace_with => value("$(match.1) $(parameters)");

      # should not change anything
      "^\s*(kernel.+)$"
      replace_with => value("$(match.0)");
}

body replace_with value(x)
{
      replace_value => "$(x)";
      occurrences => "all";
}

#######################################################

bundle agent check
{
  methods:
      "any" usebundle => dcs_check_diff("$(G.testfile).actual",
                                            "$(G.testfile).expected",
                                            "$(this.promise_filename)");
}
### PROJECT_ID: core
### CATEGORY_ID: 27
