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

app = sys.argv[1]
APP_PDD = 'pdd'
APP_PDD_OD = 'pdd-od'
APP_PDD_SP = 'pdd-sp'
APP_PDD_DZ = 'pdd-dz'
APP_PDD_RUBY = 'pdd-ruby'
APP_PDD_DS = 'pdd-ds'
isPdd = (app == APP_PDD)
isPddOd = (app == APP_PDD_OD)
isPddSp = (app == APP_PDD_SP)
isPddDz = (app == APP_PDD_DZ)
isPddRuby = (app == APP_PDD_RUBY)
isPddDs = (app == APP_PDD_DS)

if app not in [APP_PDD, APP_PDD_OD, APP_PDD_SP, APP_PDD_DZ, APP_PDD_RUBY, APP_PDD_DS]:
    print('%s 错误，请联系运维', app)
    exit()

isIntra = (sys.argv[2] == 'intra')
mode = sys.argv[4]
pubEnv = sys.argv[3]

filePath = os.path.dirname(sys.argv[0]) + '/hosts-pdd.txt'
ipsMap = CliTool.getIpsMap(filePath, isIntra, 8873)

envList = CliTool.getEnvList(sys.argv[3], ipsMap, isIntra)

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

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

localPwdFile = '/Users/tangjianhui/Documents/publish/tjds1.passwd'

localAppsPath  = CliTool.workspace()

appDir = {
    APP_PDD_OD: 'pdd-order.git'
}.get(app, 'pdd.git')

localSrc = localAppsPath + appDir + '/'

cmdTpl = 'rsync -%s --delay-updates --del --stats --port=%s --password-file=' + localPwdFile + ' --timeout=' + timeout + CliTool.excludeFile()  + localSrc + ' www@%s::%s/'

sshCmdTpl = 'rsync -%s --delay-updates --del --stats [%s] -e ssh' + CliTool.excludeFile() + localSrc + ' www@%s:/alidata/%s/'

Ct = CliTool()

if isPdd:
    Ct.publish(ips, mode, cmdTpl, isIntra, sshCmdTpl)


if isIntra is False and mode == 'real':
    distDir = ''
    zipBaseFile = datetime.datetime.now().strftime("%Y%m%d-%H%M")
    dcZipBaseDir = '/Users/tangjianhui/Documents/dczip_v2/'

    if os.path.isdir(dcZipBaseDir) is False:
        os.makedirs(dcZipBaseDir)
            
    if isPddSp is True:
        subZipDir = dcZipBaseDir + 'pdd-sp/'
        tmpZipDir = dcZipBaseDir + 'tmp/pdd-sp/'

        feLocalSrc =  localAppsPath + 'pdd-mobile-web.git/'

        cmd = 'cd %s  && yarn && yarn build' % (feLocalSrc)
        print("start process run build command: " + cmd + "\n")
        outputStr = os.system(cmd)
        print(outputStr)

        distDir = feLocalSrc + 'dist'
        if os.path.isfile(distDir + '/index.html') is False:
            print(distDir + ' 编译文件不存在!')
            exit()

        zipFile = subZipDir + ('pdd-sp_%s.zip' % zipBaseFile)

        cmd = "mkdir -p %s && mkdir -p %s && cd %s && rsync -cvzrDp --del --exclude=.git.* --exclude=/.git/* --exclude=/composer.* %s %spdd" % (subZipDir, tmpZipDir, tmpZipDir, localSrc, tmpZipDir)
        print("start process command: " + cmd + "\n")
        outputStr = CliTool.execCommand(cmd) 
        print(print(outputStr))

        htaccessName = 'htaccess_pddsp'
        htaccess = os.path.isfile(tmpZipDir + 'pdd/' + htaccessName)
    
        if htaccess is False:
            print("%s 未找到,拼多多商品打包失败" % (htaccessName))
            exit()

        cmd = "cd %s && mkdir -p pdd-mobile-web && rm -rf pdd-mobile-web/dist &&  cp -r %s ./pdd-mobile-web/ && rm -rf .htaccess htaccess_pddsp && cp pdd/htaccess_pddsp ./ && mv htaccess_pddsp .htaccess" % (tmpZipDir, distDir)
        cmd += " && zip -r %s ./ -x \'*.git*\' \'*.zip\' " % (zipFile)
        cmd += " && rm -rf %s%s" % (subZipDir, zipFile)
        cmd += " && mv %s %s" % (zipFile, subZipDir)
        
        print("start process command: " + cmd + "\n")

        outputStr = CliTool.execCommand(cmd) 
        print(print(outputStr))

    if isPdd is True or isPddOd is True:
        subZipDir = dcZipBaseDir + ('pdd/' if isPdd is True else 'pdd-od/')
        zipFile = '%s-%s.zip' % (app, zipBaseFile)
        zipFilePath = subZipDir + zipFile

        cmd = 'mkdir -p %s && cd %s && zip -r %s  ./ -x \'*.git*\' \'*.zip\' && rm -rf %s && mv %s %s' % (subZipDir, localSrc, zipFile, zipFilePath, zipFile, subZipDir)
        print("start process zip command: " + cmd + "\n")
        outputStr = CliTool.execCommand(cmd)    
        print(outputStr)

    if isPddDz is True or isPddRuby is True or isPddDs is True:
        subZipDir = dcZipBaseDir + app + '/'
        tmpZipDir = dcZipBaseDir + 'tmp/pdd-ab/'

        pddGitDir = localAppsPath + 'pdd.git/'
        pddOdGitDir = localAppsPath + 'pdd-order.git/'
        
        cmd = "mkdir -p %s && mkdir -p %s && cd %s" % (subZipDir, tmpZipDir, tmpZipDir)
        cmd += ' && rsync -cvzrDp --del --exclude=.git.* --exclude=/.git/* --exclude=/composer.* %s %spdd' % (pddGitDir, tmpZipDir)
        cmd += ' && rsync -cvzrDp --del --exclude=.git.* --exclude=/.git/* --exclude=/composer.* %s %spdd-order' % (pddOdGitDir, tmpZipDir)

        print("start process command: " + cmd + "\n")
        outputStr = CliTool.execCommand(cmd) 
        print(print(outputStr))

        htaccessName = {
            APP_PDD_DZ: 'htaccess_pdddz',
            APP_PDD_DS: 'htaccess_pddod',
            APP_PDD_RUBY: 'htaccess_ruby',
        }.get(app)

        htaccessPath = tmpZipDir + 'pdd/' + htaccessName
        print('htaccess path is %s' % htaccessPath)
        htaccess = os.path.isfile(htaccessPath)
        if htaccess is False:
            print("%s 未找到,拼多多商品打包失败" % (htaccessName))
            exit()

        cmd = ('cd ' + tmpZipDir + ' && rm -rf htaccess_* .htaccess && cp pdd/' + htaccessName + ' ./ && mv ' + htaccessName + ' .htaccess')
        print("start process zip command: " + cmd + "\n")
        outputStr = CliTool.execCommand(cmd)
        print(outputStr)  

        zipFile = subZipDir + ('%s-%s.zip' % (app, zipBaseFile))

        cmd = 'cd ' + tmpZipDir + ' && zip -r ' + zipFile + ' ./ -x \'*.git*\' \'*.zip\'  && rm -rf ' + subZipDir + zipFile + ' && mv ' + zipFile + ' ' + subZipDir
        print("start process zip command: " + cmd + "\n")
        outputStr = CliTool.execCommand(cmd) 
        print(print(outputStr))


    cmd = ('find %s -type f -name "*.zip" -mtime +3 -type f -delete && tree -L 2 %s' % (dcZipBaseDir, dcZipBaseDir))
    outputStr = CliTool.execCommand(cmd)
    print('process cmd %s', cmd)
    print(outputStr)
             
    