# Define the folder and file paths
main_folder = "main"
filenames_file_path = "filenames.txt"

# Function to insert the script tag into an HTML file
def insert_script_tag(js_content, html_file_path):
    with open(html_file_path, "r") as html_file:
        html_content = html_file.read()

    head_end_tag_position = html_content.find("</head>")

    if head_end_tag_position != -1:
        script_tag = f"<script>\n{js_content}\n</script>"
        updated_html_content = html_content[:head_end_tag_position] + script_tag + html_content[head_end_tag_position:]

        with open(html_file_path, "w") as html_file:
            html_file.write(updated_html_content)

# Read the comma-separated filenames from the text file
with open(filenames_file_path, "r") as filenames_file:
    filenames = filenames_file.read().strip().split(",")

# Process each filename
for filename in filenames:
    filename = filename.strip()
    js_file_path = main_folder + "/" + filename + ".js"
    html_file_path = "../" + filename + "/index.html"

    with open(js_file_path, "r") as js_file:
        js_content = js_file.read()

    insert_script_tag(js_content, html_file_path)
