import os
from pathlib import Path


def gebos_backup(base_dir=None):
    if base_dir is None:
        base_dir = "E:\\GEBOS"

    source_path = Path(base_dir) / "data"
    target_path = base_dir + "/archive"

    for source_file in source_path.glob("*.csv"):
        # print(source_file)
        table, timestamp = source_file.name.split("2", 1)
        timestamp = "2" + timestamp
        year = timestamp[:4]
        month = timestamp[4:6]
        target = Path(
            f"{target_path}/{year}/{table}/{year}-{month}/{source_file.name}"
        ).absolute()
        os.makedirs(target.parent, exist_ok=True)
        if target.exists():
            target.unlink()
        print(target)
        source_file.rename(target)


if __name__ == "__main__":
    gebos_backup()