@echo off
if "%1"=="-c" if "%2"=="dwnpatch" if "%3"=="-f" if "%4"=="downloadUrlJson.txt" goto correct_usage
if "%1"=="-c" if "%2"=="updatedb" if "%3"=="-b" if not "%4"=="" goto correct_usage
if "%1"=="-c" if "%2"=="updatedb" if "%3"=="-i" if "%4"=="linux" if "%5"=="-b" if not "%6"==""  goto correct_usage
if "%1"=="-c" if "%2"=="toolUpgrade" goto  correct_usage



goto wrong_usage
:correct_usage

rem set SERVER_HOME=..
set SERVER_HOME=%~dp0%

set NOPAUSE=true

rem echo %SERVER_HOME%
set JAVA_HOME=%SERVER_HOME%jre
if not "%JAVA_HOME%" == "" goto ADD_TOOLS

set JAVA=java

echo JAVA_HOME is not set.  Unexpected results may occur.
echo Set JAVA_HOME to the directory of your local JDK to avoid this message.

:ADD_TOOLS
set JAVA=%JAVA_HOME%\bin\java


set PATH=%PATH%;"%SERVER_HOME%\lib\native"

SET JAVA="%JAVA_HOME%\bin\java"

set CLASS_PATH=%SERVER_HOME%\lib\*;

echo ===============================================================================
echo .
echo .
echo   SERVER_HOME: %SERVER_HOME%
echo .
echo   JAVA: %JAVA%
echo .
echo .
echo ===============================================================================
echo .
%JAVA% -Dlog4j.configurationFile="%SERVER_HOME%log4j2.xml" -cp "%CLASS_PATH%" com.adventnet.sym.server.downloadmgr.DCDownloadMgrProxyServer %*

IF %ERRORLEVEL% EQU 6 (
    if EXIST "%~dp0\jre" (rmdir /S /Q "%~dp0\jre")
	if EXIST "%~dp0\lib" (rmdir /S /Q "%~dp0\lib")
    xcopy "%~dp0\upgrade" /E /Y "%~dp0"

    %JAVA% %JAVA_OPTS% -cp "%CLASS_PATH%" com.adventnet.sym.server.downloadmgr.ToolUpgrade %*
    IF "%2" NEQ "toolUpgrade" (
     call patchsync.bat %1 %2 %3 %4 %5 %6
    )
)
goto end

:wrong_usage
echo Wrong Usage. Refer below for correct commands
echo For DB Update excluding linux : "patchsync.bat -c updatedb -b <BUILD_NUMBER>"
echo For DB Update including linux : "patchsync.bat -c updatedb -i linux -b <BUILD_NUMBER>"
echo For Patch files download      : "patchsync.bat -c dwnpatch -f downloadUrlJson.txt"
echo For tool Upgrade : "patchsync.bat -c toolUpgrade"
echo Please check the documentation for more information - https://www.manageengine.com/patch-management/help/patch-management-for-closed-network.html#config
:end
