#发布版本脚本
option=$1
type=$2
date=$3
echo "Option: [agent(代理后台), game(游戏web端), h5(游戏h5端), home(主页), web(所有web), exe(安装包), all(所有), upsp(pc热更资源), upcp(pc热更配置), upsm(mobile热更资源), upcm(mobile热更配置)]"
echo "Type: [o1(线上1.1), o2(线上1.2), t1(测试1.1), t2(测试1.2), o1u(线上网2union), o2u(线上网3union), t1u(测试网2union), t2u(测试网3union)]"

scp=scp
ssh=ssh
root=root

homeIp='127.0.0.1'
homeIp2=''
homeIp3=''
homePort=22
homePort2=22
homePort3=22

screenIp='127.0.0.1'
screenPort=22

innerIp='127.0.0.1'

work_path=$(pwd)

if [ "$date" == "" ]; then
    date=`date +%Y-%m-%d`
fi

branch=''
srcDir=""
targetDir=""
downloadDir=""
hotupateDir=""

error(){
    echo -e "\033[31m $1 \033[0m"
}

innerType=${type:0:2}
if [ "$type" == "w2" ] || [ "$type" == "w3" ]; then
    echo "阿里 正式版本..."$type"..."$innerType
    branch="master"
    srcDir="master/"$innerType"/"$date    
    targetDir="union/"$innerType
    downloadDir=$innerType
    hotupateDir="master/"$innerType

    homeIp='216.83.55.196'
    homeIp2='134.122.132.93'
    homeIp3='143.92.39.5'

    homePort=59615
    homePort2=52961
    homePort3=57193

    screenIp='216.83.56.23'
    screenPort=57168
elif [ "$type" == "w2s" ] || [ "$type" == "w3s" ]; then
    echo "亚马逊 正式版本..."$type"..."$innerType
    branch="master"
    srcDir="master/"$innerType"/"$date    
    targetDir="union/"$innerType
    downloadDir=$innerType
    hotupateDir="master/"$innerType

    scp='scp -i /home/amazon/99Video-TY.pem'
    ssh='ssh -i /home/amazon/99Video-TY.pem'
    root=centos

    homeIp='54.95.243.213'
    homeIp2='3.37.18.24'
    homeIp3='54.255.124.239'

    screenIp='52.192.169.221'

elif [ "$type" == "w2h" ] || [ "$type" == "w3h" ]; then
    echo "hotfix测试..."$type"..."$innerType
    branch="hotfix"
    srcDir="hotfix/"$innerType"/"$date
    targetDir="hotfix/"$innerType
    downloadDir="hotfix/"$innerType
    hotupateDir="hotfix/"$innerType

    scp='scp -i /home/amazon/99Video-TY.pem'
    ssh='ssh -i /home/amazon/99Video-TY.pem'
    root=centos

elif [ "$type" == "w2d" ] || [ "$type" == "w3d" ]; then
    echo "develop测试..."$type"..."$innerType
    branch="develop"
    srcDir="develop/"$innerType"/"$date
    targetDir="develop/"$innerType
    downloadDir="develop/"$innerType
    hotupateDir="develop/"$innerType

    scp='scp -i /home/amazon/99Video-TY.pem'
    ssh='ssh -i /home/amazon/99Video-TY.pem'
    root=centos
elif [ "$type" == "w2f" ] || [ "$type" == "w3f" ]; then
    echo "feature测试..."$type"..."$innerType
    branch="feature"
    srcDir="feature/"$innerType"/"$date
    targetDir="feature/"$innerType   
    downloadDir="feature/"$innerType    
    hotupateDir="feature/"$innerType  

    scp='scp -i /home/amazon/99Video-TY.pem'
    ssh='ssh -i /home/amazon/99Video-TY.pem'
    root=centos
else

    error "\033[31m Type 类型错误... \033[0m"
    exit
fi

if [ ! -f $src]; then
    error "错误: 当前日期没有对应的文件夹"
    exit
fi
# 全局加权限
chmod 755 $srcDir/*.exe

publish(){
    targetFile=$1
    targetPath=$2
    targetCmd=$3
    targetServer=$4

    exitsFiles=''
    for i in $targetFile; do
        if [ -f $i ];then
            exitsFiles=$exitsFiles' '$i
        else
            error $i'不存在'
        fi
    done
    targetFile=$exitsFiles

    if [ "$targetServer" == "" ]; then
        targetServer="home"
    fi

    eval "targetIp=$"$targetServer"Ip"
    eval "targetIp2=$"$targetServer"Ip2"
    eval "targetIp3=$"$targetServer"Ip3"

    eval "targetPort=$"$targetServer"Port"
    eval "targetPort2=$"$targetServer"Port2"
    eval "targetPort3=$"$targetServer"Port3"

    if [ -n "$targetIp" ]; then
        echo "Start Publish ... $targetFile $targetIp $targetServer"
        $scp -P $targetPort -r $targetFile shell/archive.sh shell/unzip.sh $root@$targetIp:$targetPath
        if [ -n "$targetCmd" ]; then
            $ssh -p $targetPort $root@$targetIp $targetCmd
        fi
    fi
    if [ -n "$targetIp2" ]; then
        echo "Start Publish ... $targetFile $targetIp2 $targetServer"
        $scp -P $targetPort2 -r $targetFile shell/archive.sh shell/unzip.sh $root@$targetIp2:$targetPath
        if [ -n "$targetCmd" ]; then
            $ssh -p $targetPort2 $root@$targetIp2 $targetCmd
        fi
    fi
    if [ -n "$targetIp3" ]; then
        echo "Start Publish ... $targetFile $targetIp3 $targetServer"
        $scp -P $targetPort3 -r $targetFile shell/archive.sh shell/unzip.sh $root@$targetIp3:$targetPath
        if [ -n "$targetCmd" ]; then
            $ssh -p $targetPort3 $root@$targetIp3 $targetCmd
        fi
    fi

    echo "Finish Publish ... "$targetFile
}

publishWeb(){
    fileName=$1
    fileExt=$2
    server=$3

    cp -f $srcDir/$fileName*.$fileExt $srcDir/$fileName.$fileExt

    targetFile=$srcDir/$fileName.$fileExt
    targetPath=/etc/nginx/conf.d/$targetDir
    targetCmd="$targetPath/unzip.sh $fileName"

    publish $targetFile $targetPath "$targetCmd" $server

    rm -rf $targetFile
}

publishUpdate(){
    fileName=$1
    platform=$2
    updateCmd=$3
    server=$4

    targetCmd=""
    targetPath=/data/hotupdate/$hotupateDir/$platform
    if [ -n "$updateCmd" ]; then
        targetCmd="$targetPath/unzip.sh $platform"
    fi
    publish "$fileName" $targetPath "$targetCmd" $server
}

copyFile(){
    srcName=$1
    targetName=$2
    fileExt=$3

    cp -f $srcDir/$srcName*.$fileExt $srcDir/$targetName.$fileExt
}

# 拷贝 并 修改部分名称 保留扩展字符串
copyLimitFiles(){
    srcName=$1
    replaceName=$2    
    fileExt=$3
    deleteName=$4

    cd $srcDir

    for fileName in `ls $srcName*$fileExt`
    do
        tempName='temp_'
        copyCmd=`cp -f $fileName $tempName$fileName`
        ${copyCmd}        
    done

    if [ -n "$deleteName" ]; then
        renameCmd=`prename s/$deleteName/$fileExt/ $tempName$srcName*$fileExt`
        ${renameCmd}
    fi

    renameCmd=`prename s/$tempName$srcName/$replaceName/ $tempName$srcName*$fileExt`
    ${renameCmd}

    cd $work_path
}

renameFiles(){
    srcName=$1
    replaceName=$2    
    fileExt=$3

    renameCmd=`prename s/$srcName/$replaceName/ $srcDir/*$fileExt`
    ${renameCmd}
}

sendExeFiles(){
    targetFile=$1
    fileExt=$2

    files=$srcDir/$targetFile*$fileExt

    targetPath=/data/update/download/$downloadDir
    targetCmd="$targetPath/archive.sh copy exe"

    publish "$files" $targetPath "$targetCmd"

    rm -rf $files
}

sendWebFiles(){
    targetFile=$1
    fileExt=$2

    files=$srcDir/$targetFile*$fileExt

    targetPath=/etc/nginx/conf.d/$targetDir
    targetCmd="$targetPath/unzip.sh $targetFile"

    publish "$files" $targetPath "$targetCmd"

    rm -rf $files
}

if [ "$option" == "agent" ]; then
    publishWeb "agent_web" "tar"
elif [ "$option" == "agentM" ]; then
    publishWeb "agent_h5" "tar"
elif [ "$option" == "phone" ]; then
    publishWeb "phone_web" "tar"
elif [ "$option" == "egret" ]; then
    # 备份
    renameFiles "game_egret_web" "backup_game_egret_web" ".tar"
    # 复制并发布
    copyLimitFiles "backup_game_egret_web" "game_egret_web" ".tar" "-$branch.*"
    sendWebFiles "game_egret_web" ".tar"
    # 还原
    renameFiles "backup_game_egret_web" "game_egret_web" ".tar"
elif [ "$option" == "egretM" ]; then
    # 备份
    renameFiles "game_egret_h5" "backup_game_egret_h5" ".tar"
    # 复制并发布
    copyLimitFiles "backup_game_egret_h5" "game_egret_h5" ".tar" "-.*"
    sendWebFiles "game_egret_h5" ".tar"
    # 还原
    renameFiles "backup_game_egret_h5" "game_egret_h5" ".tar"
elif [ "$option" == "task" ]; then
    publishWeb "task_web" "tar"
elif [ "$option" == "screen" ]; then
    publishWeb "screen" "tar" "screen" 
elif [ "$option" == "payment" ]; then
    publishWeb "payment" "tar"    
elif [ "$option" == "pexe" ]; then
    copyFile "99phone-" "99Phone" "exe"
    sendExeFiles "$srcDir/99Phone.exe"
elif [ "$option" == "eexe" ]; then
    copyFile "99game_egret_zh" "99Egret" "exe"
    copyLimitFiles "99game_egret" "99Egret" ".exe" "-$branch.*"
    sendExeFiles "99Egret" ".exe"
elif [ "$option" == "wr" ]; then
    publishUpdate "$srcDir/phone_v*.tar $srcDir/version.json" "pc"
elif [ "$option" == "er" ]; then
    copyLimitFiles "game_egret_zh_v" "game_egret_v" ".tar"
    publishUpdate "$srcDir/game_egret*v*.tar $srcDir/egret_PcVersion.json" "pc"
elif [ "$option" == "emr" ]; then
    copyLimitFiles "game_egret_h5_zh" "game_egret_h5" ".zip"
    publishUpdate "$srcDir/game_egret_h5*.zip $srcDir/egret_mobile_version.json" "mobile"
else
    echo "参数错误"
fi