#!/usr/bin/python
#################################################################
#
#        Copyright 2013, Big Switch Networks, Inc.
#
# Licensed under the Eclipse Public License, Version 1.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#        http://www.eclipse.org/legal/epl-v10.html
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific
# language governing permissions and limitations under the
# License.
#
#################################################################
import os
import sys

PPE_MODULE_ROOT=os.path.abspath(__file__ + "/../..")

#
# Insert this into the ppe_field_t definition
#
s = """

    #if PPE_CONFIG_INCLUDE_CUSTOM_META_FIELDS == 1
    #define PPE_CUSTOM_META_FIELD_ENTRY(_name, _size, _offset, _shift) PPE_FIELD_META_##_name,
    #include <ppe_custom_fields.x>
    #endif

"""
fname = "%s/inc/PPE/ppe_types.h" % PPE_MODULE_ROOT
new = ''
with open(fname, "r") as f:
    for l in f.readlines():
        if "PPE_FIELD_META_FIELD_COUNT," in l:
            # Insert before this line
            new += s
        new += l

with open(fname, "w") as f:
    f.write(new)



#
# Insert the custom meta fields definitions into
# the autogenerated ppe.x field entries
#
s = """
#if PPE_CONFIG_INCLUDE_CUSTOM_META_FIELDS == 1
#define PPE_CUSTOM_META_FIELD_ENTRY(_name, _size, _offset, _shift) \\
PPE_FIELD_ENTRY(META, _name, _size, _offset + %(OFFSET)s, _shift)
#include <ppe_custom_fields.x>
#endif

"""
fname = "%s/inc/PPE/ppe.x" % PPE_MODULE_ROOT
new = ''
offset = 0
with open(fname, "r") as f:

    for l in f.readlines():
        # Need to keep track of the field offsets relative to the start of the
        # metaheader
        if l.startswith("PPE_FIELD_ENTRY(META, "):
            x = l.split(',')
            coff = int(x[-2])
            csize = int(x[-3])
            if coff != 0:
                offset = coff + csize/8
        if l.startswith("PPE_FIELD_ENTRY(META, FIELD_COUNT"):
            # Insert before this line
            new += s % (dict(OFFSET=offset))
        new += l
with open(fname, "w") as f:
    f.write(new)




#
# Insert this into ppe_field_map[]
#
newmap = """

    #if PPE_CONFIG_INCLUDE_CUSTOM_META_FIELDS == 1
    #define PPE_CUSTOM_META_FIELD_ENTRY(_name, _size, _offset, _shift) { "META_" #_name, PPE_FIELD_META_##_name },
    #include <ppe_custom_fields.x>
    #endif

"""
#
# Insert this into ppe_field_desc[]
#
newdesc = """

    #if PPE_CONFIG_INCLUDE_CUSTOM_META_FIELDS == 1
    #define PPE_CUSTOM_META_FIELD_ENTRY(_name, _size, _offset, _shift) { "None", PPE_FIELD_META_##_name },
    #include <ppe_custom_fields.x>
    #endif

"""
fname = "%s/src/ppe_enums.c" % PPE_MODULE_ROOT
new = ''
with open(fname, 'r') as f:
    for l in f.readlines():
        if '{ "META_FIELD_COUNT", PPE_FIELD_META_FIELD_COUNT },' in l:
            new += newmap
        if '{ "None", PPE_FIELD_META_FIELD_COUNT },' in l:
            new += newdesc
        new += l
with open(fname, "w") as f:
    f.write(new)


