<project name="taskengine" default="compile" basedir=".">

  <import file="build-gwt.xml"/>

  <!-- YOU MUST CONFIGURE THESE FOR YOUR SYSTEM -->
  <property name="appengine.java.sdk" location="../.." />
  <property name="incubator.dir" location="../lib" />

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

  <!-- taskengine specifics configuration; edit these for a new project -->
  <property name="gwt.module" value="com.google.appengine.demos.taskengine.Tasks" />
  <property name="gwt.moduleDeploy" value="tasks" />
  <property name="gwt.startupUrl" value="Tasks.html" />
  <!-- ............................................................ -->

  <!-- Computed variables -->
  <property name="appengine.lib" location="${appengine.java.sdk}/lib" />
  <property name="orm.lib" location="${appengine.java.sdk}/lib/tools/orm" />
  <fail message="Cannot find AppEngine SDK; please reconfigure this build file">
    <condition><not><and>
      <available file="${appengine.lib}" type="dir"/>
      <available file="${orm.lib}" type="dir"/>
    </and></not></condition>
  </fail>

  <condition property="macJVM.arg" value="-XstartOnFirstThread" else="">
    <available file="${gwt.sdk.dir}/gwt-dev-mac.jar" />
  </condition>  
  <condition property="incubator.jar" value="${incubator.dir}/gwt-incubator.jar">
    <available file="${incubator.dir}/gwt-incubator.jar" />
  </condition>

  <fail unless="incubator.jar" message="Cannot find Incubator JAR; please reconfigure this build file"/>

  <condition property="jdo.enable">
    <available file="src/META-INF/jdoconfig.xml" />
  </condition>

  <property name="out.webinf" location="war/WEB-INF" />
  <property name="out.lib" location="${out.webinf}/lib" />
  <property name="out.bin" location="${out.webinf}/classes" />

  <!-- Public build targets -->
  <target name="compile" depends="_gwtc, _serverCode" description="Builds server code and runs a GWT compile." />
  
  <target name="runserver" depends="compile">
    <java classname="com.google.appengine.tools.development.DevAppServerMain"
        classpath="${appengine.java.sdk}/lib/appengine-tools-api.jar"
        fork="true" >
      <arg value="war" />
    </java>
  </target>

  <target name="server" depends="_serverCode" description="Run the AppEngine development server (does not run a GWT compile)">
    <echo message="PLEASE BROWSE TO: http://localhost:8080/${gwt.startupUrl}"/>
    <java classname="com.google.appengine.tools.development.DevAppServerMain" classpath="${appengine.lib}/appengine-tools-api.jar" fork="yes">
      <arg value="war" />
    </java>
  </target>

  <path id="enhancer.classpath">
    <fileset dir="${orm.lib}">
      <include name="**/*.jar"/>
    </fileset>
    <pathelement location="${gwt.sdk.dir}/gwt-servlet.jar"/>
    <pathelement location="${gwt.sdk.dir}/gwt-user.jar" />
    <pathelement location="${out.bin}"/>
    <pathelement location="${incubator.jar}"/>
  </path>

  <target name="clean" description="Cleans this project's intermediate and output files">
    <delete dir="${out.bin}" failonerror="false" />
    <delete dir="${out.lib}" failonerror="false" />
    <delete dir="war/gwt" failonerror="false" />
    <delete dir="war/${gwt.moduleDeploy}" failonerror="false" />
  </target>

  <!-- Private build targets -->
  <target name="_serverCode" depends="_enhance" />
  
  <target name="_enhance" depends="_javac" if="jdo.enable" > 
    <taskdef name="datanucleusenhancer" classpathref="enhancer.classpath"
        classname="org.datanucleus.enhancer.tools.EnhancerTask"/>
    <datanucleusenhancer classpathref="enhancer.classpath" failonerror="true">
      <fileset dir="${out.bin}">        
        <include name="**/*.class"/>
      </fileset>
    </datanucleusenhancer>
  </target>

  <target name="_javac" depends="gwt.checkdistro,_libs">
    <mkdir dir="${out.bin}" />
    <javac srcdir="src" destdir="${out.bin}" debug="true" debuglevel="lines,vars,source"
        source="1.5" nowarn="true" encoding="utf-8">
      <classpath>
        <fileset dir="${orm.lib}">
          <include name="**/*.jar"/>
        </fileset>
        <fileset dir="${appengine.lib}/user">
          <include name="*.jar" />
        </fileset>
        <pathelement location="${gwt.sdk.dir}/gwt-user.jar" />
        <pathelement location="${incubator.jar}" />             
      </classpath>
    </javac>
    <copy todir="${out.bin}">
      <fileset dir="src">
        <include name="**/*.properties" />
        <include name="**/jdoconfig.xml" />
      </fileset>
    </copy>
  </target>

  <target name="_libs" depends="_jdoLibs">
    <!-- Copy libs to lib directory -->
    <mkdir dir="${out.lib}" />
    <copy todir="${out.lib}">
      <fileset dir="${appengine.lib}/user">
        <include name="*.jar" /> 
      </fileset>
    </copy>
    <copy todir="${out.lib}" file="${gwt.sdk.dir}/gwt-servlet.jar" />
  </target>

  <target name="_jdoLibs" if="jdo.enable">
    <!-- Copy datanuclues libs to lib directory -->
    <mkdir dir="${out.lib}" />
    <copy todir="${out.lib}">
      <fileset dir="${appengine.lib}/user/orm">
        <include name="**/*.jar" /> 
      </fileset>
    </copy>
  </target>

  <path id="frontend.classpath">
    <fileset dir="${orm.lib}">
      <include name="**/*.jar"/>
    </fileset>
    <pathelement path="src" />
    <pathelement location="${out.bin}" />
    <fileset file="${gwt.sdk.dir}/gwt-user.jar" />
    <fileset file="${gwt.sdk.dir}/gwt-dev-${gwt.platform}.jar" />
    <pathelement location="${incubator.jar}"/>
  </path>

  <target name="_gwtc" depends="gwt.checkdistro,_javac">
    <compile.gwtc module="${gwt.module}"
        srcdir="src"
        wardir="war"
        classpathref="frontend.classpath" />
  </target>

</project>
