@echo off

REM -------------------------------------------------------
REM Update user installed JDK Home location in settings.xml
REM -------------------------------------------------------

set CONFIGFILE="%~dp0settings.xml"
for /F "tokens=2-3 delims=^>,^<" %%i IN ('findstr /L "JDKHome" %CONFIGFILE%') do call set JAVA_HOME=%%~fsi
REM for /f "tokens=1* delims== " %%a in ('java -XshowSettings:properties -version 2^>^&1 ^| findstr /r javahome') do (echo %%b
REM call set JAVA_HOME=%%b)

set ANT_HOME=%CD%\tools\apache-ant-1.10.14

REM set PATH="C:\Windows\System32";"%ANT_HOME%\bin";"%JAVA_HOME%\bin";%PATH%
echo %path%|find /i "%ANT_HOME%\bin">nul  || set path=%path%;"%ANT_HOME%\bin"
echo %path%|find /i "%JAVA_HOME%\bin">nul  || set path=%path%;"%JAVA_HOME%\bin"
echo %path%|find /i "C:\Windows\System32">nul || set path=%path%;"C:\Windows\System32"

REM Note: hitting 'clean' everytime below is done for clarity of operation.
REM       'clean' can be removed for more efficient operation.

 IF exist %JAVA_HOME%\bin\java.exe (
 	IF exist %JAVA_HOME%\lib\tools.jar (
		IF NOT [%1]==[] (
			IF /I [%1]==[clean] (
			    GOTO RUNCLEAN 
			) ELSE IF /I [%1]==[deliver] (
			     GOTO RUNDELIVER
			) ELSE IF /I [%1]==[help] (
			     GOTO RUNHELP
			) ELSE (
				IF NOT [%2]==[] (
					REM GOTO PASSTHROUGH
					GOTO RUNPARAMTARGET2
				) ELSE (
					GOTO RUNPARAMTARGET
				)
			)
		) ELSE (
		    REM  No argument provided
			REM GOTO RUNHELP
			REM echo Running with schemas in input/xsd
			GOTO RUNBUILDTARGET
			
		)
	) ELSE (
		REM GOTO INVALIDJREHOME
		IF NOT [%1]==[] (
			IF /I [%1]==[clean] (
				GOTO RUNCLEAN
			) ELSE IF /I [%1]==[deliver] (
			     GOTO RUNDELIVER
			) ELSE IF /I [%1]==[help] (
			     GOTO RUNHELP
			) ELSE (
				IF NOT [%2]==[] (
					REM GOTO PASSTHROUGH
					GOTO RUNPARAMTARGET2
				) ELSE (
					GOTO RUNPARAMTARGET
				)
			)
		) ELSE (
		      REM  No argument provided
			REM GOTO RUNHELP
			REM echo Running with schemas in input/xsd
			GOTO RUNBUILDTARGET
			
		)
	)
 ) ELSE (
	GOTO INVALIDJAVAHOME
)

:RUNHELP
rem when to show help??
CALL "%ANT_HOME%\bin\ant" help
if %ERRORLEVEL% NEQ 0 (goto ERROR_EXIT) else GOTO END

:RUNDELIVER
CALL "%ANT_HOME%\bin\ant" deliver
if %ERRORLEVEL% NEQ 0 (goto ERROR_EXIT) else GOTO END

:RUNCLEAN
CALL "%ANT_HOME%\bin\ant" clean
if %ERRORLEVEL% NEQ 0 (goto ERROR_EXIT) else GOTO END

:RUNPARAMTARGET
CALL "%ANT_HOME%\bin\ant" clean
CALL "%ANT_HOME%\bin\ant" -DinputDirCmdLine=%1 summary
if %ERRORLEVEL% NEQ 0 (goto ERROR_EXIT) else GOTO END

:RUNPARAMTARGET2
CALL "%ANT_HOME%\bin\ant" clean
CALL "%ANT_HOME%\bin\ant" -DinputDirCmdLine=%1 -DpreviousInputDirCmdLine=%2 summary
if %ERRORLEVEL% NEQ 0 (goto ERROR_EXIT) else GOTO END

:RUNBUILDTARGET
REM CALL "%ANT_HOME%\bin\ant" clean
CALL "%ANT_HOME%\bin\ant" init
if %ERRORLEVEL% NEQ 0 (goto ERROR_EXIT) else GOTO END

:PASSTHROUGH
CALL "%ANT_HOME%\bin\ant" %1 %2
if %ERRORLEVEL% NEQ 0 (goto ERROR_EXIT) else GOTO END

:INVALIDJREHOME
echo Invalid Java Home (%JAVA_HOME%)!!
echo Schematron validator requires JDK, please refer installation section in ST.96_XSD_Validation_via_Schematron.docx for details.
echo Please update settings.xml with JDK installation path.
GOTO END

:INVALIDJAVAHOME
echo Invalid Java Home!!
echo 	Please update settings.xml with JDK installation path.

:ERROR_EXIT
echo Ant build failed!
pause

:END
echo on

