#!/bin/bash
## 权限问题，用  chmod u+x *.sh
## eg: oper.sh 

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

cmdDir=$(
  cd $(dirname "$0")
  pwd
)
username=$1     #用户名
projectName=$2  #项目名
cmd=$3          #命令

#BASH_SOURCE[0]用户所在的目录到脚本的路径
## 获取eap5.0 路径
# eap5Dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
eap5Dir="${cmdDir%/hiruntime*}"
projectPath=${eap5Dir}/desktop/preview
tmpPath=${cmdDir}/project-template

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\/'${username}'\/'${projectName}'\/setting\/desktop\/appsetting.js" type="text\/javascript"><\/script>'
source[5]='\n\t<%}%>\n'

case $cmd in 
"create") #创建
    #参数个数判断，至少4个
    if [ $# -lt 4 ];then
        echo "创建项目命令参数不对：$*"
        echo "创建项目命令正确参数：用户名 项目名 create 桌面预览端口"
        exit 1
    fi
    source ${cmdDir}/var_env.sh
    # echo ${DEV_BASE_API} 
    echo eap5Dir: ${eap5Dir}
    echo projectPath: ${projectPath}
    port=$4 #端口
    
    if [ ! -d "${projectPath}/${username}" ];then
        mkdir "${projectPath}/${username}" #创用户工作空间
    fi
    if [ -d "${projectPath}/${username}/${projectName}" ];then
        echo "项目名已经存在！"
        exit 1
    fi
    echo "<1>git正在拉取web端项目基础文件"
    git clone http://192.168.4.105:8080/gitbucket/git/08335/hivui-platform-template.git "${projectPath}/${username}/${projectName}/deploy"

    echo "<2>初始化node环境..."
    cp -rf ${tmpPath}/node_modules ${projectPath}/${username}/${projectName}/deploy/node_modules
    cd ${projectPath}/${username}/${projectName}/deploy/
    rm -rf ./build/bin
    rm -rf ./build/doc.txt

    echo "<3>初始化相关配置..."
    ## 修改模板，重命项目名
    mv project ${projectName}
    sed -i 's#\(VITE_APP_USERPATH \?= \?\).*#\1'"/e5/render/${username}"'#g' .env.production
    sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${username}"'#g' ${projectPath}/${username}/${projectName}/deploy/.env.development
    sed -i 's#\(VITE_APP_BASE_API \?= \?\).*#\1'"${DEV_BASE_API}"'#g' .env.development

    ## 替换 package.json 的 name 值 author值 ，打包时候输出静态地址是这个
    sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json
    sed -i 's#\("author": \?"\).*#\1'"${username}"'",#g' package.json
    ## 替换 processes.json 的 args 值
    sed -i '4s#\("name": \?"\).*#\1'"vite-${username}-${projectName}"'",#g' processes.json
    sed -i '7s#\("args": \?"\).*#\1'"--host --port ${port}"'",#g' processes.json

    cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiLogin
    sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html

    cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiMain
    sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html

    cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/setting/desktop/appsetting/js/1.0.0
    sed -i 's#\(projectName \?= \?\).*#\1"'"${projectName}"'";#g' appsetting.js

    echo "<4>正在启动服务..."
    npm run pm2:start
    exit 1
;;
"delete") #删除
    #参数个数判断，至少3个
    if [ $# -lt 3 ];then
        echo "删除项目命令参数不对：$*"
        echo "删除项目命令正确参数：用户名 项目名 delete"
        exit 1
    fi

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

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

    ### 删除用户项目
    rm -rf ${projectPath}/${username}/${projectName}
    echo "用户：${username}  项目：${projectName}  删除成功！"
    exit 1
;;
"restart") #重启
    echo "》》》正在重启服务..."
    curPid=`pm2 pid vite-${username}-${projectName}`
    if [ ! -z "$curPid" ];then
        echo "vite-${username}-${projectName} 正在关闭服务..."
        pm2 delete "vite-${username}-${projectName}" 
    fi
    cd ${projectPath}/${username}/${projectName}/deploy/
    npm run rm:vite
    npm run pm2:start
    pm2 save
    echo "已经重启服务！！"
    exit 1
;;
"build") #编译
    uploadPath=$4  
    modules=$5
    #参数个数判断，至少3个
    # if [ $# -lt 4 ];then
    #     echo "编译项目命令参数不对：$*"
    #     echo "编译项目命令正确参数：用户名 项目名 build 上传服务器路径 [指定编译模块]"
    #     exit 1
    # fi

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

    if [ -z "$uploadPath" ]; then
        echo '未传服务器路径，请自行拷贝！'
        exit 1;
    fi
    if [[ ! -d "$uploadPath" ]]; then
        echo "编译命令： 上传文件夹不存在, 正在帮您创建目录..."
        mkdir -p "${uploadPath}"
    fi
    echo "正在拷到服务器目录：${uploadPath}"
    # cp -f "${username}_${projectName}_dist.zip"  "${uploadPath}"
    cp -fv "${username}_${projectName}_dist.zip"  "${uploadPath}"

    echo "(‐^▽^‐) 编译打包成功！"
;;
"sync") #同步环境
    #参数个数判断，至少4个
    if [ $# -lt 4 ];then
        echo "同步项目命令参数不对：$*"
        echo "同步项目命令正确参数：用户名 项目名 sycn 同步方式"
        exit 1
    fi
    way=$4

    case $way in 
    "build") #同步打包环境
        echo "》》》正在同步打包环境..."
        rsync -auz ${tmpPath}/build ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/babel.config.js ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.postcssrc.js ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.browserslistrc ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/vite-plugin ${projectPath}/${username}/${projectName}/deploy/
        echo "》》》打包环境已同步！"
        exit 1
    ;;
    "hiui") #同步hiui资源库
        echo "》》》正在同步hi-ui库..."
        cd "${tmpPath}/"
        npm i hi-ui -S
        rm -rf ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-ui
        rsync -auz ${tmpPath}/node_modules/hi-ui ${projectPath}/${username}/${projectName}/deploy/node_modules/
        ## 取hi-ui版本 并更新root package.json
        ## cd ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-ui/
        ## sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json
        echo "hiui已同步！"
    ;;
    "taskcenter") #同步环境
        echo "》》》正在同步任务中心..."
        cd "${tmpPath}/"
        npm i hi-taskcenter -S
        rm -rf ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-taskcenter
        rsync -auz ${tmpPath}/node_modules/hi-taskcenter ${projectPath}/${username}/${projectName}/deploy/node_modules/
        ## 取hi-taskcenter版本 并更新root package.json
        ## cd ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-taskcenter/
        ## sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json

        echo "任务中心已同步！"
    ;;
    "cli") #程序环境
        rsync -auz ${tmpPath}/assets_platform ${projectPath}/${username}/${projectName}/deploy/
        echo "》》》静态共用库已同步！"

        rsync -auz ${tmpPath}/funclib ${projectPath}/${username}/${projectName}/deploy/
        echo "》》》项目funclib资源已同步！"

        rsync -auz ${tmpPath}/project/hivuiLogin ${projectPath}/${username}/${projectName}/deploy/${projectName}/
        cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiLogin
        sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html
        echo "》》》登录模板已同步！"

        rsync -auz ${tmpPath}/project/hivuiMain ${projectPath}/${username}/${projectName}/deploy/${projectName}/
        cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiMain
        sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html
        echo "》》》主页模板已同步！"

        cd "${tmpPath}/"        
        npm i 
        rsync -auz ${tmpPath}/node_modules ${projectPath}/${username}/${projectName}/deploy/
        echo "》》》node环境已同步！"

        cd "${tmpPath}/"
        npm i hi-ui -S
        rm -rf ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-ui
        rsync -auz ${tmpPath}/node_modules/hi-ui ${projectPath}/${username}/${projectName}/deploy/node_modules/
        echo "》》》hi-ui库已同步！"

        rsync -auz ${tmpPath}/build ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/babel.config.js ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.postcssrc.js ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.browserslistrc ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/vite-plugin ${projectPath}/${username}/${projectName}/deploy/

        rsync -auz ${tmpPath}/.env.development ${projectPath}/${username}/${projectName}/deploy/.env.development
        rsync -auz ${tmpPath}/.env.production ${projectPath}/${username}/${projectName}/deploy/.env.production
        rsync -auz ${tmpPath}/.env.staging ${projectPath}/${username}/${projectName}/deploy/.env.staging

        rsync -auz ${tmpPath}/vite.config.js ${projectPath}/${username}/${projectName}/deploy/vite.config.js
        echo "》》》已同步程序环境(build、babel.config.js、.postcssrc.js、.browserslistrc、vite-plugin、.env.development、.env.production、.env.staging、vite.config.js)..."

        source ${cmdDir}/var_env.sh
        ### 设置环境变量
        sed -i 's#\(VITE_APP_USERPATH \?= \?\).*#\1'"/e5/render/${username}"'#g' .env.production    
        sed -i 's#\(VITE_APP_BASE_API \?= \?\).*#\1'"${DEV_BASE_API}"'#g' ${projectPath}/${username}/${projectName}/deploy/.env.development
        sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${username}"'#g' ${projectPath}/${username}/${projectName}/deploy/.env.development
        sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${username}"'#g' ${projectPath}/${username}/${projectName}/deploy/.env.staging

        echo "》》》程序环境已同步！"
    ;;
    esac

    rsync -auz ${tmpPath}/package.json ${projectPath}/${username}/${projectName}/deploy/package.json
    cd ${projectPath}/${username}/${projectName}/deploy/
    ## 替换 package.json 的 name 值
    sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json
    sed -i 's#\("author": \?"\).*#\1'"${username}"'",#g' package.json
    echo "》》》正在重启服务..."
    pm2 delete "vite-${username}-${projectName}"
    npm run rm:vite
    npm run pm2:start
    pm2 save
    echo "》》》已经重启服务！！"
;;
esac

