#!/usr/bin/env python3
"""Lowercase every file in textures/td_sliced/ and fix the source_file= field
inside .png.import files so Godot can still find the source PNG."""

import re
from pathlib import Path

SRC = Path.home() / "godot_projects/trench/textures/td_sliced"

renamed = 0
skipped = 0
collisions = []

# Snapshot before mutating
files = sorted(SRC.iterdir())

for f in files:
    if not f.is_file():
        continue
    lower = f.name.lower()
    if lower == f.name:
        skipped += 1
        continue
    target = f.parent / lower
    if target.exists():
        collisions.append(f.name)
        continue
    f.rename(target)
    renamed += 1

# Fix source_file= inside every .png.import in the folder
for imp in sorted(SRC.glob("*.png.import")):
    text = imp.read_text()
    new_text = re.sub(
        r'(source_file="res://textures/td_sliced/)([^"]+)"',
        lambda m: f'{m.group(1)}{m.group(2).lower()}"',
        text,
    )
    if new_text != text:
        imp.write_text(new_text)

print(f"renamed: {renamed}")
print(f"already-lowercase: {skipped}")
if collisions:
    print(f"COLLISIONS (target already existed): {len(collisions)}")
    for c in collisions:
        print(f"  {c}")
