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

    <!-- dependencies (optional):
    		http://repo1.maven.org/maven2/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar
    		(actually any servlet api)
			http://repo1.maven.org/maven2/org/apache/lucene/lucene-core/2.2.0/lucene-core-2.2.0.jar
			(or newer)
    -->
	<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" maxmemory="128m"><arg line="-init -db 1"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" maxmemory="128m"><arg line="-db 2"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" maxmemory="128m"><arg line="-db 3 -out pe.html"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" maxmemory="128m"><arg line="-init -db 4"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" maxmemory="128m"><arg line="-db 5"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" maxmemory="128m"><arg line="-db 6"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" maxmemory="128m"><arg line="-db 7"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" maxmemory="128m"><arg line="-db 8"/></java>
		<java fork="true" classname="org.h2.test.bench.TestPerformance" classpathref="benchmark.drivers.path" 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>
	</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">
		<java classname="org.h2.tools.code.CodeSwitch" classpath="bin">
			<arg line="+JDK13 -JDK14 -JDK16 -AWT src/main/org/h2 -set ant-build.properties jdk 1.3"/>
		</java>
	</target>

	<target name="codeswitchJdk13" depends="codeswitchPrepare">
		<java classname="org.h2.tools.code.CodeSwitch" classpath="bin">
			<arg line="+JDK13 -JDK14 -JDK16 +AWT src/main/org/h2 -set ant-build.properties jdk 1.3"/>
		</java>
	</target>

	<target name="codeswitchJdk14" depends="codeswitchPrepare">
		<java classname="org.h2.tools.code.CodeSwitch" classpath="bin">
			<arg line="-JDK13 +JDK14 -JDK16 +AWT src/main/org/h2 -set ant-build.properties jdk 1.4"/>
		</java>
	</target>

	<target name="codeswitchJdk16" depends="codeswitchPrepare">
		<java classname="org.h2.tools.code.CodeSwitch" classpath="bin">
			<arg line="-JDK13 +JDK14 +JDK16 +AWT src/main/org/h2 -set ant-build.properties jdk 1.6"/>
		</java>
	</target>

	<target name="codeswitchJdk14Auto" if="codeswitch.14">
	    <antcall target="codeswitchJdk14" />
	</target>

	<target name="codeswitchJdk16Auto" if="codeswitch.16">
	    <antcall target="codeswitchJdk16" />
	</target>

	<target name="compileResources" depends="clean, codeswitchJdk14Auto, codeswitchJdk16Auto">
		<javac executable="${javac}" srcdir="src/main" destdir="bin" debug="true" includes="org/h2/util/Resources.java"/>
		<java classname="org.h2.util.Resources" classpath="bin"/>
		<delete>
			<fileset dir="bin" includes="org/h2/util/ResourceData.class"/>
		</delete>
		<javac 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 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 executable="${javac}" destdir="bin" debug="true">
		    <src path="src/main"/>
		    <src path="src/test"/>
			<classpath location="${path.servlet.jar}" />
			<include name="org/h2/server/web/WebServlet.java"/>
			<include name="org/h2/server/web/DbStarter.java"/>
			<include name="org/h2/test/unit/TestServlet.java"/>
		</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">
		<echo message="build-jdk:${java.specification.version} ant-build.properties/jdk:${jdk}"></echo>
		<javac executable="${javac}" destdir="bin" debug="true">
		    <src path="src/main"/>
	       	<src path="src/test"/>
	       	<src path="src/tools"/>
		    <exclude name="org/h2/fulltext/FullTextLucene.java"/>
			<exclude name="org/h2/server/web/WebServlet.java"/>
			<exclude name="org/h2/server/web/DbStarter.java"/>
			<exclude name="org/h2/test/unit/TestServlet.java"/>
	    </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="." dir="bin" fork="true">
			<arg line="-r org/h2"/>
		</java>
		<javac executable="${javac}" srcdir="bin" destdir="bin" debug="true" includes="org/h2/**"/>
    </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.CheckJavadoc" classpath="bin"/>
		<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="codeswitch.14">
			<and>
				<equals arg1="${java.specification.version}" arg2="1.4"/>
			    <equals arg1="${jdk}" arg2="1.6"/>
			</and>
	    </condition>
		<condition property="codeswitch.16">
			<and>
				<equals arg1="${java.specification.version}" arg2="1.6"/>
				<not>
				    <equals arg1="${jdk}" arg2="1.6"/>
				</not>
			</and>
	    </condition>
	</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 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 executable="${javac}" srcdir="src/main" destdir="bin" debug="false">
			<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 executable="${javac}" srcdir="src/main" destdir="bin" debug="true" includes="org/h2/util/StringUtils.java"/>
		<javac executable="${javac}" srcdir="src/test" destdir="bin" debug="true" includes="org/h2/test/bnf/*.java"/>
		<javac 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.*,org.h2.constant.*"
			doclet="org.h2.tools.doclet.Doclet"
			docletpath="bin"
		/>
		<copy todir="docs/javadoc">
			<fileset dir="src/docsrc/javadoc" includes="**/*"/>
		</copy>
	</target>

	<target name="javadocImpl" depends="javadoc">
		<mkdir dir="docs/javadocImpl"/>
		<javadoc
			sourcepath="src/main;src/test;src/tools"
			packagenames="org.h2.*"
		    excludepackagenames="org.h2.tools.doclet"
		    classpath="${path.lucene.jar};${path.servlet.jar}"
			destDir="docs/javadocImpl"
		/>
	    <!--
			doclet="org.h2.tools.doclet.Doclet"  docletpath="bin"
		    additionalparam="-J-Dh2.interfacesOnly=true -J-Dh2.destDir=docs/javadocImpl"
		-->
		<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="${java.specification.version}"/>
		</manifest>
	</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="." dir="bin">
			<arg line="codeCoverage"/>
		</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>