@echo off
chcp 65001
setlocal enabledelayedexpansion
cls

cd /d %~dp0
ECHO 检查管理员权限
net.exe session 1>NUL 2>NUL && (
    goto as_admin
) || (
    goto not_admin
)

:as_admin
echo ==========传入参数1,是否压缩:%1
echo ==========传入参数2,是否微信:%2
set pZip=%1
set pWebChat=%2
if '%pZip%'=='' (
	set pZip=1
)
if '%pWebChat%'=='' (
	set pWebChat=1
)

set rootPublished=D:\SATLIC.COM.CLOUD.UI_ADMIN
set appName=云端后台管理界面
cd ..
set zipFile=%cd%\7Zip
cd ..
set rootPath=%cd%
set rootCodePath=%cd%\5.code
set rootDist=%rootCodePath%\dist
set versionBat=%cd%\8.version\每次编译.bat
set versionFile=%cd%\8.version\versiontemp.txt

::读取配置
set file99=%cd%\config.txt
for /f "tokens=*" %%a in (%file99%) do (
	set var=%%a
 
	if "!var:~0,16!"=="applicationName:" (
		set appName=!var:~16,-1!!var:~-1,1!
	)
	
	if "!var:~0,16!"=="applicationCode:" ( 
		set appCode=!var:~16,-1!!var:~-1,1!
	)

	if "!var:~0,10!"=="published:" ( 
		set rootPublished=!var:~10,-1!!var:~-1,1!
	)
)
set rootPublished=!rootPublished!.!appCode!
set appName=!appName!
set appCode=!appCode!
echo 准备发布：%appName%
echo ====地址：%rootPublished%
echo ====编号：%appCode%
ping 127.0.0.1 -n 3 >nul

echo 准备版本号
start %versionBat%>nul
for /f "tokens=1,2,3 delims=." %%a IN (%versionFile%)  Do (
     set tag1=%%a
     set tag2=%%b
     set tag3=%%c
)
set oldVersion=!tag1!.!tag2!.!tag3!
::替换
set d=%date: =0%
set t=%time: =0%

set productVersion=%oldVersion%.%d:~2,2%%d:~5,2%%d:~8,2%%t:~0,2%%t:~3,2%%t:~6,2%
echo %productVersion%
ping 127.0.0.1 -n 3 >nul

echo 发布测试服务
if exist %rootDist% rd /q /s %rootDist%
if exist %rootPublished% rd /q /s %rootPublished%
cd /d %rootCodePath%
call npm run build:dev

set publish=%rootPublished%\test
if exist %rootDist% (
   if exist %publish% rd /q /s %publish%
   md %publish%
   xcopy %rootDist%\  %publish%\ /s /e /y /q
   echo %productVersion%>%publish%\测试服务.v%productVersion%.txt
)

echo 发布正式服务
if exist %rootDist% rd /q /s %rootDist%
cd /d %rootCodePath%
call npm run build:prod

set publish=%rootPublished%\prod
if exist %rootDist% (
   if exist %publish% rd /q /s %publish%
   md %publish%
   xcopy %rootDist%\  %publish%\ /s /e /y /q
   echo %productVersion%>%publish%\正式服务.v%productVersion%.txt
)

:zip
if exist %rootPublished%\SATLIC.COM.%appCode%.tar.gz del /s /q %rootPublished%\SATLIC.COM.%appCode%.tar.gz
if %pZip%==1 (
   goto zip2
) else (
   goto end0
)
:zip2
if exist %zipFile% (
	echo ==========打包安装包
	cd /d %zipFile%
	7za a -ttar %rootPublished%\SATLIC.COM.%appCode%.tar %rootPublished%\*
	7za a -tgzip %rootPublished%\SATLIC.COM.%appCode%.tar.gz %rootPublished%\SATLIC.COM.%appCode%.tar
	del /q %rootPublished%\SATLIC.COM.%appCode%.tar
	rd /s /q %rootPublished%\prod
	rd /s /q %rootPublished%\test
	echo 打包完成
) else (
	echo 未找到压缩软件
)

:webChat
if %pWebChat%==1 (
	if exist %rootPublished%\SATLIC.COM.%appCode%.tar.gz (
		%rootPath%\3.tools\panda\x.satlicNotice.exe 请发布:%appName% %rootPublished%\SATLIC.COM.%appCode%.tar.gz
	)
)
goto end0

:not_admin
  echo not as admin

:end0
echo 9s exit
ping 127.0.0.1 -n 3 >nul
echo 6s exit
ping 127.0.0.1 -n 3 >nul
echo 3s exit
ping 127.0.0.1 -n 3 >nul
exit
