<project name="h2" default="all" basedir=".">

	<property file="ant-build.properties" />

	<path id="benchmark.drivers.path">
		<!-- jar files required by the benchmark -->
		<pathelement location="bin"/>
		<pathelement path="${java.class.path}/"/>
		<fileset dir="${benchmark.drivers.dir}">
			<include name="**/*.jar"/>
		</fileset>
	</path>

	<target name="all" depends="jar,javadoc,docs">
		<delete includeemptydirs="true" verbose="true">
			<fileset dir="bin" includes="**/*.txt"/>
			<fileset dir="bin" includes="h2-test.exe"/>
			<fileset dir="bin" includes="systray4j.jar"/>
			<fileset dir="bin" includes="org/**/*"/>
			<fileset dir="bin" includes="org"/>
			<fileset dir="bin" includes="snoozesoft/**/*"/>
			<fileset dir="bin" includes="snoozesoft"/>
			<fileset dir="bin" includes="META-INF/**/*"/>
			<fileset dir="bin" includes="META-INF"/>
			<fileset dir="docs/html" includes="onePage.html"/>
		</delete>
	</target>

	<target name="benchmark" depends="compile">
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-init -db 1"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-db 2"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-db 3 -out pe.html"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-init -db 4"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-db 5"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-db 6"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-db 7"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-db 8"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" dir="." maxmemory="128m"><arg line="-db 9 -out ps.html"/></java>
	</target>

	<target name="clean">
		<mkdir dir="bin"/>
		<mkdir dir="odbc"/>
		<mkdir dir="docs"/>
		<delete includeemptydirs="true">
			<fileset dir="." includes="*.sql,*.txt,*.lock,**/*.db,node*"/>
			<fileset dir="bin" includes="**/*" excludes="**/*.bat,**/*.sh"/>
			<fileset dir="odbc" includes="*.a,*.def,"/>
			<fileset dir="odbc" includes="*.def,h2odbcTest.exe"/>
			<fileset dir="docs" includes="**/*"/>
		</delete>
		<delete file="src/tools/org/h2/tools/code/CodeSwitch.class"/>
	</target>

	<target name="codeswitch_prepare">
		<javac executable="${javac}" srcdir="src/tools" debug="true" includes="org/h2/tools/code/CodeSwitch.java"/>
	</target>

	<target name="codeswitch_jdk13" depends="codeswitch_prepare">
		<propertyfile file="ant-build.properties">
			<entry key="jdk" value="1.3" />
		</propertyfile>
		<java classname="org.h2.tools.code.CodeSwitch" fork="true" dir="src/tools">
			<arg line="+JDK13 -JDK14 -JDK16 ../main/org/h2"/>
		</java>
	</target>

	<target name="codeswitch_jdk14" depends="codeswitch_prepare">
		<propertyfile file="ant-build.properties">
			<entry key="jdk" value="1.4" />
		</propertyfile>
		<java classname="org.h2.tools.code.CodeSwitch" fork="true" dir="src/tools">
			<arg line="-JDK13 +JDK14 -JDK16 ../main/org/h2"/>
		</java>
	</target>

	<target name="codeswitch_jdk16" depends="codeswitch_prepare">
		<propertyfile file="ant-build.properties">
			<entry key="jdk" value="1.6" />
		</propertyfile>
		<java classname="org.h2.tools.code.CodeSwitch" fork="true" dir="src/tools">
			<arg line="-JDK13 +JDK16 +JDK14 ../main/org/h2"/>
		</java>
	</target>

	<target name="compileResources" depends="clean">
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/main" destdir="bin" debug="true" includes="org/h2/util/Resources.java"/>
		<java fork="true" classname="org.h2.util.Resources" classpath="bin" dir="."/>
		<delete>
			<fileset dir="bin" includes="org/h2/util/ResourceData.class"/>
		</delete>
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="bin" destdir="bin" debug="true" includes="org/h2/util/ResourceData.java"/>
		<delete>
			<fileset dir="bin" includes="org/h2/util/ResourceData.java"/>
		</delete>
	</target>

	<target name="compile" depends="compileResources">
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/main" destdir="bin" debug="true"/>
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/test" destdir="bin" debug="true"/>
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/tools" destdir="bin" debug="true"/>
		<unzip src="src/systray/systray4j.jar" dest="bin"/>
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/systray" destdir="bin" debug="true"/>
		<copy todir="bin" overwrite="true">
			<fileset dir="src/main" includes="META-INF/**/*"/>
			<fileset dir="src/main" includes="**/*.png"/>
			<fileset dir="src/systray" includes="**/*.ico"/>
			<fileset dir="src/installer" includes="*.bat"/>
			<fileset dir="src/test" includes="**/*.txt"/>
			<fileset dir="src/test" includes="**/*.properties"/>
		</copy>
	</target>

	<target name="createPatch" depends="clean">
		<delete file="../h2-patch.*"/>
		<tar destfile="../h2-patch.tar" basedir="." includes="src/**/*.java,build.xml"/>
		<bzip2 destfile="../h2-patch.tar.bz2" src="../h2-patch.tar"/>
		<delete file="../h2-patch.tar"/>
	</target>

	<target name="docs" depends="clean,javadoc,compile">
		<copy todir="docs">
			<fileset dir="src/docsrc" includes="index.html"/>
			<fileset dir="src/docsrc" includes="html/*" excludes="**/*.jsp" />
		</copy>
		<java classname="org.h2.tools.code.CheckTextFiles" classpath="bin"/>
		<java classname="org.h2.tools.doc.GenerateDoc" classpath="bin">
			<arg line="-in src/docsrc/html -out docs/html"/>
		</java>
		<java classname="org.h2.tools.indexer.Indexer" classpath="bin">
			<arg line="-dir docs -destDir docs/html"/>
		</java>
		<java classname="org.h2.tools.doc.MergeDocs" classpath="bin" />
		<java classname="org.h2.tools.doc.LinkChecker" classpath="bin">
			<arg line="-dir docs"/>
		</java>
		<java classname="org.h2.tools.doc.XMLChecker" classpath="bin"/>
		<java classname="org.h2.tools.doc.SpellChecker" classpath="bin"/>
	</target>

	<target name="gcj" depends="compileResources">
		<copy todir="bin">
			<fileset dir="src/main"/>
			<fileset dir="src/test"/>
		</copy>
		<replace file="bin/org/h2/util/StringUtils.java" token="//#GCJHACK" value=""/>
		<exec dir="bin" executable="cmd">
			<arg line="/c dir /b /s *.java >files.txt" />
		</exec>
		<exec dir="bin" executable="gcj">
			<arg line="-O1 -c --classpath=. -o db.o @files.txt" />
		</exec>
		<!-- -g0 -fomit-frame-pointer -fno-bounds-check -fno-store-check -O2  -->
		<exec dir="bin" executable="gcj">
			<arg line="--main=org.h2.tools.Server -s -o h2-server.exe db.o" />
		</exec>
		<exec dir="bin" executable="gcj">
			<arg line="--main=org.h2.test.TestAll -s -o h2-test.exe db.o" />
		</exec>
		<delete includeemptydirs="true">
			<fileset dir="bin" includes="**/*.java"/>
			<fileset dir="bin" includes="**/*.o"/>
		</delete>
	</target>
	
	<target name="jar" depends="compile">
		<mkdir dir="bin/META-INF"/>
		<manifest file="bin/META-INF/MANIFEST.MF">
			<!--
			<attribute name="Bundle-Activator" value="org.h2.osgi.Activator"/>
			-->
			<attribute name="Main-Class" value="org.h2.SysTray"/>
		</manifest>
		<jar jarfile="bin/h2.jar" basedir="bin" manifest="bin/META-INF/MANIFEST.MF">
			<include name="**/*.*"/>
			<exclude name="org/h2/tools/code/**/*.*"/>
			<exclude name="org/h2/tools/doc/**/*.*"/>
			<exclude name="org/h2/tools/doclet/**/*.*"/>
			<exclude name="org/h2/tools/indexer/**/*.*"/>
			<exclude name="org/h2/test/**/*.*"/>
			<exclude name="org/h2/samples/**/*.*"/>
			<exclude name="**/*.bat"/>
			<exclude name="**/*.txt"/>
		</jar>
		<copy todir="bin" file="src/systray/systray4j.dll"/>
	</target>

	<target name="jarClient" depends="compileResources">
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/main" destdir="bin" debug="true">
			<include name="org/h2/*" />
			<include name="org/h2/jdbc/**" /> 
			<include name="org/h2/jdbcx/**" />
		</javac>
		<jar jarfile="bin/h2client.jar" basedir="bin">
			<include name="**/*.*"/>
			<exclude name="**/*.bat"/>
		</jar>
	</target>
	
	<target name="jarDb" depends="compileResources">
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/main" destdir="bin" debug="true">
			<include name="org/h2/*" />
			<include name="org/h2/engine/**" /> 
			<include name="org/h2/jdbc/**" /> 
			<include name="org/h2/jdbcx/**" />
		</javac>
		<jar jarfile="bin/h2db.jar" basedir="bin">
			<include name="**/*.*"/>
			<exclude name="**/*.bat"/>
		</jar>
	</target>	

	<target name="javadoc">
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/test" destdir="bin" debug="true" includes="org/h2/test/bnf/*.java"/>
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/tools" destdir="bin" debug="true" includes="org/h2/tools/doclet/*.java"/>
		<mkdir dir="docs/javadoc"/>
		<javadoc
			sourcepath="src/main"
			packagenames="org.h2.jdbc.*,org.h2.tools.*,org.h2.api.*"
			doclet="org.h2.tools.doclet.Doclet"
			docletpath="bin"
		/>
		<copy todir="docs/javadoc">
			<fileset dir="src/docsrc/javadoc" includes="**/*"/>
		</copy>
	</target>

	<target name="mavenBuildCentral">
		<copy tofile="bin/h2-${version.name.maven}.jar" file="bin/h2.jar" />
		<copy tofile="bin/pom.xml" filtering="true" file="src/installer/pom.xml">
			<filterset>
				<filter token="version" value="${version.name.maven}"/>
		    </filterset>
		</copy>
		<jar destfile="bin/h2-${version.name.maven}-bundle.jar" basedir="bin">
			<include name="pom.xml" />
			<include name="h2-${version.name.maven}.jar" />
		</jar>
	</target>	

	<target name="mavenUploadLocal" depends="jar">
		<copy tofile="bin/pom.xml" filtering="true" file="src/installer/pom.xml">
			<filterset>
				<filter token="version" value="1.0-SNAPSHOT"/>
		    </filterset>
		</copy>
		<exec executable="mvn.bat">
			<arg value="install:install-file"/>
			<arg value="-Dversion=1.0-SNAPSHOT"/>
			<arg value="-Dfile=bin/h2.jar"/>
			<arg value="-DgroupId=org.h2database"/>
			<arg value="-DartifactId=h2"/>
			<arg value="-Dpackaging=jar"/>
			<arg value="-DpomFile=bin/pom.xml"/>
		</exec>
	</target>

	<target name="test" depends="compile">
		<java classname="org.h2.test.TestAll" fork="true" classpath="bin" />
	</target>

	<target name="testCoverage" depends="compile">
		<copy todir="bin" overwrite="true">
			<fileset dir="src/main"/>
		</copy>
		<java classname="org.h2.test.coverage.Coverage" classpath="bin" dir="bin" fork="true">
			<arg line="-e org/h2/web -e org/h2/server/Odbc -e org/h2/jdbcx -e org/h2/bnf -r org/h2"/>
		</java>
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="bin" destdir="bin" debug="true" includes="org/h2/**"/>
		<java classname="org.h2.test.TestAll" fork="true" classpath="bin" dir="bin">
			<arg line="codeCoverage"/>
		</java>
	</target>

	<target name="testProf" depends="compile">
		<java classname="org.h2.test.TestAll" fork="true" classpath="bin">
			<jvmarg value="-Xrunhprof:cpu=samples,depth=8"/>
			<!-- <jvmarg value="-agentlib:hprof=help"/> -->
		</java>
	</target>

	<target name="zip">
		<zip destfile="../h2.zip">
			<fileset dir=".." includes="h2/build.xml"/>
			<fileset dir=".." includes="h2/ant-build.properties"/>
			<fileset dir=".." includes="h2/bin/**/*"/>
			<fileset dir=".." includes="h2/docs/**/*"/>
			<fileset dir=".." includes="h2/odbc/**/*"/>
			<fileset dir=".." includes="h2/service/**/*"/>
			<fileset dir=".." includes="h2/src/**/*"/>
		</zip>
	</target>

</project>