<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="./tools/stylesheets/build.xsl"?>

<project default="dist" basedir="." name="Forrest build file">

  <description>
                   * ========================== *
                   |   Forrest Build System     |
                   * ========================== *
                                 by

                   Nicola Ken Barozzi (nicolaken@apache.org)
                   Marc Johnson (mjohnson@apache.org)
                   Jeff Turner (jefft@apache.org)

                   Run the build script: build.bat or build.sh
  </description>

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

    <buildnumber file="etc/build.number"/>
    <echo>
      --------------------------------------------------------------

      Using ${ant.version}
      Build file ${ant.file}
      Build system home ${ant.home}
      Build number ${build.number}
      Project Name ${ant.project.name}
      Java Version ${ant.java.version}      
      Timestamp ${DSTAMP}${TSTAMP}

      --------------------------------------------------------------
    </echo>

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

    <property name="name"                  value="apache-forrest"/>
    <property name="packages"              value="${xgump.module.project.packages}"/>
    <property name="version"
      value="${xgump.module.project.version.major}.${xgump.module.project.version.minor}${xgump.module.project.version.tag}"/>

    <property name="resource.dir"          location="src/resources"/>
    <property name="java.dir"          location="src/java"/>
    <property name="scratchpad.src.dir"    location="src/scratchpad/src/java"/>
    <property name="components.src.dir"    location="${java.dir}/components"/>
    <property name="build.dir"             location="build"/>
    <property name="build.docs"            location="build/docs"/>
   <!--
    <property name="build.javadocs"        value="build/docs/javadocs"/>
    -->
    <property name="build.scratchpad.dir"  location="build/scratchpad"/>
    <property name="build.components.dir"  location="build/components"/>
    <property name="build.scratchpad.classes" location="build/scratchpad/classes"/>
    <property name="build.components.classes" location="build/components/classes"/>

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

    <property name="tools.dir"             location="tools"/>
    <property name="tools.jar"             location="${java.home}/../lib/tools.jar"/>
    <available file="${tools.jar}"         property="tools.jar.present"/>

    <path id="classpath">
      <fileset dir="src/scratchpad/lib" includes="**/*.jar"/>
      <fileset dir="lib" includes="**/*.jar"/>
      <pathelement location="${tools.jar}"/>
    </path>

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

  <target name="clean" depends="init" description="Delete all generated files">
    <delete dir="${build.dir}"/>
  </target>


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


  <target name="release-dist-src" depends="init"
    description="Makes a source release distribution">
    <mkdir dir="${dist.dir}"/>

    <!-- Hackery to fix linefeeds and permissions before zipping -->
    <patternset id="scripts">
      <include name="**/*.bat"/>
      <include name="**/*.sh"/>
      <include name="**/ant"/>
      <include name="**/forrest"/>
    </patternset>

    <patternset id="noscripts">
      <exclude name="**/*.bat"/>
      <exclude name="**/*.sh"/>
      <exclude name="**/ant"/>
      <exclude name="**/forrest"/>
    </patternset>

    <property name="tmp-scripts" location="${build.dir}/tmp/scripts"/>
    <mkdir dir="${tmp-scripts}"/>
    <copy todir="${tmp-scripts}">
      <fileset dir=".">
        <patternset refid="scripts"/>
      </fileset>
    </copy>
    <fixcrlf eol="crlf" srcdir="${tmp-scripts}" includes="**/*.bat"/>
    <fixcrlf eol="lf" srcdir="${tmp-scripts}" excludes="**/*.bat"/>
    <chmod dir="${tmp-scripts}" perm="ugo+rx" excludes="**/*.bat"/> 

    <property name="release-name-src" value="${name}-${version}-src"/>
    <zip
      file="${dist.dir}/${release-name-src}.zip" >
      <zipfileset dir="." prefix="${release-name-src}">
        <exclude name="build/**"/>
        <patternset refid="noscripts"/>
      </zipfileset>
      <zipfileset prefix="${release-name-src}" dir="${tmp-scripts}"/>
      <zipfileset prefix="${release-name-src}" dir="etc" includes="RELEASE-NOTES-${version}.txt"/>
    </zip>

    <tar longfile="gnu"
      compression="gzip"
      destfile="${dist.dir}/${release-name-src}.tar.gz" >
      <tarfileset dir="." prefix="${release-name-src}" username="forrest" group="forrest">
        <exclude name="build/**"/>
        <patternset refid="noscripts"/>
      </tarfileset>
      <tarfileset prefix="${release-name-src}" dir="${tmp-scripts}"
        mode="755" username="forrest" group="forrest">
        <include name="**/*.sh"/>
        <include name="**/ant"/>
        <include name="**/forrest"/>
      </tarfileset>
      <tarfileset prefix="${release-name-src}" dir="${tmp-scripts}"
        username="forrest" group="forrest">
        <exclude name="**/*.sh"/>
        <exclude name="**/ant"/>
        <exclude name="**/forrest"/>
      </tarfileset>
      <tarfileset prefix="${release-name-src}" dir="etc" includes="RELEASE-NOTES-${version}.txt"/>
    </tar>
  </target>


  <target name="release-dist-bin" depends="init, dist-shbat" 
    description="Makes a binary release distribution">
    <property name="release-name-bin" value="${name}-${version}-bin"/>
    <zip
      file="${dist.dir}/${release-name-bin}.zip" >
      <zipfileset dir="${dist-shbat.dir}" prefix="${release-name-bin}"/>
      <zipfileset dir="etc"  prefix="${release-name-bin}/etc"/>
      <zipfileset dir="etc"  prefix="${release-name-bin}" includes="RELEASE-NOTES-${version}.txt"/>
    </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>
      <tarfileset prefix="${release-name-bin}/etc" dir="etc"/>
      <tarfileset prefix="${release-name-bin}" dir="etc" includes="RELEASE-NOTES-${version}.txt"/>
    </tar>
  </target>

  <target name="release-dist" 
    description="Makes source and binary release distributions"
    depends="release-dist-bin, release-dist-src"/>



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


  <!-- =================================================================== -->
  <!-- Make the zip that holds what a fresh site should look like          -->
  <!-- =================================================================== -->
  <target name="fresh-site-zip" depends="init">
    <!-- TODO: decide on a better place to read/set this prop from/to -->
    <property name="fresh-site.zip" value="./build/fresh-site.zip" />
    <zip destfile="${fresh-site.zip}" basedir="${resource.dir}/fresh-site" />
  </target>


  <!-- =================================================================== -->
  <!-- Make the WEB-INF directory like cocoon (CLI or webapp) needs it     -->
  <!-- Note that @tokens@ in config files will be unexpanded.              -->
  <!-- =================================================================== -->
  <target name="webapp-webinf" depends="init, scratchpad.jar, components.jar" >
    <!-- TODO: decide on a better place to read/set this prop from/to -->
    <property name="webapp.webinf.dir" value="./build/WEB-INF" />

    <mkdir dir="${webapp.webinf.dir}" />
    <copy todir="${webapp.webinf.dir}" >
      <fileset dir="${resource.dir}/conf" >
        <exclude name="*.xmap" />
        <exclude name="skinconf.xml" />
      </fileset>
    </copy>

    <mkdir dir="${webapp.webinf.dir}/classes" />
    <copy todir="${webapp.webinf.dir}/classes" >
      <fileset dir="${resource.dir}/schema">
        <include name="CatalogManager.properties"/>
      </fileset>
      <!--
      <fileset dir="${build.classes}">
        <include name="**/*.class"/>
      </fileset>
      <fileset dir="${build.scratchpad.classes}">
        <include name="**/*.class"/>
      </fileset>
      -->
    </copy>

    <mkdir dir="${webapp.webinf.dir}/lib"/>
    <copy todir="${webapp.webinf.dir}/lib">
      <!--
      <fileset dir="./lib/endorsed"    />
      <fileset dir="./lib/optional"/>
      -->
      <fileset dir="./lib/core"/>
      <fileset dir="./src/scratchpad/lib"/>
      <fileset dir="${build.scratchpad.dir}" includes="*.jar"/>
      <fileset dir="${build.components.dir}" includes="*.jar"/>
    </copy>
  </target>


  <!-- =================================================================== -->
  <!-- Make the bare context directory like cocoon (CLI or webapp) needs it-->
  <!-- =================================================================== -->
  <target name="bare-context-dir" depends="init" >
    <!-- TODO: decide on a better place to read/set this prop from/to -->
    <property name="bare-context.dir" value="./build/bare-context" />

    <!-- Copy entity catalog, entities and class files -->
    <copy todir="${bare-context.dir}/resources/schema" >
      <fileset dir="${resource.dir}/schema"/>
    </copy>

    <!-- Copy default image files. These may be overridden by identically named
    files in ${project.home}/src/documentation/resources/images. -->
    <copy todir="${bare-context.dir}/resources/images" >
      <fileset dir="${resource.dir}/images"/>
    </copy>

    <!-- Copy Chaperon grammars -->
    <copy todir="${bare-context.dir}/resources/grammars" >
      <fileset dir="${resource.dir}/grammars"/>
    </copy>


    <!-- Copy Cocoon configuration and sitemap -->
    <copy todir="${bare-context.dir}">
      <fileset dir="${resource.dir}/conf">
        <include name="*.xmap"/>
        <include name="skinconf.xml"/>
        <include name="jettyconf.xml"/>
      </fileset>
    </copy>

    <!-- Copy stylesheets -->
    <copy todir="${bare-context.dir}/resources/stylesheets" >
      <fileset dir="${resource.dir}/stylesheets" />
    </copy>

    <!-- Copy skins -->
    <copy todir="${bare-context.dir}/skins/" >
      <fileset dir="${resource.dir}/skins" />
    </copy>

  </target>

  <!-- =================================================================== -->
  <!-- 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="./lib/core" includes="*.jar" />
      </classpath>
    </taskdef>
    <!-- skinconf.xml -->
    <echo message="validating **/skinconf.xml ..."/>
    <jing compactSyntax="true" rngfile="./src/resources/schema/relaxng/skinconf.rnc">
      <fileset dir="./src/resources" includes="**/skinconf.xml"/>
    </jing>
    <!-- All stylesheets **/*.xsl -->
    <echo message="validating stylesheets **/*.xsl ..."/>
    <jing rngfile="./src/resources/schema/relaxng/xslt.rng">
      <fileset dir="./src/resources" includes="**/*.xsl"/>
      <fileset dir="./tools" includes="**/*.xsl"/>
    </jing>
    <!-- sitemap.xmap -->
    <echo message="validating **/sitemap.xmap ..."/>
    <jing rngfile="./src/resources/schema/relaxng/sitemap-v06.rng">
      <fileset dir="./src/resources/conf" includes="*.xmap"/>
    </jing>
    <!-- book.xml -->
    <!-- FIXME: Need Norm Walsh catalog entity resolver or Jeff's DoctypeChanger.
    <echo message="validating **/book.xml ..."/>
    <jing rngfile="./src/resources/schema/relaxng/book-v01.rng">
      <fileset dir="./src" includes="**/book.xml"/>
    </jing>
    -->
  </target>


  <!-- =================================================================== -->
  <!-- Make the shell-bat distribution                                     -->
  <!-- =================================================================== -->
  <target name="dist-shbat" 
    depends="init, validate-config, fresh-site-zip, webapp-webinf, bare-context-dir" >
    <!-- TODO: decide on a better place to read/set this prop from/to -->

    <!-- fresh drop-off location for the distribution -->
    <delete dir="${dist-shbat.dir}" />
    <mkdir dir="${dist-shbat.dir}" />

    <!-- copy the ant script at the heart of the distribution -->
    <!--
    <copy todir="${dist-shbat.dir}" file="forrest.build.xml" />
    -->

    <!-- copy the prepared and wrapper stuff for the distribution -->
    <copy todir="${dist-shbat.dir}">
      <fileset dir="./src/resources/forrest-shbat" />
    </copy>

    <!-- copy licenses -->
    <copy todir="${dist-shbat.dir}/legal">
      <fileset dir="./legal" />
    </copy>

    <!-- copy the fresh-site.zip to clone from -->
    <copy todir="${dist-shbat.dir}" file="${fresh-site.zip}" />

    <!-- copy the web-inf as needed by the cocoon CLI -->
    <mkdir dir="${dist-shbat.dir}/WEB-INF" />
    <copy todir="${dist-shbat.dir}/WEB-INF">
      <fileset dir="${webapp.webinf.dir}" />
    </copy>

    <mkdir dir="${dist-shbat.dir}/lib/endorsed" />
    <copy todir="${dist-shbat.dir}/lib/endorsed">
      <fileset dir="lib/endorsed" />
    </copy>

    <mkdir dir="${dist-shbat.dir}/lib/optional" />
    <copy todir="${dist-shbat.dir}/lib/optional">
      <fileset dir="lib/optional" />
    </copy>

    <!-- Copy Ant jars, so the shbat distribution is self-contained -->
    <mkdir dir="${dist-shbat.dir}/ant" />
    <copy todir="${dist-shbat.dir}/ant">
      <fileset dir="tools/ant" />
    </copy>

    <!-- Copy forrestbot -->
    <mkdir dir="${dist-shbat.dir}/forrestbot" />
    <copy todir="${dist-shbat.dir}/forrestbot">
      <fileset dir="src/resources/forrestbot">
        <exclude name="bin/**"/>
      </fileset>
    </copy>
    <copy todir="${dist-shbat.dir}/bin">
      <fileset dir="src/resources/forrestbot/bin"/>
    </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}/ant/bin" includes="*.bat"/>
    <fixcrlf eol="lf" srcdir="${dist-shbat.dir}/ant/bin" excludes="*.bat"/>
    <chmod dir="${dist-shbat.dir}/ant/bin" perm="ugo+rx" excludes="*.bat"/> 

    <!-- copy the bare-context dir as needed by the cocoon CLI -->
    <mkdir dir="${dist-shbat.dir}/context" />
    <copy todir="${dist-shbat.dir}/context">
      <fileset dir="${bare-context.dir}" />
    </copy>

    <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 it.
  | 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://xml.apache.org/forrest/ 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">
    <property name="forrest.home" value="${dist-shbat.dir}" />
    <ant antfile="${forrest.home}/forrest.antproxy.xml" target="site"/>
  </target>


  <!-- =================================================================== -->
  <!-- Generate cocoon-webapp for project-site                             -->
  <!-- =================================================================== -->
  <target name="webapp" depends="dist-shbat"
    description="Generates an unpackaged webapp of the website">
    <property name="forrest.home" value="${dist-shbat.dir}" />
    <ant antfile="${forrest.home}/forrest.antproxy.xml" target="webapp"/>
  </target>

  <!-- =================================================================== -->
  <!-- Generate a .war files of the project-site                           -->
  <!-- =================================================================== -->
  <target name="war" depends="dist-shbat"
    description="Generates a .war file containing the website">
    <property name="forrest.home" value="${dist-shbat.dir}" />
    <ant antfile="${forrest.home}/forrest.antproxy.xml" target="war"/>
  </target>

  <!-- =================================================================== -->
  <!-- Validates Forrest XML documentation                                 -->
  <!-- =================================================================== -->
  <target name="validate" depends="dist-shbat"
    description="Validates XML documentation files">
    <property name="forrest.home" value="${dist-shbat.dir}" />
    <ant antfile="${forrest.home}/forrest.antproxy.xml" target="validate"/>
  </target>

  <!-- =================================================================== -->
  <!-- ForrestBar for Mozilla                                              -->
  <!-- =================================================================== -->
  <target name="forrestbar" depends="init">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.dir}/work/forrestbar" />
    <mkdir dir="${build.dir}/work/forrestbar-jar" />
    <copy todir="${build.dir}/work/forrestbar-jar" overwrite="true">
      <fileset dir="src/resources/forrestbar" 
               includes="content/**/*.xul" />
      <filterset>
        <filter token="EMBEDDED_NAME" value="${forrestbot.links.forrest.local-embedded.name}"/>
        <filter token="EMBEDDED_URL" value="${forrestbot.links.forrest.local-embedded.url}"/> 
        <filter token="WEBAPP_NAME" value="${forrestbot.links.forrest.local-webapp.name}"/>
        <filter token="WEBAPP_URL" value="${forrestbot.links.forrest.local-webapp.url}"/> 
      </filterset>
    </copy>
    <copy todir="${build.dir}/work/forrestbar-jar" overwrite="true">
      <fileset dir="src/resources/forrestbar" 
               excludes="content/**/*.xul" />
    </copy>
    <jar jarfile="${build.dir}/work/forrestbar/forrestbar.jar">
      <fileset dir="${build.dir}/work/forrestbar-jar" includes="content/**/*" />
    </jar>
    <copy file="src/resources/forrestbar/install.js" 
          todir="${build.dir}/work/forrestbar" overwrite="true">
    </copy>
    <zip zipfile="${build.dir}/forrestbar.xpi" 
      basedir="${build.dir}/work/forrestbar" />
  </target>


  <!-- ================================== -->
  <!--        Target used by Gump         -->
  <!-- ================================== -->
  <target  name="gump" description="Target used by Gump">
    <antcall target="site"/>
    <antcall target="webapp"/>
  </target>


  <!-- =================================================================== -->
  <!-- Compile scratchpad classes                                          -->
  <!-- =================================================================== -->
  <target name="scratchpad.compile" depends="init">
    <mkdir dir="${build.scratchpad.classes}"/>
    <javac srcdir=   "${scratchpad.src.dir}"
      destdir=       "${build.scratchpad.classes}"
      debug=         "${build.compiler.debug}"
      optimize=      "${build.compiler.optimize}"
      deprecation=   "${build.compiler.deprecation}"
      nowarn=        "${build.compiler.nowarn}"
      classpathref=  "classpath">
    </javac>
  </target>

  <!-- =================================================================== -->
  <!-- Create scratchpad jar                                               -->
  <!-- =================================================================== -->
  <target name="scratchpad.jar" depends="scratchpad.compile">
    <mkdir dir="${build.scratchpad.dir}"/>
    <jar destfile="${build.scratchpad.dir}/xml-forrest-scratchpad.jar"
      basedir="${build.scratchpad.classes}"/>
  </target>


  <!-- =================================================================== -->
  <!-- Compile Cocoon components                                           -->
  <!-- =================================================================== -->
  <target name="components.compile" depends="init">
    <mkdir dir="${build.components.classes}"/>
    <javac srcdir=   "${components.src.dir}"
      destdir=       "${build.components.classes}"
      debug=         "${build.compiler.debug}"
      optimize=      "${build.compiler.optimize}"
      deprecation=   "${build.compiler.deprecation}"
      nowarn=        "${build.compiler.nowarn}"
      classpathref=  "classpath">
    </javac>
  </target>

  <!-- =================================================================== -->
  <!-- Jar Cocoon components                                               -->
  <!-- =================================================================== -->
  <target name="components.jar" depends="components.compile">
    <mkdir dir="${build.components.dir}"/>
    <jar destfile="${build.components.dir}/xml-forrest-components.jar"
      basedir="${build.components.classes}"/>
  </target>

</project>
