<?xml version="1.0" encoding="UTF-8" ?>

<project name="jdee" default="build">
  <!-- ** properties start ** -->


  <!-- configuration directory -->
  <property name="config.dir" location="./config"/>

  <!-- source code -->
  <property name="src.lisp.dir" location="./lisp"/>
  <pathconvert property="unix.src.lisp.dir" targetos="unix">
    <path path="${src.lisp.dir}"/>
    <mapper>
      <globmapper from="*" to="*"/>
    </mapper>
  </pathconvert>    
  <property name="src.java.dir" location="./java/src"/>

  <!-- third party libraries -->
  <property name="lib.java.dir" location="./java/lib"/>

  <!-- create objects -->
  <property name="build.dir" location="./build"/>

  <property name="build.config.dir" location="${build.dir}/config"/>
  <property name="config.build.file" location="./build.properties"/>

  <property name="build.lisp.dir" location="${build.dir}/lisp"/>
  <pathconvert property="unix.build.lisp.dir" targetos="unix">
    <path path="${build.lisp.dir}"/>
    <mapper>
      <globmapper from="*" to="*"/>
    </mapper>
  </pathconvert>    
  <property name="build.lisp.src.file" location="${config.dir}/build.el"/>
  <property name="build.lisp.dst.file" location="${build.config.dir}/build.el"/>
  <!-- for now, changes to this need to be reflected in the `require' in jde.el -->
  <property name="build.lisp.autoload.libname" value="jde-autoload"/>
  <property name="build.lisp.autoload.file" location="${build.lisp.dir}/${build.lisp.autoload.libname}.el"/>
  <property name="build.java.dir" location="${build.dir}/classes"/>

  <!-- staging  -->
  <property name="dist.dir" location="./dist"/>
  <property name="dist.lisp.dir" location="${dist.dir}/lisp"/>
  <property name="dist.java.dir" location="${dist.dir}/java"/>
  <property name="dist.java.lib.dir" location="${dist.java.dir}/lib"/>
  <property name="dist.java.src.dir" location="${dist.java.dir}/src"/>
  <property name="dist.java.doc.dir" location="${dist.dir}/java/doc"/>
  <property name="dist.jar.jde.file" location="${dist.java.lib.dir}/jde.jar"/>

  <property name="dist.doc.dir" location="${dist.dir}/doc"/>
  <property name="dist.doc.java.api.dir" location="${dist.doc.dir}/java/api"/>
  <property name="dist.doc.html.dir" location="${dist.doc.dir}/html"/>

  <path id="compile.classpath">
    <fileset dir="${lib.java.dir}"/>
  </path>

  <!-- ** properties end ** -->


  <target name="init">
    <tstamp/>
    <!-- developers will usually have the following config file (i.e. source
	 installed libs) -->
    <property file="${user.home}/.jdee-config.properties"/>

    <property file="${config.build.file}"/>

    <condition property="build.bin.emacs"
	       value="/Applications/Emacs.app/Contents/MacOS/Emacs">
      <and>
	<os family="mac"/>
	<available file="/Applications/Emacs.app" type="dir"/>
      </and>
    </condition>
    <condition property="build.bin.emacs"
	       value="${user.home}/Applications/Emacs.app/Contents/MacOS/Emacs">
      <and>
	<os family="mac"/>
	<available file="${user.home}/Applications/Emacs.app" type="dir"/>
      </and>
    </condition>

    <uptodate property="config.uptodate" srcfile="${config.build.file}"
	      targetfile="${config.build.file}"/>
    <mkdir dir="${build.dir}"/>
  </target>


  <!-- determine whether or not we need to compile emacs lisp/autoload files -->
  <target name="prepare-lisp-build">
    <mkdir dir="${build.lisp.dir}"/>

    <condition property="lisp.uptodate">
      <and>
	<available file="${build.lisp.autoload.file}"/>
	<uptodate>
	  <srcfiles dir="${build.lisp.dir}" includes="*.el"/>
	  <mapper type="glob" from="*.el" to="*.elc"/>
	</uptodate>
      </and>
    </condition>
  </target>

  <target name="configure" depends="init,prepare-lisp-build"
	  unless="config.uptodate"
	  description="create the configuration file the user edits for builds">

    <!-- defaults -->
    <property name="build.bin.emacs" value="emacs"/>
    <!-- end defaults -->


    <mkdir dir="${build.config.dir}"/>

    <!-- project specific defaults (not user or instance specific) -->
    <property file="${config.dir}/default.properties"/>

    <propertyfile file="${config.build.file}"
		  comment="user editable build configuration parameters">
      <entry key="config.time" type="date" value="now"/>
      <entry key="cedet.dir" value="${cedet.dir}"/>
      <entry key="elib.dir" value="${elib.dir}"/>
      <entry key="prefix.dir" value="${prefix.dir}"/>
      <entry key="build.bin.emacs" value="${build.bin.emacs}"/>
    </propertyfile>
  </target>


  <!-- configure (create/build) the emacs lisp build configuration files -->
  <target name="configure-ebuild" depends="configure,init"
	  unless="lisp.uptodate">
    <!-- copy the lisp to the build directory -->
    <copy todir="${build.lisp.dir}">
      <fileset dir="${src.lisp.dir}" includes="*.el"/>
    </copy>

    <!-- create the file used to compile the emacs lisp code -->
    <copy file="${build.lisp.src.file}" tofile="${build.lisp.dst.file}"/>
    <replace file="${build.lisp.dst.file}">
      <replacefilter token="@{cedet.dir}" value="${cedet.dir}"/>
      <replacefilter token="@{src.lisp.dir}" value="${unix.src.lisp.dir}"/>
      <replacefilter token="@{build.lisp.dir}" value="${unix.build.lisp.dir}"/>
      <replacefilter token="@{build.lisp.autoload.libname}" value="${build.lisp.autoload.libname}"/>
    </replace>
  </target>


  <target name="build-lisp" depends="configure-ebuild" unless="lisp.uptodate"
	  description="compile the JDEE Emacs lisp code">
    <!-- invoke Emacs in batch mode to creat autoloads and compile emacs
         lisp -->
    <exec dir="${build.lisp.dir}" executable="${build.bin.emacs}" failonerror="true">
      <arg value="--script"/>
      <arg value="${build.lisp.dst.file}"/>
    </exec>
  </target>


  <target name="build-java" depends="init"
	  description="compile the JDEE Java source code">
    <mkdir dir="${build.java.dir}"/>
    <javac srcdir="${src.java.dir}" destdir="${build.java.dir}"
	   classpathref="compile.classpath"/>
  </target>


  <target name="build" depends="build-lisp, build-java"
	  description="compile all JDEE source code"/>


  <target name="dist" depends="build" description="create the &quot;binary&quot; distribution">
    <!-- create dist java lib directory -->
    <mkdir dir="${dist.lisp.dir}"/>
    <copy todir="${dist.lisp.dir}">
      <fileset dir="${build.lisp.dir}"/>
    </copy>

    <!-- this subdir contains all things Java -->
    <mkdir dir="${dist.java.lib.dir}"/>
    <!-- third party libraries -->
    <copy todir="${dist.java.lib.dir}">
      <fileset dir="${lib.java.dir}"/>
    </copy>

    <!-- for those who want to look at the Java source -->
    <mkdir dir="${dist.java.src.dir}"/>
    <copy todir="${dist.java.src.dir}">
      <fileset dir="${src.java.dir}"/>
    </copy>

    <!-- user guide docs -->
    <mkdir dir="${dist.doc.html.dir}"/>
    <copy todir="${dist.doc.html.dir}">
      <!-- use the checked in generated docs for now -->
      <fileset dir="./doc/html" includes="**"/>
    </copy>

    <!-- create the JDE jar file -->
    <jar destfile="${dist.jar.jde.file}"
	 basedir="${build.java.dir}"
	 includes="**/*.class"/>

    <!--javadoc sourcepath="${src.java.dir}"
	     destdir="${dist.doc.java.api.dir}"
	     classpathref="compile.classpath"
	     author="true" version="true" use="true"
	     windowtitle="JDEE Java API"
	     /-->
  </target>

  <target name="install" depends="init"
	  description="install the derived objects (the entire dist)">
      <mkdir dir="${prefix.dir}"/>
      <copy todir="${prefix.dir}">
	  <fileset dir="${dist.dir}"/>
      </copy>
  </target>

  <target name="clean" depends="init"
	  description="delete all derived objects EXCEPT the user edited configuration file">
    <delete dir="${build.dir}"/>
    <delete dir="${dist.dir}"/>
  </target>

  <target name="clean-all" depends="clean"
	  description="delete all derived objects">
    <delete file="${config.build.file}"/>
  </target>

</project>
