#!python
import subprocess
import os
import sys
import re  # for regular expression
import shutil
import os.path

#
# variable definition
#
INCLUDES = []
CFG1_DEF_TABLES = []

# set relative path from top proj
proj_rel_dir = "../"

# call definition file
common.Source(proj_rel_dir + "def.py")

# path
src_abs_path = os.path.abspath(proj_rel_dir + SRCDIR)

# call common file
common.Source(src_abs_path + "/arch/ccrx/common.py")

#
# convert map file
#
inputfile = open("./DefaultBuild/cfg.map")
outputfile = open("cfg1_out.syms", 'w')

r1 = re.compile("^\s+(_\w+)")
r2 = re.compile("^\s+([0-9a-f]+)\s+")
line = inputfile.readline() 
while line:
    line = line.replace('\r\n','')   #delete newline
    m1 = r1.search(line)
    if m1:
        line = inputfile.readline() 
        line = line.replace('\r\n','')   #delete newline
        m2 = r2.search(line)
        outputfile.write("0x" + m2.group(1) + " T " + m1.group(1) + "\n")
    line = inputfile.readline()

inputfile.close()
outputfile.close()

if TCPIP_API_TABLE != "":
	cfg_tcpip_kernel_trb = srcdir + CFG_TCPIP_KERNEL_TRB
	cfg_tcpip_api_table = srcdir + TCPIP_API_TABLE

#
# Execute cfg path 2
#
# make command
cfg_command = cfg + " --pass 2 " + "--kernel " + CFG_KERNEL
cfg_command += " " + cfg_includes
if TCPIP_API_TABLE == "":
	cfg_command += " -T " + cfg_kernel_trb
else:
	cfg_command += " -T " + cfg_tcpip_kernel_trb + ":tcpip"
	cfg_command += " -T " + cfg_kernel_trb + ":kernel"

print cfg_command

# Execute 
try:
   output = subprocess.check_output(cfg_command, stderr=subprocess.STDOUT,)
except subprocess.CalledProcessError, e:
    print "ERROR!! : ", e.output
    sys.exit()

output.replace('\r','')
print output

##
## mov Os_Cfg_tmp.h Os_Cfg.h
##
#import filecmp
#if not os.path.isfile(r'Os_Cfg.h'):
#	print "Rename Os_Cfg_tmp.h to Os_Cfg.h"
#	shutil.move("Os_Cfg_tmp.h", "Os_Cfg.h")
#else:
#	print "compare Os_Cfg_tmp.h and Os_Cfg.h"
#	if not filecmp.cmp(r'Os_Cfg_tmp.h', r'Os_Cfg.h'):
#		print "Rename Os_Cfg_tmp.h to Os_Cfg.h"
#		shutil.move("Os_Cfg_tmp.h", "Os_Cfg.h")
#	else:
#		print "Delete Os_Cfg_tmp.h"
#		os.remove("Os_Cfg_tmp.h")

#
# Execute cfg path 3
#
# make command
cfg_command = cfg + " --pass 2 -O " + "--kernel " + CFG_KERNEL 
cfg_command += " " + cfg_includes
cfg_command += " --rom-image cfg1_out.srec --rom-symbol cfg1_out.syms"
cfg_command += " -T " + cfg_offset_trb

print cfg_command

# Execute 
try:
   output = subprocess.check_output(cfg_command, stderr=subprocess.STDOUT,)
except subprocess.CalledProcessError, e:
    print "ERROR!! : ", e.output
    sys.exit()

output.replace('\r','')
print output

#
# convert map file
#
inputfile = open("offset.h")
outputfile = open("offset.inc", 'w')

outputfile.write("; offset.inc\n");
outputfile.write("\n");

r1 = re.compile("^#define\s+([0-9|a-z|A-Z|_]+)\s+(.+)\s*$")
r2 = re.compile("^0x([0-9A-Fa-f]+)$");
line = inputfile.readline() 
while line:
    line = line.replace('\r\n','')   #delete newline
    m1 = r1.search(line)
    if m1:
        value = m1.group(2)
        m2 = r2.search(value)
        if m2:
            value = "0" + m2.group(1) + "H"
        line = "	" + m1.group(1) + "	.equ	" + value
        outputfile.write(line + "\n")
    line = inputfile.readline()

inputfile.close()
outputfile.close()
