<def-group>
  <definition class="compliance" id="service_%SERVICENAME%_enabled" version="1">
    <metadata>
      <title>Service %SERVICENAME% Enabled</title>
      <affected family="unix">
        <platform>Ubuntu 1404</platform>
      </affected>
      <description>The %SERVICENAME% service should be enabled if possible.</description>
    </metadata>
    <criteria comment="package %PACKAGENAME% installed and service %SERVICENAME% is configured to start" operator="AND">
    <extend_definition comment="%PACKAGENAME% installed" definition_ref="package_%PACKAGENAME%_installed" />
      <criteria comment="service %SERVICENAME% is configured to start and is running" operator="AND">
        <criterion comment="%SERVICENAME% is running" test_ref="test_service_running_%SERVICENAME%" />
        <criteria operator="OR" comment="service %SERVICENAME% is configured to start">
          <criterion comment="multi-user.target wants %SERVICENAME%" test_ref="test_multi_user_wants_%SERVICENAME%" />
          <criterion comment="multi-user.target wants %SERVICENAME% socket" test_ref="test_multi_user_wants_%SERVICENAME%_socket" />
        </criteria>
      </criteria>
    </criteria>
  </definition>

  <linux:systemdunitdependency_test check="all" check_existence="any_exist" comment="systemd test" id="test_multi_user_wants_%SERVICENAME%" version="1">
    <linux:object object_ref="object_multi_user_target_for_%SERVICENAME%_enabled" />
    <linux:state state_ref="state_systemd_%SERVICENAME%_on"/>
  </linux:systemdunitdependency_test>
  <linux:systemdunitdependency_object id="object_multi_user_target_for_%SERVICENAME%_enabled" comment="list of dependencies of multi-user.target" version="1">
    <linux:unit>multi-user.target</linux:unit>
  </linux:systemdunitdependency_object>
  <linux:systemdunitdependency_state id="state_systemd_%SERVICENAME%_on" comment="%SERVICENAME% listed at least once in the dependencies" version="1">
    <linux:dependency entity_check="at least one">%SERVICENAME%.service</linux:dependency>
  </linux:systemdunitdependency_state>

  <linux:systemdunitdependency_test check="all" check_existence="any_exist" comment="systemd test" id="test_multi_user_wants_%SERVICENAME%_socket" version="1">
    <linux:object object_ref="object_multi_user_target_for_%SERVICENAME%_socket_enabled" />
    <linux:state state_ref="state_systemd_%SERVICENAME%_socket_on"/>
  </linux:systemdunitdependency_test>
  <linux:systemdunitdependency_object id="object_multi_user_target_for_%SERVICENAME%_socket_enabled" comment="list of dependencies of multi-user.target" version="1">
    <linux:unit>multi-user.target</linux:unit>
  </linux:systemdunitdependency_object>
  <linux:systemdunitdependency_state id="state_systemd_%SERVICENAME%_socket_on" comment="%SERVICENAME% listed at least once in the dependencies" version="1">
    <linux:dependency entity_check="at least one">%SERVICENAME%.socket</linux:dependency>
  </linux:systemdunitdependency_state>

  <linux:systemdunitproperty_test id="test_service_running_%SERVICENAME%" check="at least one" check_existence="at_least_one_exists" comment="Test that the %SERVICENAME% service is running" version="1">
    <linux:object object_ref="obj_service_running_%SERVICENAME%"/>
    <linux:state state_ref="state_service_running_%SERVICENAME%"/>
  </linux:systemdunitproperty_test>
  <linux:systemdunitproperty_object id="obj_service_running_%SERVICENAME%" comment="Retrieve the ActiveState property of %SERVICENAME%" version="1">
    <linux:unit operation="pattern match">%SERVICENAME%\.(socket|service)</linux:unit>
    <linux:property>ActiveState</linux:property>
  </linux:systemdunitproperty_object>
  <linux:systemdunitproperty_state id="state_service_running_%SERVICENAME%" version="1" comment="%SERVICENAME% is running">
      <linux:value>active</linux:value>
  </linux:systemdunitproperty_state>
</def-group>
