<?xml version="1.0" encoding="utf-8"?>
<project default="buildAndTest">
	<description>Buildfile for this project</description>
	
	<!-- Timestamp format definition -->
	<tstamp>
		<format property="ISO-TODAY" pattern="yyyy-MM-dd"/>
	</tstamp>
	
	<!-- ================================================================== -->
	<!-- Common properties                                                  -->
	<!-- ================================================================== -->

	<property name="release-name" value="Braille Utils"/>
	<property name="release-version" value="snapshot"/>
	<property name="repository-revision" value="N/A"/> 
	
	<property name="ant-build.dir" value="${basedir}/ant-build"/>
	<property name="ant-output.dir" value="${ant-build.dir}/output"/>
	<property name="dist.dir" value="${ant-output.dir}/dist"/>
	<property name="lib.dir" value="${basedir}/lib"/>
	
	<property name="src.dir" value="${basedir}/src"/>
	<property name="test-src.dir" value="${basedir}/tests"/>
	<property name="catalog-src.dir" value="${basedir}/catalog"/>
	
	<property name="build.dir" value="${ant-output.dir}/build.temp"/>
	<property name="test-build.dir" value="${ant-output.dir}/test-build.temp"/>
	<property name="catalog-build.dir" value="${ant-output.dir}/catalog-build.temp"/>
	
	<property name="test.report.dir" value="${ant-output.dir}/test-report"/>
	<property name="test.report-temp.dir" value="${ant-output.dir}/test-report.temp"/>
	<property name="javadoc.dir" value="${ant-output.dir}/docs/javadoc"/>
	<property name="javadoc-catalog.dir" value="${ant-output.dir}/docs/catalog-javadoc"/>

	<property name="javac.source" value="1.5"/>
	<property name="javac.target" value="1.5"/>
	<property name="useExternalJarsOnLinux" value="false"/>

	<!--
		Core jars for Braille Utils
	-->
	<property name="core-jar.name" value="brailleUtils-core.jar"/>
	<property name="catalog-jar.name" value="brailleUtils-catalog.jar"/>

	<property name="braille.jar" value="${dist.dir}/${core-jar.name}"/>
	<property name="catalog.jar" value="${dist.dir}/${catalog-jar.name}"/>
	
	<!--
		UI jar for Braille Utils
	 -->
	<property name="brailleUtils.jar" value="${dist.dir}/brailleUtils-ui.jar"/>

	<!--
		Standalone distribution for Braille Utils
	 -->	
	<property name="distribution.zip" value="${dist.dir}/brailleUtils-${release-version}.zip"/>
	<property name="javadoc.zip" value="${dist.dir}/brailleUtils-${release-version}-javadoc.zip"/>

	<patternset id="braille-jar.patternset">
		<include name="org/daisy/braille/**/*.*"/>
		<include name="org/daisy/cli/**/*.*"/>
		<include name="org/daisy/factory/**/*.*"/>
		<include name="org/daisy/paper/**/*.*"/>
		<include name="org/daisy/printing/**/*.*"/>
		<include name="org/daisy/validator/**/*.*"/>
		<include name="META-INF/**/*.*"/>
		<exclude name="org/daisy/braille/ui/**/*.*"/>
	</patternset>

	<patternset id="braille-ui-jar.patternset">
		<include name="org/daisy/braille/ui/**/*.*"/>
	</patternset>

	<patternset id="catalog-jar.patternset">
		<include name="**/*.*"/>
		<exclude name="**/doc-files/**/*.*"/>
	</patternset>

	<!-- ================================================================== -->
	<!-- Private targets                                                    -->
	<!-- ================================================================== -->

        <!-- - - - - - - - - - - - - - - - - -
          target: setClasspath
         - - - - - - - - - - - - - - - - - -->

        <target name="setClasspath" depends="detectOS, debianProperties">

            <property name="file.reference.saxon"      value="lib/saxon8.jar"/>
            <property name="file.reference.stax-api"   value="lib/stax-api-1.0.1.jar"/>
            <property name="file.reference.wstx"       value="lib/wstx-lgpl-3.2.8.jar"/>
            <property name="file.reference.jing"       value="lib/jing.jar"/>
            <property name="file.reference.xercesImpl" value="lib/xercesImpl.jar"/>
            <property name="file.reference.xml-apis"   value="lib/xml-apis.jar"/>
            <property name="file.reference.isorelax"   value="lib/isorelax.jar"/>
			<property name="file.reference.junit"      value="lib/junit-4.7.jar"/>

            <path id="core.classpath">
                <pathelement location="${file.reference.saxon}"/>
                <pathelement location="${file.reference.stax-api}"/>
                <pathelement location="${file.reference.wstx}"/>
                <pathelement location="${file.reference.jing}"/>
                <pathelement location="${file.reference.xercesImpl}"/>
                <pathelement location="${file.reference.xml-apis}"/>
                <pathelement location="${file.reference.isorelax}"/>
            </path>
			
			<path id="catalog.classpath">
				<path refid="core.classpath"/>
				<fileset dir="${dist.dir}">
                    <include name="${core-jar.name}"/>
                </fileset>
            </path>
			
			<path id="test.classpath">
			    <path refid="core.classpath"/>
				<pathelement location="${file.reference.junit}"/>
                <fileset dir="${dist.dir}">
                    <include name="${core-jar.name}"/>
                    <include name="${catalog-jar.name}"/>
                </fileset>
			</path>

            <path id="test.run.classpath">
                <path refid="test.classpath"/>
				<pathelement location="${file.reference.junit}"/>
                <fileset dir="${ant-output.dir}">
                    <include name="test.jar"/>
                </fileset>
            </path>

            <path id="javadoc.classpath">
                <path refid="core.classpath"/>
            </path>

            <path id="catalog-javadoc.classpath">
                <path refid="core.classpath"/>
                <fileset dir="${dist.dir}">
                    <include name="**/*.jar"/>
                </fileset>
            </path>

            <pathconvert refid="core.classpath" property="core.classpath.attribute" pathsep=" " dirsep="/">
                <flattenmapper/>
            </pathconvert>
            
            <property name="ui.classpath.attribute" value="lib/${core-jar.name} lib/${catalog-jar.name}"/>

        </target>

        <!-- - - - - - - - - - - - - - - - - -
          target: detectOS
         - - - - - - - - - - - - - - - - - -->

        <target name="detectOS">
            <condition property="is.windows">
                <os family="windows"/>
            </condition>
            <condition property="is.macosx">
               <and>
                   <os family="mac"/>
                   <os family="unix"/>
               </and>
            </condition>
            <condition property="is.linux">
            	<and>
            		<istrue value="${useExternalJarsOnLinux}"/>
                    <and>
                        <os family="unix"/>
                    	<not><os family="mac"/></not>
                    </and>
            	</and>
            </condition>
        </target>

        <!-- - - - - - - - - - - - - - - - - -
          target: debianProperties
         - - - - - - - - - - - - - - - - - -->

        <target name="debianProperties" if="is.linux" depends="detectOS">
            <property file="build.debian.properties"/>
        </target>
	
	<!-- - - - - - - - - - - - - - - - - - 
          target: createCatalogJavadocDir                      
         - - - - - - - - - - - - - - - - - -->
    <target name="createCatalogJavadocDir">
    	<mkdir dir="${javadoc-catalog.dir}"/>
    </target>
	
	
	<!-- - - - - - - - - - - - - - - - - - 
          target: createDistDir                      
         - - - - - - - - - - - - - - - - - -->
    <target name="createDistDir">
		<mkdir dir="${dist.dir}"/>
    </target>

	<!-- - - - - - - - - - - - - - - - - - 
          target: buildCoreJar                      
         - - - - - - - - - - - - - - - - - -->
    <target name="buildCoreJar" depends="setClasspath, createDistDir">
    	<!-- Empty build dir -->
    	<delete dir="${build.dir}"/>
    	<mkdir dir="${build.dir}"/>

    	<!-- Compile core files -->
        <javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" debug="true" encoding="UTF-8" source="${javac.source}" target="${javac.target}" compiler="javac1.5">
			<classpath>
				<path refid="core.classpath"/>
			</classpath>
        </javac>

    	<!-- Build Core Jar -->
    	<jar destfile="${braille.jar}">
    		<fileset dir="${build.dir}">
    			<patternset refid="braille-jar.patternset"/>
    		</fileset>
    		<fileset dir="${src.dir}">
    			<patternset refid="braille-jar.patternset"/>  			
    		</fileset>
    		<manifest>
    			<attribute name="Built-By" value="${user.name}"/>
    			<attribute name="Built-On" value="${ISO-TODAY}"/>
    			<attribute name="Repository-Revision" value="${repository-revision}"/>
    			<attribute name="Implementation-Title" value="${release-name} Core"/>
    			<attribute name="Implementation-Version" value="${release-version}"/>
    			<attribute name="Implementation-URL" value="http://code.google.com/p/brailleutils/"/>
    			<attribute name="Class-Path" value="${core.classpath.attribute}"/>
    		</manifest>
    	</jar>
    	
    	<!-- Build UI jar -->
    	<jar destfile="${brailleUtils.jar}" >
    		<fileset dir="${build.dir}">
    			<patternset refid="braille-ui-jar.patternset"/>
    		</fileset>
    		<fileset dir="${src.dir}">
    			<patternset refid="braille-ui-jar.patternset"/>
    		</fileset>
    		<manifest>
    			<attribute name="Main-Class" value="org.daisy.braille.ui.BasicUI"/>
    			<attribute name="Built-By" value="${user.name}"/>
    			<attribute name="Built-On" value="${ISO-TODAY}"/>
    			<attribute name="Repository-Revision" value="${repository-revision}"/>
    			<attribute name="Implementation-Title" value="${release-name} UI"/>
    			<attribute name="Implementation-Version" value="${release-version}"/>
    			<attribute name="Implementation-URL" value="http://code.google.com/p/brailleutils/"/>
    			<attribute name="Class-Path" value="${ui.classpath.attribute}"/>
    		</manifest>
    	</jar>
    	
    	<!-- Remove build files -->
    	<delete dir="${build.dir}"/>
    </target>
	
	<!-- - - - - - - - - - - - - - - - - - 
          target: buildCatalogJar                      
         - - - - - - - - - - - - - - - - - -->
    <target name="buildCatalogJar" depends="buildCoreJar, createDistDir">
    	<!-- Empty catalog build dir -->
    	<delete dir="${catalog-build.dir}"/>
    	<mkdir dir="${catalog-build.dir}"/>
    		
    	<!-- Compile catalog files -->
        <javac srcdir="${catalog-src.dir}" destdir="${catalog-build.dir}" includeantruntime="false" debug="true" encoding="UTF-8" source="${javac.source}" target="${javac.target}" compiler="javac1.5">
			<classpath>
				<path refid="catalog.classpath"/>
			</classpath>
        </javac>

    	<jar destfile="${catalog.jar}">
    		<fileset dir="${catalog-build.dir}">
    			<patternset refid="catalog-jar.patternset"/>
    		</fileset>
    		<fileset dir="${catalog-src.dir}">
    			<patternset refid="catalog-jar.patternset"/>  			
    		</fileset>
    		<manifest>
    			<attribute name="Built-By" value="${user.name}"/>
    			<attribute name="Built-On" value="${ISO-TODAY}"/>
    			<attribute name="Repository-Revision" value="${repository-revision}"/>
    			<attribute name="Implementation-Title" value="${release-name} Catalog"/>
    			<attribute name="Implementation-Version" value="${release-version}"/>
    			<attribute name="Implementation-URL" value="http://code.google.com/p/brailleutils/"/>
    		</manifest>
    	</jar>
    	
    	<!-- Remove build files -->
    	<delete dir="${catalog-build.dir}"/>
    </target>
	
	<!-- ================================= 
          target: buildJavadoc                      
         ================================= -->
    <target name="buildJavadoc" depends="setClasspath" description="Build javadoc">
    	<!-- Empty javadoc dir -->
    	<delete dir="${javadoc.dir}"/>
    	<mkdir dir="${javadoc.dir}"/>
    	
    	<javadoc destdir="${javadoc.dir}"
    			windowtitle="Braille Utils"
    			encoding="UTF-8"
    			charset="UTF-8"
    			classpathref="javadoc.classpath"
    			sourcepath="${src.dir}"
    		>
    		<link offline="true" href="http://java.sun.com/javase/6/docs/api/" packagelistLoc="ant-build/resources/javase-6"/>
    	</javadoc>
    </target>
	
	<!-- ================================= 
          target: buildCatalogJavadoc                      
         ================================= -->
    <target name="buildCatalogJavadoc" depends="createCatalogJavadocDir, buildCoreJar" description="Build catalog javadoc">
    	<javadoc destdir="${javadoc-catalog.dir}"
    			windowtitle="Catalog"
    			encoding="UTF-8"
    			charset="UTF-8"
    			classpathref="catalog-javadoc.classpath"
    			sourcepath="${catalog-src.dir}"
    		>
    		<link offline="true" href="http://java.sun.com/javase/6/docs/api/" packagelistLoc="ant-build/resources/javase-6"/>
    		<fileset dir="${src.dir}">
    			<include name="**/*.java"/>
    		</fileset>
    		<fileset dir="${catalog-src.dir}">
    			<include name="**/*.java"/>
    		</fileset>
    	</javadoc>
    	<delete dir="${build.dir}"/>
    </target>
	

	<!-- ================================= 
          target: buildJavadocZip 
         ================================= -->
    <target name="buildJavadocZip" depends="buildJavadoc, createDistDir" description="Build zip javadoc">
    	<zip destfile="${javadoc.zip}">
    		<zipfileset dir="${javadoc.dir}" prefix="brailleUtils/docs/api">
    			<include name="**/*.*"/>
    		</zipfileset>
    	</zip>
    </target>
	
	<!-- ================================= 
          target: buildZip 
         ================================= -->
    <target name="buildZip" description="Build zip distribution" depends="detectOS">
    	<!-- Empty dist dir -->
    	<delete dir="${dist.dir}"/>

    	<antcall target="buildCatalogJar"/>

    	<zip destfile="${distribution.zip}">
    		<zipfileset file="${brailleUtils.jar}" prefix="brailleUtils/"/>
    		<zipfileset file="${catalog.jar}" prefix="brailleUtils/lib"/>
    		<zipfileset file="${braille.jar}" prefix="brailleUtils/lib"/>
    		<zipfileset dir="${lib.dir}" prefix="brailleUtils/lib">
    			<include name="*.jar" unless="is.linux"/>
    			<exclude name="junit*.*"/>
    		</zipfileset>
    		<zipfileset dir="${basedir}/licenses" prefix="brailleUtils/licenses/">
    			<include name="**/*.*"/>
    		</zipfileset>
    	</zip>
    </target>

	<!-- ================================= 
          target: buildAndTest             
         ================================= -->
    <target name="buildAndTest" depends="buildZip, buildJavadoc" description="Build jars, javadoc and run unit tests">
    	
    	<!-- Empty reports dir -->
    	<delete dir="${test.report.dir}"/>
    	<mkdir dir="${test.report.dir}"/>
    	
		<!-- Empty test build dir -->
    	<delete dir="${test-build.dir}"/>
    	<mkdir dir="${test-build.dir}"/>

    	<!-- Compile test files -->
	    <javac srcdir="${test-src.dir}" destdir="${test-build.dir}" includeantruntime="false" debug="true" encoding="UTF-8" source="${javac.source}" target="${javac.target}" compiler="javac1.5">
			<classpath>
				<path refid="test.classpath"/>
			</classpath>
	    </javac>

		<jar destfile="${ant-output.dir}/test.jar" >
    		<fileset dir="${test-build.dir}">
    			<patternset refid="catalog-jar.patternset"/>
    		</fileset>
			<fileset dir="${test-src.dir}">
				<patternset refid="catalog-jar.patternset"/>
			</fileset>
    	</jar>

		<mkdir dir="${test.report-temp.dir}"/>
		
		<junit fork="yes" printsummary="no" haltonfailure="no">
			<batchtest fork="yes" todir="${test.report-temp.dir}">
		      <fileset dir="${test-build.dir}">
		        <include name="**/*Test.class" />
		      	<include name="**/TestUnit.class" />
		      	<exclude name="**/Test.class" />
		      </fileset>
		    </batchtest>
		    <formatter type="xml" />
		    <classpath refid="test.run.classpath" />
		</junit>

		<junitreport todir="${test.report.dir}">
			<fileset dir="${test.report-temp.dir}">
				<include name="TEST-*.xml" />
			</fileset>
			<report todir="${test.report.dir}" />
		</junitreport>
		
		<delete dir="${test.report-temp.dir}"/>
		<delete dir="${test-build.dir}"/>
    	<delete dir="${catalog-build.dir}"/>
		<delete deleteonexit="true" file="${ant-output.dir}/test.jar"/>
    </target>

</project>