<project name="dnsjava" default="all" basedir=".">
    <property name="build_dir" value="${basedir}"/>
    <property name="src_dir" value="${basedir}"/>
    <property name="doc_dir" value="${basedir}/doc"/>
    <property name="dist_dir" value="${basedir}"/>
    <property name="tests_dir" value="${basedir}/tests"/>
    <property name="reports_dir" value="${basedir}/reports"/>
    <property name="version" value="2.1.5"/>
    <property name="jarname" value="dnsjava-${version}.jar"/>
    <property name="zipname" value="dnsjava-${version}.zip"/>
    <property name="targzname" value="dnsjava-${version}.tar.gz"/>

    <property name="j2se.javadoc"
      value="http://download.oracle.com/javase/1.4.2/docs/api/"/>

    <target name="all" description="Compile and Jar" depends="jar">
    </target>

    <target name="compile" description="Compile everything">
	<javac destdir="${build_dir}" debug="true" target="1.4" source="1.4">
	    <src path="${src_dir}"/>
	    <exclude name="tests/**"/>
	</javac>
    </target>

    <target name="spi" description="Compile the Name Service Provider code">
	<javac destdir="${build_dir}" debug="true" target="1.4" source="1.4">
	    <src path="${src_dir}"/>
	    <include name="org/xbill/DNS/spi/*.java"/>
	</javac>
    </target>

    <target name="jar" description="Makes dnsjava.jar" depends="compile">
	<jar jarfile="${dist_dir}/${jarname}"
		basedir="${build_dir}" includes="**/*.class **/*.properties">
	    <exclude name="org/xbill/DNS/tests/*.class"/>
	    <exclude name="tests/**"/>
	    <manifest>
		<attribute name="Implementation-Title" value="dnsjava"/>
		<attribute name="Implementation-Version" value="${version}"/>
	    </manifest>
	    <metainf dir="org/xbill/DNS/spi/">
		<include name="services/*"/>
	    </metainf>
	</jar>
    </target>
	
	<target name="bundle" description="Creates an OSGi bundle" depends="jar">
		<get src="http://www.aqute.biz/repo/biz/aQute/bnd/0.0.384/bnd-0.0.384.jar"
			dest="${build_dir}/bnd.jar"/>
		<taskdef resource="aQute/bnd/ant/taskdef.properties"
			classpath="${build_dir}/bnd.jar"/> 
		<echo file="${dist_dir}/dnsjava-${version}.bnd" append="false">
			Bundle-Version: ${version}
			Bundle-Name: dnsjava is an implementation of DNS in Java
			Bundle-SymbolicName: org.xbill.dns
			Export-Package: org.xbill.DNS;version=${version},org.xbill.DNS.spi;version=${version},org.xbill.DNS.utils;version=${version},org.xbill.DNS.windows;version=${version}
			Bundle-Vendor: dnsjava.org
			Bundle-RequiredExecutionEnvironment: J2SE-1.4
			Import-Package: !org.xbill.DNS*,!sun.*,*
		</echo>
		<bndwrap
			definitions="${dist_dir}"
			jars="${dist_dir}/${jarname}"
			output="${dist_dir}/org.xbill.dns_${version}.jar"/>
		<delete file="${dist_dir}/dnsjava-${version}.bnd"/>
	</target>

    <target name="clean" description="Remove old class files">
	<delete>
	    <fileset dir="${build_dir}" includes="**/*.class"/>
	    <fileset dir="${build_dir}" includes="*.bnd *.jar *.zip *.tar.gz jcoverage.ser"/>
	</delete>
    </target>

    <target name="docs" description="Makes Javadoc documentation">
	<javadoc destdir="${doc_dir}"
		 sourcepath="${src_dir}"
		 packagenames="org.xbill.DNS,org.xbill.DNS.utils,org.xbill.DNS.spi"
		 windowtitle="dnsjava documentation">

	    <link href="${j2se.javadoc}"/>
	</javadoc>
    </target>

    <target name="docsclean" description="Remove old documentation">
	<delete dir="${doc_dir}"/>
    </target>

    <target name="reportsclean" description="Remove old reports">
	<delete dir="${reports_dir}"/>
    </target>

    <property name="source_excludes"
	      value="**/*.class,${jarname},${zipname},${targzname}" />

    <target name="sourcedist" description="Builds source distributions"
	    depends="compile,docs">
	<zip destfile="${dist_dir}/${zipname}">
	    <zipfileset dir="${src_dir}" prefix="dnsjava-${version}"
			excludes="${source_excludes}" />
	</zip>
	<tar destfile="${dist_dir}/${targzname}" compression="gzip">
	    <tarfileset dir="${src_dir}" prefix="dnsjava-${version}"
			excludes="${source_excludes}" />
	</tar>
    </target>

    <target name="compile_tests" depends="compile">
	<javac destdir="${tests_dir}" debug="true" target="1.4" source="1.4">
	    <src path="${tests_dir}"/>
	    <exclude name="org/xbill/DNS/DNSSECWithLunaProviderTest**"/>
	</javac>
    </target>

    <target name="run_tests">
      <path id="tests_classpath">
	<path location="${basedir}"/>
	<path location="${tests_dir}"/>
      </path>
      <antcall target="_run_tests">
	<param name="classpath_name" value="tests_classpath"/>
      </antcall>
    </target>

    <target name="_run_tests" depends="compile_tests">
      <junit fork="yes" forkmode="perTest" dir="${basedir}"
             haltonerror="on" haltonfailure="on">
	<classpath>
          <path location="${basedir}"/>
          <path location="${tests_dir}"/>
	</classpath>
	<formatter type="plain" usefile="no"/>
	<test name="${testcase}" if="testcase"/>
	<batchtest unless="testcase">
	  <fileset dir="${tests_dir}">
	  <include name="**/*Test.class"/>
	</fileset>
	</batchtest>
      </junit>
    </target>
    
    <target name="coverage_report" depends="clean,compile_tests" if="jcoverage_dir">
      <taskdef resource="tasks.properties">
	<classpath>
		<path location="${jcoverage_dir}/jcoverage.jar"/>
		<path location="${jcoverage_dir}/lib/log4j/1.2.8/*.jar"/>
		<path location="${jcoverage_dir}/log4j.jar"/>
		<path location="${jcoverage_dir}/bcel.jar"/>
		<path location="${jcoverage_dir}/oro.jar"/>
		<path location="${jcoverage_dir}/gnu.getopt.jar"/>
	</classpath>
      </taskdef>
      <instrument todir="${basedir}/iclasses">
	<fileset dir="${basedir}">
	  <include name="**/*.class"/>
	  <exclude name="iclasses/**"/>
	  <exclude name="tests/**"/>
	</fileset>
	<ignore regex="org.xbill.DNS.Tokenizer$*"/>
      </instrument>
      <path id="itests_classpath">
	<path location="${basedir}/iclasses"/>
	<path location="${tests_dir}"/>
	<path location="${jcoverage_dir}/jcoverage.jar"/>
	<path location="${jcoverage_dir}/lib/log4j/1.2.8/*.jar"/>
	<path location="${jcoverage_dir}/log4j.jar"/>
	<path location="${jcoverage_dir}/bcel.jar"/>
	<path location="${jcoverage_dir}/oro.jar"/>
	<path location="${jcoverage_dir}/gnu.getopt.jar"/>
      </path>
      <antcall target="_run_tests">
	<param name="classpath_name" value="itests_classpath"/>
      </antcall>
      <report srcdir="${basedir}" destdir="${reports_dir}/coverage"/>
      <delete dir="${basedir}/iclasses"/>
    </target>

</project>
