import os

count = 0
for dirpath, dirnames, filenames in os.walk("."):
    for filename in filenames:
        if filename.endswith(".html"):
            filepath = os.path.join(dirpath, filename)
            try:
                with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
                    content = f.read()

                content = content.replace("KnowledgeHut", "SureCertificate")
                content = content.replace("knowledgehut", "surecertificate")
                content = content.replace("Knowledgehut", "Surecertificate")
                content = content.replace("upGrad", "")
                
                # Super fast string replace instead of regex
                content = content.replace('.html"', '.php"')
                content = content.replace(".html'", ".php'")
                content = content.replace('.html#', '.php#')
                
                # Fast split
                header_split = content.split('</header>', 1)
                if len(header_split) == 2:
                    content = "<?php include $_SERVER['DOCUMENT_ROOT'] . '/header.php'; ?>\n" + header_split[1]

                footer_split = content.split('<footer', 1)
                if len(footer_split) == 2:
                    content = footer_split[0] + "\n<?php include $_SERVER['DOCUMENT_ROOT'] . '/footer.php'; ?>\n"

                new_filepath = filepath[:-5] + ".php"
                with open(new_filepath, 'w', encoding='utf-8') as f:
                    f.write(content)
                os.remove(filepath)
                count += 1
            except Exception as e:
                pass

print(f"Done! Converted {count} files.")
