<!-- Build BEAST FX. JUnit test is available for this build. -->
<project basedir="." default="build_jar_all_BeastFX" name="BUILD_BEAST_FX">
	<description>
	    Build BEAST FX. 
	    JUnit test is available for this build.
	    $Id: build_BEAST_FX.xml $
	</description>

	<!-- set global properties for this build -->
	<property name="srcFX" location="../BeastFX/src" />
	<property name="buildFX" location="../BeastFX/build" />
	<property name="libFX" location="../BeastFX/locallib" />
	<!--
	<property name="javafx-home" location="/usr/share/java/"/>
-->
	<property name="javafx-home" location="/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/"/>
	<property name="release_dir" value="release" />
	<property name="distFX" location="${buildFX}/dist" />
	<property name="beast2path" location="../beast2" />
	<property name="srcBeast2" location="${beast2path}/src" />
	<property name="beast2classpath" location="${beast2path}/build" />
	<property name="Add_on_dir" value="${release_dir}/add-on" />

    <import file="${beast2path}/build.xml" />


	<path id="classpath">
		<fileset dir="${libFX}" includes="testfx.jar"/>
		<fileset dir="${libFX}" includes="aquafx-0.1.jar"/>
		<fileset dir="resources/themes" includes="FXSkins-1.0.0.jar"/>
		<fileset dir="resources/themes" includes="jmetro-11.6.15.jar"/>
        <path location="${buildFX}"/>
        
		<fileset dir="${libFX}" includes="jam.jar"/>
		<fileset dir="${beast2path}/lib" includes="beagle.jar"/>
		<fileset dir="${beast2path}/lib" includes="colt.jar"/>
        <fileset dir="${beast2path}/lib" includes="antlr-runtime-4.10.1.jar"/>
		<fileset dir="${beast2path}/lib" includes="commons-math3-3.6.1.jar"/>

        <fileset dir="${beast2path}/lib/junit" includes="junit-platform-console-standalone-1.8.2.jar"/>
        <fileset dir="${libFX}" includes="assertj-core-3.20.2.jar"/>
	
		<fileset dir="${javafx-home}" includes="jfxrt.jar"/>
		<fileset dir="${javafx-home}/../" includes="jfswt.jar"/>
<!--
		<fileset dir="${javafx-home}" includes="javafx-swt.jar"/>
		<fileset dir="${javafx-home}" includes="javafx-base.jar"/>
		<fileset dir="${javafx-home}" includes="javafx-controls.jar"/>
		<fileset dir="${javafx-home}" includes="javafx-fxml.jar"/>
		<fileset dir="${javafx-home}" includes="javafx-graphics.jar"/>
		<fileset dir="${javafx-home}" includes="javafx-media.jar"/>
		<fileset dir="${javafx-home}" includes="javafx-swing.jar"/>
		<fileset dir="${javafx-home}" includes="javafx-web.jar"/>
-->
        <path location="${beast2classpath}"/>
	</path>

	<!-- start -->
	<target name="initBeastFX">
		<echo message="${ant.project.name}: ${ant.file}" />

		<available file="version.xml" property="versionAvailable"/>
        <fail unless="versionAvailable">
              ** Required file version.xml does not exist. **
        </fail>

        <!-- Read package name and version from xml file -->
        <xmlproperty file="version.xml" prefix="fromVersionFile" />
        <property name="version" value="${fromVersionFile.package(version)}" />
        <echo message="Version: ${version}" />
	</target>

	<target name="cleanBeastFX">
    	<delete dir="${buildFX}" />
	</target>

	<!-- clean previous build, and then compile Java source code, and Juint test -->
	<target name="build_all_BeastFX" depends="cleanBeastFX,compile-allBeastFX,junitBeastFX"
        description="Clean and Build all run-time stuff">
	</target>

	<!-- clean previous build, compile Java source code, and Junit test, and make the beast.jar and beauti.jar -->
	<target name="build_jar_all_BeastFX" depends="cleanBeastFX,compile-allBeastFX,junitBeastFX,dist_all_BeastFX"
        description="Clean and Build all run-time stuff">
	</target>


	<!-- No JUnit Test, clean previous build, compile Java source code, and make the BeastFX.jar and beauti.jar -->
	<target name="build_jar_all_BeastFX_NoJUnitTest" depends="cleanBeastFX,compile-allBeastFX,dist_all_BeastFX"
        description="Clean and Build all run-time stuff">
	</target>

	<!-- compile Java source code -->
	<target name="compile-allBeastFX" depends="initBeastFX,beast.pkgmgmt,beast.base">

	    <!-- Capture the path as a delimited property using the refid attribute -->
	    <property name="myclasspath" refid="classpath"/>
	    <!-- Emit the property to the ant console -->
	    <echo message="Classpath = ${myclasspath}"/>

		<mkdir dir="${buildFX}" />

		<!-- Compile the java code from ${srcFX} into ${buildFX} /bin -->
		<javac srcdir="${srcFX}" destdir="${buildFX}" classpathref="classpath" 
		       fork="true"
		       memoryinitialsize="256m"
		       memorymaximumsize="1024m"
		       >
			<include name="beastfx/**/**" />
			<!-- compile JUnit test classes -->
			<include name="test/**" />
		</javac>
        <copy todir="${build}">
            <fileset dir="${srcFX}" includes="**/*.png" />
        </copy>

		<echo message="Successfully compiled." />
	</target>

	<!-- make the BeastFX.hjar and TreeAnnotator.jar -->
	<target name="dist_all_BeastFX" depends="compile-allBeastFX" description="create BeastFX jar">
		<!-- Create the distribution directory -->
		<mkdir dir="${distFX}" />

		<!-- Put everything in ${buildFX} into the beast.jar file -->
		<jar jarfile="${distFX}/BeastFX.jar">
			<manifest>
				<attribute name="Built-By" value="${user.name}" />
				<attribute name="Main-Class" value="${main_class_BEASTFX}" />
			</manifest>
			<fileset dir="${buildFX}">
				<include name="**/*.class" />
				<include name="javascript/**/*" />
				<include name="beastfx*" />
			</fileset>
			<fileset dir="${beast2classpath}">
				<include name="**/*" />
			</fileset>
			<zipgroupfileset dir="${beast2path}/lib" includes="jam.jar" />
			<zipgroupfileset dir="${beast2path}/lib" includes="beagle.jar" />
			<zipgroupfileset dir="${beast2path}/lib" includes="colt.jar" />
			<zipgroupfileset dir="${beast2path}/lib" includes="commons-math3-3.1.1.jar" />
		</jar>

		<jar jarfile="${distFX}/BeastFX.src.jar">
			<fileset dir="${srcFX}">
				<include name="**/*" />
			</fileset>
		</jar>
		<jar jarfile="${distFX}/BeastFX.addon.jar">
			<manifest>
				<attribute name="Built-By" value="${user.name}" />
			</manifest>
			<fileset dir="${buildFX}">
				<include name="**/*"/>
			</fileset>
		</jar>
	</target>

	<!-- run beast.jar -->
	<target name="run_BeastFX">
		<java jar="${distFX}/BeastFX.jar" fork="true" />
	</target>

	
	<!-- JUnit test -->
	<target name="junitBeastFX">
		<mkdir dir="${report}" />
		
		<copy todir="${libFX}">
			<fileset dir="${javafx-home}" includes="jfxrt.jar"/>
			<fileset dir="${javafx-home}/../" includes="jfswt.jar"/>
			<!--
			<fileset dir="${javafx-home}" includes="javafx-base-11.jar"/>
			<fileset dir="${javafx-home}" includes="javafx-controls-11.jar"/>
			<fileset dir="${javafx-home}" includes="javafx-fxml-11.jar"/>
			<fileset dir="${javafx-home}" includes="javafx-graphics-11.jar"/>
			<fileset dir="${javafx-home}" includes="javafx-web-11.jar"/>
			<fileset dir="${javafx-home}" includes="javafx-media-11.jar"/>
			<fileset dir="${javafx-home}" includes="javafx-swing-11.jar"/>
		-->
		</copy>

		<java classpathref="classpath" classname="org.junit.platform.console.ConsoleLauncher" fork="true" failonerror="true">
			<!--jvmarg line="-Djava.library.path=/usr/lib/x86_64-linux-gnu/jni/"/-->
			<jvmarg line="-Djava.library.path=/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64"/>
			<jvmarg line="-Dbeast.user.package.dir=NONE"/>
			<!--
				IF UNCOMMENTING, RESTORE DOUBLE - - in next two lines
			<jvmarg line="- - module-path=${libFX}"/>
			<jvmarg line="- - add-modules=javafx.base,javafx.fxml,javafx.graphics,javafx.web,javafx.media,javafx.swing"/>
		-->
            <arg line="-c test.beastfx.app.beauti.BeautiSimpleTest"/>
            
            <arg line="--reports-dir build/junitreport"/>
        </java>
        <junitreport todir="build/junitreport">
            <fileset dir="build/junitreport">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="build/junitreport"/>
        </junitreport>
		<echo message="JUnit test finished." />
	</target>
    
    <target name="junitreport">
		<junitreport todir="${report}">
			<fileset dir="${report}" includes="*.xml"/>
			<report format="frames" todir="${report}"/>
		</junitreport>
		<echo message="JUnit test report finished." />
	</target>


	<target name="test-all" depends="junitBeastFX">
		<echo message="All tests done." />
	</target>

	<target name="addon"
	        depends="build_jar_all_BeastFX_NoJUnitTest"
	        description="release BEAST 2 package version of BeastFX">

		<delete dir="${Add_on_dir}" />
		<!-- Create the release directory -->
		<mkdir dir="${Add_on_dir}" />
		<mkdir dir="${Add_on_dir}/lib" />
		<mkdir dir="${Add_on_dir}/examples" />
		<mkdir dir="${Add_on_dir}/templates" />
		<mkdir dir="${Add_on_dir}/html" />

		<copy todir="${Add_on_dir}">
			<fileset file="version.xml"/>
		</copy>
		<copy todir="${Add_on_dir}/examples">
			<fileset dir="examples" includes="*.xml">
			</fileset>
		</copy>

		<copy todir="${Add_on_dir}/html">
			<fileset dir="html" includes="**/*"/>
		</copy>

		<copy todir="${Add_on_dir}/lib">
			<fileset  file="${distFX}/BeastFX.addon.jar" />
		</copy>
		<copy todir="${Add_on_dir}">
			<fileset file="${distFX}/BeastFX.src.jar" />
		</copy>
<!--
		<copy todir="${Add_on_dir}/templates">
			<fileset file="templates/BeastFX.xml" />
		</copy>
-->
		<jar jarfile="${distFX}/BeastFX.addon.v${version}.zip">
			<fileset dir="${Add_on_dir}">
				<include name="**/*" />
			</fileset>
        </jar>
		<echo message="Add-on version release is finished." />
	</target>

</project>
