<?xml version="1.0" encoding="UTF-8"?>
<project name="ARX" default="release" basedir=".">

    <description>Builds, tests, and runs the project.</description>

    <!-- defines the version number -->
    <property name="arx.version" value="3.6.0" />

    <!-- add extra parameter to javadoc generation if Java 1.8 is used -->
    <condition property="javadoc.additionalparam" value="-notimestamp -Xdoclint:none" else="-notimestamp">
        <equals arg1="${ant.java.version}" arg2="1.8" />
    </condition>

    <!-- defines the to be included libraries -->
    <fileset id="lib.jars" dir="lib">
        <or>
            <filename name="**/colt-1.2.0.jar" />
            <filename name="**/commons-lang-2.6.jar" />
            <filename name="**/commons-math3-3.1.1.jar" />
            <filename name="**/objectselector-0.1-lib.jar" />
            <filename name="**/commons-io-2.4.jar" />
            <filename name="**/commons-codec-1.5.jar" />
            <filename name="**/commons-logging-1.1.jar" />
            <filename name="**/commons-validator-1.4.1.jar" />
            <filename name="**/dom4j-1.6.1.jar" />
            <filename name="**/stax-api-1.0.1.jar" />
            <filename name="**/xmlbeans-2.3.0.jar" />
            <filename name="**/poi-3.10-FINAL-20140208.jar" />
            <filename name="**/poi-ooxml-3.10-FINAL-20140208.jar" />
            <filename name="**/poi-ooxml-schemas-3.10-FINAL-20140208.jar" />
            <filename name="**/hppc-0.6.0.jar" />
            <filename name="**/univocity-parsers-2.4.1.jar" />
            <filename name="**/newtonraphson-0.0.1.jar" />
            <filename name="**/jhpl-0.0.1.jar" />
            <filename name="**/mahout-core-0.9.jar" />
            <filename name="**/mahout-math-0.11.1.jar" />
            <filename name="**/hadoop-core-1.2.1.jar" />
            <filename name="**/guava-18.0.jar" />
            <filename name="**/slf4j-api-1.7.13.jar" />
			<filename name="**/pdfbox-app-2.0.1.jar" />
			<filename name="**/pdfbox-layout.jar" />
        </or>
    </fileset>

    <fileset id="gui.jars" dir="lib">
        <or>
            <filename name="**/org.eclipse.draw2d_3.8.1.201301141834.jar" />
            <filename name="**/org.eclipse.core.commands_3.6.2.v20130123-162658.jar" />
            <filename name="**/org.eclipse.core.runtime_3.8.0.v20120912-155025.jar" />
            <filename name="**/org.eclipse.equinox.common_3.6.100.v20120522-1841.jar" />
            <filename name="**/org.eclipse.jface_3.8.102.v20130123-162658.jar" />
            <filename name="**/libjhc-1.0.0-swt.jar" />
            <filename name="**/junit-4.11.jar" />
            <filename name="**/log4j-1.2.17.jar" />
            <filename name="**/swtknob-1.0.0.jar" />
            <filename name="**/org.eclipse.nebula.widgets.nattable.core_1.1.0.201405012245.jar" />
            <filename name="**/org.swtchart_0.8.0.v20120301.jar" />
            <filename name="**/swttable-0.0.1.jar" />
            <filename name="**/swtchoicesdialog-0.0.1.jar" />
            <filename name="**/swtrangeslider-0.0.1.jar" />
            <filename name="**/swttiles-0.0.1.jar" />
            <filename name="**/swtpreferences-0.0.1.jar" />
            <filename name="**/swtsimplebrowser-0.0.1.jar" />
            <filename name="**/jtds-1.3.1.jar" />
            <filename name="**/mysql-connector-java-5.1.38-bin.jar" />
            <filename name="**/postgresql-9.3-1101.jdbc41.jar" />
            <filename name="**/sqlite-jdbc-3.7.2.jar" />
            <filename name="**/ojdbc7.jar" />
            <filename name="**/org.eclipse.nebula.visualization.widgets_1.0.0.201511041902.jar" />
            <filename name="**/org.eclipse.nebula.visualization.xygraph_1.0.0.201511041902.jar" />
        </or>
    </fileset>

    <fileset id="test.jars" dir="lib">
        <or>
            <filename name="**/junit-4.11.jar" />
            <filename name="**/hamcrest-core-1.3.jar" />
        </or>
    </fileset>


    <available file=".git" type="dir" property="git.present" />

    <!-- https://stackoverflow.com/questions/2974106/how-to-lookup-the-latest-git-commit-hash-from-an-ant-build-script -->
    <target name="git.revision" description="Store git revision in ${repository.version}" if="git.present">
        <exec executable="git" outputproperty="git.revision" failifexecutionfails="false" errorproperty="">
            <arg value="describe" />
            <arg value="--tags" />
            <arg value="--always" />
            <arg value="HEAD" />
        </exec>
        <condition property="repository.version" value="${git.revision}" else="unknown">
            <and>
                <isset property="git.revision" />
                <length string="${git.revision}" trim="yes" length="0" when="greater" />
            </and>
        </condition>
    </target>

    <target name="compileTest">
        <delete dir="build/test" failonerror="true" />
        <mkdir dir="build/test" />
        <mkdir dir="build/junitReports" />

        <javac includeantruntime="false" srcdir="src/main:src/gui:src/example:src/test/" destdir="build/test" source="1.7">
            <classpath>
                <fileset refid="lib.jars" />
                <fileset refid="gui.jars" />
                <fileset refid="test.jars" />
                <path path="lib/swt/swt-4.2.1-gtk-linux-x86.jar" />
            </classpath>
        </javac>

        <copy todir="build/test/org/deidentifier/arx/gui/resources" overwrite="true">
            <fileset dir="src/gui/org/deidentifier/arx/gui/resources" defaultexcludes="yes">
                <exclude name="**/*.java" />
            </fileset>
        </copy>
        <copy todir="build/test/org/deidentifier/arx/risk/resources/us" overwrite="true">
            <fileset dir="src/main/org/deidentifier/arx/risk/resources/us" defaultexcludes="yes">
                <exclude name="**/*.java" />
                <exclude name="**/*.txt" />
            </fileset>
        </copy>
        <copy todir="build/test/org/deidentifier/arx/certificate/resources" overwrite="true">
            <fileset dir="src/main/org/deidentifier/arx/certificate/resources" defaultexcludes="yes">
                <exclude name="**/*.java" />
                <exclude name="**/*.txt" />
            </fileset>
        </copy>
    </target>

    <target name="benchmark" depends="git.revision,compileTest">
        <junit showoutput="yes" fork="yes">
            <jvmarg value="-DBenchmark=build/junitReports" />
            <jvmarg value="-DVersion=${git.revision}" />
            <classpath>
                <fileset refid="lib.jars" />
                <fileset refid="test.jars" />
                <path path="build/test" />
                <path path="lib/log4j/log4j-1.2.17.jar" />
                <path path="lib/sqlite/sqlite-jdbc-3.7.2.jar" />
                <path path="lib/swt/swt-4.2.1-gtk-linux-x86.jar" />
            </classpath>
            <batchtest todir="build/junitReports">
                <fileset dir="src/test/">
                    <include name="**/TestAnonymizationKAnonymity.java" />
                    <include name="**/TestAnonymizationLDiversity.java" />
                    <include name="**/TestAnonymizationDPresence.java" />
                    <include name="**/TestAnonymizationTCloseness.java" />
                    <include name="**/TestAnonymizationCombinations.java" />
                    <include name="**/TestAnonymizationDifferentialPrivacy.java" />
                    <include name="**/TestAnonymizationKMap.java" />
                    <include name="**/TestAnonymizationRiskBased.java" />
                    <include name="**/TestAnonymizationLocalRecoding.java" />
                    <include name="**/TestAnonymizationOrderedDistanceTCloseness.java" />
                    <include name="**/TestAnonymizationEntropyLDiversity.java" />
                	<include name="**/TestAnonymizationProfitability.java" />
                </fileset>
                <formatter type="plain" usefile="false" />
                <formatter type="xml" />
            </batchtest>
        </junit>
        <antcall target="analyse-benchmark" />
    </target>

    <target name="analyse-benchmark">
        <java jar="arx-analyse.jar" fork="true">
        </java>
    </target>

    <target name="test" depends="compileTest">
        <junit showoutput="yes">
            <classpath>
                <fileset refid="lib.jars" />
                <fileset refid="test.jars" />
                <path path="build/test" />
                <path path="lib/log4j/log4j-1.2.17.jar" />
                <path path="lib/sqlite/sqlite-jdbc-3.7.2.jar" />
                <path path="lib/swt/swt-4.2.1-gtk-linux-x86.jar" />
            </classpath>
            <batchtest todir="build/junitReports" fork="yes">
                <fileset dir="src/test/" includes="**/Test*.java" />
                <formatter type="plain" usefile="false" />
                <formatter type="xml" />
            </batchtest>
        </junit>
    </target>


    <target name="release" depends="jars">
    </target>

    <target name="clean" depends="end-clean">
        <delete dir="jars" failonerror="true" />
    </target>

    <target name="end-clean">
        <delete dir="build/lib" failonerror="true" />
        <delete dir="build/gui" failonerror="true" />
        <delete dir="build/extLibs" failonerror="true" />
        <delete dir="build" failonerror="true" />
    </target>

    <target name="prepare" depends="clean">
        <mkdir dir="build" />
        <mkdir dir="build/extLibs" />
        <mkdir dir="build/lib" />
        <mkdir dir="build/gui" />
        <mkdir dir="jars" />
    </target>

    <target name="clean-javadoc">
        <delete includeEmptyDirs="true" failonerror="true">
            <fileset dir="doc/api" includes="**/*" defaultexcludes="yes">
                <exclude name="doc/api/.svn" />
            </fileset>
        </delete>
        <delete includeEmptyDirs="true" failonerror="true">
            <fileset dir="doc/gui" includes="**/*" defaultexcludes="yes">
                <exclude name="doc/gui/.svn" />
            </fileset>
        </delete>
        <delete includeEmptyDirs="true" failonerror="true">
            <fileset dir="doc/dev" includes="**/*" defaultexcludes="yes">
                <exclude name="doc/dev/.svn" />
            </fileset>
        </delete>
    </target>

    <target name="javadoc-api" depends="clean-javadoc">
        <javadoc overview="doc/overview-api.html" destdir="doc/api" source="1.7" notree="false" nonavbar="false" noindex="false" splitindex="true" access="public" use="true" author="Florian Kohlmayer and Fabian Praßer" version="${arx.version}" windowtitle="ARX API Documentation" private="false" failonerror="true" additionalparam="${javadoc.additionalparam}">

            <classpath>
                <fileset refid="lib.jars" />
                <path path="src/main" />
            </classpath>

            <packageset dir="src/main/" defaultexcludes="yes">
                <exclude name="org/deidentifier/arx/algorithm/" />
                <exclude name="org/deidentifier/arx/framework/" />
            </packageset>

        </javadoc>
    </target>

    <target name="javadoc-dev" depends="clean-javadoc">
        <javadoc overview="doc/overview-dev.html" destdir="doc/dev" source="1.7" notree="false" use="true" nonavbar="false" noindex="false" splitindex="false" author="Florian Kohlmayer and Fabian Praßer" version="${arx.version}" windowtitle="ARX Developer Documentation" private="false" failonerror="true" additionalparam="${javadoc.additionalparam}">
            <classpath>
                <fileset refid="lib.jars" />
                <path path="src/main" />
            </classpath>

            <packageset dir="src/main/" defaultexcludes="yes">
            </packageset>

        </javadoc>
    </target>


    <target name="javadoc-gui" depends="clean-javadoc">
        <javadoc overview="doc/overview-gui.html" destdir="doc/gui" source="1.7" notree="false" use="true" nonavbar="false" noindex="false" splitindex="false" author="Florian Kohlmayer and Fabian Praßer" version="${arx.version}" windowtitle="ARX GUI Documentation" private="false" failonerror="true" additionalparam="${javadoc.additionalparam}">
            <classpath>
                <fileset refid="lib.jars" />
                <fileset refid="gui.jars" />
                <path path="lib/swt/swt-4.2.1-gtk-linux-x86.jar" />
                <path path="src/main" />
            </classpath>

            <packageset dir="src/main/" defaultexcludes="yes">
                <exclude name="org/deidentifier/arx/algorithm/" />
                <exclude name="org/deidentifier/arx/framework/" />
            </packageset>

            <packageset dir="src/gui/" defaultexcludes="yes">
            </packageset>

        </javadoc>
    </target>

    <target name="javadoc" depends="javadoc-api,javadoc-gui,javadoc-dev">
    </target>

    <target name="compile" depends="prepare" description="compile the source ">
        <javac includeantruntime="false" srcdir="src/main/" destdir="build/lib" source="1.7">
            <classpath>
                <fileset refid="lib.jars" />
                <path path="src/main" />
            </classpath>
        </javac>
        <javac includeantruntime="false" srcdir="src/main:src/gui/" destdir="build/gui" source="1.7">
            <classpath>
                <fileset refid="lib.jars" />
                <fileset refid="gui.jars" />
                <path path="lib/swt/swt-4.2.1-gtk-linux-x86.jar" />
                <path path="src/main" />
            </classpath>
        </javac>
        <copy todir="build/gui/org/deidentifier/arx/gui/resources" overwrite="true">
            <fileset dir="src/gui/org/deidentifier/arx/gui/resources" defaultexcludes="yes">
                <exclude name="**/*.java" />
            </fileset>
        </copy>
        <copy todir="build/main/org/deidentifier/arx/risk/resources/us" overwrite="true">
            <fileset dir="src/main/org/deidentifier/arx/risk/resources/us" defaultexcludes="yes">
                <exclude name="**/*.java" />
                <exclude name="**/*.txt" />
            </fileset>
        </copy>
        <copy todir="build/main/org/deidentifier/arx/certificate/resources" overwrite="true">
            <fileset dir="src/main/org/deidentifier/arx/certificate/resources" defaultexcludes="yes">
                <exclude name="**/*.java" />
                <exclude name="**/*.txt" />
            </fileset>
        </copy>
        <copy todir="build/gui/org/deidentifier/arx/risk/resources/us" overwrite="true">
            <fileset dir="src/main/org/deidentifier/arx/risk/resources/us" defaultexcludes="yes">
                <exclude name="**/*.java" />
                <exclude name="**/*.txt" />
            </fileset>
        </copy>
        <copy todir="build/gui/org/deidentifier/arx/certificate/resources" overwrite="true">
            <fileset dir="src/main/org/deidentifier/arx/certificate/resources" defaultexcludes="yes">
                <exclude name="**/*.java" />
                <exclude name="**/*.txt" />
            </fileset>
        </copy>
    </target>

    <target name="buildExternalLibs">
        <jar jarfile="build/extLibs/lib.jar">
            <zipgroupfileset refid="lib.jars" />
        </jar>
        <jar jarfile="build/extLibs/gui.jar">
            <zipgroupfileset refid="gui.jars" />
        </jar>
    </target>

    <target name="jar-win" depends="compile, buildExternalLibs">
        <jar destfile="jars/arx-${arx.version}-win.jar">
            <fileset dir="build/gui" defaultexcludes="no" />
            <zipfileset src="build/extLibs/lib.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="build/extLibs/gui.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="lib/swt/swt-4.2.1-win32-win32-x86.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <manifest>
                <attribute name="Main-Class" value="org.deidentifier.arx.gui.Main" />
            </manifest>
        </jar>
        <jar destfile="jars/arx-${arx.version}-win-64.jar">
            <fileset dir="build/gui" defaultexcludes="no" />
            <zipfileset src="build/extLibs/lib.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="build/extLibs/gui.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="lib/swt/swt-4.2.1-win32-win32-x86_64.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <manifest>
                <attribute name="Main-Class" value="org.deidentifier.arx.gui.Main" />
            </manifest>
        </jar>
    </target>

    <target name="jar-linux" depends="compile, buildExternalLibs">
        <jar destfile="jars/arx-${arx.version}-gtk.jar">
            <fileset dir="build/gui" defaultexcludes="no" />
            <zipfileset src="build/extLibs/lib.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="build/extLibs/gui.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="lib/swt/swt-4.2.1-gtk-linux-x86.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <manifest>
                <attribute name="Main-Class" value="org.deidentifier.arx.gui.Main" />
            </manifest>
        </jar>
        <jar destfile="jars/arx-${arx.version}-gtk-64.jar">
            <fileset dir="build/gui" defaultexcludes="no" />
            <zipfileset src="build/extLibs/lib.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="build/extLibs/gui.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="lib/swt/swt-4.2.1-gtk-linux-x86_64.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <manifest>
                <attribute name="Main-Class" value="org.deidentifier.arx.gui.Main" />
            </manifest>
        </jar>
    </target>

    <target name="jar-osx" depends="compile, buildExternalLibs">
        <jar destfile="jars/arx-${arx.version}-osx.jar">
            <fileset dir="build/gui" defaultexcludes="no" />
            <zipfileset src="build/extLibs/lib.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="build/extLibs/gui.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="lib/swt/swt-4.2.1-cocoa-macosx.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <manifest>
                <attribute name="Main-Class" value="org.deidentifier.arx.gui.Main" />
            </manifest>
        </jar>
        <jar destfile="jars/arx-${arx.version}-osx-64.jar">
            <fileset dir="build/gui" defaultexcludes="no" />
            <zipfileset src="build/extLibs/lib.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="build/extLibs/gui.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <zipfileset src="lib/swt/swt-4.2.1-cocoa-macosx-x86_64.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
            <manifest>
                <attribute name="Main-Class" value="org.deidentifier.arx.gui.Main" />
            </manifest>
        </jar>
    </target>

    <target name="jar-libs" depends="compile, buildExternalLibs">
        <jar destfile="jars/arx-${arx.version}-src.jar">
            <fileset dir="src/gui" defaultexcludes="no" />
            <fileset dir="src/main" defaultexcludes="no" />
        </jar>
        <jar destfile="jars/libarx-${arx.version}-src.jar">
            <fileset dir="src/main" defaultexcludes="no" />
            <fileset dir="src/test" defaultexcludes="no" />
            <fileset dir="src/example" defaultexcludes="no" />
        </jar>
        <jar destfile="jars/libarx-${arx.version}.jar">
            <fileset dir="build/lib" defaultexcludes="no" />
            <zipfileset src="build/extLibs/lib.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
        </jar>
        <jar destfile="jars/libarx-${arx.version}-min.jar">
            <fileset dir="build/lib" defaultexcludes="no" />
            <zipfileset src="lib/objectselector/objectselector-0.1-lib.jar" excludes="META-INF/*.RSA,META-INF/*.SF,META-INF/*.DSA" />
        </jar>
    </target>

    <target name="jar-docs" depends="compile, javadoc">
        <jar destfile="jars/libarx-${arx.version}-doc.jar">
            <fileset dir="doc/api" defaultexcludes="no" />
        </jar>
        <jar destfile="jars/libarx-${arx.version}-dev-doc.jar">
            <fileset dir="doc/dev" defaultexcludes="no" />
        </jar>
        <jar destfile="jars/arx-${arx.version}-doc.jar">
            <fileset dir="doc/gui" defaultexcludes="no" />
        </jar>
    </target>

    <target name="jars" depends="jar-win, jar-linux, jar-osx, jar-libs, jar-docs">
        <antcall target="end-clean" />
    </target>
</project>
