#!/usr/bin/python
# -*- coding: cp936 -*-


import os
import sys
import struct
from BaseProc import *
import logging
import logger

logp = logger.FinalLogger.getLogger()

#参数判断,出错提示
paraLen = len(sys.argv)
if (paraLen != 5):
    logp.debug ('The para must equal to 4')
    logp.debug ('1 : script file (string)')
    logp.debug ('2 : src file (string)')
    logp.debug ('3 : dst file type (int)')
    logp.debug ('4 : max test id (uint)')
    logp.debug ('5 : src file  top dir')
    os._exit(1)


scrptPath = os.path.dirname(sys.argv[0])
sys.path.append(scrptPath + "\\DataFlash\\")
sys.path.append(scrptPath + "\\flash\\")

#调试开发使用以下目录，给客户端做开发用上述目录
#sys.path.append("flash\\")
#sys.path.append("DataFlash\\")

from procAll import *
from procCycleData import*
from procES import *
from procSG import *
from procRES import *


#输入的参数信息显示
logp.debug ('-------------para info begin-------------')
logp.debug ('scrip:' + sys.argv[0])
logp.debug ('src:'+ sys.argv[1])
logp.debug ('dst file type :'+ sys.argv[2])
logp.debug ('max test id:'+ sys.argv[3])

f = open(sys.argv[1], 'rb')
srcType = f.read(4)
f.close()

srcType = struct.unpack('<I', srcType)
logp.debug ('src type:'+ str(srcType[0]))

logp.debug ('src bak:'+ sys.argv[1] + '.pre' + str(srcType[0]))
dstFile = sys.argv[1]
logp.debug ('dst:'+ dstFile)

logp.debug ('dst type:'+ sys.argv[2])
logp.debug ('-------------para info end-------------')

if (srcType[0] == int(sys.argv[2])):
    logp.debug ('src type equal to dst type ! exit !')
    sys.exit(0)

#文件类型选择及处理
fileTypeKey = ("DataFlash\\", "flash\\CycleData\\", "flash\\ES\\", "flash\\SG\\","flash\\RES\\","not found")
for i in fileTypeKey:
    if (-1 != sys.argv[1].find(i)):
        break
    
#处理dataflash\
if (i == fileTypeKey[0]):
    for i in range(int(srcType[0]), int(sys.argv[2])):
        fn = sys.argv[1]
        if (procAll(i, i + 1, fn)):
            logp.debug ('if (procAll(i, i + 1, fn)):')
            os._exit(1)
        
#处理CycleData\
elif (i == fileTypeKey[1]):
    for i in range(int(srcType[0]), int(sys.argv[2])):
        fn = sys.argv[1]
        if (verCycleData(i, i + 1, fn)):
            logp.debug ('if (verCycleData(i, i + 1, fn)):')
            os._exit(1)
        
#处理ES\
elif (i == fileTypeKey[2]):
    for i in range(int(srcType[0]), int(sys.argv[2])):
        fn = sys.argv[1]
        if (verESData(i, i + 1, fn, int(sys.argv[3]), int(srcType[0]), sys.argv[4])):
            logp.debug ('if (verESData(i, i + 1, fn, int(sys.argv[3]), int(sys.argv[2]), sys.argv[4])):')
            os._exit(1)

#处理SG\
elif (i == fileTypeKey[3]):
    for i in range(int(srcType[0]), int(sys.argv[2])):
        fn = sys.argv[1]
        if (verSGData(i, i + 1, fn)):
            logp.debug ('if (verSGData(i, i + 1, fn)):')
            os._exit(1)
        
#处理RES\
elif (i == fileTypeKey[4]):
    for i in range(int(srcType[0]), int(sys.argv[2])):
        fn = sys.argv[1]
        if (verRESData(i, i + 1, fn)):
            logp.debug ('if (verRESData(i, i + 1, fn)):')
            os._exit(1)

#not found
else:
    logp.debug ('not support')
    os._exit(1)
    
