<?xml version="1.0"?>
<!--
  Copyright 2002-2004 The Apache Software Foundation or its licensors,
  as applicable.

  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 default="jar" basedir="." name="Forrest build file">

  <description>
    Build system for Apache Forrest.
    Run the build script: build.bat or build.sh
  </description>

   <target name="init">
    <tstamp>
      <format property="YEAR" pattern="yyyy" locale="en"/>
    </tstamp>

    <xmlproperty file="status.xml"     prefix="xstatus"  keeproot="false" collapseAttributes="true" />   

    <property name="build.compiler.emacs"       value="on"/>
    <property name="build.compiler.warnings"    value="true"/>
    <property name="build.compiler.pedantic"    value="false"/>
    <property name="build.compiler.depend"      value="true"/>
    <property name="build.compiler.fulldepend"  value="true"/>
    <property name="build.compiler.debug"       value="on"/>    
    <property name="build.compiler.optimize"    value="off"/>
    <property name="build.compiler.deprecation" value="on"/>
    <property name="build.compiler.nowarn"      value="on"/>    
    <property name="build.compiler.type"        value="classic"/>
    <property name="build.compiler.vm"          value="1.2"/>
   
    <property name="name" value="apache-forrest"/>
    <property name="forrest.version" value="0.7"/>
    <property name="forrest.home" location=".."/>
    <property name="forrest.core" location="."/>

    <property name="java.dir"              location="java"/>
    <property name="plugins.dir"           location="${forrest.home}/plugins"/>
    <property name="build.dir"             location="${forrest.home}/build"/>
    <property name="build.docs"            location="${build.dir}/docs"/>
    <property name="build.classes"         location="${build.dir}/classes"/>

    <property name="dist.dir"              location="${build.dir}/dist" />
    <property name="dist-shbat.dir"        location="${dist.dir}/shbat" />

    <property name="tools.dir"             location="${forrest.home}/tools"/>
    <property name="tools.jar"             location="${java.home}/../lib/tools.jar"/>
    <available file="${tools.jar}"         property="tools.jar.present"/>
    
    <property name="src-fresh-site"         location="${forrest.home}/main/fresh-site"/>
    
    <property name="forrestbot.dir"         location="${forrest.home}/tools/forrestbot"/>

    <mkdir dir="${build.dir}"/>
    
    <buildnumber file="${build.dir}/build.number"/>
    <echo>
      --------------------------------------------------------------

      Using ${ant.version}
      Build file ${ant.file}
      Use 'build.[sh|bat] -projecthelp' to see other options.
      Build system home ${ant.home}
      Build number ${build.number}
      Project Name ${ant.project.name}
      Java Version ${ant.java.version}      
      Timestamp ${DSTAMP}${TSTAMP}
      This is: ${name} ${forrest.version}

      --------------------------------------------------------------
    </echo>
    
    <path id="classpath">
      <fileset dir="${forrest.home}/lib" includes="**/*.jar"/>
      <fileset dir="${tools.dir}/jetty" includes="servlet*.jar"/>
      <pathelement location="${tools.jar}"/>
    </path>

  </target>

  <target name="clean" depends="init" description="Delete all generated files">
    <delete dir="${build.dir}"/>
    <delete dir="${forrestbot.dir}/logs"/>
    <delete dir="${forrestbot.dir}/work"/>
    <delete dir="${forrestbot.dir}/build"/>
    <delete dir="${forrestbot.dir}/sites"/>
    <delete dir="${forrestbot.dir}/webapp/target"/>
    <delete dir="${forrestbot.dir}/webapp/maven.log"/>
    <delete dir="webapp/WEB-INF/logs"/>
  </target>

  <target name="dist-clean" depends="init, clean"
     description="Delete all non-forrest files (*.settings, *.bak, *~)">
    <delete dir="${forrestbot.dir}/*.settings"/>
    <delete file="**/*.bak"/>
    <delete file="**/*~"/>
  </target>

  <!-- =================================================================== -->
  <!-- Make release distributions                                          -->
  <!-- =================================================================== -->


  <target name="release-dist" depends="init, dist-clean, dist-shbat" 
    description="Makes release distributions">
    <property name="release-name-bin" value="${name}-${forrest.version}"/>
    <zip
      file="${dist.dir}/${release-name-bin}.zip" >
      <zipfileset dir="${dist-shbat.dir}" prefix="${release-name-bin}"/>
    </zip>
    <tar longfile="gnu" compression="gzip"
      destfile="${dist.dir}/${release-name-bin}.tar.gz" >

      <tarfileset prefix="${release-name-bin}" dir="${dist-shbat.dir}"
        username="forrest" group="forrest">
        <exclude name="**/*.sh"/>
        <exclude name="**/ant"/>
        <exclude name="**/forrest"/>
      </tarfileset>
      <tarfileset prefix="${release-name-bin}" dir="${dist-shbat.dir}"
        mode="755" username="forrest" group="forrest">
        <include name="**/*.sh"/>
        <include name="**/ant"/>
        <include name="**/forrest"/>
      </tarfileset>
    </tar>
  </target>


  <!-- =================================================================== -->
  <!-- Make all known distributions                                        -->
  <!-- =================================================================== -->
  <target name="dist" 
    description="Makes all the known incarnations of forrest"
    depends="clean,dist-shbat" />

  <!-- =================================================================== -->
  <!-- Validate the important core configuration files                     -->
  <!-- =================================================================== -->
  <target name="validate-config" depends="init"
    description="Validate the important core configuration files">
    <!-- FIXME allow switch if="validate.config"  -->
    <taskdef name="jing" classname="com.thaiopensource.relaxng.util.JingTask">
      <classpath>
        <fileset dir="${forrest.home}/lib/core" includes="*.jar" />
      </classpath>
    </taskdef>
    <!-- skinconf.xml -->
    <!-- FIXME: do we need to validate skinconf.xml to build forrest? -->
    <echo message="validating **/skinconf.xml ..."/>
    <xmlvalidate failonerror="true" lenient="no" warn="yes">
      <xmlcatalog id="xcat">
        <catalogpath>
          <pathelement
            location="${forrest.core}/webapp/resources/schema/catalog.xcat"/>
        </catalogpath>
      </xmlcatalog>
      <fileset dir="${forrest.core}/fresh-site"
        includes="**/skinconf.xml" />
    </xmlvalidate>
    <!-- All stylesheets **/*.xsl -->
    <echo message="validating stylesheets **/*.xsl ..."/>
    <jing rngfile="${forrest.core}/webapp/resources/schema/relaxng/xslt.rng">
      <fileset dir="${forrest.core}" includes="**/*.xsl"/>
      <fileset dir="${tools.dir}" includes="**/*.xsl"/>
    </jing>
    <!-- sitemap.xmap -->
    <!-- commented out see http://issues.apache.org/bugzilla/show_bug.cgi?id=33378 
    <echo message="validating **/sitemap.xmap ..."/>
    <jing rngfile="${forrest.core}/webapp/resources/schema/relaxng/sitemap-v06.rng">
      <fileset dir="${forrest.core}/webapp/" includes="*.xmap"/>
    </jing>
    -->
    <!-- book.xml -->
    <!-- FIXME: Need Norm Walsh catalog entity resolver or Jeff's DoctypeChanger.
    <echo message="validating **/book.xml ..."/>
    <jing rngfile="${forrest.core}/resources/schema/relaxng/book-v01.rng">
      <fileset dir="${forrest.core}" includes="**/book.xml"/>
    </jing>
    -->
  </target>
 
  <target name="dist-shbat" 
    depends="init, jar, validate-config" >

    <!-- fresh drop-off location for the distribution -->
    <delete dir="${dist-shbat.dir}" />
    <mkdir dir="${dist-shbat.dir}" />
    
    <echo>Copy Forrest core files from ${forrest.home}</echo>
    <copy todir="${dist-shbat.dir}">
      <fileset dir="${forrest.home}" >
        <exclude name="build/**"/>
        <exclude name="whiteboard/**"/>
        <exclude name="plugins/**"/>
      </fileset>
    </copy>
    
    <echo>Copy plugin related files from ${plugins.dir}</echo>
    <copy todir="${dist-shbat.dir}/plugins">
       <fileset dir="${plugins.dir}" >
        <include name="plugins.xml"/>
        <include name="build.xml"/>
        <include name="pluginTemplate/**"/>
        <include name="org.apache.forrest.plugin.output.pdf/**"/>
      </fileset>
    </copy>
    
    <echo>Copy whiteboard plugin related files</echo>
    <copy todir="${dist-shbat.dir}/whiteboard/plugins">
       <fileset dir="${forrest.home}/whiteboard/plugins" >
        <include name="whiteboard-plugins.xml"/>
        <include name="build.xml"/>
      </fileset>
    </copy>
    
    <echo>Copy forrest.jar</echo>
    <copy todir="${dist-shbat.dir}">
       <fileset dir="${forrest.home}" >
        <include name="build/xml-forrest.jar"/>
      </fileset>
    </copy>
    
    <!-- Fix bin/ permissions -->
    <fixcrlf eol="crlf" srcdir="${dist-shbat.dir}/bin" includes="*.bat"/>
    <fixcrlf eol="lf" srcdir="${dist-shbat.dir}/bin" excludes="*.bat"/>
    <chmod dir="${dist-shbat.dir}/bin" perm="ugo+rx" excludes="*.bat"/> 

    <!-- Fix ant/bin/ permissions -->
    <fixcrlf eol="crlf" srcdir="${dist-shbat.dir}/tools/ant/bin" includes="*.bat"/>
    <fixcrlf eol="lf" srcdir="${dist-shbat.dir}/tools/ant/bin" excludes="*.bat"/>
    <chmod dir="${dist-shbat.dir}/tools/ant/bin" perm="ugo+rx" excludes="*.bat"/> 

    <echo>
  *-----------------------------------------------------------------
  | installation notice 
  *-----------------------------------------------------------------
  | You have succesfully built the shell-bat distribution of Forrest.
  | Please find it at: ${dist-shbat.dir}
  | Please copy the contents to the install directory of your choice
  | Please have the environment variable FORREST_HOME point to
  |  ${dist-shbat.dir}
  | It is recommended to add
  |    unix: $FORREST_HOME/bin: to your $PATH
  |    win: %FORREST_HOME%\bin; to your %PATH%
  | Calling
  |    unix: $FORREST_HOME/bin/forrest -projecthelp
  |    win: %FORREST_HOME%\bin\forrest -projecthelp
  | will list options for the 'forrest' command
  | More help at http://forrest.apache.org/ and forrest-dev@xml.apache.org
  *-----------------------------------------------------------------
    </echo>
  </target>

  <!-- =================================================================== -->
  <!-- Generate project-site                                               -->
  <!-- =================================================================== -->
  <target name="docs" depends="site"/>
  <target name="site" depends="dist-shbat"
    description="Generates static HTML documentation">
    <ant antfile="${forrest.core}/forrest.build.xml" target="site"/>
  </target>
  <target name="webapp" depends="dist-shbat"
    description="Builds a cocoon webapp for local execution">
    <ant antfile="${forrest.core}/forrest.build.xml" target="webapp"/>
  </target>

  <!-- ================================== -->
  <!--        Test targets                -->
  <!-- ================================== -->
  <target name="test" 
          depends="testseed, testPlugins" 
          description="Test that the features are okay before committing."/>
  
  <target name="testPlugins"
          depends="cleanPlugins"
          description="Test each type of the plugin">
    <!--
      FIXME: these don't work as the compile stage executes since $java.dir = FORREST_HOME/main/java
      instead of PLUGIN_HOME/java
    <echo>=============================</echo>
    <echo>Testing using an input plugin</echo>
    <echo>=============================</echo>
    <ant antfile="${forrest.home}/plugins/org.apache.forrest.plugin.excel/build.xml" target="test">
      <property name="basedir" location="${forrest.home}/plugins/org.apache.forrest.plugin.excel"/>
    </ant>
    
    <echo>=============================</echo>
    <echo>Testing using an ouptut plugin</echo>      
    <echo>=============================</echo>
    <ant antfile="${forrest.home}/plugins/org.apache.forrest.plugin.output.pdf/build.xml" target="test">
      <property name="basedir" location="${forrest.home}/plugins/org.apache.forrest.plugin.output.pdf"/>
    </ant>
    
    <echo>=============================</echo>
    <echo>Testing using an internal plugin</echo>
    <echo>=============================</echo>
    <ant antfile="${forrest.home}/plugins/IMSManifest/build.xml" target="test">
      <property name="basedir" location="${forrest.home}/plugins/IMSManifest"/>
    </ant>
    -->
  </target>
  
  <target name="cleanPlugins"
          description="Remove installed plugins before testing to ensure they are correctly installed when run">
    <delete dir="${forrest.home}/build/plugins"/>
  </target>

  <target name="testseed" depends="init, cleanPlugins">
    <echo>=============================</echo>
    <echo>Testing a seeded site</echo>
    <echo>=============================</echo>
    <property name="test.dir" value="${build.dir}/test"/>
    <delete dir="${test.dir}"/>
    <mkdir dir="${test.dir}"/>
    <ant antfile="${forrest.core}/forrest.build.xml" target="seed">
      <property name="forrest.home" value="${forrest.home}"/>  
      <property name="project.home" value="${test.dir}"/>
    </ant>
    <ant antfile="${forrest.core}/forrest.build.xml" target="site">
      <property name="forrest.home" value="${forrest.home}"/>  
      <property name="project.home" value="${test.dir}"/>
    </ant>
  </target>

  <!-- ==================================================
       Target used by Gump for the main "forrest" project.
       Our gump descriptor also has other projects for other aspects of forrest build:
       http://cvs.apache.org/viewcvs.cgi/gump/project/forrest.xml
       ================================================== -->
  <target  name="gump" description="Target used by Gump: forrest">
    <antcall target="jar"/>
  </target>
  
  <!-- =================================================================== -->
  <!-- Compile Cocoon java classes                                         -->
  <!-- =================================================================== -->
  <target name="compile" depends="init">
    <mkdir dir="${build.classes}"/>
    <javac srcdir=   "${java.dir}"
      destdir=       "${build.classes}"
      debug=         "${build.compiler.debug}"
      optimize=      "${build.compiler.optimize}"
      deprecation=   "${build.compiler.deprecation}"
      nowarn=        "${build.compiler.nowarn}"
      classpathref=  "classpath">
    </javac>
  </target>

  <!-- =================================================================== -->
  <!-- Jar Cocoon classes                                                  -->
  <!-- =================================================================== -->
  <target name="jar" depends="init, compile">
    <jar destfile="${build.dir}/xml-forrest.jar"
      basedir="${build.classes}"/>
    <echo>
  *-----------------------------------------------------------------
  | Installation notice 
  *-----------------------------------------------------------------
  | You have now built the ${forrest.version} version of Forrest.
  | Please set the environment variable FORREST_HOME to point to
  |  ${forrest.home}
  | It is recommended to add
  |    unix: $FORREST_HOME/bin: to your $PATH
  |    win: %FORREST_HOME%\bin; to your %PATH%
  | Then do 'forrest -projecthelp' to list options for the 'forrest' command
  | More help at http://forrest.apache.org/
  *-----------------------------------------------------------------
    </echo>
  </target>

  <!-- =================================================================== -->
  <!-- Generate DTDs with entities expanded                                -->
  <!-- =================================================================== -->
  <target name='expand-dtd' depends="init"
     description="Generates '-full' DTDs, with all entity refs expanded, for use with catalog-ignorant editors like IDEA">
	<taskdef name="dtd2xml" classpathref="classpath" classname="org.cyberneko.dtd.anttasks.DTD2XML"/>
	<property name="dtdx.dir" location="${build.dir}/tmp/dtdx"/>
	<mkdir dir="${dtdx.dir}"/>
	<!-- Generate an intermediate XML representation of each DTD -->
	<dtd2xml classpathref="classpath" outputDir="${dtdx.dir}" preserveDirs="yes" extension=".dtdx">
		<fileset dir="${forrest.core}/webapp/resources/schema/dtd">
			<include name="changes-v12.dtd"/>
			<include name="changes-v13.dtd"/>
			<include name="document-v11.dtd"/>
			<include name="document-v12.dtd"/>
			<include name="document-v13.dtd"/>
			<include name="faq-v12.dtd"/>
			<include name="faq-v13.dtd"/>
			<include name="howto-v12.dtd"/>
			<include name="howto-v13.dtd"/>
			<include name="todo-v12.dtd"/>
			<include name="todo-v13.dtd"/>
      <include name="v20/document-v20.dtd"/>
      <include name="v20/changes-v20.dtd"/>
      <include name="v20/faq-v20.dtd"/>
      <include name="v20/howto-v20.dtd"/>
      <include name="v20/todo-v20.dtd"/>
		</fileset>
	</dtd2xml>

	<!-- Now transform the intermediate format back to the regular DTD format -->
	<!-- Note that we could also transform to XSD or RNG -->
	<xslt basedir="${dtdx.dir}"
		destdir="${dist-shbat.dir}/webapp/resources/schema/dtd"
		extension="-full.dtd"
    style="${forrest.home}/tools/dtdconverters/dtdx2dtd.xsl">
    <include name="*.dtdx"/>
    <include name="v20a/*.dtdx"/>
  </xslt>

  </target>


  <!-- =================================================================== -->
  <!-- Use Apache Ant to generate a patch file                             -->
  <!-- =================================================================== -->
    <target name="patch" depends="init">
<!-- Do not advertise this capability. May need to be removed.
     It makes a messy patch when they have other pending changes.
        description="Create a patch from your working copy, ready for contribution">
-->
      <ant antfile="${forrest.home}/tools/targets/patch.xml" target="patchpackage"/>
    </target>


</project>
