<project name="webpages" default="main" basedir=".">
<!--
  The values to customize for each application:

  appname: the name of the application ( "simple" ). Defaults to 
           project name, if set.


  Global properties you must set( can be set in jaxm.properties )

  servlet.jar: location of servlet.jar

  Other properties, with defaults:

  webapps.dir: directory where the app will be built. 
          Default to in-place ( very usefull if you create your app under webapps/ ).

  wars.dir: directory where the .war file will be constructed. 
            If not set, no war will be generated.

  compile.debug
  compile.deprecation
  compile.optimize: javac customization

  -->
  <property file="${user.home}/build.properties"/>   <!-- User local        -->
  <property file="${user.home}/.ant.properties"/>    <!-- User local        -->
  <property file="build.properties"/>                <!-- Component local   -->
  <property file="../../build.properties"/>          <!-- For all webapps -->
  <property file="../../../build.properties"/>             
 
  <property name="appname" value="${ant.project.name}"/>

  <property name="appdir" location=".."/>

  <property name="compile.debug" value="true" />
  <property name="compile.optimize" value="false" />
  <property name="compile.deprecation" value="false" />

  <!-- Classpath
  <fileset id="extra-classpath" dir="." >
  </fileset>
  -->
  <!-- ==================== Init targets ==================== -->
  <target name="init" depends="checks,build.dir.webapps,build.dir.local"
	  description="Initialization and checkup" >
    <echo message="-------- Building ${appname} to ${build.dir} "/>
  </target>
  
  <target name="prepare.build" 
	  if="webapps.dir"
	  description="Preparing the webapp dir if not in-place" >
    <mkdir dir="${webapps.dir}/${appname}"/>
    <mkdir dir="${webapps.dir}/${appname}/WEB-INF"/>
    <mkdir dir="${webapps.dir}/${appname}/WEB-INF/lib"/>
    <mkdir dir="${webapps.dir}/${appname}/WEB-INF/src"/>
    <mkdir dir="${webapps.dir}/${appname}/WEB-INF/classes"/>
    <copy todir="${webapps.dir}/${appname}" >
      <fileset dir="${appdir}" />
    </copy>
  </target>

  <target name="prepare" depends="init,prepare.build">
    <mkdir dir="${build.dir}/${appname}/WEB-INF/lib"/>
    <mkdir dir="${build.dir}/${appname}/WEB-INF/classes"/>
    <mkdir dir="${appdir}/WEB-INF/lib"/>
  </target>
  
  <!-- ==================== Build ==================== -->

  <target name="main" depends="prepare,compile.src,compile.classes" />

  <target name="compile.src" if="src.available" >
    <javac srcdir="${appdir}/WEB-INF/src"
           destdir="${build.dir}/${appname}/WEB-INF/classes"
	   debug="${compile.debug}"
	   deprecation="${compile.deprecation}"
	   optimize="${compile.optimize}">
      <classpath >
	<pathelement location="${servlet.jar}" />
	<fileset id="app-classpath" dir="${appdir}/WEB-INF/lib">
	  <include name="*.jar" />
	</fileset>
      </classpath>
    </javac>
    <copy todir="${build.dir}/${appname}/WEB-INF/classes">
      <fileset dir="${appdir}/WEB-INF/src">
	<include name="**/*.properties" />
	<include name="**/*.dtd" />
	<include name="**/*.xml" />
      </fileset>
    </copy>
  </target>

  <target name="compile.classes" if="classes.available" >
    <javac srcdir="${appdir}/WEB-INF/classes"
           destdir="${build.dir}/${appname}/WEB-INF/classes"
	   debug="${compile.debug}"
	   deprecation="${compile.deprecation}"
	   optimize="${compile.optimize}">
      <classpath >
	<pathelement location="${servlet.jar}" />
	<fileset id="app-classpath" dir="${appdir}/WEB-INF/lib">
	  <include name="*.jar" />
	</fileset>
      </classpath>
    </javac>

  </target>

  <!-- ==================== Cleanup & Packaging ==================== -->

  <target name="clean" depends="init">
    <delete dir="{build.dir}/${appname}/WEB-INF/classes"/>
  </target>

  <target name="war" 
	  depends="main" 
	  description="Creating the WAR file" 
	  if="wars.dir" >
    <mkdir dir="${wars.dir}" />
    <jar jarfile="${wars.dir}/${appname}.war"
	 basedir="${build.dir}/${appname}" >
      <include name="**"/>
    </jar>
  </target>

  <!-- ==================== Logic and Guessing ==================== -->

  <target name="checks" depends="set.available,check.appname,check.servlet" />

  <target name="set.available" >
    <!-- Environement check -->
    <property name="src.base" location="../.." />
    <available property="appname.correct" file="../../${appname}/WEB-INF/build.xml" />
    <available property="servlet.available" file="${servlet.jar}" />
    <available property="src.available" file="src" />
    <available property="classes.available" file="classes" />
  </target>
  
  <target name="check.servlet" unless="servlet.available" >
    <fail message="Servlet.jar not found at ${servlet.jar}, make sure servlet.jar is set" />
  </target>

  <target name="check.appname" unless="appname.correct" >
    <fail message="Appname not set, couldn't find ${appname} in ${src.base}" />
  </target>
  
  <target name="build.dir.webapps" if="webapps.dir" >
    <property name="build.dir" value="${webapps.dir}" />
  </target>

  <target name="build.dir.local" unless="webapps.dir" >
    <property name="build.dir" location="../.." />
  </target>

</project>
