import os
import re
import glob
import random

course_files = [
    'advanced-excel-course-in-meerut.php',
    'angular-js-course-in-meerut.php',
    'basic-computer-course-in-meerut.php',
    'c-and-c-course-in-meerut.php',
    'cloud-computing-course-in-meerut.php',
    'corel-draw-course-in-meerut.php',
    'data-science-course-in-meerut.php',
    'digital-marketing-course-in-meerut.php',
    'doeacc-o-level.php',
    'graphic-designing-course-in-meerut.php',
    'photoshop-course-in-meerut.php',
    'python-course-in-meerut.php',
    'tally-course-in-meerut.php',
    'web-designing-course-in-meerut.php',
    'best-institute-in-meerut.php',
    'ccc-institute-in-meerut.php',
    'top-sap-institute-in-meerut.php',
    'industrial-training-in-meerut.php'
]

for filepath in course_files:
    if not os.path.exists(filepath):
        continue
        
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
        
    # Extract title and desc
    title_match = re.search(r'\$pageTitle\s*=\s*"(.*?)";', content)
    desc_match = re.search(r'\$pageDesc\s*=\s*"(.*?)";', content)
    
    title = title_match.group(1) if title_match else 'Best Computer Training Course in Meerut'
    desc = desc_match.group(1) if desc_match else 'Learn top IT skills at ITMeerut with practical training.'
    
    # Generate course name from filename
    name_parts = filepath.replace('-in-meerut.php', '').replace('-course', '').replace('.php', '').split('-')
    course_name = ' '.join([part.capitalize() for part in name_parts])
    
    if course_name.lower() == 'doeacc o level':
        course_name = 'DOEACC O Level'
    if course_name.lower() == 'c and c':
        course_name = 'C & C++ Programming'
    if course_name.lower() == 'angular js':
        course_name = 'Angular JS'
        
    random_img = f"img{random.randint(1, 9)}.webp"
    
    new_content = f"""<?php
$pageTitle = "{title}";
$pageDesc = "{desc}";
$courseName = "{course_name}";
$courseImage = "{random_img}";
include 'course_template.php';
?>"""

    with open(filepath, 'w', encoding='utf-8') as f:
        f.write(new_content)
        
    print(f"Updated {filepath}")

print("All course files updated to use course_template.php")
