#######################################################
#
# See ticket ENT-13536 for info
#
#######################################################

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

body package_method mock
{
  package_changes => "individual";
  package_list_command => "$(g.pm) --list-installed";

  package_list_name_regex => "^[^:]*";
  package_list_version_regex => ":(?<=:).*(?=:)";
  package_list_arch_regex => "[^:]\w+$";
  package_installed_regex => "^[^:]*";

  package_add_command => "$(g.pm) --add ";
  package_update_command => "$(g.pm) --update ";
  package_delete_command => "$(g.pm) --delete ";
  package_verify_command => "$(g.pm) --verify ";
}

bundle common g
{
  classes:
      "mpm_declared" not => strcmp(getenv("MOCK_PACKAGE_MANAGER", "65535"), "");

  vars:
    mpm_declared::
      "pm" string => getenv("MOCK_PACKAGE_MANAGER", "65535");

    !mpm_declared::
      "pm" string => "$(G.mock_package_manager)";
}

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

bundle agent init
{
  commands:
      "$(g.pm) --clear-installed";
      "$(g.pm) --clear-available";
      "$(g.pm) --populate-available imagisoft:1.0i:teapot";

  files:
      "$(G.testfile)"
        delete => tidy;
}

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

bundle agent test
{
  meta:
      "test_skip_unsupported" string => "windows",
        meta => { "ENT-10215" };

  vars:
      "name" string => "imagisoft";
      "version" string => "1.0i";
      "arch" string => "teapot";

  packages:
      "imagisoft; echo 'Hello CFEngine' > '$(G.testfile)';"
        package_policy => "add",
        package_method => mock;
}

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

bundle agent check
{
  reports:
      "$(this.promise_filename) $(with)"
        with => ifelse(fileexists("$(G.testfile)"), "FAIL", "Pass");
}

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

bundle agent clean
{
  methods:
      "init";
}
