<project name="lightcrafts" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

  <tstamp/>

  <!--===== Properties =====================================================-->

  <property file="resources/com/lightcrafts/utils/resources/Version.properties"/>

  <property name="common-jar" value="products/lightcrafts.jar"/>

	<exec executable="cat" vmlauncher="true" failonerror="true"
	      outputproperty="app-version">
		<arg value="version.txt"/>
	</exec>

  <condition property="UNIVERSAL" value="1" else="0">
    <isset property="UNIVERSAL"/>
  </condition>
  <condition property="USE_ICC" value="1" else="0">
    <isset property="USE_ICC"/>
  </condition>

	<!--===== Application build ==============================================-->

  <condition property="MAKE" value="make">
    <not>
      <or>
        <os name="freebsd"/>
        <os name="SunOS"/>
      </or>
    </not>
  </condition>
  <condition property="MAKE" value="gmake">
    <or>
      <os name="freebsd"/>
      <os name="SunOS"/>
    </or>
  </condition>

  <target name="resolve" depends="init-ivy" unless="offline">
    <ivy:resolve file="${basedir}/ivy.xml"/>
    <ivy:retrieve/>
  </target>

  <property name="ivy.version" value="2.4.0"/>
  <property name="ivy.jar.dir" value="${basedir}/lib"/>
  <property name="ivy.jar.file"
            value="${ivy.jar.dir}/ivy.jar"/>

  <target name="download-ivy" unless="offline">
    <mkdir dir="${ivy.jar.dir}"/>
    <get src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar"
         dest="${ivy.jar.file}" usetimestamp="true"/>
  </target>

  <target name="init-ivy" depends="download-ivy" unless="ivy.initialized">
    <path id="ivy.lib.path">
      <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
    </path>
    <taskdef resource="org/apache/ivy/ant/antlib.xml"
             uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
    <property name="ivy.initialized" value="true"/>
  </target>

  <available file="/usr" type="dir" property="have.usr"/>

  <path id="compile.classpath">
    <fileset dir="lib">
      <include name="*.jar"/>
    </fileset>
  </path>

  <target name="init_classpath" if="have.usr">
    <path id="compile.classpath">
      <fileset dir="lib">
        <include name="*.jar"/>
      </fileset>
      <fileset dir="/usr">
        <include name="share/java*/**/*.jar"/>
        <include name="local/share/java/classes/*.jar"/>
        <include name="jdk/packages/lib/ext/*.jar"/>
      </fileset>
    </path>
  </target>

  <target name="javac" depends="resolve, init_classpath">
    <exec executable="${MAKE}" dir="products" failonerror="true"
          vmlauncher="true">
      <arg value="-j"/>
    </exec>
    <mkdir dir="build"/>
    <mkdir dir="javah"/>
    <javac srcdir="src" destdir="build" includeantruntime="false" debug="true"
      release="11" nativeheaderdir="javah" classpathref="compile.classpath"/>
  </target>

  <target name="coprocesses">
    <exec executable="${MAKE}" dir="coprocesses" failonerror="true"
          vmlauncher="true">
      <arg value="USE_ICC=${USE_ICC}"/>
      <arg value="UNIVERSAL=${UNIVERSAL}"/>
    </exec>
  </target>

  <target name="revision">
    <mkdir dir="build/com/lightcrafts/utils/resources"/>
    <exec executable="git">
      <arg line="log -1"/>
      <redirector output="build/com/lightcrafts/utils/resources/Revision"
                  logError="true"/>
    </exec>
    <exec executable="git">
      <arg line="config --list"/>
      <redirector output="build/com/lightcrafts/utils/resources/Revision"
                  logError="true" append="true"/>
    </exec>
    <copy file="version.txt"
          tofile="build/com/lightcrafts/utils/resources/Version"/>
  </target>

  <target name="resources" depends="revision">
    <copy todir="build">
      <fileset dir="resources" excludes=".git"/>
      <fileset dir="locale" excludes=".git"/>
    </copy>
  </target>

  <target name="jni">
    <exec executable="${MAKE}" dir="jnisrc" failonerror="true" vmlauncher="true">
      <arg value="USE_ICC=${USE_ICC}"/>
      <arg value="UNIVERSAL=${UNIVERSAL}"/>
    </exec>
  </target>

  <target name="build" depends="javac, coprocesses, resources, jni"/>

	<!--===== Make a jar file ================================================-->

  <target name="jar" depends="build">
    <delete file="${common-jar}"/>
    <jar jarfile="${common-jar}">
      <fileset dir="build"/>
      <manifest>
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name="Main-Class" value="com.lightcrafts.app.Application"/>
        <attribute name="Implementation-Title" value="${app-name}"/>
        <attribute name="Implementation-Vendor" value="Light Crafts, Inc."/>
        <attribute name="Implementation-Version"
                   value="${app-version} ${TODAY}"/>
      </manifest>
    </jar>
  </target>

  <target name="check-for-jar">
    <condition property="jar-exists">
      <available file="${common-jar}"/>
    </condition>
  </target>

  <target name="jar-if-necessary" depends="check-for-jar" unless="jar-exists">
    <antcall target="jar"/>
  </target>

	<!--===== Make test ======================================================-->

  <target name="test" depends="build">
    <ant dir="test" target="build"/>
    <ant dir="test" target="docs-test"/>
  </target>

	<!--===== Run the application ============================================-->

  <target name="run" depends="jar-if-necessary">
    <java classname="com.lightcrafts.app.Application" dir="products"
          failonerror="true" fork="true" maxmemory="768m">
      <classpath>
        <fileset dir="products">
          <include name="*.jar"/>
        </fileset>
      </classpath>
      <sysproperty key="java.library.path" value="."/>
      <sysproperty key="lightcrafts.debug" value="1"/>
    </java>
  </target>

  <!-- Same as the "run" target, but with the test classes in the classpath: -->
  <target name="run-test" depends="jar-if-necessary">
    <java classname="com.lightcrafts.app.Application" dir="products"
          failonerror="true" fork="true" maxmemory="768m">
      <classpath>
        <fileset dir="products">
          <include name="*.jar"/>
        </fileset>
        <pathelement path="test/build"/>
      </classpath>
      <assertions>
        <enable/>
      </assertions>
      <sysproperty key="java.library.path" value="."/>
      <sysproperty key="lightcrafts.debug" value="1"/>
      <sysproperty key="com.sun.management.jmxremote" value=""/>
      <jvmarg value="-Xdebug"/>
      <jvmarg value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"/>
    </java>
  </target>

	<!--===== Javadoc ========================================================-->

  <target name="javadoc">
    <mkdir dir="doc"/>
    <javadoc destdir="doc">
      <fileset dir="src"/>
    </javadoc>
  </target>

  <!--===== Clean up =======================================================-->

  <target name="clean-up-native">
    <exec executable="${MAKE}" dir="jnisrc">
      <arg value="-j"/>
      <arg value="${clean-method}"/>
    </exec>
    <exec executable="${MAKE}" dir="coprocesses">
      <arg value="-j"/>
      <arg value="${clean-method}"/>
    </exec>
  </target>

  <target name="clean-native">
    <antcall target="clean-up-native">
      <param name="clean-method" value="distclean"/>
    </antcall>
  </target>

  <target name="clean-up">
    <parallel>
      <delete dir="build"/>
      <delete dir="javah"/>
    </parallel>
    <antcall target="clean-native"/>
    <exec executable="${MAKE}" dir="products">
      <arg value="-j"/>
      <arg value="${clean-method}"/>
    </exec>
    <ant dir="test" target="${clean-method}"/>
  </target>

  <target name="distclean">
    <antcall target="clean-up">
      <param name="clean-method" value="distclean"/>
    </antcall>
  </target>
  <target name="clean" depends="distclean"/>

  <target name="mostlyclean">
    <antcall target="clean-up">
      <param name="clean-method" value="mostlyclean"/>
    </antcall>
  </target>

</project>
<!-- vim:set et sw=2 ts=2: -->
