#!/bin/bash
## 权限问题，用  chmod u+x *.sh
## eg:
## cd /mobileproject/$1/XcxProject hivui create $1


# 用户名  
# 项目名 
# 操作 （create , restart , start , kill ,  stop , del , build , sync）
# 部署路径 (upload)
# 同步内容（node_modules , build , cli  ）



# 引用环境变量----cd 不要用绝对路径，后面要改
cd /eap5.0/hiruntime/project/desktop
source ./var_env.sh
# echo ${DEV_BASE_API}

if [ $# -gt 4 ] ; then
    echo "$3 操作命令有误，请重新输入！"
    exit 1
fi


# basePath='/opt/eap5/desktop'
basePath='/eap5.0/desktop'
tagPath=$basePath'/preview'
# tmpPath=$basePath'/project-template'
tmpPath='/eap5.0/hiruntime/project/desktop/project-template'
gitTmp='/eap5.0/hiruntime/project/desktop/__gittmp'
workName=$1
projectName=$2
oper=$3
uploadPath=$4
syncWay=$4
  
source[0]='\t<% if(htmlWebpackPlugin.vite){%>\n\t<!--预览时 公共库-->'
source[1]='\n\t<script src="\/'$projectName'\/setting\/desktop\/development.js" type="text\/javascript"><\/script>'
source[2]='\n\t<%}%>'
source[3]='\n\t<% if(!htmlWebpackPlugin.vite){%>\n\t<!--发布时 公共库-->'
source[4]='\n\t<script src="\/${serverName}\/render\/'${workName}'\/'$projectName'\/setting\/desktop\/appsetting.js" type="text\/javascript"><\/script>'
source[5]='\n\t<%}%>\n'
## aaa=$(pm2 id "vite-${workName}-${projectName}")
## [] 说明服务有启动但是，没有执行
## if [ ${#aaa} = 2 ] ; then
##	echo "==================="
## fi

## 参数  工号$1  项目名$2  端口$3
## 工号=工作空间
## 1、判断工作空间 没有就创建
# if [ ! -d "${tagPath}/${workName}" ];then
#     echo "${oper} 命令： 工作空间不存在！"
#     exit 1
# fi

## 2、判断项目名
if [ ! -d "${tagPath}/${workName}/${projectName}" ];then
    echo "${oper} 命令： 项目名不存在！"
    # exit 1
fi

## 返回预览目录
# cd ${tagPath}/${workName}/${projectName}/deploy


if [ $3 = "restart" ] ; then
    ### 用pm2管理，用pm2 重启指定的
    pm2 restart "vite-${workName}-${projectName}" 
    #yarn run pm2:restart vite-${workName}-${projectName}
fi

if [ $3 = "start" ] ; then    
    pm2 delete "vite-${workName}-${projectName}"
    ### 用pm2管理，用pm2 重启指定的
    cd ${tagPath}/${workName}/${projectName}/deploy
    yarn run pm2:start
fi

if [ $oper = "kill" ] ; then
    ### 用pm2管理，用pm2 重启指定的
    pm2 delete "vite-${workName}-${projectName}" 
    #yarn run pm2:kill vite-${workName}-${projectName}
fi

if [ $oper = "stop" ] ; then
    ### 用pm2管理，用pm2 重启指定的
    pm2 stop "vite-${workName}-${projectName}" 
    #yarn run pm2:stop vite-${workName}-${projectName}
fi

if [ $oper = "del" ] ; then

    if [ $workName = "admin" -a $projectName = "systpl" ] ; then
        echo "系统模板，不可删除！"
        exit 1
    fi

    curPid=`pm2 pid vite-${workName}-${projectName}`
    if [ ! -z "$curPid" ];then
        echo "vite-${workName}-${projectName} 服务正在关闭！"
        pm2 delete "vite-${workName}-${projectName}" 
    fi

    ### 删除用户项目
    cd "${tagPath}/${workName}"
    rm -r -f "${projectName}"
    echo "vite-${workName}-${projectName} 删除成功！"

fi

if [ $oper = "build" ] ; then
    ### 用pm2管理，用pm2 重启指定的
    cd "${tagPath}/${workName}/${projectName}/deploy"
    npm run build
    #yarn run pm2:stop vite-${workName}-${projectName}    
    cd "${tagPath}/${workName}/${projectName}/deploy/"
    if [[ ! -d "${tagPath}/${workName}/${projectName}/deploy/dist/yslz/funclib/desklib" ]]; then
        mkdir -p "${tagPath}/${workName}/${projectName}/deploy/dist/yslz/funclib/desklib"
    fi
    cp -rf "${tagPath}/${workName}/${projectName}/deploy/yslz/funclib/desklib" "${tagPath}/${workName}/${projectName}/deploy/dist/yslz/funclib/"
  
    ## 删除之前打包文件 
    rm -rf "${workName}_${projectName}_dist.zip"
    cd "${tagPath}/${workName}/${projectName}/deploy/dist" 
    mkdir -p "${tagPath}/${workName}/${projectName}/deploy/dist/${workName}/${projectName}"
    cp -rf "${tagPath}/${workName}/${projectName}/deploy/dist/${projectName}" "${tagPath}/${workName}/${projectName}/deploy/dist/${workName}/"
    rm -rf "${tagPath}/${workName}/${projectName}/deploy/dist/${projectName}"
    zip -q -r "${workName}_${projectName}_dist.zip"  ./*
    cp -rf "${tagPath}/${workName}/${projectName}/deploy/dist/${workName}_${projectName}_dist.zip" ../
    cd ../

    if [[ ! -d "$uploadPath" ]]; then
        echo "${oper} 命令： 上传文件夹不存在, 正在帮您创建目录..."
        mkdir -p "${uploadPath}"
    fi
    echo "正在拷到服务器目录：${uploadPath}"
    # cp -f "${workName}_${projectName}_dist.zip"  "${uploadPath}"
    cp -fv "${workName}_${projectName}_dist.zip"  "${uploadPath}"

    echo "(‐^▽^‐) 编译打包成功！"
fi


if [ $oper = "sync" ] ; then

    git clone http://192.168.4.105:8080/gitbucket/git/08335/hivui-platform-template.git "${gitTmp}"
    echo ${gitTmp} 
    echo ${tmpPath}
    rsync -auz "${gitTmp}/" ${tmpPath}
    rm -rf ${gitTmp}

    if [ $syncWay = "build" ] ; then
        echo "正在同步build..."
        rsync -auz ${tmpPath}/build ${tagPath}/${workName}/${projectName}/deploy/
        rsync -auz ${tmpPath}/babel.config.js ${tagPath}/${workName}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.postcssrc.js ${tagPath}/${workName}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.browserslistrc ${tagPath}/${workName}/${projectName}/deploy/
        rsync -auz ${tmpPath}/vite-plugin ${tagPath}/${workName}/${projectName}/deploy/
        echo "build已同步！"
        exit 
    fi
    
    if [ $syncWay = "assets" ] ; then
        echo "正在同步assets_platform资源..."
        rsync -auz ${tmpPath}/assets_platform ${tagPath}/${workName}/${projectName}/deploy/
        echo "assets_platform资源已同步！"
        exit 
    fi

    if [ $syncWay = "login" ] ; then
        echo "正在同步login模块..."
        rsync -auz ${tmpPath}/project/hivuiLogin ${tagPath}/${workName}/${projectName}/deploy/${projectName}/
        cd ${tagPath}/${workName}/${projectName}/deploy/${projectName}/hivuiLogin
        sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html
        echo "login已同步！"
        exit 
    fi
    
    if [ $syncWay = "main" ] ; then
        echo "正在同步main模块..."
        rsync -auz ${tmpPath}/project/hivuiMain ${tagPath}/${workName}/${projectName}/deploy/${projectName}/
        cd ${tagPath}/${workName}/${projectName}/deploy/${projectName}/hivuiMain
        sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html
        echo "main已同步！"
        exit 
    fi
    
    if [ $syncWay = "mainui" ] ; then

        echo "正在同步login模块..."
        rsync -auz ${tmpPath}/project/hivuiLogin ${tagPath}/${workName}/${projectName}/deploy/${projectName}/
        cd ${tagPath}/${workName}/${projectName}/deploy/${projectName}/hivuiLogin
        sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html
        echo "login已同步！"

        echo "正在同步main模块..."
        rsync -auz ${tmpPath}/project/hivuiMain ${tagPath}/${workName}/${projectName}/deploy/${projectName}/
        cd ${tagPath}/${workName}/${projectName}/deploy/${projectName}/hivuiMain
        sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html
        echo "main已同步！"

        exit 

    fi

    if [ $syncWay = "hiui" ] ; then
        echo "正在同步hi-ui库..."
        cd "${tmpPath}/"
        npm i hi-ui -S
        rm -rf ${tagPath}/${workName}/${projectName}/deploy/node_modules/hi-ui
        rsync -auz ${tmpPath}/node_modules/hi-ui ${tagPath}/${workName}/${projectName}/deploy/node_modules/
        ## 取hi-ui版本 并更新root package.json
        ## cd ${tagPath}/${workName}/${projectName}/deploy/node_modules/hi-ui/
        ## sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json
        
        echo "hiui已同步！"
    fi
    
    if [ $syncWay = "taskcenter" ] ; then
        echo "正在同步hi-taskcenter库..."
        cd "${tmpPath}/"
        npm i hi-taskcenter -S
        rm -rf ${tagPath}/${workName}/${projectName}/deploy/node_modules/hi-taskcenter
        rsync -auz ${tmpPath}/node_modules/hi-taskcenter ${tagPath}/${workName}/${projectName}/deploy/node_modules/
        ## 取hi-taskcenter版本 并更新root package.json
        ## cd ${tagPath}/${workName}/${projectName}/deploy/node_modules/hi-taskcenter/
        ## sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json

        echo "hi-taskcenter已同步！"
    fi
    
    if [ $syncWay = "mod" ] ; then
        echo "正在同步node_modules环境..."
        cd "${tmpPath}/"        
        npm i 
        rsync -auz ${tmpPath}/node_modules ${tagPath}/${workName}/${projectName}/deploy/
        echo "node_modules已同步！"
    fi
    
    if [ $syncWay = "config" ] ; then
        echo "正在同步配置环境, .evn文件、vite.config.js、babel.config.js、.postcssrc.js ..."
        cd "${tmpPath}/"      
        npm i 
        rsync -auz ${tmpPath}/.env.development ${tagPath}/${workName}/${projectName}/deploy/.env.development
        rsync -auz ${tmpPath}/.env.production ${tagPath}/${workName}/${projectName}/deploy/.env.production
        rsync -auz ${tmpPath}/.env.staging ${tagPath}/${workName}/${projectName}/deploy/.env.staging

        rsync -auz ${tmpPath}/vite.config.js ${tagPath}/${workName}/${projectName}/deploy/vite.config.js
        rsync -auz ${tmpPath}/babel.config.js ${tagPath}/${workName}/${projectName}/deploy/babel.config.js
        rsync -auz ${tmpPath}/.postcssrc.js ${tagPath}/${workName}/${projectName}/deploy/.postcssrc.js


        sed -i 's#\(VITE_APP_USERPATH \?= \?\).*#\1'"/e5/render/${workName}"'#g' .env.production
        sed -i 's#\(VITE_APP_BASE_API \?= \?\).*#\1'"${DEV_BASE_API}"'#g' ${tagPath}/${workName}/${projectName}/deploy/.env.development
        sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${workName}"'#g' ${tagPath}/${workName}/${projectName}/deploy/.env.development
        sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${workName}"'#g' ${tagPath}/${workName}/${projectName}/deploy/.env.staging

        echo "配置环境已同步！"
    fi

    ### 脚手架 [登录模板、主页模板]
    if [ $syncWay = "cli" ] ; then

        echo "=====正在同步assets_platform资源...====="
        rsync -auz ${tmpPath}/assets_platform ${tagPath}/${workName}/${projectName}/deploy/
        echo "=====assets_platform资源已同步！====="

        echo "=====正在同步funclib资源...====="
        rsync -auz ${tmpPath}/funclib ${tagPath}/${workName}/${projectName}/deploy/
        echo "=====funclib资源已同步！====="

        echo "=====正在同步login模块...====="
        rsync -auz ${tmpPath}/project/hivuiLogin ${tagPath}/${workName}/${projectName}/deploy/${projectName}/
        cd ${tagPath}/${workName}/${projectName}/deploy/${projectName}/hivuiLogin
        sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html
        echo "=====login已同步！====="

        echo "=====正在同步main模块...====="
        rsync -auz ${tmpPath}/project/hivuiMain ${tagPath}/${workName}/${projectName}/deploy/${projectName}/
        cd ${tagPath}/${workName}/${projectName}/deploy/${projectName}/hivuiMain
        sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html
        echo "=====main已同步！====="

        echo "=====正在同步node_modules环境...====="
        cd "${tmpPath}/"        
        npm i 
        rsync -auz ${tmpPath}/node_modules ${tagPath}/${workName}/${projectName}/deploy/
        echo "=====node_modules已同步！====="

        echo "=====正在同步hi-ui库...====="
        cd "${tmpPath}/"
        npm i hi-ui -S
        rm -rf ${tagPath}/${workName}/${projectName}/deploy/node_modules/hi-ui
        rsync -auz ${tmpPath}/node_modules/hi-ui ${tagPath}/${workName}/${projectName}/deploy/node_modules/
        echo "=====正在同步hi-ui库已同步！====="

        echo "=====正在同步程序环境(build、babel.config.js、.postcssrc.js、.browserslistrc、vite-plugin、.env.development、.env.production、.env.staging、vite.config.js)...====="
        rsync -auz ${tmpPath}/build ${tagPath}/${workName}/${projectName}/deploy/
        rsync -auz ${tmpPath}/babel.config.js ${tagPath}/${workName}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.postcssrc.js ${tagPath}/${workName}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.browserslistrc ${tagPath}/${workName}/${projectName}/deploy/
        rsync -auz ${tmpPath}/vite-plugin ${tagPath}/${workName}/${projectName}/deploy/

        rsync -auz ${tmpPath}/.env.development ${tagPath}/${workName}/${projectName}/deploy/.env.development
        rsync -auz ${tmpPath}/.env.production ${tagPath}/${workName}/${projectName}/deploy/.env.production
        rsync -auz ${tmpPath}/.env.staging ${tagPath}/${workName}/${projectName}/deploy/.env.staging

        rsync -auz ${tmpPath}/vite.config.js ${tagPath}/${workName}/${projectName}/deploy/vite.config.js
       
        ### 设置环境变量
        sed -i 's#\(VITE_APP_USERPATH \?= \?\).*#\1'"/e5/render/${workName}"'#g' .env.production    
        sed -i 's#\(VITE_APP_BASE_API \?= \?\).*#\1'"${DEV_BASE_API}"'#g' ${tagPath}/${workName}/${projectName}/deploy/.env.development
        sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${workName}"'#g' ${tagPath}/${workName}/${projectName}/deploy/.env.development
        sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${workName}"'#g' ${tagPath}/${workName}/${projectName}/deploy/.env.staging

        echo "=====程序环境已同步！====="

        exit
    fi
    # 读取版本号
    # v_hiui = $(node -e "(function () { console.log(require('./package.json').dependencies['hi-ui']) })()")
    # v_task = $(node -e "(function () { console.log(require('./package.json').dependencies['hi-taskcenter']) })()")

    rsync -auz ${tmpPath}/package.json ${tagPath}/${workName}/${projectName}/deploy/package.json
    cd ${tagPath}/${workName}/${projectName}/deploy/
    ## 替换 package.json 的 name 值
    sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json
    sed -i 's#\("author": \?"\).*#\1'"${workName}"'",#g' package.json
    pm2 delete "vite-${workName}-${projectName}"
    npm run rm:vite
    npm run pm2:start
    pm2 save

fi

