<?xml version="1.0"?>
<!-- 
	build.xml file for building sqljet without grandle.
	Origin: sqljet 1.0.4
	Modified for packaging v1.1.4 in Fedora by ismael@olea.org
-->
<project name="sqljet" default="deploy" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
  <property file="sqljet.build.properties"/>
  <property name="build.vcs.number.1" value="local"/>
  <property name="build.number" value="${sqljet.version.major}.${sqljet.version.minor}.${sqljet.version.micro}.b${build.vcs.number.1}"/>
  <property name="version.number" value="${sqljet.version.major}.${sqljet.version.minor}.${sqljet.version.micro}"/>
  <target name="init-build.number" if="build.vcs.number.1">
    <mkdir dir="build"/>
    <copy file="sqljet.build.properties" tofile="build/sqljet.build.properties"/>
    <replace file="build/sqljet.build.properties" token="local" value="${build.vcs.number.1}"/>
  </target>
  <target name="clean">
    <!-- First, create directories, which will be removed, 
			otherwise the removal may not be able to 
			if directories don't exist yet -->
    <mkdir dir="build"/>
    <mkdir dir="sqljet/bin"/>
    <mkdir dir="sqljet-test/bin"/>
    <mkdir dir="sqljet-examples/browser/bin"/>
    <delete verbose="false" includeemptydirs="true" failonerror="true">
      <fileset dir="build">
        <include name="**/**"/>
      </fileset>
    </delete>
    <delete verbose="false" includeemptydirs="true" failonerror="false">
      <fileset dir="sqljet/bin">
        <include name="**/**"/>
      </fileset>
      <fileset dir="sqljet-test/bin">
        <include name="**/**"/>
      </fileset>
      <fileset dir="sqljet-examples/browser/bin">
        <include name="**/**"/>
      </fileset>
      <fileset dir="sqljet-examples/simple/bin">
        <include name="**/**"/>
      </fileset>
    </delete>
  </target>
  <target name="antlr">
    <java classname="org.antlr.Tool" fork="true" failonerror="true">
      <arg value="-Xconversiontimeout"/>
      <arg value="100000"/>
      <arg value="-report"/>
      <arg value="-fo"/>
      <arg value="sqljet/src/main/antlr/org/tmatesoft/sqljet/core/internal/lang"/>
      <arg value="sqljet/src/main/antlr/org/tmatesoft/sqljet/core/internal/lang/Sql.g"/>
      <classpath>
        <pathelement location="/usr/share/java/antlr32/antlr-3.2.jar"/>
        <pathelement location="/usr/share/java/antlr32/antlr-runtime-3.2.jar"/>
        <pathelement location="/usr/share/java/stringtemplate4.jar"/>
        <pathelement path="${java.class.path}"/>
      </classpath>
    </java>
    <!-- add @supressWarning to generated classes -->
    <!-- ANTLR >= 3.4 already does that, build would fail with dupe annotation
    	<replace file="sqljet/src/org/tmatesoft/sqljet/core/internal/lang/SqlLexer.java" token="public class SqlLexer">
    		<replacevalue>@SuppressWarnings({"unused"})
public class SqlLexer</replacevalue>
    	</replace>
    	<replace file="sqljet/src/org/tmatesoft/sqljet/core/internal/lang/SqlParser.java" token="public class SqlParser">
    		<replacevalue>@SuppressWarnings({"unused", "unchecked"})
public class SqlParser</replacevalue>
    	</replace>
    	-->
  </target>
  <target name="compile" depends="antlr, manifest">
    <mkdir dir="sqljet/bin"/>
    <mkdir dir="sqljet-test/bin"/>
    <javac destdir="sqljet/bin" srcdir="sqljet/src/main/java" debug="true" verbose="no">
      <compilerarg value="-O"/>
      <classpath>
        <pathelement location="/usr/share/java/antlr32/antlr-runtime-3.2.jar"/>
      </classpath>
    </javac>
    <javac destdir="sqljet-test/bin" srcdir="sqljet/src/test/" debug="true" verbose="no">
      <compilerarg value="-O"/>
      <classpath path="sqljet/bin"/>
      <classpath>
        <fileset dir="sqljet-test/bin" includes="*.jar"/>
        <pathelement location="/usr/share/java/junit.jar"/>
        <pathelement location="/usr/share/java/easymock2.jar"/>
        <pathelement location="/usr/share/java/antlr32/antlr-runtime-3.2.jar"/>
      </classpath>
    </javac>
  </target>
  <target name="sqljet-browser" depends="compile, manifest" description="compiles sqljet browser">
    <mkdir dir="sqljet-browser/bin"/>
    <javac destdir="sqljet-browser/bin" srcdir="sqljet-browser/src"  verbose="no" debug="true">
      <compilerarg value="-O"/>
      <classpath>
        <pathelement location="/usr/share/java/netbeans/swing-outline.jar"/>
      </classpath>
      <classpath path="sqljet/bin"/>
    </javac>
  </target>
  <target name="generate-key">
    <mkdir dir="build"/>
    <genkey alias="tmate" storepass="secret" keystore="build/keystore">
      <dname>
        <param name="CN" value="TMate Software"/>
        <param name="C" value="CZ"/>
      </dname>
    </genkey>
  </target>
  <target name="javadoc" description="Build the javadocs for the source code.">
    <mkdir dir="build"/>
    <mkdir dir="build/javadoc"/>
    <path id="doc.classpath">
      <pathelement location="/usr/share/java/antlr32/antlr-runtime-3.2.jar"/>
    </path>
    <javadoc destdir="build/javadoc" packagenames="org.tmatesoft.sqljet" 
	sourcepath="sqljet/src"  verbose="no" classpathref="doc.classpath" 
	overview="sqljet/src/main/javadoc/overview.html" failonerror="false" additionalparam="-Xdoclint:none">
      <packageset dir="sqljet/src/main/java" defaultexcludes="yes">
        <exclude name="**/test/**"/>
      </packageset>
      <header><![CDATA[<a target="_top" href="http://sqljet.com/">SQLJet&nbsp;Home<a/>]]></header>
      <bottom><![CDATA[Copyright &#169; 2009-2010 TMate Software Ltd. All Rights Reserved.]]></bottom>
    </javadoc>
    <zip destfile="build/sqljet.${build.number}.doc.zip">
      <zipfileset dir="build/javadoc" prefix="javadoc">
        <exclude name="**/.*"/>
        <exclude name=".*"/>
      </zipfileset>
    </zip>
  </target>
  <target name="manifest">
    <manifest file="manifest.mf">
      <attribute name="Built-By" value="TMate Software"/>
      <attribute name="Main-Class" value="org.tmatesoft.sqljet.browser.DBBrowser"/>
      <attribute name="Implementation-Title" value="SQLJet Database Browser"/>
      <attribute name="Implementation-Version" value="${build.number}"/>
      <attribute name="Implementation-Vendor" value="TMate Software"/>
    </manifest>
  </target>
  <target name="pom">
    <replace file="pom.xml" token="%VSQLJET%" value="${build.number}"/>
    <replace file="pom.xml" token="%VANTLR%" value="${antlr.version}"/>
  </target>
  <target name="osgi" depends="manifest">
    <mkdir dir="sqljet-osgi/bin"/>
    <copy file="manifest.mf" tofile="build/osgi-manifest"/>
    <replace file="build/osgi-manifest" token="%sqljet.version%" value="${build.number}"/>
    <replace file="build/osgi-manifest" token="%antlr.version%" value="${antlr.version}"/>
    <javac destdir="sqljet-osgi/bin" srcdir="sqljet/src/test/" debug="true">
      <compilerarg value="-O"/>
      <classpath path="sqljet/bin"/>
      <classpath>
        <fileset dir="sqljet-test/bin" includes="*.jar"/>
        <pathelement location="/usr/share/java/junit.jar"/>
        <pathelement location="/usr/share/java/easymock2.jar"/>
        <pathelement location="/usr/share/java/antlr32/antlr-runtime-3.2.jar"/>
      </classpath>
    </javac>
    <jar destfile="build/org.tmatesoft.sqljet_${build.number}.jar" manifest="build/osgi-manifest">
      <fileset dir="sqljet-osgi/bin">
        <include name="**/**"/>
      </fileset>
      <fileset dir="build">
        <include name="sqljet.build.properties"/>
      </fileset>
      <fileset dir="/usr/share/java/">
        <include name="antlr3-runtime.jar"/>
        <include name="ANTLR-LICENSE"/>
      </fileset>
      <zipfileset dir="" prefix="META-INF">
        <include name="CHANGES.txt"/>
        <include name="README.txt"/>
        <include name="LICENSE.txt"/>
      </zipfileset>
    </jar>
    <delete file="build/osgi-manifest"/>
  </target>
  <target name="tarball">
    <tar destfile="build/sqljet.${build.number}.tar.gz" compression="gzip">
      <fileset dir="">
        <include name="**/**"/>
        <exclude name="**/.svn"/>
        <exclude name="build/**"/>
        <exclude name="sqljet/bin/**"/>
        <exclude name="sqljet-test/bin/**"/>
        <exclude name="sqljet-examples/simple/bin/**"/>
        <exclude name="sqljet-examples/browser/bin/**"/>
      </fileset>
    </tar>
  </target>
  <target name="deploy" depends="clean, init-build.number, compile, sqljet-browser, generate-key, osgi, tarball, javadoc">
    <mkdir dir="build"/>
    <jar destfile="build/sqljet.${build.number}.jar">
      <fileset dir="sqljet/bin">
        <include name="**/**"/>
      </fileset>
      <fileset dir="build">
        <include name="sqljet.build.properties"/>
      </fileset>
    </jar>
    <zip destfile="build/sqljet.${build.number}.src.zip">
      <zipfileset dir="sqljet/src" prefix="src">
        <exclude name="**/.*"/>
        <exclude name=".*"/>
      </zipfileset>
      <zipfileset dir="">
        <include name="COPYING"/>
        <include name="README.txt"/>
        <include name="CHANGES"/>
      </zipfileset>
    </zip>
    <!-- examples (browser) -->
    <jar destfile="build/sqljet-browser.${build.number}.jar" manifest="manifest.mf">
      <fileset dir="sqljet-examples/browser/bin">
        <include name="**/**"/>
      </fileset>
      <zipfileset src="sqljet-examples/browser/lib/org-netbeans-swing-outline.jar">
        <include name="**/**"/>
      </zipfileset>
    </jar>
    <delete file="build/MANIFEST.MF"/>
    <mkdir dir="build/lib"/>
    <copy file="lib/antlr-runtime-${antlr.version}.jar" tofile="build/lib/antlr-runtime-${antlr.version}-${build.number}.jar"/>
    <copy file="lib/ANTLR-LICENSE" tofile="build/lib/ANTLR-LICENSE"/>
    <copy todir="build">
      <fileset dir="sqljet-examples/browser">
        <include name="browser.jnlp"/>
      </fileset>
    </copy>
    <replace token="%version%" file="build/browser.jnlp" value="${build.number}">
		</replace>
    <signjar alias="tmate" keystore="build/keystore" storepass="secret" lazy="true">
      <path>
        <fileset dir="build">
          <exclude name="org.tmatesoft.sqljet_${build.number}.jar"/>
          <include name="**/*.jar"/>
        </fileset>
      </path>
    </signjar>
    <zip destfile="build/sqljet-browser.${build.number}.src.zip">
      <zipfileset dir="sqljet-examples/browser/src" prefix="src">
        <exclude name="**/.*"/>
        <exclude name=".*"/>
      </zipfileset>
      <zipfileset dir="">
        <include name="COPYING"/>
      </zipfileset>
    </zip>
    <zip destfile="build/sqljet-browser.${build.number}.jnlp.zip">
      <zipfileset dir="build" prefix="browser">
        <include name="sqljet.${build.number}.jar"/>
        <include name="sqljet-browser.${build.number}.jar"/>
      </zipfileset>
      <zipfileset dir="build/lib" prefix="browser">
        <include name="**/**"/>
      </zipfileset>
      <zipfileset dir="build" prefix="">
        <include name="browser.jnlp"/>
      </zipfileset>
    </zip>
    <zip destfile="build/sqljet.${build.number}.zip">
      <zipfileset dir="build" prefix="sqljet.${build.number}">
        <include name="sqljet.${build.number}.jar"/>
        <include name="sqljet.${build.number}.src.zip"/>
        <include name="sqljet.${build.number}.doc.zip"/>
        <include name="sqljet-browser.${build.number}.jar"/>
        <include name="sqljet-browser.${build.number}.src.zip"/>
      </zipfileset>
      <zipfileset dir="lib" prefix="sqljet.${build.number}">
        <include name="**/**"/>
      </zipfileset>
      <zipfileset dir="" prefix="sqljet.${build.number}">
        <include name="COPYING"/>
        <include name="README.txt"/>
        <include name="CHANGES"/>
      </zipfileset>
    </zip>
    <delete file="build/sqljet.${build.number}.src.zip"/>
    <delete file="build/sqljet.${build.number}.jar"/>
    <delete file="build/sqljet-browser.${build.number}.src.zip"/>
    <delete file="build/sqljet-browser.${build.number}.jar"/>
    <delete file="build/sqljet.build.properties"/>
    <delete file="build/keystore"/>
    <delete file="build/browser.jnlp"/>
    <delete dir="build/lib" includeemptydirs="true">
      <include name="**/**"/>
    </delete>
    <delete file="build/sqljet.${build.number}.doc.zip">
		</delete>
    <delete dir="build/javadoc" includeemptydirs="true">
      <include name="**/**"/>
    </delete>
  </target>
  <target name="deploy-maven" depends="clean,compile">
    <condition property="repo.path" value="${maven.repo.path}" else="/home/rsync/m2repo">
      <isset property="maven.repo.path"/>
    </condition>
    <path id="maven-ant-tasks.classpath" path="tools/maven-ant-tasks-2.0.10.jar"/>
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath"/>
    <mkdir dir="build/maven-deploy"/>
    <mkdir dir="build/maven-deploy/org.tmatesoft.sqljet-${version.number}"/>
    <jar destfile="build/maven-deploy/org.tmatesoft.sqljet-${version.number}/sqljet-${version.number}.jar">
      <fileset dir="sqljet/bin">
        <include name="**/**"/>
      </fileset>
      <fileset dir="build">
        <include name="sqljet.build.properties"/>
      </fileset>
      <fileset dir="">
        <include name="COPYING"/>
      </fileset>
    </jar>
    <jar destfile="build/maven-deploy/org.tmatesoft.sqljet-${version.number}/sqljet-${version.number}-sources.jar">
      <fileset dir="">
        <include name="README.txt"/>
        <include name="CHANGES"/>
        <include name="COPYING"/>
      </fileset>
      <fileset dir="sqljet/src">
        <exclude name=".svn"/>
        <exclude name=".svn/**"/>
      </fileset>
      <fileset dir="build">
        <include name="sqljet.build.properties"/>
      </fileset>
    </jar>
    <copy file="pom.xml" tofile="build/maven-deploy/org.tmatesoft.sqljet-${version.number}/pom.xml"/>
    <replace token="%sqljet.version%" value="${version.number}" file="build/maven-deploy/org.tmatesoft.sqljet-${version.number}/pom.xml"/>
    <replace token="%antlr.version%" value="${antlr.version}" file="build/maven-deploy/org.tmatesoft.sqljet-${version.number}/pom.xml"/>
    <artifact:pom id="sqljet.pom" file="build/maven-deploy/org.tmatesoft.sqljet-${version.number}/pom.xml"/>
    <mkdir dir="build/maven-deploy/m2repo"/>
    <artifact:deploy file="build/maven-deploy/org.tmatesoft.sqljet-${version.number}/sqljet-${version.number}.jar">
      <attach file="build/maven-deploy/org.tmatesoft.sqljet-${version.number}/sqljet-${version.number}-sources.jar" classifier="sources"/>
      <remoteRepository url="file://${repo.path}"/>
      <pom refid="sqljet.pom"/>
    </artifact:deploy>
    <delete dir="build/maven-deploy">
      <include name="org.tmatesoft.sqljet-${version.number}/**"/>
    </delete>
    <delete dir="build/maven-deploy/org.tmatesoft.sqljet-${version.number}"/>
  </target>
  <target name="test" depends="compile">
    <echoproperties prefix="os."/>
    <echoproperties prefix="java.vm."/>

    <junit>
      <jvmarg value="-ea"/>
      <jvmarg value="-server"/>
      <formatter type="plain" usefile="false"/>
      <classpath path="sqljet/bin"/>
      <classpath path="sqljet-test/bin"/>
      <classpath>
        <pathelement location="/usr/share/java/junit.jar"/>
        <pathelement location="/usr/share/java/easymock2.jar"/>
        <pathelement location="/usr/share/java/antlr32/antlr-runtime-3.2.jar"/>
      </classpath>

      <batchtest fork="yes" todir="sqljet-test/bin">
        <fileset dir="sqljet/src">
          <include name="**/*Test*.java"/>
          <exclude name="**/*Abstract*.java"/>
          <exclude name="**/*Mock*.java"/>
        </fileset>
      </batchtest>
      <sysproperty key="SQLJET_FILE_LOG" value="false"/>
      <sysproperty key="SQLJET_FILE_PERFORMANCE_LOG" value="false"/>
      <sysproperty key="SQLJET_PAGER_LOG" value="false"/>
      <sysproperty key="SQLJET_TESTS_LOGGING" value="false"/>
      <sysproperty key="SqlJetBtreeTableTest.DELETE_COPY" value="true"/>
      <sysproperty key="SqlJetBtreeTableTest.REPEATS_COUNT" value="100"/>
    </junit>
  </target>
  <target name="test-stress" depends="deploy">
    <echoproperties prefix="os."/>
    <echoproperties prefix="java.vm."/>
    <junit>
      <jvmarg value="-ea"/>
      <jvmarg value="-server"/>
      <formatter type="plain" usefile="false"/>
      <classpath path="sqljet/bin"/>
      <classpath path="sqljet-test/bin"/>
      <classpath>
        <fileset dir="sqljet-test/lib" includes="*.jar"/>
        <fileset dir="lib" includes="*.jar"/>
      </classpath>
      <batchtest fork="yes" todir="sqljet-test/bin">
        <fileset dir="sqljet-test/src">
          <include name="**/*Stress*.java"/>
          <exclude name="**/*Abstract*.java"/>
          <exclude name="**/*Mock*.java"/>
        </fileset>
      </batchtest>
      <sysproperty key="SQLJET_FILE_LOG" value="false"/>
      <sysproperty key="SQLJET_FILE_PERFORMANCE_LOG" value="false"/>
      <sysproperty key="SQLJET_PAGER_LOG" value="false"/>
      <sysproperty key="SQLJET_TESTS_LOGGING" value="false"/>
      <sysproperty key="SqlJetBtreeTableTest.DELETE_COPY" value="true"/>
      <sysproperty key="SqlJetBtreeTableTest.REPEATS_COUNT" value="100"/>
    </junit>
  </target>
  <target name="jars" depends="clean, manifest, compile">
    <mkdir dir="build"/>
    <antcall target="init-build.number"/>
    <jar destfile="build/sqljet.jar" manifest="manifest.mf">
      <fileset dir="sqljet/bin">
        <include name="**/**"/>
      </fileset>
      <fileset dir="build">
        <include name="sqljet.build.properties"/>
      </fileset>
    </jar>
  </target>
</project>
