#!python
import subprocess
import os
import sys

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

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

TCPIP_API_TABLE = ""
CFG_TCPIP_KERNEL_TRB = ""

# 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")

#
# delete generated files
#
if os.path.exists("cfg1_out.db"):
	os.remove("cfg1_out.db")
if os.path.exists("cfg2_out.db"):
	os.remove("cfg2_out.db")
if os.path.exists("cfg3_out.db"):
	os.remove("cfg3_out.db")
if os.path.exists("cfg1_out.c"):
	os.remove("cfg1_out.c")
if os.path.exists("kernel_cfg.c"):
	os.remove("kernel_cfg.c")
if os.path.exists("kernel_cfg.h"):
	os.remove("kernel_cfg.h")
if os.path.exists("offset.h"):
	os.remove("offset.h")
if os.path.exists("tinet_cfg.c"):
	os.remove("tinet_cfg.c")
if os.path.exists("tinet_cfg.h"):
	os.remove("tinet_cfg.h")

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

#
# make command
#
cfg_command = cfg + " --pass 1 " + "--kernel " + CFG_KERNEL 
cfg_command += " " + cfg_includes
if TCPIP_API_TABLE == "":
	cfg_command += " --api-table " + cfg_api_table
else:
	cfg_command += " --api-table " + cfg_tcpip_api_table + ":tcpip"
	cfg_command += " --api-table " + cfg_api_table + ":kernel"
cfg_command += " " + cfg_cfg1_def_tables
cfg_command += " -M cfg1_out_c.d"
cfg_command += " " + proj_rel_dir + CFG_FILE

print cfg_command

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

print output
