<project name="tdb" basedir="." default="compile">
  <echo>os.name: ${os.name}</echo>
  <property environment="env"/>
  <!-- Eventually install.lib needs to be OS aware -->
  <property name="gsdl3home" value="${env.GSDL3SRCHOME}" />
  <property name="classdir" value="${basedir}/build/classes"/>
  <property name="installdir" value="${basedir}/linux"/>
  <property name="install.lib" value="${basedir}/linux/lib"/>
  <property name="jni.lib" value="${gsdl3home}/lib/jni"/>
  <property name="web.lib" value="${gsdl3home}/web/WEB-INF/lib"/>

  <path id="gsdl3jars">
    <fileset dir="${web.lib}" includes="**/*.jar" />
  </path>
  
  <target name="help">
    <echo>Targets:</echo>
    <echo />
    <echo> help      - this message</echo>
    <echo> compile   - configure and compile TDB libraries, binaries, and</echo>
    <echo>             drivers</echo>
    <echo> install   - add TDB support to Greenstone2/Greenstone3</echo>
    <echo> install-tdbjava - install just the TBDJava support into Greenstone3</echo>
    <echo> uninstall - remove TDB support from Greenstone2/Greenstone3</echo>
    <echo> clean     - uninstall and remove compiled libraries, binaries,</echo>
    <echo>             and drivers</echo>
    <echo />
  </target>

  <target name="compile">
    <echo message=" * Configure and compile TDB" />
    <exec executable="/bin/bash">
      <arg value="-c"/>
      <arg value="source setup.bash ; ./CASCADE-MAKE.sh"/>
    </exec>
    <!-- Compile TDBJava within Ant -->
    <mkdir dir="${classdir}" />
    <javac srcdir="src/java" destdir="${classdir}" classpathref="gsdl3jars"
	   includeantruntime="false" />
    <jar destfile="${install.lib}/TDBJava.jar" basedir="${classdir}">
      <manifest>
	<attribute name="Class-Path" value="gsdl3.jar log4j-1.2.8.jar" />
      </manifest>
    </jar>
  </target>

  <target name="clean" depends="uninstall">
    <exec executable="/bin/bash">
      <arg value="-c"/>
      <arg value="./CASCADE-MAKE.sh distclean"/>
    </exec>
    <delete dir="${installdir}"/>
  </target>

  <target name="install-tdbjava">
    <echo>Install the TDBJava files into ${jni.lib}</echo>
    <copy file="${install.lib}/TDBJava.jar" todir="${web.lib}"/>
    <copy file="${install.lib}/libTDBJava.so" todir="${jni.lib}"/>
    <copy file="${install.lib}/libtdb.so" todir="${jni.lib}"/>
  </target>

  <target name="install" depends="compile,install-tdbjava">
    <echo>Enable TDB support in Greenstone2 build library</echo>
    <exec executable="/bin/bash">
      <arg value="-c"/>
      <arg value="source setup.bash ; ./enable_tdb.sh "/>
    </exec>
    <echo>Reconfigure, recompile, and reinstall Greenstone2 build library"</echo>
    <subant>
      <fileset dir="${gsdl3home}" includes="build.xml" />
      <target name="configure-common-src" />
      <target name="compile-common-src" />
    </subant>
  </target>

  <target name="uninstall">
    <echo>Remove TDB support in Greenstone2 build library</echo>
    <exec executable="/bin/bash">
      <arg value="-c"/>
      <arg value="source setup.bash ; ./disable_tdb.sh "/>
    </exec>
    <echo>Reconfigure, recompile, and reinstall Greenstone2 build library"</echo>
    <subant>
      <fileset dir="${gsdl3home}" includes="build.xml" />
      <target name="configure-common-src" />
      <target name="compile-common-src" />
    </subant>
    <echo>Remove the TDBJava files from Greenstone3/lib/jni</echo>
    <delete file="${lib.jni}/TDBJava.jar" />
    <delete file="${lib.jni}/libTDBJava.so" />
    <delete file="${lib.jni}/libtdb.so" />
  </target>
</project>
