#!/usr/bin/python3

import os
import configparser
import subprocess

def main():
    conf = configparser.RawConfigParser(delimiters=("=",))
    conf.read("/etc/samba/smb.conf")

    global_repository = conf.get("global", "recycle:repository", fallback=".recycle")
    global_cleanupdays = conf.get("global", "recycle:cleanupdays", fallback="3")

    for section in conf.sections():
        if section == "global":
            continue
        try:
            path = conf.get(section, "path")
        except configparser.NoOptionError:
            continue
        repository = conf.get(section, "recycle:repository", fallback=global_repository)
        cleanupdays = conf.get(section, "recycle:cleanupdays", fallback=global_cleanupdays)
        repository_path = os.path.join(path, repository)

        if os.path.exists(repository_path):
            subprocess.run(
                ["find", repository_path, "-type", "f", "-mtime", f"+{cleanupdays}", "-delete"],
                check=False,
            )
            subprocess.run(
                ["find", repository_path, "-type", "d", "-empty", "-delete"],
                check=False,
            )

if __name__ == "__main__":
    main()
