<project name="Pathogen" default="build" basedir=".">
    <description>
        Build file for PATHOGEN release versions, It is also used by Hudson Pathogen project.
        $Id: build_pathogen.xml,v 1.5 2006/09/09 18:16:17 rambaut Exp $
    </description>
    <!-- set global properties for this build -->
    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="lib" location="lib"/>
    <property name="dist" location="dist"/>

    <path id="classpath">
		<fileset dir="${lib}" includes="**/*.jar"/>
	</path>

    <property environment="env"/>

    <target name="init">
        <!-- Create the time stamp -->
        <echo message="${ant.project.name}: ${ant.file}" />
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>
        <mkdir dir="${dist}"/>
    </target>

    <target name="compile" depends="init">
        <!-- Compile the java code from ${src} into ${build} -->
        <javac source="1.6" srcdir="${src}" destdir="${build}" classpathref="classpath">
            <include name="dr/app/pathogen/**"/>
            <include name="dr/app/beauti/**"/>
            <include name="dr/app/tools/**"/>
            <include name="dr/app/util/**"/>
            <include name="dr/evolution/io/**"/>
            <include name="dr/evolution/datatype/**"/>
            <include name="dr/evolution/sequence/**"/>
            <include name="dr/evolution/alignment/**"/>
            <include name="dr/evolution/tree/**"/>
            <include name="dr/evolution/util/**"/>
            <include name="dr/app/gui/**"/>
            <include name="dr/matrix/**"/>
            <include name="dr/stats/**"/>
            <include name="dr/math/**"/>
            <include name="dr/util/**"/>
            <include name="org/virion/jam/**"/>
        </javac>
    </target>

    <target name="build" depends="compile" description="generate the jar file">
        <!-- Create the distribution directory -->
        <mkdir dir="${dist}"/>

        <!-- Put everything in ${build} into the treestat.jar file -->
        <jar jarfile="${dist}/pathogen.jar">
            <manifest>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Main-Class" value="dr.app.pathogen.PathogenApp"/>
            </manifest>
            <fileset dir="${build}">
                <include name="dr/app/pathogen/**/*.class"/>
                <include name="dr/app/beauti/**/*.class"/>
                <include name="dr/app/tools/**/*.class"/>
                <include name="dr/app/util/**/*.class"/>
                <include name="dr/evolution/io/**/*.class"/>
                <include name="dr/evolution/datatype/**/*.class"/>
                <include name="dr/evolution/sequence/**/*.class"/>
                <include name="dr/evolution/alignment/**/*.class"/>
                <include name="dr/evolution/tree/**/*.class"/>
                <include name="dr/evolution/util/**/*.class"/>
                <include name="dr/app/gui/**/*.class"/>
                <include name="dr/matrix/**/*.class"/>
                <include name="dr/stats/**/*.class"/>
                <include name="dr/math/**/*.class"/>
                <include name="dr/util/**/*.class"/>
            </fileset>
            <fileset dir="">
                <include name="images/*.png"/>
            </fileset>
            <fileset dir="${src}">
                <include name="dr/app/pathogen/**/*.png"/>
                <include name="dr/app/gui/**/*.png"/>
                <include name="org/virion/jam/**/*.png"/>
                <include name="dr/gui/table/**/*.png"/>
            </fileset>
            <zipgroupfileset dir="${lib}" includes="**/jebl.jar"/>
            <zipgroupfileset dir="${lib}" includes="**/jam.jar"/>
            <zipgroupfileset dir="${lib}" includes="**/figtreepanel.jar"/>
        </jar>
    </target>

    <property name="version" value="1.5" />
    <property name="packaging_tools" value="packaging_tools" />
    <property name="release_dir" value="release_pathogen" />
    <property name="name" value="Path-O-Gen" />

    <property name="common_dir" value="${release_dir}/common" />

    <property name="Mac_dir" value="${release_dir}/Mac" />
    <property name="Linux_dir" value="${release_dir}/Linux" />
    <property name="Windows_dir" value="${release_dir}/Windows" />

    <property name="Mac_package_dir" value="${Mac_dir}/${name} v${version}" />
    <property name="Linux_package_dir" value="${Linux_dir}/${name}_v${version}" />
    <property name="Windows_package_dir" value="${Windows_dir}/${name} v${version}" />

    <!-- Need to either install Launch4j under {BEAST workspace}/${release}
                 or install it in the default directory and change the location of launch4j.dir -->
    <target name="windows" depends="build" description="release Windows version of Pathogen">

        <delete dir="${Windows_package_dir}" />
        <!-- Create the release directory -->
        <mkdir dir="${Windows_package_dir}" />

        <property name="launch4j.dir" location="${Windows_dir}/launch4j" />
        <taskdef name="launch4j" classname="net.sf.launch4j.ant.Launch4jTask"
            classpath="${launch4j.dir}/launch4j.jar :${launch4j.dir}/lib/xstream.jar" />

        <copy file="${dist}/pathogen.jar" todir="${Windows_package_dir}/lib"/>
        <copy todir="${Windows_package_dir}/lib">
            <fileset dir="${Windows_dir}/lib"/>
        </copy>
        <copy file="${common_dir}/README.txt" todir="${Windows_package_dir}"/>

        <launch4j configFile="${Windows_dir}/pathogen_launch4j.xml"
                  jar="${dist}/pathogen.jar"
                  outfile="${Windows_package_dir}/${name} v${version}.exe"
                  fileVersion="${version}.0.0"
                  txtFileVersion="${version}"
                  productVersion="${version}.0.0"
                  txtProductVersion="${version}"/>

        <zip destfile="${release_dir}/${name} v${version}.zip">
            <zipfileset dir="${Windows_package_dir}" prefix="${name} v${version}"/>
        </zip>

        <echo message="Windows version release is finished." />
    </target>

    <target name="linux" depends="build" description="release Linux/Unix version of Pathogen">
        <delete dir="${Linux_package_dir}" />
        <!-- Create the release directory -->
        <mkdir dir="${Linux_package_dir}" />

        <copy todir="${Linux_package_dir}/bin">
            <fileset dir="${Linux_dir}/scripts"/>
        </copy>

        <chmod dir="${Linux_package_dir}/bin" perm="755" includes="**/**"/>

        <copy file="${Linux_dir}/icons/pathogen.png" todir="${Linux_package_dir}/images"/>
        <copy file="${dist}/pathogen.jar" todir="${Linux_package_dir}/lib"/>
        <copy todir="${Linux_package_dir}/lib">
            <fileset dir="${Linux_dir}/lib"/>
        </copy>
        <copy file="${common_dir}/README.txt" todir="${Linux_package_dir}"/>

        <tar destfile="${release_dir}/${name}_v${version}.tgz" compression="gzip">
            <tarfileset dir="${Linux_package_dir}" prefix="${name}_v${version}"/>
        </tar>

        <echo message="Linux/Unix version release is finished." />
    </target>

    <target name="mac" depends="build" description="release Mac version of Pathogen">
        <delete dir="${Mac_package_dir}" />
        <!-- Create the release directory -->
        <mkdir dir="${Mac_package_dir}" />

        <copy file="${common_dir}/README.txt" todir="${Mac_package_dir}"/>

        <taskdef name="jarbundler" classname="net.sourceforge.jarbundler.JarBundler"/>

        <!-- create a jar bundle for the mac -->
        <jarbundler dir="${Mac_package_dir}"
                        name="${name} v${version}"
                        mainclass="dr.app.pathogen.PathogenApp"
                        stubfile="${packaging_tools}/mac/universalJavaApplicationStub"
                        icon="${Mac_dir}/icons/Path-O-Gen.icns"
                        jvmversion="1.6+"
                        arguments=""
                        version="${version}"
                        copyright="${name} v${version}, Copyright 2006-2015, Andrew Rambaut"
                        bundleid="pathogen" >
            <javaproperty name="apple.laf.useScreenMenuBar" value="true"/>
            <jarfileset dir="${dist}">
                <include name="**/pathogen.jar"/>
            </jarfileset>
            <jarfileset dir="${Mac_dir}/lib">
                <include name="**/*.jar"/>
            </jarfileset>
        </jarbundler>

        <echo message="Building disk image." />

        <!-- create disk image -->
        <exec executable="/usr/bin/hdiutil">
            <arg value="create"/>
            <arg value="-ov"/>
            <arg value="-srcfolder"/>
            <arg value="${Mac_package_dir}"/>
            <arg value="-volname"/>
            <arg value="${name} v${version}"/>
            <arg value="-imagekey"/>
            <arg value="zlib-level=6"/>
            <arg value="${release_dir}/${name} v${version}.dmg"/>
        </exec>

        <echo message="Mac version release is finished." />
    </target>
</project>