#!/usr/bin/python3

import os

TEMPLATE_DIR = os.path.join(os.getcwd(), "templates")
print (TEMPLATE_DIR)

with open(os.path.join(TEMPLATE_DIR, "header.html")) as f:
    header = f.read()

with open(os.path.join(TEMPLATE_DIR, "footer.html")) as f:
    footer = f.read()

os.chdir("usr/share/linuxmint/mintreport/reports")
for dir_name in os.listdir("."):
    os.chdir(dir_name)
    if os.path.exists("page.html"):
        template = "page.template"
    elif os.path.exists("problem.html") and os.path.exists("workaround.html") and os.path.exists("solution.html"):
        template = "problem_workaround_solution.template"
    else:
        print("Couldn't find a matching template for %s" % dir_name)
        continue

    with open("content.html", "w") as content:
        content.write(header)
        with open(os.path.join(TEMPLATE_DIR, template)) as f:
            for line in f:
                if "$INCLUDE:" not in line:
                    content.write(line)
                else:
                    include = line.strip().replace("$INCLUDE:", "").lower() + ".html"
                    with open(include) as i:
                        content.write(i.read())
        content.write(footer)
    os.chdir("..")
os.chdir("..")
