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

isIntra = (sys.argv[2] == 'intra')
app = sys.argv[1]
env = sys.argv[3]
mode = sys.argv[4]
mode2 = mode
isMcPre = (env == 'mc-pre')
isPre = (env == 'pre')
is_web_app = app in ['honor-1688-v2', 'honor-1688-order-ry', 'honor-1688-dm', 'honor-1688-cg', 'honor-1688-dm-cg']

if isMcPre is True or isPre is True:
    module = 'www/' + app + '-pre'
else:
    module = 'www/' + app

if app in ['honor-1688-dm', 'honor-1688-dm-cg', 'honor-1688-dm-ds']:
    hostFile = '/hosts/hosts-jst-tbdz.txt' 
elif app in ['honor-1688-ds-tbk-api']:
    hostFile = '/hosts/hosts-jst-tbk.txt' 
elif app in ['honor-1688-order-ry', 'honor-1688-order-ry-v2']:
    hostFile = '/hosts/hosts-jst-ry.txt' 
else:
    hostFile = '/hosts/hosts-jst-acn.txt' 

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

ipsMap = CliTool.getIpsMap(filePath, isIntra, 8873, module)

envList = CliTool.getEnvList(env, ipsMap, isIntra)

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

for key, value in ips.items():
    if '-sw' in key or '-w' in key:
        ips_sw[key] = value

if isIntra is True:
    localPwdFile = '/home/porsche/publish/tjds1.passwd'
    localSrc = '/alidata/www/' + app + '/'
else:
    localPwdFile = '/Users/tangjianhui/Documents/publish/tjds1.passwd'
    repo = {
        'honor-1688-dm-ds': 'honor-1688-ds-api',
        'honor-1688-ds-tbk-api': 'honor-1688-ds-api',
        'honor-1688-order-ry': 'honor-1688-v2',
    }.get(app, app)
    localSrc = CliTool.workspace() + repo + '.git/'


exclude_ext = ''
cmd_pub_tpl = 'rsync -%s --delay-updates {} --stats --port=%s --password-file={} --timeout=120 {} {} {} www@%s::%s/'

def build_exclude_ext(mode):
    if mode == 'api':
        return ' --include=/api/ --exclude=/web/* '
    
    if mode == 'web':
        return ' --exclude=/api/* --include=/web/dist/ --include=/web/distv1/ '

    return ''

def pubish_sw(code_type, mode_type):
    if code_type == 'api':
        return 
    
    if not ips_sw:
        print("sw 机器为空")
        exit()
        
    exclude_ext_web =  build_exclude_ext('web')
    Ct = CliTool()

    cmd_tpl_web_no_del =  cmd_pub_tpl.format('', localPwdFile,  CliTool.excludeFile(), exclude_ext_web, localSrc)
    print("\n\n正在发布[SW]机器 and no --del ...")
    Ct.publish(ips_sw, mode_type, cmd_tpl_web_no_del)
    
    if code_type == 'web':
        cmd_tpl_web =  cmd_pub_tpl.format('--del', localPwdFile,  CliTool.excludeFile(), exclude_ext_web, localSrc)
        print("\n\n正在发布[SW]机器")
        Ct.publish(ips_sw, mode_type, cmd_tpl_web)


if len(sys.argv) > 5 and mode in ['api', 'web']:
    exclude_ext = build_exclude_ext(mode)
    mode = sys.argv[5]


##real或者api发布
if not ips:
    print("sw 机器为空")
    exit()

if is_web_app is True:
    ##sw机器先发布
    pubish_sw(mode2, mode)

    ##web仅发布sw机器
    if mode2 == 'web':
        exit()

cmd_tpl_all = cmd_pub_tpl.format('--del', localPwdFile,  CliTool.excludeFile(), exclude_ext, localSrc)
Ct = CliTool()
syntax_check = False if isIntra is True else True
Ct.publish(ips, mode, cmd_tpl_all, php_syntax_check = syntax_check)