import os
import re

# Set your target directory here (use '.' for the current directory)
TARGET_DIR = "."

def clean_html_files(directory):
    # Regex pattern to match the entire container block from opening tag to its closing </div>
    # Using re.DOTALL so '.' matches newlines as well
    pattern = re.compile(
        r'<div\s+class=["\']social-icon-float-container["\'][^>]*>.*?</div>\s*</div>', 
        re.DOTALL | re.IGNORECASE
    )

    modified_count = 0
    total_files = 0

    print(f"Scanning directory: {os.path.abspath(directory)} for HTML files...\n")

    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(".html"):
                total_files += 1
                file_path = os.path.join(root, file)
                
                try:
                    with open(file_path, "r", encoding="utf-8") as f:
                        content = f.read()
                    
                    # Check if the social container exists in this file
                    if pattern.search(content):
                        new_content, count = pattern.subn("", content)
                        
                        with open(file_path, "w", encoding="utf-8") as f:
                            f.write(new_content)
                            
                        print(f"[CLEANED] Removed {count} instance(s) from: {file_path}")
                        modified_count += 1
                    else:
                        print(f"[SKIPPED] No target container found in: {file_path}")
                        
                except Exception as e:
                    print(f"[ERROR] Could not process {file_path}: {e}")

    print(f"\nDone! Scanned {total_files} HTML files. Successfully cleaned {modified_count} file(s).")

if __name__ == "__main__":
    clean_html_files(TARGET_DIR)