{ pkgs, user, ... }:
{
  home.packages = let
    u = pkgs.writers.writePython3Bin "nx_fix_campuszeit_python" {
        flakeIgnore = [ "E302" "E305" "E226" "E501" ];
      } /* python */ ''
import os
import sys

def replace_campus_timezone(directory):
    if not os.path.isdir(directory):
        print(f"Error: {directory} is not a valid directory.")
        return

    for filename in os.listdir(directory):
        filepath = os.path.join(directory, filename)
        if not os.path.isfile(filepath):
            continue

        with open(filepath, 'r', encoding='utf-8') as f:
            content = f.read()

        if 'TZID:Europe/Berlin' in content or 'TZID="Europe/Berlin"' in content:
            # Remove VTIMEZONE block for CampusNetZeit (optional depending on needs)
            # Use a regex if multiple VTIMEZONE blocks may exist
            start_idx = content.find('BEGIN:VTIMEZONE')
            end_idx = content.find('END:VTIMEZONE', start_idx)
            if start_idx != -1 and end_idx != -1:
                content = content[:start_idx] + content[end_idx + len('END:VTIMEZONE\n'):]

            # Replace all TZID references
            content = content.replace('TZID:Europe/Berlin', 'TZID:Europe/Berlin')
            content = content.replace('TZID="Europe/Berlin"', 'TZID="Europe/Berlin"')

            with open(filepath, 'w', encoding='utf-8') as f:
                f.write(content)

            print(f"Updated time zone in: {filename}")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python replace_timezone.py <directory>")
    else:
        replace_campus_timezone(sys.argv[1])
'';
  in [
    (pkgs.writeShellApplication {
      name = "nx_fix_campuszeit";
      text = /*bash*/ ''
        echo CHANGING OWNERSHIP OF "$(realpath "$1")" RECURSIVELY
        read -r -p "Continue?"
        sudo chown -R ${user} "$1" 
        ${u}/bin/nx_fix_campuszeit_python "$1"
        sudo chown -R radicale "$1"
      '';
    })
  ];
}
