#!/usr/bin/python3.6

from pygments import highlight
import easygui as eg
from pygments.lexers import get_lexer_for_filename
from pygments.formatters import get_formatter_for_filename 
import os
import sys

def pygmentize_by_filenames(sourcepath,outputpath):
    lexer = get_lexer_for_filename(sourcepath)
    formatter = get_formatter_for_filename(outputpath)
    with open(sourcepath) as f:
        code = f.read()
        result = highlight(code,lexer,formatter)
    with open(outputpath,'w') as f:
        f.write(result)
        
if __name__ == '__main__':
    n = len(sys.argv)
    assert n != 1, "ERROR: must have at least one command line argument"
    spath = sys.argv[1]
    if len(sys.argv) == 2:
           opath = spath + ".rtf" # default output type is rtf
    else:
           opath = sys.argv[2]
    # if len(sys.argv) == 1:
    #     msg = "Enter the input and output filenames"
    #     title = "Code Highlighter"
    #     fieldNames = ["Source Code File","Formatted Output File"]
    #     fieldValues = [] # we start with blanks for the values
    #     fieldValues = eg.multenterbox(msg,title, fieldNames)
    #     # make sure that none of the fields was left blank
    #     while True:
    #         if fieldValues is None: break
    #         errmsg = ""
    #         for i in range(len(fieldNames)):
    #             if fieldValues[i].strip() == "":
    #                 errmsg += '%s is a required field.\n\n' % fieldNames[i]

    #         if errmsg == "":
    #             break # no problems found
    #         fieldValues = eg.multenterbox(errmsg, title, fieldNames, fieldValues)
    #     spath, opath = fieldValues

    pygmentize_by_filenames(spath,opath)

    
