import glob
import re

for filepath in glob.glob('*.html'):
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Pattern to match the back link div
    pattern = r'<!-- Back Link -->\s*<div style="padding: 2rem; background: #f5f3ec; text-align: center;">\s*<a href="[^"]+" style="color: #4a2a18; text-decoration: none; font-weight: 600;">.*?</a>\s*</div>'
    
    # Another pattern for safe measure
    pattern_alt = r'<div style="padding: 2rem; background: #f5f3ec; text-align: center;">\s*<a href="(community|ministries)\.html"[^>]*>.*?</a>\s*</div>'
    
    new_content = re.sub(pattern, '', content, flags=re.DOTALL)
    new_content = re.sub(pattern_alt, '', new_content, flags=re.DOTALL)
    
    # Let's also catch the case where the invalid character is explicitly present (like â†  Back to Community)
    # The regex above `.*?` should catch it anyway, but just in case:
    pattern_invalid = r'<!-- Back Link -->\s*<div[^>]*>\s*<a[^>]*>.*?Back to.*?</a>\s*</div>'
    new_content = re.sub(pattern_invalid, '', new_content, flags=re.DOTALL)
    
    if new_content != content:
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(new_content)
        print(f'Removed back link from {filepath}')
