<!--
  ~ Copyright (c) 2007-2012 The Broad Institute, Inc.
  ~ SOFTWARE COPYRIGHT NOTICE
  ~ This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
  ~
  ~ This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
  ~
  ~ This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
  ~ Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
  -->

<project name="IGV" default="all" basedir=".">
    <import file="build_macros.xml"/>

    <property name="jar.name" value="igv.jar"/>
    <property name="main-class" value="org.broad.igv.ui.Main"/>

    <!-- Source and lib directories -->
    <property name="src.dir" value="${basedir}/src/"/>
    <property name="lib.dir" value="${basedir}/lib/"/>
    <property name="testsrc.dir" value="${basedir}/test/src"/>
    <property name="testlib.dir" value="${basedir}/test/lib"/>

    <!-- Temp directory.  Sources are compiled here prior to archiving -->
    <property name="tmp.dir" value="${basedir}/tmp/"/>
    <property name="testtmp.dir" value="${basedir}/testtmp/"/>
    <property name="resource.dir" value="${tmp.dir}resources/"/>

    <!-- Distribution directory.  Binary distribution is placed here -->
    <property name="dist.dir" value="${basedir}/"/>
    <property name="jar.path" value="${dist.dir}${jar.name}"/>
    <property name="dist.lib.dir" value="${dist.dir}lib/"/>
    <property name="download.dir" value="${basedir}"/>

    <property name="scripts.dir" value="${basedir}/scripts"/>
    <property name="docs.dir" value="${basedir}/docs"/>
    <property name="api.docs.dir" value="${docs.dir}/api"/>

    <property name="javac.nowarn" value="off"/>

    <property name="batik-codec-finame" value="batik-codec__V1.7.jar"/>
    <property name="goby-io-finame" value="goby-io-igv__V1.0.jar"/>

    <!-- Create class path for manifest -->
    <path id="@jars">
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
            <exclude name="appbundler*.jar"/>
        </fileset>
    </path>

    <!-- Clean all generated artifacts -->
    <target name="clean">
        <delete dir="${tmp.dir}"/>
        <delete dir="${testtmp.dir}"/>
    </target>


    <!-- Would like to be able to set bcel to true or false
    This is easy in ant 1.8, but not earlier versions-->
    <condition property="act.include.libs">
        <and>
            <available classname="org.apache.tools.ant.util.depend.bcel.FullAnalyzer"/>
            <istrue value="${include.libs}"/>
        </and>
    </condition>

    <!-- Unzip jars to the tmp directory -->
    <target name="prepare" if="act.include.libs">
        <unzip dest="${tmp.dir}">
            <!-- Copy almost everything to tmp directory
            Certain jars we include whole, because we need their manifests
            Batik and Goby get copied later.
            Certain jars we just want to ignore (junit, appbundler)-->
            <patternset>
                <include name="**/*"/>
                <exclude name="META-INF/**"/>
                <exclude name="${lib.dir}/*"/>
            </patternset>
            <fileset dir="${lib.dir}">
                <include name="**/*.jar"/>
                <exclude name="appbundler*.jar"/>
                <exclude name="**/*junit*.jar"/>
                <exclude name="**/${batik-codec-finame}"/>
                <exclude name="**/${goby-io-finame}"/>
                <exclude name="**/*AppleJavaExtensions.jar"/>
            </fileset>
        </unzip>
    </target>

    <target name="rewrite-log" if="log.level">
        <set_log_level targetFile="${tmp.dir}log4j.properties" logLevel="${log.level}"/>
    </target>

    <target name="init" depends="clean">
        <mkdir dir="${tmp.dir}"/>
        <mkdir dir="${testtmp.dir}"/>
        <mkdir dir="${dist.dir}"/>

        <!-- Copy resource files to the tmp directory -->
        <copy todir="${tmp.dir}" overwrite="true">
            <fileset dir="${src.dir}">
                <exclude name="**/*.form"/>
                <exclude name="**/*.java"/>
                <exclude name="**/*.svn"/>
                <exclude name="**/manifest.mf"/>
            </fileset>
        </copy>

        <antcall target="prepare"/>
        <write_version targetFile="${resource.dir}about.properties"/>

        <antcall target="rewrite-log"/>

        <condition property="compile.tests">
            <and>
                <not>
                    <isset property="notests"/>
                </not>
                <available file="${testsrc.dir}" type="dir"/>
            </and>
        </condition>
    </target>

    <!-- We compile IGV and the tests separately, because we only want
    to include the main classes in the JAR and so it's easier to run
    tests later. -->
    <target name="compile" depends="init" description="Compile main source files to temp directory">
        <javac includeantruntime="false"
               srcdir="${src.dir}" debug="on" destdir="${tmp.dir}" encoding="${java.encoding}"
               target="${javac.target}" source="${javac.target}" nowarn="${javac.nowarn}">
            <classpath refid="@jars"/>
        </javac>
        <!--
        <copy todir="${tmp.dir}/META-INF/services" overwrite="true">
            <fileset dir="${basedir}/services"/>
        </copy>
        -->
    </target>

    <target name="build-java-agent" description="Create JavaAgent.jar in testlib directory. Necessary for profiling"
            depends="compile">
        <jar destfile="${testlib.dir}/JavaAgent.jar" basedir="${tmp.dir}" compress="true" duplicate="preserve">
            <include name="org/broad/igv/util/JavaAgent.class"/>
            <include name="org/broad/igv/util/JavaAgent$*.class"/>
            <manifest>
                <attribute name="Premain-Class" value="org.broad.igv.util.JavaAgent"/>
            </manifest>
    </jar>
    </target>


    <target name="compileTests" if="compile.tests" description="Compile test files to testtmp directory, WITHOUT compiling main source files">
        <javac srcdir="${testsrc.dir}" debug="on" destdir="${testtmp.dir}" includeantruntime="true"
               target="${javac.target}" source="${javac.target}" encoding="${java.encoding}" nowarn="${javac.nowarn}">
            <classpath refid="test.classpath"/>
        </javac>
    </target>

    <macrodef name="build">
        <attribute name="includedFileset"/>

        <sequential>
            <jar destfile="${jar.path}" basedir="${tmp.dir}" compress="true" duplicate="preserve">
                <fileset refid="@{includedFileset}"/>
                <manifest>

                    <attribute name="Permissions" value="all-permissions"/>
                    <attribute name="Application-Name" value="${ant.project.name}"/>
                    <!-- Here just to be a good citizen. Apparently reading from the correct manifest
                    is tricky. We rely on a separate properties file-->
                    <attribute name="Class-Path" value="${class-path}"/>
                    <attribute name="Built-By" value="${user.name}"/>
                    <attribute name="Main-Class" value="${main-class}"/>

                    <section name="org/broad/igv">
                        <attribute name="Specification-Title" value="${ant.project.name}"/>
                        <attribute name="Specification-Version" value="${version}"/>
                        <attribute name="Specification-Vendor" value="${vendor}"/>
                        <attribute name="Implementation-Version" value="${version} ${build-number}"/>
                        <attribute name="Implementation-Vendor" value="${vendor}"/>
                    </section>

                </manifest>
            </jar>
            <copy file="${lib.dir}${batik-codec-finame}" tofile="${dist.dir}${batik-codec-finame}" overwrite="true"/>
            <copy file="${lib.dir}${goby-io-finame}" tofile="${dist.dir}${goby-io-finame}" overwrite="true"/>
            <delete dir="${tmp.dir}"/>
            <delete dir="${testtmp.dir}"/>
        </sequential>
    </macrodef>

    <target name="build-small" depends="compile,compileTests" unless="act.include.libs"
            description="Build the IGV JAR, without including libraries">
        <fileset dir="${tmp.dir}" id="files"/>

        <pathconvert property="class-path" pathsep=" " dirsep="/" refid="@jars">
            <map from="${lib.dir}" to="lib/"/>
        </pathconvert>

        <build includedFileset="files"/>
    </target>

    <target name="build-all" depends="compile,compileTests" if="act.include.libs"
            description="Build the IGV JAR, including libraries, using BCEL to take only required classes">

        <classfileset dir="${tmp.dir}" id="files">
            <root classname="${main-class}"/>
        </classfileset>

        <property name="class-path" value="${batik-codec-finame} ${goby-io-finame}"/>

        <build includedFileset="files"/>
    </target>

    <condition property="isWindows">
        <os family="windows" />
    </condition>

    <condition property="isLinux">
        <os family="unix" />
    </condition>

    <condition property="isMac">
        <os family="mac" />
    </condition>

    <!-- Copy launching scripts to dest dir, where igv.jar is -->
    <target name="copy-launch-scripts-win" if="isWindows">
        <copy file="scripts/igv.bat" todir="${dist.dir}" overwrite="false"/>
        <copy file="scripts/igvtools.bat" todir="${dist.dir}" overwrite="false"/>
        <copy file="scripts/igvtools_gui.bat" todir="${dist.dir}" overwrite="false"/>
    </target>
    <target name="copy-launch-scripts-mac" if="isMac">
        <copy file="scripts/igv.command" todir="${dist.dir}" overwrite="false"/>
        <copy file="scripts/igvtools" todir="${dist.dir}" overwrite="false"/>
        <copy file="scripts/igvtools_gui.command" todir="${dist.dir}" overwrite="false"/>
    </target>
    <target name="copy-launch-scripts-linux" if="isLinux">
        <copy file="scripts/igv.sh" todir="${dist.dir}" overwrite="false"/>
        <copy file="scripts/igvtools" todir="${dist.dir}" overwrite="false"/>
        <copy file="scripts/igvtools_gui" todir="${dist.dir}" overwrite="false"/>
    </target>

    <target name="build" depends="build-small,build-all"/>

    <target name="all" depends="build, clean"/>

    <!-- Run all tests with ant -->
    <path id="test.classpath">
        <path refid="@jars"/>
        <fileset dir="${testlib.dir}">
            <include name="fest*.jar"/>
            <include name="jcip*.jar"/>
        </fileset>
        <pathelement location="${testtmp.dir}"/>
        <pathelement location="${tmp.dir}"/>
    </path>

    <property name="test.reports" value="${basedir}/reports"/>
    <property name="test.reports.tmp" value="${basedir}/reportstmp"/>
    <property name="LARGE_DATA_DIR" value="${basedir}/test/largedata/"/>
    <!--Path to mongodb executable. For testing; is optional-->
    <property name="MONGO_EXEC_PATH" value="mongod"/>
    <!--Can override this on command line to see test output-->
    <property name="show.test.out" value="no"/>

    <!--For testing jenkins mostly. When set to true, there is a test in ReadmeParserTest which will fail-->
    <property name="make.fail" value="false"/>

    <!--Hosted data test explicitly excluded, never run automated, can run manual-->
    <fileset dir="${testtmp.dir}" id="allfileset">
        <include name="**/*.class"/>
        <exclude name="**/*$*.class"/>
    </fileset>

    <fileset dir="${testtmp.dir}" id="longfileset">
        <include name="**/*HostedData*.class"/>
        <include name="**/*IGVTools*.class"/>
        <include name="**/*TrackLoader*.class"/>
        <exclude name="**/*$*.class"/>
    </fileset>

    <fileset dir="${testtmp.dir}" id="uifileset">
        <include name="**/IGVCommandBarTest.class"/>
        <include name="**/CommandExecutorTest.class"/>
        <include name="**/MainTest.class"/>
        <include name="**/IGVTestHeaded.class"/>
        <include name="**/CoverageTrackTest.class"/>
        <include name="**/TrackLoaderTestHeaded.class"/>
        <exclude name="**/*$*.class"/>
    </fileset>

    <!-- This looks a bit complicated but there is logic to it. We define this target
     to be run by any other test runners, after they define an appropriate fileset.
     We define files of tests to be run above (allfileset and uifileset), one
     can also define their own by setting the filesetpattern property.
     e.g.
     >ant tests
     runs all tests
     >ant -Dfilesetpattern *BED* tests
     would run any test with BED in the file name.
     >ant -Dfilesetpattern IGVToolsTest tests
     would simply run IGVToolsTest.
     -->
    <target name="actruntests" depends="compileTests">
        <delete dir="${test.reports}"/>
        <mkdir dir="${test.reports}"/>

        <junit haltonfailure="no" showoutput="${show.test.out}" printsummary="on" failureproperty="tests.failed"
               maxmemory="2000m">

            <jvmarg value="-javaagent:${testlib.dir}/JavaAgent.jar"/>
            <jvmarg value="-Dfile.encoding=${java.encoding}"/>
            <jvmarg value="-Dinclude.longrunning=${include.longrunning}"/>
            <jvmarg value="-Dignore.ioexceptions=${ignore.ioexceptions}"/>
            <sysproperty key="LARGE_DATA_DIR" value="${LARGE_DATA_DIR}"/>
            <sysproperty key="MONGO_EXEC_PATH" value="${MONGO_EXEC_PATH}"/>
            <sysproperty key="make.fail" value="${make.fail}"/>

            <classpath refid="test.classpath"/>
            <formatter type="xml" usefile="true"/>
            <batchtest fork="yes" todir="${test.reports}" if="filesetpattern">
                <fileset dir="${testtmp.dir}">
                    <include name="**/${filesetpattern}.class"/>
                    <exclude name="**/*$*.class"/>
                </fileset>
            </batchtest>
            <batchtest fork="yes" todir="${test.reports}" unless="filesetpattern">
                <fileset refid="${filesetid}"/>
            </batchtest>
        </junit>

    </target>

    <target name="makereports" depends="actruntests">
        <junitreport todir="${test.reports}">
            <fileset dir="${test.reports}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${test.reports}"/>
        </junitreport>
        <fail message="Test failure detected, check test results." if="tests.failed"/>
    </target>

    <!-- Run tests without compiling source files. Primarily used
    for testing packaged jar-->
    <target name="tests_nocompile">
        <antcall target="makereports">
            <param name="filesetid" value="allfileset"/>
        </antcall>
    </target>

    <target name="tests" depends="compile, tests_nocompile"/>

    <target name="uitests" depends="compile">
        <antcall target="makereports">
            <param name="filesetid" value="uifileset"/>
        </antcall>
    </target>


    <!-- Re-run failed tests only. Copy reports to a temp file
        because otherwise they would get deleted-->
    <fileset dir="${testtmp.dir}" id="failedtests">
        <include name="**/*.class"/>
        <exclude name="**/*$*.class"/>
        <custom classname="util.FailedTestSelector" classpath="${testtmp.dir}">
            <param name="reportDir" value="${test.reports.tmp}"/>
        </custom>
    </fileset>

    <property name="input.test.reports" value="${test.reports}"/>
    <target name="failedtests" depends="compile">
        <delete dir="${test.reports.tmp}"/>
        <copy todir="${test.reports.tmp}" overwrite="true">
            <fileset dir="${input.test.reports}">
                <include name="**/*.xml"/>
            </fileset>
        </copy>

        <antcall target="makereports">
            <param name="filesetid" value="failedtests"/>
        </antcall>

        <delete dir="${test.reports.tmp}"/>
    </target>

    <!-- Deploy the application -->
    <target name="deploy">
        <copy todir="${deploy.dir}" overwrite="true">
            <fileset dir="${dist.dir}">
                <include name="lib/**"/>
            </fileset>
            <fileset file="${dist.dir}${jar.name}"/>
        </copy>
    </target>

    <!--
    We use publishedApiDoclet to generate documentation only for those methods/fields
    which are marked as part of the api.

    See http://developer.berlios.de/projects/padoclet/
    and
    http://developer.berlios.de/forum/forum.php?forum_id=22147
    -->
    <target name="create-api-doc">
        <delete dir="${api.docs.dir}"/>
        <mkdir dir="${api.docs.dir}"/>
        <javadoc access="public"
                 useexternalfile="yes"
                 additionalparam="-quiet"
                 destdir="${api.docs.dir}"
                 packagenames="org.broad.igv.*"
                 sourcepath="src"
                 defaultexcludes="yes"
                 author="true"
                 version="true"
                 use="true"
                 windowtitle="IGV API Documentation">

            <tag name="api_since" description="Since: " scope="all"/>
            <tag name="api_until" description="Until: "/>
            <doclet name="de.kruis.padoclet.PublishedApiDoclet" path="${basedir}/docs/lib/publishedApiDoclet1.5-0.4.jar">
                <param name="-padDefaultIsExclude"/>
                <param name="-padFilterDefault" value=".*"/>
                <param name="-padExcludeTag" value="noapi"/>
                <param name="-padIncludeTag" value="api"/>
            </doclet>

        </javadoc>
    </target>


    <!-- Build MacOS native app-->
    <!-- Create the app bundle from an input jar-->
    <macrodef name="bundle-jar-macro">
        <attribute name="version"/>
        <attribute name="dist.dir"/>
        <attribute name="jar.name"/>
        <sequential>
            <!-- Define the appbundler task -->
            <taskdef name="bundleapp" classname="com.oracle.appbundler.AppBundlerTask" classpath="${basedir}/ant/appbundler-1.0.jar"/>
            <bundleapp outputdirectory="${download.dir}"
                       name="IGV_@{version}"
                       displayname="${ant.project.name}"
                       identifier="${main-class}"
                       shortversion="${version}"
                       applicationCategory="public.app-category.developer-tools"
                       mainclassname="${main-class}"
                       icon="${basedir}/resources/igv_icon.icns">
                <!-- Bundle JRE located at dir-->
                <!--runtime dir="${env.JAVA_HOME}" /-->
                <classpath dir="@{dist.dir}">
                    <include name="@{jar.name}"/>
                    <include name="${batik-codec-finame}"/>
                    <include name="${goby-io-finame}"/>
                </classpath>
                <option value="-Dapple.laf.useScreenMenuBar=true"/>
                <argument value="-Xmx4g"/>
                <argument value="-Xdock:name=${ant.project.name}"/>
            </bundleapp>

        </sequential>

    </macrodef>

    <!-- Target to be called  as part of building all-->
    <target name="bundle" depends="build-all">
        <bundle-jar-macro version="${version}" dist.dir="${dist.dir}" jar.name="${jar.name}"/>
    </target>

    <!-- Target to bundle an existing build -->
    <target name="bundle-existing">
        <bundle-jar-macro  version="${version}" dist.dir="${dist.dir}" jar.name="${jar.name}"/>
    </target>

</project>