@echo off
rem ---------------------------------------------------------------------------
rem deploy script for the MWMS Server
rem ---------------------------------------------------------------------------
setlocal
set "RAR=D:\Program Files\7-Zip\7z.exe"
REM set "MWMS_VERSION=1.1.0-SNAPSHOT"
set "MWMS_VERSION=1.0.0"

rem Guess WORKSPACE_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%WORKSPACE_BASE%" == "" goto gotBase
rem set WORKSPACE_BASE=mwms-silicon-server directory
set "WORKSPACE_BASE=%CURRENT_DIR%\..\.."
echo WORKSPACE_BASE="%WORKSPACE_BASE%"
:gotBase
if not exist "%WORKSPACE_BASE%\Environment" echo "%WORKSPACE_BASE%\Environment is not exist" && goto end
set "WORKSPACE_ENV=%WORKSPACE_BASE%\Environment"
:gotEnv

if not exist "%WORKSPACE_ENV%\config" echo "%WORKSPACE_ENV%\config is not exist" && goto end
set "WORKSPACE_ENV_CONFIG=%WORKSPACE_ENV%\config"

rem set application profile directory
rem if not exist "%WORKSPACE_ENV%\config\profile\TST" echo "%WORKSPACE_ENV%\config\profile\TST is not exist" && goto end
rem set "WORKSPACE_ENV_PROFILE=%WORKSPACE_ENV%\config\profile\TST"
:gotEnvconfig

if not exist "%WORKSPACE_ENV%\cmd" echo "%WORKSPACE_ENV%\cmd is not exist" && goto end
set "WORKSPACE_ENV_CMD=%WORKSPACE_ENV%\cmd"
:gotEnvcmd

if not exist "%WORKSPACE_ENV_CMD%\zidmboot.sh" echo "%WORKSPACE_ENV_CMD%\zidmboot.sh is not exist" && goto end

if exist "%WORKSPACE_BASE%\output" goto gotOutput
mkdir "%WORKSPACE_BASE%\output"
:gotOutput
if exist "%WORKSPACE_BASE%\output\TST" goto gotDevOutput
mkdir "%WORKSPACE_BASE%\output\TST"
:gotDevOutput
if not exist "%WORKSPACE_BASE%\output\TST\MWMS.server" goto gotMakeBaseDir
rd /s /Q "%WORKSPACE_BASE%\output\TST\MWMS.server\bin"
rd /s /Q "%WORKSPACE_BASE%\output\TST\MWMS.server\conf"
rd /s /Q "%WORKSPACE_BASE%\output\TST\MWMS.server\lib"
rd /s /Q "%WORKSPACE_BASE%\output\TST\MWMS.server\logs"
rd /s /Q "%WORKSPACE_BASE%\output\TST\MWMS.server\temp"
goto gotSolution
:gotMakeBaseDir
mkdir "%WORKSPACE_BASE%\output\TST\MWMS.server"
:gotSolution

cd  "%WORKSPACE_BASE%\output\TST\MWMS.server"
set "MWMS_BASE=%cd%"

if not exist "%WORKSPACE_BASE%\zi-mwms-tv-boot\target\zi-mwms-tv-boot-%MWMS_VERSION%.jar" echo The MWMSSolution Execute jar is not defined correctly &&  goto end
:gotExcuteJar

if exist "%MWMS_BASE%\bin" goto gotBindir
mkdir "%MWMS_BASE%\bin"
:gotBindir

if exist "%MWMS_BASE%\conf" goto gotConfdir
mkdir "%MWMS_BASE%\conf"
:gotConfdir

if exist "%MWMS_BASE%\lib" goto gotLibdir
mkdir "%MWMS_BASE%\lib"
:gotLibdir

if exist "%MWMS_BASE%\logs" goto gotLogsdir
mkdir "%MWMS_BASE%\logs"
:gotLogsdir

if exist "%MWMS_BASE%\temp" goto gotTempdir
mkdir "%MWMS_BASE%\temp"
:gotTempdir

if "%MWMS_CONFIGPATH%" == "" goto emptyConfigpath
set "MWMS_CONFIGPATH=%MWMS_CONFIGPATH%"
:emptyConfigpath
set "MWMS_CONFIGPATH=%MWMS_BASE%\conf"

if "%MWMS_BINPATH%" == "" goto emptyBinpath
set "MWMS_BINPATH=%MWMS_BINPATH%"
:emptyBinpath
set "MWMS_BINPATH=%MWMS_BASE%\bin"

if "%RAR%" == "" echo The 7ZIP environment variable is not defined correctly && goto end
:emptyRAR

rem clear config and execute jar
rd /s /Q %MWMS_CONFIGPATH% & md %MWMS_CONFIGPATH%
rd /s /Q %MWMS_BINPATH% & md %MWMS_BINPATH%

rem copy config file to conf dictory
ROBOCOPY %WORKSPACE_ENV_CONFIG% %MWMS_CONFIGPATH% /E /MT:10
rem ROBOCOPY %WORKSPACE_ENV_PROFILE% %MWMS_CONFIGPATH%\profile /E /MT:10
rem rd /S /Q %MWMS_CONFIGPATH%\profile\DEV
rem rd /S /Q %MWMS_CONFIGPATH%\profile\PRD
rem rd /S /Q %MWMS_CONFIGPATH%\profile\TST
rem del /Q /F %MWMS_CONFIGPATH%\profile\application.yml
ROBOCOPY %WORKSPACE_ENV_CMD% %MWMS_BINPATH% /MT:1
ROBOCOPY %WORKSPACE_BASE%\zi-mwms-tv-boot\target %MWMS_BASE%\lib zi-mwms-tv-boot-%MWMS_VERSION%.jar
ren "%MWMS_BASE%\lib\zi-mwms-tv-boot-%MWMS_VERSION%.jar" "bootstrap.jar"

rem compress tar
rem set hour , if hour < 10, then 01 ~ 09
set currenthour=%time:~0,2%
if "%currenthour:~0,1%" NEQ " " goto gotCurrentHour
set currenthour="0"%currenthour:~1,1%
:gotCurrentHour

set "ymd=%date:~0,4%%date:~5,2%%date:~8,2%%currenthour%%time:~3,2%%time:~6,2%"
"%RAR%" a -ttar "%WORKSPACE_BASE%\output\TST\MWMS.server-%MWMS_VERSION%-%ymd%.tar" "%MWMS_BASE%"
"%RAR%" a -tgzip "%WORKSPACE_BASE%\output\TST\MWMS.server-%MWMS_VERSION%-%ymd%.tar.gz" "%WORKSPACE_BASE%\output\TST\MWMS.server-%MWMS_VERSION%-%ymd%.tar"
del /F "%WORKSPACE_BASE%\output\TST\MWMS.server-%MWMS_VERSION%-%ymd%.tar"
cd %WORKSPACE_BASE%\output\TST
if not exist %MWMS_BASE% goto end
rd /s /Q %MWMS_BASE%

:end