#!/usr/share/ucs-test/runner python3
## desc: Check for missing LDAP index
## bugs:
##  - 57193
## exposure: safe

import re
import sys
from typing import Dict, Set, Tuple

RE = re.compile(r'mdb_(approx|equality|inequality|substring)_candidates: \((\w+)\) not indexed')
LOG = '/var/log/syslog'
IGNORE = {
    'aAAARecord',  # FIXME: pres,+eq: ../../../../management/univention-directory-manager-modules/modules/univention/admin/handlers/__init__.py:2300
    'krb5ValidEnd',  # inequality! dont know where this originates from...
    'krb5ValidStart',  # inequality! dont know where this originates from...
    'memberOf', # FIXME: definitely keycloak, probably radius, maybe more ...
    'msPrintAttributes',  # LOW: ../../../services/univention-s4-connector/modules/univention/admin/handlers/settings/msprintconnectionpolicy.py:115
    'msPrintServerName',  # LOW: ../../../services/univention-s4-connector/modules/univention/admin/handlers/settings/msprintconnectionpolicy.py:116
    'msPrintUNCName',  # LOW: ../../../services/univention-s4-connector/modules/univention/admin/handlers/settings/msprintconnectionpolicy.py:117
    'msPrinterName',  # LOW: ../../../services/univention-s4-connector/modules/univention/admin/handlers/settings/msprintconnectionpolicy.py:118
    'name',  # FIXME: attribute 'name' SUP commonName surname countryName localityName stateOrProvinceName organizationName organizationalUnitName title givenName initials generationQualifier dmdName pseudonym ipServiceProtocol nisMapName nisDomain Solaris…
    'objectClass',  # IGN: pres,eq; temporal issue?
    'sambaLogonHours',  # LOW: only tested by ../../../../test/ucs-test/tests/59_udm/61_test_udm_users.py:52
    'SAMLServiceProviderIdentifier',  # LOW: only used ../../../../management/univention-management-console-module-diagnostic/umc/python/diagnostic/plugins/04_saml_certificate_check.py and ../../../../management/univention-management-console/usr/share/univention-management-console/saml/update_metadata and join scripts
    'structuralObjectClass', # base/univention-updater/script/check.sh
    'uid',  # IGN: pres,eq,sub,approx
    'univentionAppID',  # FIXME: ../../management/univention-appcenter/python/appcenter/utils.py:636
    'univentionComputerPortal', # from 4.4's portal during upgrade tests
    'univentionDefaultClientGroup',  # eq: services/univention-ad-connector/scripts/well-known-sid-object-rename
    'univentionDefaultComputerGroup',  # eq: services/univention-ad-connector/scripts/well-known-sid-object-rename
    'univentionDefaultDomainControllerGroup',  # eq: services/univention-ad-connector/scripts/well-known-sid-object-rename
    'univentionDefaultDomainControllerMasterGroup',  # eq: services/univention-ad-connector/scripts/well-known-sid-object-rename
    'univentionDefaultGroup',  # eq: services/univention-ad-connector/scripts/well-known-sid-object-rename
    'univentionDefaultMemberserverGroup',  # eq: services/univention-ad-connector/scripts/well-known-sid-object-rename
    'univentionDhcpFixedAddress',  # FIXME: ../../management/univention-directory-manager-modules/modules/univention/admin/handlers/__init__.py:2368 et al
    'univentionMonitoringAlertHosts',  # FIXME: ../../../../monitoring/univention-monitoring-client/src/univention/admin/hooks.d/monitoring.py:49
    'univentionNagiosParent',  # FIXME: ../../../../management/univention-directory-manager-modules/modules/univention/admin/nagios.py:296
    'univentionNetworkAccess',  # FIXME: ../../../../services/univention-radius/conffiles/etc/freeradius/3.0/sites-available/default:402 et al
    'univentionNetworkLink',  # FIXME: ../../../../management/univention-directory-manager-modules/modules/univention/admin/handlers/networks/network.py:224
    'univentionNewPortalCategories',  # FIXME: ../../management/univention-portal/udm/handlers/category.py:128
    'univentionNewPortalCategoryEntries',  # FIXME: ../../management/univention-portal/udm/handlers/entry.py:202 et al
    'univentionNewPortalFolderEntries',  # FIXME: ../../management/univention-portal/udm/handlers/folder.py:130 et al
    'univentionNewPortalMenuLinks',  # FIXME: ../../management/univention-portal/udm/handlers/entry.py:194
    'univentionNewPortalUserLinks',  # FIXME: ../../management/univention-portal/udm/handlers/entry.py:198
    'univentionOffice365ADConnectionAlias',  # app/product tests of office365
    'univentionOffice365Enabled',  # app/product tests of office365
    'univentionOperatingSystem',  # base/univention-updater/script/check.sh
    'univentionPolicyObject',  # no idea where this is from, but it makes sense
    'univentionPortalEntryPortal',  # from 4.4's portal during upgrade tests
    'univentionPrinterSpoolHost',  # FIXME: ../../management/univention-directory-manager-modules/modules/univention/admin/handlers/shares/printer.py:274 and ../../management/univention-directory-manager-modules/modules/univention/admin/handlers/shares/printergroup.py:125 et al
    'univentionShareHost',  # FIXME: ../../../../base/univention-quota/quota.py:170 and ../../../../services/univention-nfs/nfs-shares.py:60 and ../../../../services/univention-samba/debian/univention-samba.postinst:116
    'univentionSharePath',  # FIXME: ../../../../base/univention-quota/quota.py:170 and ../../../../services/univention-nfs/nfs-shares.py:60 and ../../../../services/univention-samba/debian/univention-samba.postinst:116
    'univentionShareSambaInheritAcls',  # services/univention-samba/debian/univention-samba.postinst during upgrade from older version
    'univentionShareSambaNtAclSupport',  # services/univention-samba/debian/univention-samba.postinst during upgrade from older version
    'userOptionsPreset',  # LOW: ../../../../management/univention-directory-manager-modules/modules/univention/admin/handlers/settings/usertemplate.py:366 and ../../../../management/univention-directory-manager-modules/univention-migrate-users-to-ucs4.3:332 et al
}

counts: Dict[str, Tuple[int, Set[str]]] = {}

with open(LOG, 'rb') as fd:
    for line in fd:
        m = RE.search(line.decode('UTF-8', 'ignore'))
        if m:
            typ, attr = m.groups()
            count, types = counts.get(attr, (0, set()))
            counts[attr] = (count + 1, types | {typ})

print(
    '\n'.join(
        ('{}: {} for {}{}'.format(attr, count, ','.join(types), "" if attr in IGNORE else " NEW") for attr, (count, types) in sorted(counts.items())),
    ),
)
sys.exit(bool(set(counts) - IGNORE))
