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

<!-- ============================================================================= +
 | Copyright (C) 1999-2004, The Apache Software Foundation.   All rights reserved. |
 + =============================================================================== +
 |                                                                                 |
 | Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
 | this file except in  compliance with the License.  You may obtain a copy of the |
 | License at <http://www.apache.org/licenses/LICENSE-2.0>.                        |
 |                                                                                 |
 | Unless required by applicable law or agreed to in writing, software distributed |
 | under the License  is distributed on an  "AS IS"  BASIS,  WITHOUT WARRANTIES OR |
 | CONDITIONS  OF ANY KIND,  either express  or implied.  See the License  for the |
 | specific language governing permissions and limitations under the License.      |
 + ============================================================================== -->

<project name="Apache Cocoon Kernel" default="all">

  <property name="version"                  value="0.1"/>
  <property name="package"                  value="org.apache.cocoon.kernel"/>

  <property name="jar.startup"              value="cocoon-kernel-startup-${version}.jar"/>
  <property name="jar.runtime"              value="cocoon-kernel-runtime-${version}.jar"/>
  
  <property name="build.dir"                value="${basedir}/build"/>
  <property name="build.kernel.dir"         value="${build.dir}/kernel"/>
  <property name="build.startup.dir"        value="${build.dir}/startup"/>
  <property name="build.runtime.dir"        value="${build.dir}/runtime"/>
  <property name="build.documentation.dir"  value="${build.dir}/documentation"/>

  <property name="source.dir"               value="${basedir}/sources"/>
  <property name="source.webapp.dir"        value="${source.dir}/webapp"/>
  <property name="source.startup.dir"       value="${source.dir}/startup"/>
  <property name="source.runtime.dir"       value="${source.dir}/runtime"/>
  <property name="source.documentation.dir" value="${source.dir}/documentation"/>
  
  <property name="webapp.dir"               value="${basedir}/webapps"/>
  <property name="webapp.docs.dir"          value="${webapp.dir}/docs"/>
  <property name="webapp.root.dir"          value="${webapp.dir}/root"/>

  <property name="tools.dir"                value="${source.dir}/tools"/>

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

  <path id="classpath.startup">
    <fileset dir="lib" includes="**/*.jar"/>
  </path>

  <path id="classpath.runtime">
    <fileset dir="lib" includes="**/*.jar"/>
    <fileset dir="${build.dir}" includes="${jar.startup}"/>
  </path>

  <path id="classpath.apidocs">
    <fileset dir="lib" includes="**/*.jar"/>
    <fileset dir="sources" includes="**/*.jar"/>
  </path>

  <!-- =========================================================================== +
   | Compilation and archival tasks                                                |
   + ============================================================================ -->

  <target name="compile.startup" depends="prepare">
    <javac
        classpathref="classpath.startup"
        srcdir="${source.startup.dir}"
        destdir="${build.startup.dir}"
        debug="true">
      <include name="**/*.java"/>
    </javac>
  </target>

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

  <target name="compile.runtime" depends="archive.startup">
    <javac
        classpathref="classpath.runtime"
        srcdir="${source.runtime.dir}"
        destdir="${build.runtime.dir}"
        debug="true">
      <include name="**/*.java"/>
    </javac>
  </target>

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

  <target name="archive.startup" depends="compile.startup">
    <jar
        destfile="${build.dir}/${jar.startup}"
        filesonly="true">
      <fileset dir="${build.startup.dir}">
        <include name="**/*.class"/>
        <exclude name="**/.*"/>
      </fileset>
      <fileset dir="${build.startup.dir}">
        <exclude name="**/.java"/>
        <exclude name="**/.html"/>
        <exclude name="**/.*"/>
      </fileset>
    </jar>
  </target>

  <target name="archive.runtime" depends="compile.runtime">
    <jar
        destfile="${build.dir}/${jar.runtime}"
        filesonly="true">
      <fileset dir="${build.runtime.dir}">
        <include name="**/*.class"/>
        <exclude name="**/.*"/>
      </fileset>
      <fileset dir="${build.runtime.dir}">
        <exclude name="**/.java"/>
        <exclude name="**/.html"/>
        <exclude name="**/.*"/>
      </fileset>
    </jar>
  </target>

  <!-- =========================================================================== +
   | JavaDoc generation                                                            |
   + ============================================================================ -->

  <target name="javadoc.generate" unless="javadoc.uptodate" depends="webapp.prepare">
    <javadoc destdir="${webapp.docs.dir}/javadoc"
        author="true"
        version="true"
        use="true"
        windowtitle="Apache Cocoon Kernel ${version}"
        classpathref="classpath.apidocs">
      <packageset dir="${build.documentation.dir}"/>
      <group
          title="Startup"
          packages="${package}:${package}.configuration:${package}.startup"/>
      <group
          title="Runtime"
          packages="${package}.deployment:${package}.description"/>
      <group
          title="Plugins"
          packages="${package}.plugins:${package}.plugins.*"/>
      <link
          href="http://java.sun.com/j2se/1.4.2/docs/api/package-list"
          packagelistLoc="${tools.dir}/package-list/j2se"
          offline="true"/>
      <link
          href="http://java.sun.com/j2ee/1.4/docs/api/package-list"
          packagelistLoc="${tools.dir}/package-list/j2ee"
          offline="true"/>
    </javadoc>
  </target>

  <target name="javadoc" description="  Create the API documentation">
    <!-- copy all sources into one biiiiig directory -->
    <mkdir dir="${webapp.docs.dir}/javadoc"/>
    <copy todir="${build.documentation.dir}">
      <mapper>
        <mapper
            type="regexp"
            from="^(blocks|extensions|contracts|modules)/([a-z\-\_\.]+)/src/(.*)\.(html|java)$"
            to="\3.\4"/>
        <mapper
            type="regexp"
            from="^(startup|runtime)/(.*)\.(html|java)$"
            to="\2.\3"/>
      </mapper>
      <fileset dir="${source.dir}">
        <include name="**/*.java"/>
        <include name="**/*.html"/>
      </fileset>
    </copy>
    <!-- run javadoc over the whole thing if (and only if) we need to -->
    <uptodate property="javadoc.uptodate">
      <srcfiles dir= "${build.documentation.dir}" includes="**/*.java"/>
      <mapper type="merge" to="${webapp.docs.dir}/javadoc/index.html"/>
    </uptodate>
    <antcall target="javadoc.generate"/>
  </target>
  
  <!-- =========================================================================== +
   | Kernel related tasks                                                          |
   + ============================================================================ -->

  <target name="kernel.component">
    <ant antfile="${tools.dir}/build-kernel.xml" dir="${basedir}" inheritAll="false">
      <property name="component.name"   value="${name}"/>
      <property name="component.source" value="${source.dir}/${name}"/>
      <property name="component.target" value="${build.kernel.dir}/${name}"/>
      <property name="component.deploy" value="${webapp.root.dir}/WEB-INF/kernel"/>
    </ant>
  </target>

  <!-- ========================================================================== -->
  
  <target name="kernel.components">
    <antcall target="kernel.component"><param name="name" value="contracts/datasource"/></antcall>
    <antcall target="kernel.component"><param name="name" value="contracts/jaxp"/></antcall>
    <antcall target="kernel.component"><param name="name" value="contracts/repository"/></antcall>
    <antcall target="kernel.component"><param name="name" value="contracts/servlet"/></antcall>
    <antcall target="kernel.component"><param name="name" value="extensions/logging"/></antcall>
    <antcall target="kernel.component"><param name="name" value="extensions/jaxp"/></antcall>
    <antcall target="kernel.component"><param name="name" value="modules/ant"/></antcall>
    <antcall target="kernel.component"><param name="name" value="modules/commons-collections"/></antcall>
    <antcall target="kernel.component"><param name="name" value="modules/commons-pool"/></antcall>
    <antcall target="kernel.component"><param name="name" value="blocks/datasource-commons-dbcp"/></antcall>
    <antcall target="kernel.component"><param name="name" value="blocks/datasource-hsqldb"/></antcall>
    <antcall target="kernel.component"><param name="name" value="blocks/jaxp-saxon"/></antcall>
    <antcall target="kernel.component"><param name="name" value="blocks/servlet-jasper"/></antcall>
    <antcall target="kernel.component"><param name="name" value="blocks/repository-file"/></antcall>
  </target>

  <!-- =========================================================================== +
   | Web-Application generation                                                    |
   + ============================================================================ -->

  <target name="webapp.prepare" depends="prepare">
    <mkdir dir="${webapp.docs.dir}"/>
    <mkdir dir="${webapp.root.dir}"/>
  </target>
  
  <!-- ========================================================================== -->

  <target name="webapp.root" depends="webapp.prepare,archive.startup,archive.runtime,kernel.components">
    <copy todir="${webapp.root.dir}">
      <fileset dir="${source.webapp.dir}"></fileset>
    </copy>
    <mkdir dir="${webapp.root.dir}/WEB-INF/lib"/>
    <mkdir dir="${webapp.root.dir}/WEB-INF/kernel"/>
    <copy todir="${webapp.root.dir}/WEB-INF/lib"    file="${build.dir}/${jar.startup}"/>
    <copy todir="${webapp.root.dir}/WEB-INF/kernel" file="${build.dir}/${jar.runtime}"/>
  </target>

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

  <target name="webapp.docs" depends="webapp.prepare">
    <copy todir="${webapp.docs.dir}">
      <fileset dir="${source.documentation.dir}"></fileset>
    </copy>
  </target>

  <!-- =========================================================================== +
   | Global and generic tasks                                                      |
   + ============================================================================ -->

  <target name="prepare">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${build.kernel.dir}"/>
    <mkdir dir="${build.startup.dir}"/>
    <mkdir dir="${build.runtime.dir}"/>
    <mkdir dir="${build.documentation.dir}"/>
  </target>

  <!-- ========================================================================== -->
  
  <target name="webapp" depends="webapp.root,webapp.docs"
      description="  Build the web applications (excluding javadoc)"/>

  <!-- ========================================================================== -->
  
  <target name="all" depends="webapp,javadoc"         
      description="* Build the web applications (including javadoc)"/>

  <!-- ========================================================================== -->
  
  <target name="run" depends="webapp" description="  Run the servlet environment">
    <java classname="org.mortbay.jetty.Server" fork="true">
      <sysproperty key="java.endorsed.dirs"           value="${basedir}/lib/endorsed"/>
      <sysproperty key="org.mortbay.util.URI.charset" value="UTF-8"/>
      <sysproperty key="jetty.home"                   value="${basedir}"/>
      <sysproperty key="LOG_DATE_FORMAT"              value="[yyyy/MM/dd hh:mm:ss.SSS]"/>

      <arg value="${basedir}/etc/admin.xml"/>
      <arg value="${basedir}/etc/jetty.xml"/>
      
      <classpath>
        <fileset dir="${basedir}/lib" includes="**/*.jar"/>
      </classpath>
    </java>
  </target>

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

  <target name="clean" description="  Clean interim compilation targets">
    <delete dir="${build.dir}"/>
  </target>

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

  <target name="distclean" depends="clean" description="  Revert to checkout state">
    <delete dir="${webapp.dir}"/>
  </target>

</project>
