@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

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

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 NOT [%1]==[] (
			IF /I [%1]==[clean] (
			    GOTO RUNCLEAN 
			) ELSE IF /I [%1]==[deliver] (
			     GOTO RUNDELIVER
			) ELSE IF /I [%1]==[help] (
			     GOTO RUNHELP
			) ELSE IF /I [%1]==[integritycheck] (
				IF NOT [%2]==[] (
						GOTO RUNPARAMINTEGRITYCHECK
					) ELSE (
					GOTO RUNINTEGRITYCHECK
				)
			) ELSE (
			echo %1
				IF NOT [%2]==[] (
					REM GOTO PASSTHROUGH
					GOTO RUNPARAMTARGET2
				) ELSE (
					echo run summary with %1
					GOTO RUNPARAMTARGET
				)
			)
		) ELSE (
		    REM  No argument provided
			REM GOTO RUNHELP
			REM echo Running with schemas in input/xsd
			GOTO RUNBUILDTARGET
			
		)
	


: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

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

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

:RUNPARAMINTEGRITYCHECK
CALL "%ANT_HOME%\bin\ant" clean
CALL "%ANT_HOME%\bin\ant" -DinputDirCmdLine=%2 integritycheck
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

