import sys
import os

# friend_rankserver_z$id_test
# friend_imserver_z$id_test
# friend_masterserver_z$id_test
# friend_wsproxy_z$id_test
# statserver_z$id_test
# mail_z$id_test
# loginserver_z$id_test
# cloud_z$id_test
# gamelog_z$id_test
# service_z$id_test
# web3service_z$id_test
# game2006_wsproxy_z$id_test
# game2006_masterverver_z$id_test
# game2006_gameserver_z$id_test

projects = [
    {
        'name': 'friend_rankserver',
        'git': 'friend_rankserver_z$id_test',
        'conf': 'prod/friend/friend_rankserver_z$id_test',
        'targetConf': 'prod/friend/friend_rankserver_z$id_test',
        'res': 'prod/friend/friend_rankserver_z$id_test',
        'targetRes': 'prod/friend/friend_rankserver_z$id_test',
    },
    {
        'name': 'friend_imserver',
        'git': 'friend_imserver_z$id_test',
        'conf': 'prod/friend/friend_imserver_z$id_test',
        'targetConf': 'prod/friend/friend_imserver_z$id_test',
        'res': 'prod/friend/friend_imserver_z$id_test',
        'targetRes': 'prod/friend/friend_imserver_z$id_test',
    },
    {
        'name': 'friend_masterserver',
        'git': 'friend_masterserver_z$id_test',
        'conf': 'prod/friend/friend_masterserver_z$id_test',
        'targetConf': 'prod/friend/friend_masterserver_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'friend_wsproxy',
        'git': 'friend_wsproxy_z$id_test',
        'conf': 'prod/friend/friend_wsproxy_z$id_test',
        'targetConf': 'prod/friend/friend_wsproxy_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'mail',
        'git': 'mail_z$id_test',
        'conf': 'prod/global/mail_z$id_test',
        'targetConf': 'prod/global/mail_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'gamelog',
        'git': 'gamelog_z$id_test',
        'conf': 'prod/global/gamelog_z$id_test',
        'targetConf': 'prod/global/gamelog_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'loginserver',
        'git': 'loginserver_z$id_test',
        'conf': 'prod/global/loginserver_z$id_test',
        'targetConf': 'prod/global/loginserver_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'cloud',
        'git': 'cloud_z$id_test',
        'conf': 'prod/global/cloud_z$id_test',
        'targetConf': 'prod/global/cloud_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'service',
        'git': 'service_z$id_test',
        'conf': 'prod/global/service_z$id_test',
        'targetConf': 'prod/global/service_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'web3service',
        'git': 'web3service_z$id_test',
        'conf': 'prod/global/web3service_z$id_test',
        'targetConf': 'prod/global/web3service_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'httpproxy',
        'git': 'httpproxy_z$id_test',
        'conf': 'prod/global/httpproxy_z$id_test',
        'targetConf': 'prod/global/httpproxy_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'game2006_wsproxy',
        'git': 'game2006_wsproxy_z$id_test',
        'conf': 'prod/game2006/wsproxy_z$id_test',
        'targetConf': 'prod/game2006/wsproxy_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'game2006_masterserver',
        'git': 'game2006_masterserver_z$id_test',
        'conf': 'prod/game2006/masterserver_z$id_test',
        'targetConf': 'prod/game2006/masterserver_z$id_test',
        'res': '',
        'targetRes': '',
    },
    {
        'name': 'game2006_gameserver',
        'git': 'game2006_gameserver_z$id_test',
        'conf': 'prod/game2006/gameserver_z$id_test',
        'targetConf': 'prod/game2006/gameserver_z$id_test',
        'res': 'prod/game2006/gameserver_z$id_test',
        'targetRes': 'prod/game2006/gameserver_z$id_test',
    },
    {
        'name': 'game2006api',
        'git': 'game2006api_z$id_test',
        'conf': 'prod/game2006/game2006api_z$id_test',
        'targetConf': 'prod/game2006/game2006api_z$id_test',
        'res': 'prod/game2006/game2006api_z$id_test',
        'targetRes': 'prod/game2006/game2006api_z$id_test',
    },
    {
        'name': 'statserver',
        'git': 'statserver_z$id_test',
        'conf': 'prod/global/statserver_z$id_test',
        'targetConf': 'prod/global/statserver_z$id_test',
        'res': '',
        'targetRes': '',
    },
]

def pubOne(srcId, targetId):
    resDir = '~/opensource/res/'
    confDir = '~/opensource/conf/'
    gitDir = '~/opensource/publish' + '/z' + targetId
    os.system('mkdir -p ' + gitDir)
    for proj in projects:
        srcProjName = proj['git'].replace('$id', srcId)
        targetProjName = proj['git'].replace('$id', targetId)
        cmd = 'cd ' + gitDir + ' && git clone git@git.kingsome.cn:publish/' + srcProjName + '.git\n'
        cmd += 'cd ' + gitDir + '/' + srcProjName + ' && git remote set-url origin git@git.kingsome.cn:publish/' + targetProjName+ '.git\n'
        cmd += 'cd ' + gitDir + '/' + srcProjName + ' && git push\n'
        print(cmd)
        #os.system(cmd)
        if proj['conf']:
            srcConf = proj['conf'].replace('$id', srcId)
            targetConf = proj['targetConf'].replace('$id', targetId)
            cmd = 'cp -r ' + confDir + srcConf + ' ' + confDir + targetConf
            print(cmd)
            #os.system(cmd)
        if proj['res']:
            srcRes = proj['res'].replace('$id', srcId)
            targetRes = proj['targetRes'].replace('$id', targetId)
            cmd = 'cp -r ' + resDir + srcRes + ' ' + resDir + targetRes
            print(cmd)
           #os.system(cmd)

def pub():
    srcId = sys.argv[1]
    targetIds = sys.argv[2].split(',')
    for targetId in targetIds:
        pubOne(srcId, targetId)

pub()
