@echo off

set OUTPUT_FILE=CodeCoverageProperties.xml
set OUTPUT_FILE64=CodeCoverageProperties64.xml
set CODE_COVERAGE_BUILDTOOLKIT=%cd%

REM Check for the 'buildtoolkit' directory 
cd ..
set TOOLKIT_DIR=buildtoolkit
IF EXIST %TOOLKIT_DIR% GOTO DIR_EXISTS

REM The directory structure of the build toolkit offering changed in the 3.x version. 
REM Check for the 3.x directory structure if we haven't found the buildtoolkit directory yet
cd ..
set TOOLKIT_DIR=jazz\buildsystem\buildtoolkit
IF NOT EXIST %TOOLKIT_DIR% GOTO NO_BUILDTOOLKIT_DIR

:DIR_EXISTS
REM Directory for the IBM Rational Team Concert Build System Toolkit installation.
cd %TOOLKIT_DIR%
set BUILDTOOLKIT=%cd%

:NO_BUILDTOOLKIT_DIR
cd %CODE_COVERAGE_BUILDTOOLKIT%\plugins

REM Obtain the latest versions of the dependency files
for /f "delims= tokens=1" %%c in ('dir /B /OD "com.ibm.rational.llc.common_*"') do set COMMON_JAR=%%c
for /f "delims= tokens=1" %%c in ('dir /B /OD "com.ibm.rational.llc.engine_*"') do set ENGINE_JAR=%%c
for /f "delims= tokens=1" %%c in ('dir /B /OD "com.ibm.rational.llc.report.birt_*"') do set REPORT_BIRT_JAR=%%c

for /f "delims= tokens=1" %%c in ('dir /B /OD "org.eclipse.jdt.core_*"') do set JDT_CORE_JAR=%%c
for /f "delims= tokens=1" %%c in ('dir /B /OD "jdtCompilerAdapter*"') do set JDT_COMPILER_JAR=%%c
cd ..

REM Determine the JVMTI library to use.
REM Check if an argument was passed to require a 32 bit JVMTI library to be used.
IF "%1" == "/32" GOTO JVMTI_32BIT
IF EXIST "%CODE_COVERAGE_BUILDTOOLKIT%\64bit" GOTO JVMTI_64BIT

:JVMTI_32BIT
set JVMTI_LIB_PREFIX=32bit
goto JVMTI_LIB_PREFIX_SET

:JVMTI_64BIT
set JVMTI_LIB_PREFIX=64bit

:JVMTI_LIB_PREFIX_SET
IF NOT EXIST "%CODE_COVERAGE_BUILDTOOLKIT%\%JVMTI_LIB_PREFIX%\plugins\org.eclipse.tptp.javaprofiler" (
	echo Cannot find the "%CODE_COVERAGE_BUILDTOOLKIT%\%JVMTI_LIB_PREFIX%\plugins\org.eclipse.tptp.javaprofiler" directory.
	goto END
)

echo ^<project^> > %OUTPUT_FILE%

IF EXIST "%BUILDTOOLKIT%" (
	echo ^<property name="jazz-build-toolkit" value="%BUILDTOOLKIT%"/^> >> %OUTPUT_FILE%
)

echo ^<property name="code-coverage-ext-dir" value="%CODE_COVERAGE_BUILDTOOLKIT%"/^> >> %OUTPUT_FILE%
echo ^<property name="code-coverage-plugins-dir" value="${code-coverage-ext-dir}\plugins"/^> >> %OUTPUT_FILE%
echo ^<property name="code-coverage-report-dir" value="${code-coverage-ext-dir}\CodeCoverageReports"/^> >> %OUTPUT_FILE%
echo ^<property name="llc-common-plugin" value="%COMMON_JAR%"/^> >> %OUTPUT_FILE%
echo ^<property name="llc-engine-plugin" value="%ENGINE_JAR%"/^> >> %OUTPUT_FILE%
echo ^<property name="llc-report-birt-plugin" value="%REPORT_BIRT_JAR%"/^> >> %OUTPUT_FILE%
echo ^<property name="eclipse-jdt-compiler-adapter-plugin" value="%JDT_COMPILER_JAR%"/^> >> %OUTPUT_FILE%
echo ^<property name="eclipse-jdt-core-plugin" value="%JDT_CORE_JAR%"/^> >> %OUTPUT_FILE%
echo ^<property name="llc-jvm-arg-engine" value="-Xbootclasspath/a:${code-coverage-plugins-dir}\${llc-engine-plugin}"/^> >> %OUTPUT_FILE%
echo ^<property name="llc-jvm-arg-output" value="-Dcoverage.out.file="/^> >> %OUTPUT_FILE%

echo ^<import file="${code-coverage-ext-dir}\CoverageToolkitTaskDefs.xml"/^> >> %OUTPUT_FILE%

IF EXIST "%BUILDTOOLKIT%" (
	echo ^<import file="${jazz-build-toolkit}\BuildToolkitTaskDefs.xml"/^> >> %OUTPUT_FILE%
)

REM Generate Code Coverage extension property files for 32-bit and 64-bit on 64-bit platforms
IF EXIST "%CODE_COVERAGE_BUILDTOOLKIT%\64bit" (
	copy /y %OUTPUT_FILE% %OUTPUT_FILE64%
	echo ^<property name="llc-jvmti-library" value="%CODE_COVERAGE_BUILDTOOLKIT%\64bit\plugins\org.eclipse.tptp.javaprofiler\JPIBootLoader"/^> >> %OUTPUT_FILE64%
	echo ^<property name="llc-jvm-arg-jvmti" value="-agentpath:${llc-jvmti-library}=JPIAgent:server=standalone,file=;ProbekitAgent:ext-pk-BCILibraryName=BCIEngProbe,ext-pk-probescript="/^> >> %OUTPUT_FILE64%
	echo ^</project^> >> %OUTPUT_FILE64%
	echo ^<property name="llc-jvmti-library" value="%CODE_COVERAGE_BUILDTOOLKIT%\32bit\plugins\org.eclipse.tptp.javaprofiler\JPIBootLoader"/^> >> %OUTPUT_FILE%
	echo ^<property name="llc-jvm-arg-jvmti" value="-agentpath:${llc-jvmti-library}=JPIAgent:server=standalone,file=;ProbekitAgent:ext-pk-BCILibraryName=BCIEngProbe,ext-pk-probescript="/^> >> %OUTPUT_FILE%
	echo ^</project^> >> %OUTPUT_FILE%
	goto DONE
)

echo ^<property name="llc-jvmti-library" value="%CODE_COVERAGE_BUILDTOOLKIT%\%JVMTI_LIB_PREFIX%\plugins\org.eclipse.tptp.javaprofiler\JPIBootLoader"/^> >> %OUTPUT_FILE%
echo ^<property name="llc-jvm-arg-jvmti" value="-agentpath:${llc-jvmti-library}=JPIAgent:server=standalone,file=;ProbekitAgent:ext-pk-BCILibraryName=BCIEngProbe,ext-pk-probescript="/^> >> %OUTPUT_FILE%

echo ^</project^> >> %OUTPUT_FILE%


:DONE
echo Code Coverage properties files successfully generated!
echo.

:END