#!/bin/sh

OUTPUT_FILE=CodeCoverageProperties.xml
OUTPUT_FILE64=CodeCoverageProperties64.xml

CODE_COVERAGE_BUILDTOOLKIT=`pwd`
TOOLKIT_DIR=buildtoolkit
cd ..

if [ ! -d "`pwd`/${TOOLKIT_DIR}" ]; then
	cd ..
	TOOLKIT_DIR=jazz/buildsystem/buildtoolkit
	if [ -d "`pwd`/${TOOLKIT_DIR}" ]; then
		# Directory for the IBM Rational Team Concert Build System Toolkit installation.
		BUILDTOOLKIT=`pwd`/${TOOLKIT_DIR}
	fi
else
	# Directory for the IBM Rational Team Concert Build System Toolkit installation.
	BUILDTOOLKIT=`pwd`/${TOOLKIT_DIR}
fi

cd "$CODE_COVERAGE_BUILDTOOLKIT"/plugins

# Obtain the latest versions of the dependency files
COMMON_JAR=`ls com.ibm.rational.llc.common_*.jar | sort | tail -1`;
ENGINE_JAR=`ls com.ibm.rational.llc.engine_*.jar | sort | tail -1`;
REPORT_BIRT_JAR=`ls com.ibm.rational.llc.report.birt_*.jar | sort | tail -1`;

JDT_CORE_JAR=`ls org.eclipse.jdt.core_*.jar | sort | tail -1`;
JDT_COMPILER_JAR=`ls jdtCompilerAdapter*.jar | sort | tail -1`;
cd ..

# Determine the JVMTI library to use.
# Check if an argument was passed to require a 31/32 bit JVMTI library to be used.
# On z/OS the system is OS/390.
MACHINE_TYPE=`uname -s`
# On Linux, use uname -m to determine the machine type.
if [ "$MACHINE_TYPE" = "Linux" ]; then
	MACHINE_TYPE=`uname -m`
fi

if [ -n "$1" ]; then
	# On zOS and Linux 390
	if [ "$MACHINE_TYPE" = "OS/390" -o "$MACHINE_TYPE" = "s390" -o "$MACHINE_TYPE" = "s390x" ]; then
		if [ "$1" = "-31" ]; then
			JVMTI_LIB_PREFIX=31bit
		fi
	else
		if [ "$1" = "-32" ]; then
			JVMTI_LIB_PREFIX=32bit
		fi
	fi
fi

if [ -z "$JVMTI_LIB_PREFIX" ]; then
	if [ -d "${CODE_COVERAGE_BUILDTOOLKIT}/64bit" ]; then
		JVMTI_LIB_PREFIX=64bit
	else
		# On zOS and Linux 390
		if [ "$MACHINE_TYPE" = "OS/390" -o "$MACHINE_TYPE" = "s390" -o "$MACHINE_TYPE" = "s390x" ]; then
			JVMTI_LIB_PREFIX=31bit
		else
			JVMTI_LIB_PREFIX=32bit
		fi
	fi
fi

if [ ! -d "${CODE_COVERAGE_BUILDTOOLKIT}/${JVMTI_LIB_PREFIX}/plugins/org.eclipse.tptp.javaprofiler" ]; then
	echo "Cannot find the ${CODE_COVERAGE_BUILDTOOLKIT}/${JVMTI_LIB_PREFIX}/plugins/org.eclipse.tptp.javaprofiler directory."
	exit 1
fi

if [ "$MACHINE_TYPE" = "OS/390" ]; then
	echo \<?xml version=\"1.0\" encoding=\"IBM-1047\"?\> > ${OUTPUT_FILE}
	echo \<project\> >> ${OUTPUT_FILE}
else
	echo \<project\> > ${OUTPUT_FILE}
fi

if [ -d "$BUILDTOOLKIT" ]; then
	echo \<property name=\"jazz-build-toolkit\" value=\"${BUILDTOOLKIT}\"/\> >> ${OUTPUT_FILE}
fi

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_BUILDTOOLKIT}/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_CORE_JAR}\"/\> >> ${OUTPUT_FILE}
echo \<property name=\"eclipse-jdt-core-plugin\" value=\"${JDT_COMPILER_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 [ -d "$BUILDTOOLKIT" ]; then
	echo \<import file=\"\${jazz-build-toolkit}/BuildToolkitTaskDefs.xml\"/\> >> ${OUTPUT_FILE}
fi

if [ -d "${CODE_COVERAGE_BUILDTOOLKIT}/64bit" ]; then # 64 bit case
	cp ${OUTPUT_FILE} ${OUTPUT_FILE64}
	echo \<property name=\"llc-jvmti-library\" value=\"${CODE_COVERAGE_BUILDTOOLKIT}/64bit/plugins/org.eclipse.tptp.javaprofiler/libJPIBootLoader.so\"/\> >> ${OUTPUT_FILE64}
	if [ "$MACHINE_TYPE" = "OS/390" -o "$MACHINE_TYPE" = "s390" -o "$MACHINE_TYPE" = "s390x" ]; then
		echo \<property name=\"llc-jvmti-library\" value=\"${CODE_COVERAGE_BUILDTOOLKIT}/31bit/plugins/org.eclipse.tptp.javaprofiler/libJPIBootLoader.so\"/\> >> ${OUTPUT_FILE}
	else
		echo \<property name=\"llc-jvmti-library\" value=\"${CODE_COVERAGE_BUILDTOOLKIT}/32bit/plugins/org.eclipse.tptp.javaprofiler/libJPIBootLoader.so\"/\> >> ${OUTPUT_FILE}
	fi	
		if [ "$MACHINE_TYPE" = "OS/390" ]; then
			echo \<property name=\"llc-jvm-arg-jvmti\" value=\"-agentpath:\${llc-jvmti-library}=JPIAgent:server=standalone,file=,jpi_home=${CODE_COVERAGE_BUILDTOOLKIT}/64bit/plugins/org.eclipse.tptp.javaprofiler\;ProbekitAgent:ext-pk-BCILibraryName=BCIEngProbe,ext-pk-probescript=\"/\> >> ${OUTPUT_FILE64}
			echo \</project\> >> ${OUTPUT_FILE64}
			echo \<property name=\"llc-jvm-arg-jvmti\" value=\"-agentpath:\${llc-jvmti-library}=JPIAgent:server=standalone,file=,jpi_home=${CODE_COVERAGE_BUILDTOOLKIT}/31bit/plugins/org.eclipse.tptp.javaprofiler\;ProbekitAgent:ext-pk-BCILibraryName=BCIEngProbe,ext-pk-probescript=\"/\> >> ${OUTPUT_FILE}
			echo \</project\> >> ${OUTPUT_FILE}
		else
			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-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}
		fi		
else # 32 or 31 bit cases
	echo \<property name=\"llc-jvmti-library\" value=\"${CODE_COVERAGE_BUILDTOOLKIT}/${JVMTI_LIB_PREFIX}/plugins/org.eclipse.tptp.javaprofiler/libJPIBootLoader.so\"/\> >> ${OUTPUT_FILE}
		if [ "$MACHINE_TYPE" = "OS/390" ]; then # OS/390 case
			echo \<property name=\"llc-jvm-arg-jvmti\" value=\"-agentpath:\${llc-jvmti-library}=JPIAgent:server=standalone,file=,jpi_home=${CODE_COVERAGE_BUILDTOOLKIT}/${JVMTI_LIB_PREFIX}/plugins/org.eclipse.tptp.javaprofiler\;ProbekitAgent:ext-pk-BCILibraryName=BCIEngProbe,ext-pk-probescript=\"/\> >> ${OUTPUT_FILE}
			echo \</project\> >> ${OUTPUT_FILE}
		else # not OS/390 case
			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}
		fi
	
	
fi		

echo Code Coverage properties file successfully generated!
echo 
