#!/usr/bin/python

def fulltext_from_filelist(filelistname, onlystatus = ""):
    fulltext = "Summary for "+filenamelist+"\n\n"
    fulltext += "[[_TOC_]]"
    overview = {"filename": [], "author": [], "status": []}
    with open(filelistname, "r") as f:
        infilename = f.readline().rstrip("\n")
        while infilename:
            with open(infilename, "r") as ftext:
                addtext = ftext.read()
                infotext = addtext[addtext.find("---")+3:addtext.find("---", 5)].split("\n")
                status, author = "", ""
                for line in infotext:
                    if line.find("status:")> -1:
                        status = line[9:len(line)]
                    if line.find("Author")> -1:
                        author = line[9:len(line)]
                takeit = True
                if onlystatus:
                    takeit = False
                    if status:
                        if status == onlystatus:
                            takeit = True
                if takeit:
                    fulltext += addtext[addtext.find("---", 5)+3:len(addtext)] + "\n"
                overview["filename"].append(infilename)
                overview["author"].append(author)
                overview["status"].append(status)
            infilename = f.readline().rstrip("\n")
            
    return fulltext, overview

deliverable_description, status1 = fulltext_from_filelist("filelist_D4-8.txt")

with open("Deliverable_4-8.md", "w") as f:
    f.write(deliverable_description)

deliverable_manuals, status2 = fulltext_from_filelist("filelist_D4-9.txt")

with open("Deliverable_4-9.md", "w") as f:
    f.write(deliverable_manuals)

with open("statuspage.md", "w") as f:
    status = "## Current page status\n\n"
    status += "| Filename | Author | Status |\n"
    status += "| -------- | ------ | ------ |\n"
    for stats in (status1, status2)
    for i in range(len(stats["filename"])):
        status += "| " + stats["filename"] \
                + " | " + stats["author"] \
                + " | " + stats["status"] + " |\n"
    f.write(status)