<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="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="compile" depends="switchSourceAuto, resources, downloadServletJar, downloadLuceneJar">
		<javac executable="${javac}" destdir="bin" debug="true" debuglevel="lines,source" >
			<classpath location="ext/servlet-api-2.4.jar" />
		    <classpath location="ext/lucene-core-2.2.0.jar" />
		    <src path="src/main"/>
	       	<src path="src/test"/>
	       	<src path="src/tools"/>
	    </javac>
		<copy todir="bin" overwrite="true">
			<fileset dir="src/main" includes="META-INF/services/*"/>
			<fileset dir="src/installer" includes="*.bat"/>
			<fileset dir="src/installer" includes="*.sh"/>
			<fileset dir="src/test" excludes="**/*.java,**/package.html"/>
		</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" debuglevel="lines,source" includes="org/h2/**"/>
    </target>

	<target name="docs" depends="clean, javadoc, compile">
		<copy todir="docs">
			<fileset dir="src/docsrc" includes="index.html"/>
		</copy>
		<java classname="org.h2.build.code.CheckJavadoc" classpath="bin"/>
		<java classname="org.h2.build.code.CheckTextFiles" classpath="bin"/>
		<java classname="org.h2.build.doc.GenerateDoc" classpath="bin"/>
		<java classname="org.h2.build.i18n.PrepareTranslation" classpath="bin"/>
		<java classname="org.h2.build.indexer.Indexer" classpath="bin"/>
		<java classname="org.h2.build.doc.MergeDocs" classpath="bin"/>
		<java classname="org.h2.build.doc.WebSite" classpath="bin"/>
		<java classname="org.h2.build.doc.LinkChecker" classpath="bin"/>
		<java classname="org.h2.build.doc.XMLChecker" classpath="bin"/>
		<java classname="org.h2.build.doc.SpellChecker" classpath="bin"/>
	</target>
    
    <target name="downloadServletJar" depends="init" unless="servlet.jar.present">
        <get dest="ext/servlet-api-2.4.jar"
            src="http://repo1.maven.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.jar" />
    </target>

    <target name="downloadLuceneJar" depends="init" unless="lucene.jar.present">
        <get dest="ext/lucene-core-2.2.0.jar"
            src="http://repo1.maven.org/maven2/org/apache/lucene/lucene-core/2.2.0/lucene-core-2.2.0.jar" />
    </target>

	<target name="init">
		<mkdir dir="ext"/>
		<available file="ext/servlet-api-2.4.jar" property="servlet.jar.present"/>
		<available file="ext/lucene-core-2.2.0.jar" property="lucene.jar.present"/>
	</target>

	<target name="jar" depends="compile, 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/build/**/*.*"/>
			<exclude name="org/h2/dev/**/*.*"/>
			<exclude name="org/h2/samples/**/*.*"/>
			<exclude name="org/h2/test/**/*.*"/>
			<exclude name="**/*.bat"/>
			<exclude name="**/*.sh"/>
			<exclude name="**/*.txt"/>
		</jar>
	</target>

	<target name="jarClient" depends="resources, manifest">
		<javac executable="${javac}" srcdir="src/main" destdir="bin" debug="true" debuglevel="lines,source">
			<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"/>
			<exclude name="**/*.sh"/>
		</jar>
	</target>

	<target name="jarDb" depends="resources, 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"/>
			<exclude name="**/*.sh"/>
		</jar>
	</target>

	<target name="javadoc">
		<javac executable="${javac}" srcdir="src/main" destdir="bin" debug="true" debuglevel="lines,source" includes="org/h2/util/StringUtils.java"/>
		<javac executable="${javac}" srcdir="src/test" destdir="bin" debug="true" debuglevel="lines,source" includes="org/h2/test/bnf/*.java"/>
		<javac executable="${javac}" srcdir="src/tools" destdir="bin" debug="true" debuglevel="lines,source" includes="org/h2/build/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.build.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"
		    classpath="${path.lucene.jar};${path.servlet.jar};${java.class.path}"
			packagenames="org.h2.*"
			doclet="org.h2.build.doclet.Doclet"  
		    docletpath="bin"
		    additionalparam="-J-Dh2.interfacesOnly=false -J-Dh2.destDir=docs/javadocImpl"
		/>
		<javadoc
			sourcepath="src/main;src/test;src/tools"
		    noindex="true"
			packagenames="org.h2.*"
		    excludepackagenames="org.h2.build.*,org.h2.dev.*"
		    classpath="${path.lucene.jar};${path.servlet.jar};${java.class.path}"
			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="resources" depends="clean">
	    <mkdir dir="bin/org/h2/util"/>
	    <zip destfile="bin/org/h2/util/data.zip" 
	        basedir="src/main" 
	        compress="false"
	        excludes="**/*.java,**/package.html,**/java.sql.Driver" />
	</target>

	<target name="switchSourcePrepare" depends="clean">
		<javac executable="${javac}" srcdir="src/tools" destdir="bin" debug="true" debuglevel="lines,source" includes="org/h2/build/code/SwitchSource.java"/>
	</target>

    <target name="switchSourceAndroid" depends="switchSourcePrepare">
		<java classname="org.h2.build.code.SwitchSource" classpath="bin">
			<arg line="-version 1.3 -AWT -dir src/main/org/h2"/>
		</java>
	</target>

	<target name="switchSourceAuto" depends="switchSourcePrepare">
		<java classname="org.h2.build.code.SwitchSource" classpath="bin">
			<arg line="-auto -dir src/main/org/h2"/>
		</java>
	</target>

	<target name="switchSourceJdk13" depends="switchSourcePrepare">
		<java classname="org.h2.build.code.SwitchSource" classpath="bin">
			<arg line="-version 1.3 +AWT -dir src/main/org/h2"/>
		</java>
	</target>

	<target name="switchSourceJdk14" depends="switchSourcePrepare">
		<java classname="org.h2.build.code.SwitchSource" classpath="bin">
			<arg line="-version 1.4 +AWT -dir src/main/org/h2"/>
		</java>
	</target>

	<target name="switchSourceJdk16" depends="switchSourcePrepare">
		<java classname="org.h2.build.code.SwitchSource" classpath="bin">
			<arg line="-version 1.6 +AWT -dir src/main/org/h2"/>
		</java>
	</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="jar">
		<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.*"/>
			<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>