#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from inc import CliTool
import sys, os

zk = sys.argv[1]
envs = sys.argv[2]
real_envs = envs

isNodeVideoZk = (zk in ['node-video'])

isIntraIp = True
forePort = False
hostFile ='/hosts/hosts-honor-vpc.txt'
if zk in ['dm.erp', 'easyboss']:
    hostFile = '/hosts/hosts-easyboss.txt'

if zk in ['greatboss']:
    hostFile = '/hosts-tc-us-vpc.txt'
    isIntraIp = False

if zk in ['plw']:
    hostFile = '/hosts/hosts-plw.txt'
    isIntraIp = False

if zk in ['joy-tbk']:
    hostFile = '/hosts-honor-jdbk.txt'
    forePort = True

if zk in ['gold', 'joy-gold', 'jvc-gold', 'dm-video']:
    hostFile = '/hosts/hosts-gold.txt'
    forePort = True

if zk in ['gwms']:
    hostFile = '/hosts/hosts-wms.txt'

if zk in ['tms']:
    hostFile = '/hosts-mjs-vpc.txt'

if zk in ['xiuxiu', 'xiuxiu-digshow']:
    hostFile = '/hosts/hosts-ykt.txt'
    
if zk in ['jddf', 'jddf-od']:
    hostFile = '/hosts/hosts-honor-i.txt'

if zk in ['zndz-jddf', 'zndz-jddf-od']:
    hostFile = '/hosts/hosts-honor-i.txt'

if zk in ['earth', 'earth.desktop']:
    hostFile = '/hosts/hosts-earth.txt'

filePath = os.path.dirname(sys.argv[0]) + hostFile

ipsMap = CliTool.getIpsMap(filePath, isIntraIp, 8875, 's', forePort = forePort)

if zk in ['earth'] and envs in ['real', 'pre']:
    if envs in 'real':
        real_envs += ',jst-realzk'
    if envs in 'pre':
        real_envs += ',jst-prezk'

envList = CliTool.getEnvList(real_envs, ipsMap, isIntraIp)

ips = {}
for env in envList:
    ips = dict(ips, **ipsMap[env])

mode = sys.argv[3]

argvNum = len(sys.argv)
if argvNum >= 5:
    timeout = sys.argv[4]
else:
    timeout = 120
timeout = str(timeout)

localPwdFile = '/home/porsche/publish/tjds2.passwd'

if isNodeVideoZk is True:
    zkFile = 'Zk.' + zk  + '.js'
elif zk in ['ai-ps', 'xiuxiu-node-api', 'node-video-v2']:
    zkFile = 'Zk.' + zk  + '.json'
else:
    zkFile = 'Zk.' + zk  + '.inc'

localSrc = '/alidata/s/' + zkFile

cmdTpl = 'rsync -%s --delay-updates --del --stats --port=%s --password-file=' + localPwdFile + ' --timeout=' + timeout + ' --exclude=.gitmodules --exclude=.git --exclude=.svn --exclude=.buildpath --exclude=.project --exclude=.settings --exclude=.DS_Store --exclude=.gitignore ' + localSrc + ' root@%s::%s/' + zkFile


Ct = CliTool()
Ct.publish(ips, mode, cmdTpl)
