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

# 引用环境变量
cd /eap5.0/hiruntime/project/desktop
source ./var_env.sh
# echo ${DEV_BASE_API}

if [ $# != 3 ] ; then
    echo "创建项目的参数不正确，请输入（用户ID，项目名，预览端口）"
    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'
workName=$1
projectName=$2
port=$3 
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'
# echo 's/<\/head>/'${source[*]}'<\/head>/g'
# exit 1;
## 参数  工号$1  项目名$2  端口$3
## 工号=工作空间
## 1、判断工作空间 没有就创建
if [ ! -d "${tagPath}/${workName}" ];then
    mkdir "${tagPath}/${workName}"
fi
## 2、判断项目名
if [ -d "${tagPath}/${workName}/${projectName}" ];then
    ## 项目名存在，退出
    echo "项目名存在"
    exit 1
fi

echo "下载模板中..."
## 3、下载模板 ,如果很多用户同时下载到同一目录
git clone http://192.168.4.105:8080/gitbucket/git/08335/hivui-platform-template.git "${tagPath}/${workName}/${projectName}/deploy"
 
## 复制 node_modules
echo "环境配置中..."
cp -rf ${tmpPath}/node_modules ${tagPath}/${workName}/${projectName}/deploy/node_modules
cd ${tagPath}/${workName}/${projectName}/deploy/
## 重命名
mv project ${projectName}
rm -r -f oldPackage build/oldWebpack
## 替换 .env.production 的 name 值
sed -i 's#\(VITE_APP_USERPATH \?= \?\).*#\1'"/e5/render/${workName}"'#g' .env.production

## 替换 .env.development 的 name 值
sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${workName}"'#g' ${tagPath}/${workName}/${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'"${workName}"'",#g' package.json
## 替换 processes.json 的 args 值
sed -i '4s#\("name": \?"\).*#\1'"vite-${workName}-${projectName}"'",#g' processes.json
sed -i '7s#\("args": \?"\).*#\1'"--host --port ${port}"'",#g' processes.json

cd ${tagPath}/${workName}/${projectName}/deploy/${projectName}/hivuiLogin

sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html

cd ${tagPath}/${workName}/${projectName}/deploy/${projectName}/hivuiMain

sed -i 's/<\/head>/'"${source[*]}"'<\/head>/g' index.html

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

## 返回预览目录
cd ${tagPath}/${workName}/${projectName}/deploy
### 启动 用yarn 可以把 --host --port 带入 script  dev
## yarn run dev --host --port $port
echo "正在启动服务..."
npm run pm2:start