<?xml version="1.0"?>
<project name="MixConfig" default="dist" basedir=".">
  <description>
    Build file for the MixConfig Tool
  </description>
	
  <taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask"/>
	
  <!-- Set global properties for this build -->
  <property name="src.dir" location="src"/>
  <property name="bin.dir" location="bin"/>
  <property name="dist.dir" location="dist"/>
  <property name="lib.dir" location="lib"/>	

  <property name="build.compiler" value="javac1.6"/>
  <!-- JonDo locations -->
  <property name="jondo.dir" location="${lib.dir}/jondo"/>
  <property name="jondo.bin.dir" location="${jondo.dir}/build"/>	
 
  <!-- URLs -->
  <property name="bcprov.url" value="http://www.bouncycastle.org/download/bcprov-jdk16-139.jar"/>
  <property name="bcpg.url" value="http://www.bouncycastle.org/download/bcpg-jdk16-139.jar"/>
  <property name="bclight.url" value="http://anon.inf.tu-dresden.de/develop/BouncyCastleLightForMixConfig.jar"/>
	
  <target name="init-jondo">
  	<mkdir dir="${lib.dir}"/>
  	<!-- Check out a JonDo working copy from the head revision of the JonDos public repository -->
  	<svn>
  		<checkout url="https://svn.jondos.de/svnpub/JonDonym/trunk" destPath="${lib.dir}/jondo"/>
  	</svn>
  	
  	<!-- Call the compile target on jondo -->
    <ant antfile="${jondo.dir}/build.xml" dir="${jondo.dir}" target="jar-full">
  		<property name="build.compile.version" value="${java.specification.version}"/>
    </ant>
  </target>
	
  <target name="init-libs">
    <mkdir dir="${lib.dir}"/>
  	<!-- Get external libraries via HTTP -->
  	<get src="${bclight.url}" dest="${lib.dir}/bclight.jar" usetimestamp="true" ignoreerrors="true" verbose="true"/>
  	<!-- get src="${bcprov.url}" dest="${lib.dir}/bcprov.jar" usetimestamp="true" ignoreerrors="true" verbose="true"/ -->
  	<!-- get src="${bcpg.url}" dest="${lib.dir}/bcpg.jar" usetimestamp="true" ignoreerrors="true" verbose="true"/ -->
  	
  	<available file="${lib.dir}/bclight.jar" property="bouncycastle.available"/>
  	<!-- available file="${lib.dir}/bcprov.jar" property="bcprov.available"/ -->
  	<!-- available file="${lib.dir}/bcpg.jar" property="bcpg.available"/ -->
  	
 	<fail unless="bouncycastle.available" message="Bouncycastle library is not available"/>
 	<!-- fail unless="bcprov.available" message="Bouncycastle 'bcprov' library is not available"/ -->
 	<!-- fail unless="bcpg.available" message="Bouncycastle 'bcpg' library is not available"/ -->
  </target>
	
  <target name="init-classpath" depends="init-jondo, init-libs">
    <!-- Init the classpath for compile-->	
    <path id="classpath">
      <pathelement location="${bin.dir}"/>
      <pathelement location="${jondo.bin.dir}"/>      
      <!-- Add all *.jars located in ${lib.dir}-->
      <fileset dir="${lib.dir}" includes="*.jar"/>
    </path>  	
  </target>	
	
  <target name="compile" depends="init-classpath" description="Compile the source" >
    <!-- Create the build directory structure -->
    <mkdir dir="${bin.dir}"/>
  	<!-- Compile the java code from ${src.dir} into ${bin.dir} -->
    <javac srcdir="${src.dir}" destdir="${bin.dir}" target="1.5" debug="true" debuglevel="lines,source">
      <classpath refid="classpath"/>
    </javac>
  </target>

  <target name="dist" depends="compile" description="Generate the distribution">
    <!-- Create the distribution directory -->
    <mkdir dir="${dist.dir}"/>
    <!-- Create the time stamp -->
    <tstamp/>
  	<!-- Put everything from ${bin.dir} into a jar-file -->
    <jar destfile="${dist.dir}/MixConfig-${DSTAMP}.jar" basedir="${bin.dir}" manifest="MANIFEST.MF">
      <!-- MixConfig classes, images, help etc. -->
      <fileset dir="." includes="images/ help/ certificates/"/>
	  <fileset dir="src" includes="MixConfigMessages_en.properties"/>
      <fileset dir="src" includes="MixConfigMessages_de.properties"/>
      <!-- JonDo classes -->
      <fileset dir="${lib.dir}/jondo/build" includes="**/res/*"/>
      <fileset dir="${lib.dir}/jondo/build" includes="anon/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="anon/util/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="anon/crypto/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="anon/pay/xml/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="anon/pay/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="anon/infoservice/*.class"/>
      
      <fileset dir="${lib.dir}/jondo/build" includes="anon/terms/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="anon/terms/template/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="gui/**/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="logging/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="platform/*.class"/>
      <fileset dir="${lib.dir}/jondo/build" includes="platform/*.class"/>
      <!-- External library extracted classes from *.jars -->
      <zipfileset src="${lib.dir}/bclight.jar" includes="**/*.class"/>
      <!-- zipfileset src="${lib.dir}/bcprov.jar" includes="**/*.class"/ -->
      <!-- zipfileset src="${lib.dir}/bcpg.jar" includes="**/*.class"/ -->
    </jar>
  </target>

  <target name="dist-genjar" depends="compile" description="Generate the distribution using genjar">
    <!-- Create the distribution directory -->
	<mkdir dir="${dist.dir}"/>
	<!-- Use genjar to create a jar-file of reduced size -->
	<!-- FIXME: This is currently not yet working! -->
    <taskdef resource="genjar.properties"/>
	<genjar jarfile="${dist.dir}/MixConfig-${DSTAMP}.jar">
	  <!-- Specify the main class only? -->	
	  <class>
	  	<fileset dir="${bin.dir}">
	  	  <include name="mixconfig/MixConfig.class"/>
	  	</fileset>
	  </class>		
	  <!-- How to specify this correctly? -->	
	  <classpath refid="classpath"/>		
	  <!-- Additional resources -->
	  <resource>	
	    <!-- MixConfig classes, images, help etc. -->
	    <fileset dir="." includes="images/ help/"/>
		<fileset dir="${src.dir}" includes="MixConfigMessages_en.properties"/>
      </resource>	  
	</genjar>
  </target>	
		
  <target name="dist-clean" description="Remove the dist-directory" >
    <!-- Delete only the ${dist.dir} directory tree -->
    <delete dir="${dist.dir}"/>
  </target>

  <target name="compile-clean" description="Remove the bin-directory" >
    <!-- Delete only the ${bin.dir} directory tree -->
	<delete dir="${bin.dir}"/>
  </target>

  <target name="lib-clean" description="Remove the lib-directory" >
    <!-- Delete only the ${lib.dir} directory tree -->
	<delete dir="${lib.dir}"/>
  </target>	
	
  <target name="clean" depends="dist-clean, compile-clean, lib-clean" description="Clean up everything"/>
</project>