#!/usr/bin/env python

import re
import sys

comps = []

header = open(sys.argv[1] + '/inc/hal_tbl.h', 'w')
code = open(sys.argv[1] + '/src/hal_tbl.c', 'w')

for infile in sys.argv[2:]:
    with open(infile) as f:
        pins = []
        compname = ''
        for line in f:
            comp = re.search('COMP\((\w*)\);', line)
            if comp:
                compname = comp.groups()[0]
            pin = re.search('HAL_PIN\((\w*)\)', line)
            if pin:
                pins.append((pin.groups()[0], int(1)))
            pin = re.search('HAL_PINA\((\w*),\s*(\d*)\)', line)
            if pin:
                pins.append((pin.groups()[0], int(pin.groups()[1])))
        comps.append((compname, pins, infile))

header.write("#pragma once\n")
header.write("//generated by " + sys.argv[0] + " DO NOT EDIT\n\n")

header.write("struct pin_ctx_t{\n")
header.write("   hal_pin_inst_t rt_prio;\n")
header.write("   hal_pin_inst_t frt_prio;\n")
# header.write("   hal_pin_inst_t rt_calc_time;\n")
# header.write("   hal_pin_inst_t rt_start_time;\n")
# header.write("   hal_pin_inst_t frt_calc_time;\n")
# header.write("   hal_pin_inst_t frt_start_time;\n")
# header.write("   hal_pin_inst_t nrt_calc_time;\n")
# header.write("   hal_pin_inst_t nrt_start_time;\n")

header.write("};\n\n")

for comp_name, pins, file_name in comps:
    header.write("struct " + comp_name + "_pin_ctx_t{ // found in " + file_name + "\n")
    header.write("   hal_pin_inst_t rt_prio;\n")
    header.write("   hal_pin_inst_t frt_prio;\n")
    # header.write("   hal_pin_inst_t rt_calc_time;\n")
    # header.write("   hal_pin_inst_t rt_start_time;\n")
    # header.write("   hal_pin_inst_t frt_calc_time;\n")
    # header.write("   hal_pin_inst_t frt_start_time;\n")
    # header.write("   hal_pin_inst_t nrt_calc_time;\n")
    # header.write("   hal_pin_inst_t nrt_start_time;\n")
    
    for (p, i) in pins:
        if i > 1:
            header.write("   hal_pin_inst_t " + p + "[" + str(i) + "]" + ";\n")
        else:
            header.write("   hal_pin_inst_t " + p + ";\n")
    header.write("};\n\n")
    
header.write("extern const hal_comp_t * comps[];\n")
header.write("extern const pin_t pins[];\n")
header.write("extern const uint32_t comp_count;\n")
header.write("extern const uint32_t pin_count;\n\n")

for comp_name, pins, file_name in comps:
    header.write("extern const hal_comp_t " + comp_name + "_comp_struct; // found in " + file_name + "\n")


code.write("#include \"hal.h\"\n")
code.write("//generated by " + sys.argv[0] + " DO NOT EDIT\n\n")
code.write("const hal_comp_t * comps[] = {\n")
for comp_name, pins, file_name in comps:
   code.write("   &" + comp_name + "_comp_struct, // found in " + file_name + "\n")
code.write("};\n\n")
code.write("const uint32_t comp_count = sizeof(comps) / sizeof(comps[0]);\n\n")

code.write("const pin_t pins[] = {\n")
for comp_name, pins, file_name in comps:
    code.write("   // pins for comp " + comp_name + " found in " + file_name + "\n")
    code.write("   \"rt_prio\",\n")
    code.write("   \"frt_prio\",\n")
    # code.write("   \"rt_calc_time\",\n")
    # code.write("   \"rt_start_time\",\n")
    # code.write("   \"frt_calc_time\",\n")
    # code.write("   \"frt_start_time\",\n")
    # code.write("   \"nrt_calc_time\",\n")
    # code.write("   \"nrt_start_time\",\n")
    
    for (p, i) in pins:
        if i > 1:
            for j in range(i):
                code.write("   \"" + p + str(j) + "\",\n")
        else:
            code.write("   \"" + p + "\",\n")

code.write("};\n\n")
code.write("const uint32_t pin_count = sizeof(pins) / sizeof(pins[0]);\n\n")

header.close()
code.close()
