#!/usr/bin/python3

import sys
import os
# import tty
# import termios

from pathlib import Path

shebang = ("\
#!/usr/bin/python3\n\
#\n\
")

mainstr = ("\
import sys\n\
import os\n\
\n\
\n\
\n\
def main():\n\
\n\
    return 0\n\
\n\
exit(main())\n\
")

def get_yn(prompt):
    while True:
        yn = input("%s [y/n]: " % prompt)
        if yn and yn in "yYnN":
            break

    if yn in "yY":
        return True
    else:
        return False

def check_file(scriptname):

    if os.path.isfile(scriptname):
        print("Script %s exists." % scriptname)
        if not get_yn("Do you want to overwrite?"):
            print("You chose not to overwrite existing script: %s" % scriptname)
            print("Exiting ...")
            return False

    return True

def create_file(scriptname):
    with open(scriptname, 'w') as newscript:
        newscript.write(shebang)
        newscript.write("# %s\n#\n\n" % scriptname)
        newscript.write(mainstr)
    return 0

def main():
    scriptname = sys.argv[1]
    if not check_file(scriptname):
        return(1)
    create_file(scriptname)
    os.system("chmod +x %s" % scriptname)

    return 0

exit(main())
