#!/usr/bin/env python3
############################################################
#  Author: John M. Morrison.  Released without warrantee with a GPL v3 license.
#  8 Dec 2021:   upgrated to Python 3 and f-strings.
#  28 Aug 2023:  allow user to specify date; default is today.
#                add full documentation
#                add the if __name__ idiom.
#                add overwrite check
#  Date format must be entered as yyyymmdd.
#  usages:
#       python makeC.py filename   (default date is today)
#       python makeC.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:
#       a skeleton C file with a comment header and a 
#       date stamp.
############################################################

from sys import argv
import os.path
from datetime import datetime
##make a file name
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]

## todo: make sure we are not clobbering and warn
full_name = root + ".c"
if os.path.exists(full_name):
    reply = input("File exists; do you want to clobber it (y/n)? ")
    if reply.lower() != "y":
        print("Bailing.  I won't overwrite your existing file.")
        quit()

with open(full_name, "w") as fp:
    fp.write(r"/**************************************************")
    fp.write(f"""
*   Author: Morrison
*   Date:   {date}
*   Program {full_name}""")
    fp.write("\n")
    fp.write(r"***************************************************/")
    fp.write(r"""
#include<stdio.h>
int main(int argc, char** argv)
{
    return 0;
}""")
