#!/usr/bin/python3
#
# UMC module for UDM
#  cleanup old Univention directory report files
#
# SPDX-FileCopyrightText: 2007-2026 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

import os
import time

import univention.config_registry as ucr


configRegistry = ucr.ConfigRegistry()
configRegistry.load()

now = time.time()
max_age = int(configRegistry.get('directory/reports/cleanup/age', '43200'))

old = []
dirname = '/usr/share/univention-management-console-module-udm'
for f in os.listdir(dirname):
    if not f.startswith('univention-directory-reports-') or not f.endswith('.pdf'):
        continue
    creation_date = os.stat(os.path.join(dirname, f))[8]
    age = now - creation_date
    if age > max_age:
        old.append(f)

if old:
    for f in old:
        os.unlink(os.path.join(dirname, f))
