# -*- coding:utf-8 -*-
# python ctemplate.py test

import io
import sys
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

class ctemplate:

    __fileName = ' '
    __sourceName = ' '
    __headerName = ' '
    __Author = 'Kinve JQ'
    __time = datetime.datetime.now().strftime('%Y-%m-%d')
    __Comments_Head = ( '/************************************************************************\n' +
                        '*                             ---- 居加居 ----                           \n')
    __fileComments =  { '维护记录':'\n*    '+__time+unicode('.Kinve:1.创建文件'),
                        '程序维护':' ',
                        '编写日期':__time,
                        '版本':'V0.1',
                        '说明':' ',
                        '编写人员':__Author,
                        '文件名':' ',
    }
    __Comments_Tail = ( '* 免责声明：\n' +
                        '* (c) Author ' + __Author + '. All rights reserved.\n' +
                        '**************************************************************************/\n\n')

    __CommentsOrder = ('文件名','编写人员','说明','版本','编写日期','程序维护','维护记录')

    def __init__(self,s):
        self.__fileName = s
        self.__sourceName = s + ".c"
        self.__headerName = s + '.h'

    def generateCommentsAtTop(self,name):
        self.__fileComments['文件名'] = name
        #头部
        comments =  self.__Comments_Head
        #find max length of string
        maxLen = 0
        for s in self.__CommentsOrder:
            if(len(s) > maxLen):
                maxLen = len(s)

        for k in self.__CommentsOrder:
            #utf-8,汉字占3个byte  
            alignSpaceAmount = (maxLen - len(k))/3*2 + 4
            alignSpace = alignSpaceAmount * ' '
            comments += ('*  '+ k + ':' + alignSpace  + self.__fileComments[k] + '\n')
        #尾部
        comments += self.__Comments_Tail

        return comments

    def generateCommentsInTheEnd(self):
        return      ("\n/*************** (C) COPYRIGHT Kinve JQ  *******END OF FILE **************/\n")

    def createSource(self):
        fh = io.open(self.__sourceName,mode='w',encoding='utf-8')
        cm = self.generateCommentsAtTop(self.__sourceName)
        cm += ("#include \"%s\"\n" %self.__headerName) 
        cm += ("\n"*5)
        cm += self.generateCommentsInTheEnd()
        fh.write(unicode(cm))
        fh.close()

    def createHeader(self):
        fh = io.open(self.__headerName,mode='w',encoding='utf-8')
        cm = self.generateCommentsAtTop(self.__headerName)
        cm += "#ifndef __%s_H_\n" %self.__fileName.upper()
        cm += "#define __%s_H_\n" %self.__fileName.upper() + "\n"
        cm += "/* 包含的头文件----------------------------------------------------------*/\n"
        cm += "#include <stdlib.h>\n"
        cm += "/* 宏定义 ---------------------------------------------------------------*/\n\n"
        cm += "/* 类型定义 -------------------------------------------------------------*/\n\n"
        cm += "/* 常量定义 -------------------------------------------------------------*/\n\n"
        cm += "/* 数据定义 -------------------------------------------------------------*/\n\n"
        cm += "/* 函数定义--------------------------------------------------------------*/\n\n"
        cm += ("\n"*1)
        cm += "#endif\n"
        cm += self.generateCommentsInTheEnd()
        fh.write(unicode(cm))
        fh.close()

    def createTemplatePairs(self):
        self.createSource()
        self.createHeader()


if __name__ == '__main__':
        if len(sys.argv) != 2:
            sys.stderr.write("please input corret parameter")
        else:
            s = sys.argv[1]
            ct = ctemplate(s)
            ct.createTemplatePairs()