# -*- coding: utf-8 -*-


import Files
import os
import datetime

if __name__ == '__main__':
    Header = r''' 
# ****************************************************************************** 
# (C) Automotive-Lighting Reutlingen GmbH  -  ALRT/EEG                           
#     Tuebinger Strasse 123, 72762 Reutlingen, Germany                           
# ****************************************************************************** 
# ------------------------------------------------------------------------------ 
# file   {0}        
# Writer   {1}         
#  \\ brief customer make files for HiRain.   
#           
# ------------------------------------------------------------------------------ 
C_SRC_CUST_HIRAIN_{2} = \
'''
    """
    {2} DIRNAME  = current directory Name 
    """
    Tail = r'''
C_SRC_CUST_HIRAIN                     := $(C_SRC_CUST_HIRAIN)\
                                         $(C_SRC_CUST_HIRAIN_{0})
    '''

    writer = r"xukaiming"
    gen_date = datetime.date.today()
    cwd_path = os.getcwd()
    dirfilter_strs = [".idea", "__pycache__", "venv"]
    # 获取所有的子文件夹
    sub_dirs = Files.getSubDir("./", dirfilter_strs)
    for subdir in sub_dirs:
 
        text_Body = ""
        text = ""
        # gmk 以文件夹名字作为gmk文件名
        gmkName = subdir + ".gmk"
        fileName = os.path.basename(gmkName)
        innerSrcName = fileName.split(".")[0]
        # 获取文件列表
        files = Files.find_file(subdir, True, ".c", None)
        CFileList = []
        for srcFile in files:
            srcFile = srcFile.replace(cwd_path, "        $(PATH_SRC_HIRAIN)")
            srcFile = srcFile.replace("\\", "/")
            srcFile = srcFile.replace(".c", "")
            CFileList.append(srcFile)
        textHeader = Header.format(fileName, writer,  innerSrcName)
        textBody = "\\\n".join(CFileList)
        textTail = Tail.format(innerSrcName)
        text_Body = textHeader + textBody + "\n" + textTail

        if len(CFileList) > 0:
            fileName = os.path.join(cwd_path, gmkName)
            with open(fileName, 'w') as file:
                file.write(text_Body)
                file.close()
            # 填充其他内容
            # 保存工程名
    print('create all gmk successed!\r\n')
