@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.9.16
set PATH="C:\Windows\System32";%ANT_HOME%\bin";"%JAVA_HOME%\bin";%PATH%

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
GOTO END

:RUNDELIVER
CALL "%ANT_HOME%\bin\ant" deliver
GOTO END

:RUNCLEAN
CALL "%ANT_HOME%\bin\ant" clean
GOTO END

:RUNPARAMTARGET
CALL "%ANT_HOME%\bin\ant" clean
CALL "%ANT_HOME%\bin\ant" -DinputDirCmdLine=%1 summary
GOTO END

:RUNPARAMTARGET2
CALL "%ANT_HOME%\bin\ant" clean
CALL "%ANT_HOME%\bin\ant" -DinputDirCmdLine=%1 -DpreviousInputDirCmdLine=%2 summary
GOTO END

:RUNBUILDTARGET
REM CALL "%ANT_HOME%\bin\ant" clean
CALL "%ANT_HOME%\bin\ant" init
GOTO END

:PASSTHROUGH
CALL "%ANT_HOME%\bin\ant" %1 %2
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.

:END
echo on

