import os
import shutil
import re

src_fav = r"C:\Users\Abhiram P M\Downloads\fav.png"
dst_fav = r"d:\iss\Franciscan Society\assets\images\fav.png"
dst_fav_root = r"d:\iss\Franciscan Society\favicon.ico"

if os.path.exists(src_fav):
    shutil.copy2(src_fav, dst_fav)
    shutil.copy2(src_fav, r"d:\iss\Franciscan Society\assets\images\favicon.png")
    shutil.copy2(src_fav, dst_fav_root)
    print(f"Copied {src_fav} to {dst_fav} and root favicon.ico")
else:
    print(f"Source favicon not found at {src_fav}")

# Update all 33 HTML files
html_files = [f for f in os.listdir('.') if f.endswith('.html')]

favicon_tags = """    <link rel="icon" type="image/png" href="assets/images/fav.png">
    <link rel="shortcut icon" href="assets/images/fav.png">
    <link rel="apple-touch-icon" href="assets/images/fav.png">"""

for fname in html_files:
    with open(fname, 'r', encoding='utf-8') as f:
        c = f.read()

    # Remove existing favicon tags
    c = re.sub(r'<link\s+rel=["\'](?:icon|shortcut icon|apple-touch-icon)["\'][^>]*>\s*', '', c)

    # Insert new favicon tags after <head>
    if '<head>' in c:
        c = c.replace('<head>', '<head>\n' + favicon_tags, 1)

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

print(f"Updated favicon links to assets/images/fav.png in all {len(html_files)} HTML files!")
