@echo off
rem ---------------------------------------------------------------------------
rem deploy script for the MWMS Server
rem ---------------------------------------------------------------------------
setlocal
set "RAR=C:\Program Files\7-Zip\7z.exe"
set "WMS_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=WMS-parent directory
set "WORKSPACE_BASE=%CURRENT_DIR%\..\.."
echo WORKSPACE_BASE="%WORKSPACE_BASE%"
:gotBase
if not exist "%WORKSPACE_BASE%\zi-wms-environment" (
    echo "%WORKSPACE_BASE%\zi-wms-environment is not exist"
    goto end
)
set "WORKSPACE_ENV=%WORKSPACE_BASE%\zi-wms-environment"
echo WORKSPACE_ENV="%WORKSPACE_ENV%"
:gotEnv

if not exist "%WORKSPACE_ENV%\config" (
    echo "%WORKSPACE_ENV%\config is not exist"
    goto end
)
set "WORKSPACE_ENV_CONFIG=%WORKSPACE_ENV%\config"
echo WORKSPACE_ENV_CONFIG="%WORKSPACE_ENV_CONFIG%"
:gotEnvconfig

if not exist "%WORKSPACE_ENV%\cmd" (
    echo "%WORKSPACE_ENV%\cmd is not exist"
    goto end
)
set "WORKSPACE_ENV_CMD=%WORKSPACE_ENV%\cmd"
echo 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"
echo Created output directory
:gotOutput
if exist "%WORKSPACE_BASE%\output\DEV" goto gotDevOutput
mkdir "%WORKSPACE_BASE%\output\DEV"
echo Created DEV directory
:gotDevOutput
if not exist "%WORKSPACE_BASE%\output\DEV\WMS.server" goto gotMakeBaseDir
rd /s /Q "%WORKSPACE_BASE%\output\DEV\WMS.server\bin"
rd /s /Q "%WORKSPACE_BASE%\output\DEV\WMS.server\conf"
rd /s /Q "%WORKSPACE_BASE%\output\DEV\WMS.server\lib"
rd /s /Q "%WORKSPACE_BASE%\output\DEV\WMS.server\logs"
rd /s /Q "%WORKSPACE_BASE%\output\DEV\WMS.server\temp"
goto gotSolution
:gotMakeBaseDir
mkdir "%WORKSPACE_BASE%\output\DEV\WMS.server"
echo Created WMS.server base directory
:gotSolution

cd "%WORKSPACE_BASE%\output\DEV\WMS.server"
set "WMS_BASE=%cd%"
echo WMS_BASE="%WMS_BASE%"

if not exist "%WORKSPACE_BASE%\zi-wms-boot\target\zi-wms-boot-%WMS_VERSION%.jar" (
    echo The WMSSolution Execute jar is not defined correctly
    goto end
)
:gotExcuteJar

if exist "%WMS_BASE%\bin" goto gotBindir
mkdir "%WMS_BASE%\bin"
echo Created bin directory
:gotBindir

if exist "%WMS_BASE%\conf" goto gotConfdir
mkdir "%WMS_BASE%\conf"
echo Created conf directory
:gotConfdir

if exist "%WMS_BASE%\lib" goto gotLibdir
mkdir "%WMS_BASE%\lib"
echo Created lib directory
:gotLibdir

if exist "%WMS_BASE%\logs" goto gotLogsdir
mkdir "%WMS_BASE%\logs"
echo Created logs directory
:gotLogsdir

if exist "%WMS_BASE%\temp" goto gotTempdir
mkdir "%WMS_BASE%\temp"
echo Created temp directory
:gotTempdir

if "%WMS_CONFIGPATH%" == "" goto emptyConfigpath
set "WMS_CONFIGPATH=%WMS_CONFIGPATH%"
:emptyConfigpath
set "WMS_CONFIGPATH=%WMS_BASE%\conf"
echo WMS_CONFIGPATH="%WMS_CONFIGPATH%"

if "%WMS_BINPATH%" == "" goto emptyBinpath
set "WMS_BINPATH=%WMS_BINPATH%"
:emptyBinpath
set "WMS_BINPATH=%WMS_BASE%\bin"
echo WMS_BINPATH="%WMS_BINPATH%"

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

rem clear config and execute jar
rd /s /Q %WMS_CONFIGPATH% & md %WMS_CONFIGPATH%
echo Cleared and recreated config path
rd /s /Q %WMS_BINPATH% & md %WMS_BINPATH%
echo Cleared and recreated bin path

rem copy config file to conf dictory
ROBOCOPY %WORKSPACE_ENV_CONFIG% %WMS_CONFIGPATH% /E /MT:10
echo Copied config files to conf directory
ROBOCOPY %WORKSPACE_ENV_CMD% %WMS_BINPATH% /MT:1
echo Copied command files to bin directory
ROBOCOPY %WORKSPACE_BASE%\zi-wms-boot\target %WMS_BASE%\lib zi-wms-boot-%WMS_VERSION%.jar
ren "%WMS_BASE%\lib\zi-wms-boot-%WMS_VERSION%.jar" "bootstrap.jar"
echo Copied and renamed JAR file

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%"
echo ymd=%ymd%

"%RAR%" a -ttar "%WORKSPACE_BASE%\output\DEV\WMS.server-%WMS_VERSION%-%ymd%.tar" "%WMS_BASE%"
echo Created TAR archive
"%RAR%" a -tgzip "%WORKSPACE_BASE%\output\DEV\WMS.server-%WMS_VERSION%-%ymd%.tar.gz" "%WORKSPACE_BASE%\output\DEV\WMS.server-%WMS_VERSION%-%ymd%.tar"
echo Created GZipped TAR archive
del /F "%WORKSPACE_BASE%\output\DEV\WMS.server-%WMS_VERSION%-%ymd%.tar"
echo Deleted intermediate TAR archive

cd %WORKSPACE_BASE%\output\DEV
if not exist %WMS_BASE% goto end
rd /s /Q %WMS_BASE%
echo Removed WMS.server base directory

:end
pause
