#!python
# -*- coding: utf-8 -*-
import os.path
import os
import shutil

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

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

src_abs_path = os.path.abspath(SRCDIR)
wd_abs_path = os.path.abspath('.')
cfg_mtsp = wd_abs_path + r"\cfg\cfg.mtsp"
kernel_mtsp = wd_abs_path + r"\kernel\kernel.mtsp"
asp_mtpj = wd_abs_path + r"\asp.mtpj"


if COPY_SAMPLE1:
	shutil.copy(src_abs_path + r'/sample/sample1.c', '.')
	shutil.copy(src_abs_path + r'/sample/sample1.h', '.')
	shutil.copy(src_abs_path + r'/sample/sample1.cfg', '.')

#
# close project
#
#project.Close() 
print wd_abs_path + "\\asp_dcre"
project.Create(wd_abs_path + "\\asp_dcre",
				MicomType.RX, 
				TARGET_MCU, 
				ProjectKind.Empty,
				Compiler.CC_RX,
				False)

project.Create(wd_abs_path + "\\cfg\\cfg",
				MicomType.RX, 
				TARGET_MCU, 
				ProjectKind.Empty,
				Compiler.CC_RX,
				True)

project.Create(wd_abs_path + "\\kernel\\kernel",
				MicomType.RX, 
				TARGET_MCU, 
				ProjectKind.Library,
				Compiler.CC_RX,
				True)

#
# Change debug tool
#
#debugger.DebugTool.Change(DebugTool.Serial)

#
# Add files for asp
#
for file in asp_app_files:
	project.File.Add(wd_abs_path + "\\" + file, u"app")

for file in asp_syssvc_files:
	project.File.Add(src_abs_path + "\\" + file, u"syssvc")

for file in asp_library_files:
	project.File.Add(src_abs_path + "\\" + file, u"library")

for file in asp_configuration_files:
	project.File.Add(wd_abs_path + "\\cfg\\" + file, u"configuration")

str = src_abs_path + '\\' + statup_file
project.File.Add(str, u"スタートアップ")

#
# Add files fot cfg
#
project.Change(cfg_mtsp)

for file in cfg_configuration_files:
	project.File.Add(wd_abs_path + "\\cfg\\" + file, u"configuration")

#
# Add files fot kernel
#
project.Change(kernel_mtsp)

for file in kernel_kernel_files:
	project.File.Add(src_abs_path + "\\" + file, u"kernel")

for file in kernel_arch_files:
	project.File.Add(src_abs_path + "\\" + file, u"arch")

for file in kernel_target_files:
	project.File.Add(src_abs_path + "\\" + file, u"target")

for file in kernel_pdic_files:
	project.File.Add(src_abs_path + "\\" + file, u"pdic")


project.Close(True)


#
# Modify asp.mtpfj
#
inputstr = ReadFile('asp.mtpj')

# Set ccrx include path
inputstr = NewSetCcrxIncludes(inputstr, asp_rel_includes, INCLUDES, SRCDIR)
# Set libary path and file
inputstr = NewSetLibFiles(inputstr, asp_lib_files)
# change Option
inputstr = ChangeItemXml(inputstr, 'OutputMessageFormat-0', '%Program% %Options%')
inputstr = ChangeItemXml(inputstr, 'LinkOptionStart-0', 'B_1,R_1,B_2,R_2,B,R,SI/00000000,C$VECT,P_START,C_1,C_2,C,D_1,D_2,D,P,W_1,W_2,W,L/FFF00000,FVECT/FFFFFFD0')
inputstr = ChangeItemXml(inputstr, 'LinkOptionConvertFileName-0', '%ProjectName%.srec')
inputstr = ChangeItemXml(inputstr, 'LinkOptionShowSymbol-0', 'True')
inputstr = NewSetItemXml(inputstr, 'LinkOptionRom-0', 'D=R\nD_1=R_1\nD_2=R_2\n')
# Set ccrx assembler include path
inputstr = NewSetItemXml(inputstr, 'AsmOptionInclude-0', SRCDIR + "target/" + TARGET)
# Set Prebuild/Postbuile
inputstr = NewSetPostbuild(inputstr, asp_post_python_files, SRCDIR)

WriteFile('asp.mtpj', inputstr)

#
# Modify ./cfg/cfg.mtsp
#
inputstr = ReadFile('./cfg/cfg.mtsp')

# Set ccrx include path
inputstr = NewSetCcrxIncludes(inputstr, cfg_rel_includes, INCLUDES, "../" + SRCDIR)
# change Option
inputstr = ChangeItemXml(inputstr, 'OutputMessageFormat-0', '%Program% %Options%')
inputstr = ChangeItemXml(inputstr, 'LinkOptionConvertFileName-0', 'cfg1_out.srec')
inputstr = ChangeItemXml(inputstr, 'LinkOptionConvertFolder-0', '.')
inputstr = ChangeItemXml(inputstr, 'LinkOptionStart-0', 'C_1,C_2,C,P/0FFFF8100')
inputstr = ChangeItemXml(inputstr, 'LinkOptionRom-0', '')
inputstr = ChangeItemXml(inputstr, 'LinkOptionShowSymbol-0', 'True')
# Set Prebuild/Postbuile
inputstr = NewSetPrebuild(inputstr, cfg_pre_python_files, "../" + SRCDIR)
inputstr = NewSetPostbuild(inputstr, cfg_post_python_files, "../" + SRCDIR)

inputstr = NewSetDefine(inputstr, cfg_define)

WriteFile('./cfg/cfg.mtsp', inputstr)

#
# Mofity ./kernel/kernel.mtsp
#
inputstr = ReadFile('./kernel/kernel.mtsp')

# Set ccrx include path
inputstr = NewSetCcrxIncludes(inputstr, kernel_rel_includes, INCLUDES, "../" + SRCDIR)
# Set ccrx define
inputstr = NewSetDefine(inputstr, kernel_define)
# Set ccrx assembler include path
inputstr = NewSetItemXml(inputstr, 'AsmOptionInclude-0', "../cfg")
# change Option

WriteFile('./kernel/kernel.mtsp', inputstr)

project.Open(wd_abs_path + r'\asp.mtpj')
project.Change(asp_mtpj)
