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

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

	<!-- jar files required by the benchmark -->
	<path id="benchmark.drivers.path">
		<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">
			<fileset dir="bin" includes="**/*.txt"/>
			<fileset dir="bin" includes="h2-test.exe"/>
			<fileset dir="bin" includes="org/**/*"/>
			<fileset dir="bin" includes="org"/>
			<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" depends="init">
		<mkdir dir="bin"/>
		<mkdir dir="docs"/>
		<delete includeemptydirs="true">
			<fileset dir="." includes="*.sql,*.txt,*.lock,**/*.db,node*"/>
			<fileset dir="bin" includes="**/*" excludes="**/*.bat,**/*.sh"/>
			<fileset dir="docs" includes="**/*"/>
		</delete>
		<delete file="src/tools/org/h2/tools/code/CodeSwitch.class"/>
	</target>

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

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

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

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

	<target name="compileResources" depends="clean, compileTest">
		<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="compileFullTextLucene" depends="compileFullTextLuceneTest, compile" if="lucene.jar.present">
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/main" destdir="bin" debug="true">
			<classpath location="${path.lucene.jar}" />
		    <include name="org/h2/fulltext/FullTextLucene.java"/>
		</javac>
	</target>
    
	<target name="compileFullTextLuceneTest" depends="init" unless="lucene.jar.present">
		<echo message="Lucene jar not found, LuceneFullText not compiled."/>
		<echo message="Please set ant-build.properties / path.lucene.jar"/>
	</target>    
	
	<target name="compileServlet" depends="compileServletTest, compile" if="servlet.jar.present">
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/tools" destdir="bin" debug="true">
			<classpath location="${path.servlet.jar}" />
			<include name="org/h2/server/web/*.*"/>
		</javac>
	</target>
	
	<target name="compileServletTest" depends="init" unless="servlet.jar.present">
		<echo message="Servlet API jar not found, console servlet not compiled."/>
		<echo message="Please set ant-build.properties / path.servlet.jar"/>
	</target>

	<target name="compile" depends="compileResources, compileTest">
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="src/main" destdir="bin" debug="true">
		    <exclude name="org/h2/fulltext/FullTextLucene.java"/>
	    </javac>
		<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">
			<exclude name="org/h2/server/web/*.*"/>
		</javac>
		<copy todir="bin" overwrite="true">
			<fileset dir="src/main" includes="META-INF/**/*"/>
			<fileset dir="src/main" includes="**/*.png"/>
			<fileset dir="src/main" includes="**/*.sql"/>
			<fileset dir="src/installer" includes="*.bat"/>
			<fileset dir="src/test" includes="**/*.txt"/>
			<fileset dir="src/test" includes="**/*.properties"/>
		</copy>
	</target>
    
    <target name="compileCoverage" 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="-r org/h2"/>
		</java>
		<javac target="${jdk}" source="${jdk}" executable="${javac}" srcdir="bin" destdir="bin" debug="true" includes="org/h2/**"/>
    </target>
	
	<target name="compileTest" unless="java.version.ok">
		<echo message="Java version is ${java.specification.version} but source code is switched to ${jdk}."/>
		<echo message="Run ant codeswitchJdk... first."/>
	</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.i18n.PrepareTranslation" classpath="bin"/>
		<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="init">
		<available file="${path.servlet.jar}" property="servlet.jar.present"/>
		<available file="${path.lucene.jar}" property="lucene.jar.present"/>
		<condition property="java.version.ok">
			<or>
				<and>
					<equals arg1="${java.specification.version}" arg2="1.6"/>
					<equals arg1="${jdk}" arg2="1.6"/>
				</and>
				<and>
					<equals arg1="${java.specification.version}" arg2="1.5"/>
					<equals arg1="${jdk}" arg2="1.4"/>
				</and>
				<and>
					<equals arg1="${java.specification.version}" arg2="1.4"/>
					<equals arg1="${jdk}" arg2="1.4"/>
				</and>
			</or>
		</condition>
		<echo message="build-jdk:${java.specification.version} ant-build.properties/jdk:${jdk}"></echo>
	</target>
	
	<target name="jar" depends="compile, compileServlet, compileFullTextLucene, manifest">
		<manifest file="bin/META-INF/MANIFEST.MF" mode="update">
			<attribute name="Main-Class" value="org.h2.tools.Console"/>
		</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>
	</target>

	<target name="jarClient" depends="compileResources, manifest">
		<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" manifest="bin/META-INF/MANIFEST.MF">
			<include name="**/*.*"/>
			<exclude name="**/*.bat"/>
		</jar>
	</target>
	
	<target name="jarDb" depends="compileResources, manifest">
		<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" manifest="bin/META-INF/MANIFEST.MF">
			<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.jdbcx.*,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="javadocImpl">
		<mkdir dir="docs/javadocImpl"/>
		<javadoc
			sourcepath="src/main"
			packagenames="org.h2.jdbc.*,org.h2.tools.*,org.h2.api.*,org.h2.store.*"
			destDir="docs/javadocImpl"
		/> 		<!--	doclet="org.h2.tools.doclet.Doclet"  docletpath="bin"-->
		<copy todir="docs/javadoc">
			<fileset dir="src/docsrc/javadoc" includes="**/*"/>
		</copy>
	</target>    
    
    <target name="manifest">
		<mkdir dir="bin/META-INF"/>
		<manifest file="bin/META-INF/MANIFEST.MF">
			<!--
			<attribute name="Bundle-Activator" value="org.h2.osgi.Activator"/>
			-->
			<attribute name="Implementation-Title" value="H2 Database Engine"/>
			<attribute name="Implementation-URL" value="http://www.h2database.com"/>
			<attribute name="Implementation-Version" value="${version.name.maven}"/>
			<attribute name="Build-Jdk" value="${jdk}"/>
		</manifest>
	</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="mavenDeployCentral" depends="jar">
		<copy tofile="bin/h2-${version.name.maven}.jar" file="bin/h2.jar" />
		<copy tofile="bin/pom.xml" filtering="true" file="src/installer/pomUpload.xml">
			<filterset>
				<filter token="version" value="${version.name.maven}"/>
		    </filterset>
		</copy>
		<exec executable="mvn.bat">
			<arg value="deploy:deploy-file"/>
			<arg value="-Dfile=bin/h2.jar"/>
			<arg value="-Durl=file://C:/data/h2database/m2-repo"/>
			<arg value="-Dpackaging=jar"/>
			<arg value="-DpomFile=bin/pom.xml"/>
		</exec>
	</target>

	<target name="mavenInstallLocal" 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="-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="compileCoverage">
		<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="warConsole" depends="compileServlet, jar">
		<fail unless="servlet.jar.present" message="Servlet API jar not found"/>
		<war destfile="bin/h2console.war" webxml="src/tools/WEB-INF/web.xml" basedir="src/tools/WEB-INF" includes="console.html">
			<lib file="bin/h2.jar" />
		</war>
	</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/service/**/*"/>
			<fileset dir=".." includes="h2/src/**/*"/>
		</zip>
	</target>

</project>