<def-group>
  <definition class="compliance" id="aide_build_database" version="2">
    {{{ oval_metadata("The aide database must be initialized.", rule_title=rule_title) }}}
    <criteria operator="AND">
      <extend_definition comment="Aide is installed" definition_ref="package_aide_installed" />
      <!-- Only the configuration when location of the Aide database file is specified in the
           form of absolute path is checked. To check any possible relative path configuration
           that might happen would require to browse whole file system (resources expensive operation).
           Case when Aide database file is configured to be written into an SQL table (possibly
           even on remote host) is out of scope of this check -->
      <criterion test_ref="test_aide_operational_database_absolute_path" />
    </criteria>
  </definition>

  <!-- OVAL object to collect filename for Aide build database -->
  <ind:textfilecontent54_object id="object_aide_operational_database_filename" version="1">
    <ind:filepath>/etc/aide.conf</ind:filepath>
    {{% if product in ['sle12', 'sle15', 'slmicro5'] %}}
      <ind:pattern operation="pattern match">^database=file:/([/a-z.]+)$</ind:pattern>
    {{% elif product in ['sle16', 'slmicro6'] %}}
      <ind:pattern operation="pattern match">^database_in=file:/([/a-z.]+)$</ind:pattern>
    {{% elif product == "rhel9" %}}
      <!-- RHEL 9.8 (aide-0.19.2-2.el9 and newer) uses _in suffix for database_in line -->
      <ind:pattern operation="pattern match">^database(?:_in)?=file:(?:@@{DBDIR})?/(?:[a-z.]+/)*([a-z.]+)$</ind:pattern>
    {{% elif product in ['ol10', 'rhel10', 'fedora' ] %}}
      <ind:pattern operation="pattern match">^database_in=file:(?:@@{DBDIR})?/(?:[a-z.]+/)*([a-z.]+)$</ind:pattern>
    {{% else %}}
      <ind:pattern operation="pattern match">^database=file:(?:@@{DBDIR})?/(?:[a-z.]+/)*([a-z.]+)$</ind:pattern>
    {{% endif %}}
    <!-- From aide.conf(5) - "If there are multiple database_out lines, then the first one is used" =>
                  therefore we will retrieve only the first instance -->
    <ind:instance operation="equals" datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>

  <!-- OVAL variable to concatenate directory path and filename of operational Aide build database file into final absolute path -->
  <local_variable id="variable_aide_operational_database_absolute_path" datatype="string" comment="Absolute path of Aide build database file" version="1">
    <concat>
      {{% if 'sle' not in product and 'slmicro' not in product %}}
        <object_component object_ref="object_aide_build_database_dirpath" item_field="subexpression" />
      {{% endif %}}
      <literal_component>/</literal_component>
      <object_component object_ref="object_aide_operational_database_filename" item_field="subexpression" />
    </concat>
  </local_variable>

{{% if 'sle' not in product and 'slmicro' not in product %}}
  <!-- OVAL object to collect directory path for Aide build database directory -->
  <ind:textfilecontent54_object id="object_aide_build_database_dirpath" version="1">
    <ind:filepath>/etc/aide.conf</ind:filepath>
    <ind:pattern operation="pattern match">^@@define[\s]DBDIR[\s]+(/.*)$</ind:pattern>
    <ind:instance operation="equals" datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>
{{% endif %}}

  <unix:file_object id="object_aide_operational_database_absolute_path" version="1">
    <unix:filepath var_ref="variable_aide_operational_database_absolute_path" var_check="at least one" />
  </unix:file_object>

  <unix:file_test id="test_aide_operational_database_absolute_path" check="all" check_existence="all_exist"
  comment="Testing existence of operational aide database file" version="1">
    <unix:object object_ref="object_aide_operational_database_absolute_path" />
  </unix:file_test>

</def-group>
