#!/usr/bin/env python3
#  Author: John M. Morrison.  Released without warrantee with a GPL v3 license.
#  18 Aug 2023:  trailing / deleted from self-closing tags.
#  28 Aug 2023:  allow user to specify date; default is today.
#                convert to modern format strings
#                add full documentation
#  Date format must be entered as yyyymmdd.
#  usages:
#       python makeThree.py filename   (default date is today)
#       python makeThree.py filename yyyymmdd  (specify date)
#  usage note: if you are running on UNIX with execute permissions, you can
#              omit the "python" in the two commands above if this file is located
#              in a directory in your $PATH.
#  product:
#       three files, HTML, CSS, and JavaScript all linked to each other and
#       date stamped.
from sys import argv
from datetime import datetime
if len(argv) == 3:  #user specified date supplied
    date_data = argv[2]
    year = date_data[:4]
    month = date_data[4:6]
    day = date_data[6:8]
    date = datetime.fromisoformat(f"{year}-{month}-{day}")
    date = date.date()
elif len(argv) == 2:
    date = datetime.date(datetime.now())  #default to today
else:
    print("Usage:  python makeThree.py yyyymmdd")
    print("Bailing.... try again.")
    quit()

root = argv[1]
# Create and populate the HTML file.
with open(root + ".html", "w") as out_file:
    out_file.write("<!doctype html>\n")
    out_file.write("<!--Author: Morrison-->\n")
    out_file.write(f"<!--Date: {date}-->\n")
    out_file.write(f"""
<html lang="en">
<head>
<title>{root}</title>
<meta charset="utf-8">
<link rel="stylesheet" href="{root}.css">
<script src="{root}.js"></script>
</head>
<body>
</body>
</html>""")

# Create and fill the CSS file.
with open(root + ".css", "w") as out_file:
    out_file.write("/*Author: Morrison*/\n")
    out_file.write(f"/*Date: {date}*/\n")
    out_file.write("""
h1, h2, .display
{
    text-align:center;
}
canvas
{
    border:solid 1px black;
    background-color:white;
}
""")

with open(root + ".js", "w") as out_file:
    out_file.write("/*Author: Morrison\n")
    out_file.write("Date: %s  */\n"% date)
    out_file.write("""addEventListener("load", main);
function main()
{
}""")
