#!/usr/bin/env python3
#
#@file make_protocol.py
#@brief python script to generate code for PolyPacket
#@author Jason Berger
#@date 02/19/2019
#

import sys
import os
from mako.template import Template
import yaml
import argparse
import pkgutil
from mrtutils.mrtTemplateHelper import *
from mrtutils.device import Device as Device


# Initialize the argument parser
def init_args():
    global parser
    parser = argparse.ArgumentParser("Tool to generate code and documentation for PolyPacket protocol")
    parser.add_argument('-i', '--input', type=str, help='input file to parse', default="")
    parser.add_argument('-o', '--output', type=str, help='Output path', default=".")
    parser.add_argument('-d', '--document', type=str, help='documentation path', default="")
    parser.add_argument('-t', '--template', type=str, help='path to create template', default="")
    

def buildTemplate(object, templateFile, outputFile):
    exists= False
    cr = CodeReplacer()
    newContents =""
    headerPattern = r"@file (.*?\.)(.*?) \*/"
    if os.path.isfile(outputFile):
        exists = True
        curFile = open(outputFile, "r")
        text = curFile.read()
        cr.loadText(text)
        cr.loadText(text,headerPattern)

    template = Template(pkgutil.get_data('mrtutils',templateFile) )
    newContents = "\n".join(template.render(obj = object).splitlines())
    newContents = cr.insertBlocks(newContents)
    newContents = cr.insertBlocks(newContents,headerPattern)
    cr.printDrops()
    text_file = open( outputFile , "w")
    text_file.write(newContents)
    text_file.close()

def buildDeviceSource(device , outputPath, baseName =''):

    if baseName == '':
        baseName = device.name.lower() 

    buildTemplate(device, 'templates/device_header_template.h', outputPath +"/"+baseName + ".h" )
    buildTemplate(device, 'templates/device_source_template.c', outputPath +"/"+baseName + ".c" )
    buildTemplate(device, 'templates/device_regs_header.h', outputPath +"/"+baseName + "_regs.h" )


def main():
    global path
    global parser
    global args

    init_args()
    args= parser.parse_args()
    argCount = len(sys.argv)
    

    inputFile = args.input
    path = args.output
    docPath = args.document
    templatePath = args.template

    device = Device('unnamed')

    if not templatePath =="":
        templatePath.replace("yml","") 
        buildTemplate(device, 'templates/device_descriptor_template.yml', templatePath + ".yml") 
        sys.exit()


    if inputFile == "":
        print("No input file specified, use -t to create a template file")
        sys.exit()


    # if os.path.isfile(inputFile):
    print("parsing " + inputFile )
    device.parseYAML(inputFile)
    

    print(" Generating Register files for " + device.name)
    buildDeviceSource(device, path)

    if not docPath == "":
        buildTemplate(device, 'templates/device_doc.md', docPath +"/README.md" )
        buildTemplate(device, 'templates/device_regmap.html', docPath +"/REGMAP.html" )
        


if __name__ == "__main__":
    main()