<?xml version="1.0"?>
<project name="Processing PDE" default="build">

  <!-- Current version of Ant that works with Processing -->
  <!-- (Cannot use ant.version as the name of the property
       because that conflicts with the built-in variable) -->
  <property name="ant.version.num" value="1.10.15" />
  <!-- the .zip file to be downloaded -->
  <property name="ant.zip" value="apache-ant-${ant.version.num}-bin.zip" />

  <!-- TODO implement a fallback URL that points to a location
       on download.processing.org so it's available forever. -->
  <property name="ant.url"
            value="https://dlcdn.apache.org//ant/binaries/${ant.zip}" />

  <mkdir dir="lib" />

  <fileset id="ant.files" dir="lib">
    <include name="ant.jar" />
    <include name="ant-launcher.jar" />
  </fileset>

  <condition property="ant.present">
    <resourcecount refid="ant.files" when="eq" count="2" />
  </condition>

  <!-- ok to ignore failed downloads if we at least have a version that's local -->
  <condition property="ant.ignorable" value="false" else="true">
    <isset property="ant.present" />
  </condition>

  <target name="download-ant" unless="ant.present">
    <retry retrycount="5" retrydelay="10000">
      <sequential>
        <get src="${ant.url}" dest="${ant.zip}"
             ignoreerrors="${ant.ignorable}"
             usetimestamp="true" />
    
        <property name="ant.zip.prefix" value="apache-ant-${ant.version.num}/lib" />
    
        <unzip src="${ant.zip}" dest="lib">
          <patternset>
            <!-- unzip a single jar from the ant.zip.prefix subdirectory in the .zip -->
            <include name="${ant.zip.prefix}/ant.jar" />
            <include name="${ant.zip.prefix}/ant-launcher.jar" />
          </patternset>
          <mapper>
            <!-- remove the ant.zip.prefix from the path when saving the .jar -->
            <globmapper from="${ant.zip.prefix}/*" to="*" />
          </mapper>
        </unzip>
      </sequential>
    </retry>
    <delete file="${ant.zip}" />
  </target>

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

  <!-- Current version of JNA that works with Processing -->
  <property name="jna.version" value="5.12.1" />
  <!-- the .zip file to be downloaded -->
  <property name="jna.zip" value="${jna.version}.zip" />

  <!-- TODO implement a fallback URL that points to a location
       on download.processing.org so it's available forever. -->
  <property name="jna.url"
            value="https://github.com/java-native-access/jna/archive/${jna.zip}" />

  <fileset id="jna.files" dir="lib">
    <include name="jna.jar" />
    <include name="jna-platform.jar" />
  </fileset>

  <condition property="jna.present">
    <resourcecount refid="jna.files" when="eq" count="2" />
  </condition>
  <!-- <available file="lib/jna.jar" property="jna.present" /> -->

  <!-- ok to ignore failed downloads if we at least have a version that's local -->
  <condition property="jna.ignorable" value="false" else="true">
    <isset property="jna.present" />
  </condition>

  <target name="download-jna" unless="jna.present">
    <get src="${jna.url}" dest="."
         ignoreerrors="${jna.ignorable}"
         usetimestamp="true" />

    <property name="jna.zip.prefix" value="jna-${jna.version}/dist" />

    <unzip src="${jna.zip}" dest="lib">
      <patternset>
        <!-- unzip a single jar from the jna.zip.prefix subdirectory in the .zip -->
        <include name="${jna.zip.prefix}/jna.jar" />
        <include name="${jna.zip.prefix}/jna-platform.jar" />
      </patternset>
      <mapper>
        <!-- remove the jna.zip.prefix from the path when saving the .jar -->
        <globmapper from="${jna.zip.prefix}/*" to="*" />
      </mapper>
    </unzip>
    <delete file="${jna.zip}" />
  </target>

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

  <property name="flatlaf.version" value="2.4" />

  <fileset id="flatlaf.files" dir="lib">
    <include name="flatlaf.jar" />
  </fileset>

  <!-- TODO implement a fallback URL that points to a location
       on download.processing.org so it's available forever. -->
  <property name="flatlaf.url"
            value="https://repo1.maven.org/maven2/com/formdev/flatlaf/${flatlaf.version}/flatlaf-${flatlaf.version}.jar" />

  <available file="lib/flatlaf.jar" property="flatlaf.present" />

  <!-- ok to ignore failed downloads if we at least have a version that's local -->
  <condition property="flatlaf.ignorable" value="false" else="true">
    <isset property="flatlaf.present" />
  </condition>

  <target name="download-flatlaf" unless="flatlaf.present">
    <get src="${flatlaf.url}" dest="lib/flatlaf.jar"
         ignoreerrors="${flatlaf.ignorable}"
         usetimestamp="true" />
  </target>

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

  <target name="compile" description="Compile sources"
          depends="download-ant, download-jna, download-flatlaf">
    <condition property="core-built">
      <available file="../core/library/core.jar" />
    </condition>
    <fail unless="core-built" message="Please build the core library first and make sure it is located at ../core/library/core.jar" />

    <mkdir dir="bin" />

    <copy todir="bin">
      <fileset dir="src">
        <!-- copy languages files -->
        <include name="processing/app/languages/*.properties" />
        <!-- FlatLaf.properties and any others -->
        <include name="processing/app/laf/*.properties" />
      </fileset>
    </copy>

    <!-- in some cases, pde.jar was not getting built
         https://github.com/processing/processing/issues/1792 -->
    <delete file="pde.jar" />

    <!-- env used to set classpath below -->
    <property environment="env" />

    <javac source="17"
           target="17"
           destdir="bin"
           excludes="**/tools/format/**"
           encoding="UTF-8"
           includeAntRuntime="false"
           classpath="../core/library/core.jar;
                      lib/ant.jar;
                      lib/ant-launcher.jar;
                      lib/flatlaf.jar;
                      lib/jna.jar;
                      lib/jna-platform.jar"
           debug="on"
           nowarn="true">
      <compilerarg value="-Xlint:deprecation" />
      <src path="src" />
      <src path="ant" />
    </javac>
  </target>

  <target name="build" depends="compile" description="Build PDE">
    <jar basedir="bin" destfile="pde.jar" />
  </target>

  <target name="clean-libs" description="Remove the downloaded libraries">
    <delete>
      <fileset refid="ant.files" />
      <fileset refid="flatlaf.files" />
      <fileset refid="jna.files" />
    </delete>

    <!--
    <antcall target="download-ant" />
    <antcall target="download-flatlaf" />
    <antcall target="download-jna" />
    -->
  </target>

  <target name="clean" description="Clean the build directories">
    <delete dir="bin" />
    <delete file="pde.jar" />
  </target>
</project>
