#!/usr/bin/env python3

from datetime import datetime
from pathlib import Path
from repo import repo

if __name__ == '__main__':
    parent = Path(".", recursive=True)
    paths = list(parent.glob("*/Pkgfile"))


    with open(Path(__file__).parent / "files/header.html") as f:
        header = f.read()
        output = header.splitlines()

    for index, path in enumerate(paths):
        current = "even" if index % 2 == 0 else "odd"
        p = repo(path)
        line = f'<tr class="{current}"><td><a href="{p.url}">{p.name}</a></td><td><a href="./{p.name}">{p.version}</a></td><td>{p.description}</td><td>{p.update}</td></tr>'
        output.append(line)

    output.append("</table>")
    output.append(f'<p class="footer">In total {index+1} ports')
    output.append(f'<p class="creator">Generated by portspage.py on {datetime.now()}</p>')
    output.append("</body>")
    output.append("</html>")

    with open('index.html','w') as f:
        f.write("\n".join(output))
