#! /usr/bin/python
# -*- coding: UTF-8 -*-

# coding: utf-8

import os,sys
import random
import string
import re
import time
import json
import shutil
import hashlib
import time
import argparse
import file_utils

import sys

script_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
currentSdkpath = script_path + '/currentSdk'
sdkpath = script_path + '/sdk'


ignore_path_text = [".a", ".storyboard", ".py",".framework",".DS_Store",".xcuserstate",".jpg",".png"]

ignore_sdk = ['yfsdk']

keyFile = 'zooKeys.json'




def replaceJarNameInFile(full_path, new_text, old_text, tag):
    with open(full_path, "r") as fileObj:
        all_text = fileObj.read()
        fileObj.close()
    if old_text:
        oldjar = "%s_%s.jar" % (tag, old_text)
        newjar = "%s_%s.jar" % (tag, new_text)
        all_text = all_text.replace(oldjar, newjar)
        print ("\t替换: %s -> %s" % (oldjar, newjar))
        with open(full_path, "w") as fileObj:
            fileObj.write(all_text)
            fileObj.close()
    else:
        print ('cant not find old text')



def getoldJarDate(full_path):
    with open(full_path, "r") as fileObj:
        all_text = fileObj.read()
        fileObj.close()
        # 提取jar包日期
    old_text = (re.findall(r'jm_sdk_([^.jar]+)', all_text)[0])
    return old_text

def getoldNopayJarDate(full_path):
    with open(full_path, "r") as fileObj:
        all_text = fileObj.read()
        fileObj.close()
        # 提取jar包日期
    if all_text.find("jm_sdk_nopay") > -1:
        old_text = (re.findall(r'jm_sdk_nopay_([^.jar]+)', all_text)[0])
        return old_text
    return ""

def replaceNopayJar():
    global  sdkpath
    global  currentSdkpath
    print ('start replace jar ...')
    print (sdkpath)
    jarName = ''
    for parent, folders, files in os.walk(currentSdkpath):
        for file in files:
            if file.find(".jar") > -1 and file.find("nopay") > -1:
                jarName = file
                print('jarName -->'+ jarName)
    fullJarPath = os.path.join(currentSdkpath,jarName)
    newDate = (re.findall(r'jm_sdk_nopay_([^.jar]+)', jarName)[0])

    #修改config字段 及替换Jar包
    list = os.listdir(sdkpath)
    for l in list:
        if l in ignore_sdk:
            continue
        full_l_path = os.path.join(sdkpath,l)
        full_lib_path = os.path.join(full_l_path,'libs')
        if os.path.isdir(full_l_path):
            configPath = os.path.join(full_l_path,'libs/config.json')
            configPath = os.path.normcase(configPath)
            if os.path.exists(configPath):
                print(configPath)
                oldDate = getoldNopayJarDate(configPath)
                if oldDate == "":
                    continue
                # 修改config字段
                replaceJarNameInFile(configPath,newDate,oldDate,"jm_sdk_nopay")
                oldJarPath = '%s/jm_sdk_nopay_%s.jar' % (full_lib_path,oldDate)
                #删除旧文件
                if os.path.exists(oldJarPath):
                    print ('delete ---> %s' % oldJarPath)
                    try:
                        os.remove(os.path.normcase(oldJarPath))
                    except Exception as e:
                        print (e)
                    else:
                        print("File is deleted successfully")
                newJarPath = '%s/jm_sdk_nopay_%s.jar' % (full_lib_path,newDate)
                print('复制: %s ---> %s' % (fullJarPath,newJarPath))
                shutil.copyfile(fullJarPath, newJarPath)  # 复制文件

                print('-------------------------')

    print ("\nfinished replace jar")



######修改config字段 及替换Jar包
def replaceJar():
    global  sdkpath
    global  currentSdkpath
    print ('start replace jar ...')
    print (sdkpath)
    jarName = ''
    for parent, folders, files in os.walk(currentSdkpath):
        for file in files:
            if file.find(".jar") > -1 and file.find("nopay") < 1:
                jarName = file
                print('jarName -->'+ jarName)
    fullJarPath = os.path.join(currentSdkpath,jarName)
    newDate = (re.findall(r'jm_sdk_([^.jar]+)', jarName)[0])

    #修改config字段 及替换Jar包
    list = os.listdir(sdkpath)
    for l in list:
        if l in ignore_sdk:
            continue
        full_l_path = os.path.join(sdkpath,l)
        full_lib_path = os.path.join(full_l_path,'libs')
        if os.path.isdir(full_l_path):
            configPath = os.path.join(full_l_path,'libs/config.json')
            configPath = os.path.normcase(configPath)
            if os.path.exists(configPath):
                print(configPath)
                oldDate = getoldJarDate(configPath)
                # 修改config字段
                replaceJarNameInFile(configPath,newDate,oldDate,"jm_sdk")
                oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
                #删除旧文件
                if os.path.exists(oldJarPath):
                    print ('delete ---> %s' % oldJarPath)
                    try:
                        os.remove(os.path.normcase(oldJarPath))
                    except Exception as e:
                        print (e)
                    else:
                        print("File is deleted successfully")
                newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
                print('复制: %s ---> %s' % (fullJarPath,newJarPath))
                shutil.copyfile(fullJarPath, newJarPath)  # 复制文件

                print('-------------------------')

    print ("\nfinished replace jar")


######替换Res
def replaceRes():
    global  sdkpath
    global  currentSdkpath
    print ('start replace jar ...')
    resPath = os.path.join(currentSdkpath,"res")
    #print (resPath)
    list = os.listdir(sdkpath)
    for l in list:
        if l.find("jm") > -1:
            des_res_path = os.path.join(sdkpath,l,"res")
            print (des_res_path)
            file_utils.copyDir(resPath,des_res_path)

    #修改config字段 及替换Jar包
    # for l in list:
    #     full_l_path = os.path.join(sdkpath,l)
    #     full_lib_path = os.path.join(full_l_path,'libs')
    #     if os.path.isdir(full_l_path):
    #         configPath = os.path.join(full_l_path,'libs/config.json')
    #         configPath = os.path.normcase(configPath)
    #         if os.path.exists(configPath):
    #             print(configPath)
    #             oldDate = getoldJarDate(configPath)
    #             # 修改config字段
    #             replaceJarNameInFile(configPath,newDate,oldDate)
    #             oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
    #             #删除旧文件
    #             if os.path.exists(oldJarPath):
    #                 print ('delete ---> %s' % oldJarPath)
    #                 try:
    #                     os.remove(os.path.normcase(oldJarPath))
    #                 except Exception as e:
    #                     print (e)
    #                 else:
    #                     print("File is deleted successfully")
    #             newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
    #             print('复制: %s ---> %s' % (fullJarPath,newJarPath))
    #             shutil.copyfile(fullJarPath, newJarPath)  # 复制文件
    #
    #             print('-------------------------')

    print ("\nfinished replace res")

def main():
    #replaceJar()
    replaceNopayJar()
	#####修改了资源文件,要替换#####
    #replaceRes()


if __name__ == "__main__":
    main()