#!/usr/bin/env python3

# mkpy: Python script template maker.
# Updated November 2020

import sys, os.path, os#, argparse
# from termcolor import colored

#TODO: argparse to create robust and more or less feature ready app.
#parser = argparse.ArgumentParser(description='Python script template maker.')
#parser.add_argument('filename', help='Python file script to create.')
#parser.add_argument('--limit', '-l', type=int, help='')
#parser.add_argument('--version', '-v', action='version', version='%(prog)s 0.1')

def CheckFilename(filename):
    """
    Check if the file exists
    """
    while os.path.isfile(filename):
        print(f"\n[-] {filename} exists")
        filename=input("[+] Pick new namefile: ")
    return filename

def __main__(): # Main
    # Check number of arguments & error handling
    argc = int(len(sys.argv) - 1)
    if (argc != 1):
        print (f"\n[-] Provide a valid filename.")
        print (f"\n[-] Usage: mkpy [filename]\n")
        sys.exit(1)
    else:
        filename = sys.argv[1]
        user = os.environ.get("USER")

    # Path to my Pyscript directory
    PathDir = "/Users/" + user + "/Code/PyScript/" + filename

    # Check File
    CheckedFile = CheckFilename(PathDir)

    # Create the script file
    with open(CheckedFile, "w+") as textFile:
        textFile.write("#!/usr/bin/env python3\n\n")
        Description=input("\nBrief script descritption: ")
        textFile.write(f"# {CheckedFile}\n# {Description}\n# Author: Wilfrantz")
    textFile.close()

    # Open script file with system default file editor
    editor = os.environ.get('EDITOR')
    cmd = f"{editor} {CheckedFile}"
    os.system(cmd)


if __name__ == '__main__':
    __main__()
