<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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="build-site" default="docs" basedir=".">

    <!-- Initialization properties -->
    <property name="project.name"   value="site"/>
    <property name="docs.src" location="xdocs"/>
    <property name="docs.dest" location="docs"/>
    <property name="target.dir" location="target"/>
    <property name="docs.downloads.src" location="${docs.src}/downloads"/>

    <!-- This docs.downloads.dest value is passed into xdocs/downloads/downloads-cgi.xsl  -->
    <property name="docs.downloads.dest" location="${docs.dest}/site/downloads"/>

    <!-- This docs.downloads.tmp value is passed into xdocs/downloads/download-pages.xsl  -->
    <property name="docs.downloads.tmp" location="${target.dir}/download-xdocs"/>
	
	<!-- Directory to which news XML content files are generated. -->
	<property name="news.gen.dir" location="${target.dir}/news-gen"/>
	<!-- Directory to which RSS file is generated. -->
    <property name="rss.output.dir" location="${docs.dest}/site"/>

  <target name="clean" description="Delete intermediate files.">
    <delete dir="${target.dir}" quiet="yes"/>
  </target>       
  
	
  <target name="news-gen" depends="CheckXSL" description="Generates RSS file and new XML content files from news.xml">
  	<mkdir dir="${rss.output.dir}"/>
  	<mkdir dir="${news.gen.dir}"/>
  	
	<xslt style="xdocs/stylesheets/news2content.xsl" 
		in="news.xml" 
		out="${rss.output.dir}/rss.xml" force="true">
		<param name="base.dir" expression="${news.gen.dir}"/>
	</xslt>  	
  	
  </target>
	
  <macrodef name="transform">
  	<attribute name="from" description="Directory containing source content files"/>
  	<attribute name="to" description="Output directory"/>
  	<attribute name="relative-path" default="../.." description="Relative path to root index folder."/>
  	
  	<sequential>
  		<!-- empty.xml is only in the root directory, but doesn't hurt -->
  	    <xslt basedir="@{from}"
  	          destdir="@{to}"
  	          extension=".html"
  	          style="xdocs/stylesheets/site.xsl"
  	          excludes="empty.xml"
  	          includes="*.xml"
  	          force="true">
  	      <param name="relative-path" expression="@{relative-path}"/>
  	    </xslt>
  	</sequential>
  </macrodef>	
  
  <target name="init">
    <!-- Show the build version in gump logs -->
      <echo>Version $Revision$ Last updated: $Date$</echo>
      <!--antcall target="_gump_run"/-->
  	
  	<!-- Check that the correct charset is being used -->
	<xslt style="xdocs/stylesheets/charsetchk.xsl" 
		in="xdocs/empty.xml" 
		out="${docs.downloads.tmp}/charsetchk.html" force="true">
  	</xslt>
 	 <condition property="compareOK">
	    <filesmatch file1="xdocs/charsetchk.html" file2="${docs.downloads.tmp}/charsetchk.html"/>
    </condition>
 	<fail unless="compareOK">
 		Failed to process some special characters - check the default encoding.
 		On Windows, try setting the code page - e.g. CHCP 1252 - before running Ant.
 	</fail>
  	<delete file="${docs.downloads.tmp}/charsetchk.html"></delete>
  </target>
  
  <target name="docs" depends="init,CheckXSL,news-gen"
   description="Render HTML Pages via XSLT">
  
    <condition property="target.vm" value="valid">
      <or>
        <equals arg1="1.6" arg2="${java.specification.version}"/>
        <equals arg1="1.5" arg2="${java.specification.version}"/>
        <equals arg1="1.4" arg2="${java.specification.version}"/>
      </or>
    </condition>
    <fail unless="target.vm" message="Must build with Java 1.4/1.5/1.6"/>

    <!-- Create destination directory if necessary -->
    <mkdir     dir="${docs.dest}"/>

    <!-- Transform the xml files - requires Ant 1.6.2 -->
<!-- Attempt to do the below styles all in one go
    <xslt   basedir="${docs.src}"
            destdir="${docs.dest}"
            style="xdocs/stylesheets/site.xsl"
            excludes="empty.xml"
            >
        <mapper type="glob" from="*.xml" to="*.html"/>
    </xslt>
-->

    <echo>*** Top level documentation</echo>

    <transform from="${docs.src}" to="${docs.dest}" relative-path="."/>
    
    <echo>*** site subdirectory</echo>

  	<transform from="${docs.src}/site" to="${docs.dest}/site" relative-path=".."/>


    <echo>*** site/pmc directory</echo>
  	
 	<transform from="${docs.src}/site/pmc" to="${docs.dest}/site/pmc"/>
  	

    <echo>*** site/news directory</echo>
  	
	<transform from="${docs.src}/site/news" to="${docs.dest}/site/news"/>
	<transform from="${news.gen.dir}" to="${docs.dest}/site/news"/>
 
    <echo>*** Build and/or empty download xdocs</echo>
    <mkdir dir="${docs.downloads.tmp}"/>
  	
  	<!-- Need to empty the input directory, otherwise obsolete files can remain -->
    <delete dir="${docs.downloads.tmp}" file="*.xml"/>

    <!-- Seems like we could do one transform and generate the .cgi at the same time, using a text
          output method. -->
          
    <xslt in="${docs.downloads.src}/downloads.xml" 
      out="${docs.downloads.tmp}/index.xml" 
      style="${docs.downloads.src}/download-page.xsl"/>
    
    <!-- empty.xml/.html isn't used except to trigger the creation of the individual project download
         files. -->
         
    <xslt in="${docs.downloads.src}/downloads.xml" 
      out="${docs.downloads.tmp}/empty.xml" 
      style="${docs.downloads.src}/download-pages.xsl">
     <param name="docs.downloads.tmp" expression="${docs.downloads.tmp}"/>
  	</xslt>
    <delete file="${docs.downloads.tmp}/empty.xml"/>

  	<echo>*** Convert download xdocs to download pages</echo>
  	
	<transform from="${docs.downloads.tmp}" to="${docs.downloads.dest}"/>


    <!-- create .cgi files -->
    <echo>*** Create download .cgi file</echo>
    <xslt     in="${docs.downloads.src}/downloads.xml"
              out="${docs.downloads.dest}/empty.xml"
              style="${docs.downloads.src}/downloads-cgi.xsl">
        <param name="docs.downloads.dest" expression="${docs.downloads.dest}"/>
    </xslt>
    <delete file="${docs.downloads.dest}/empty.xml"/>
  	
    <echo>*** Copying static resource files</echo>
    <copy    todir="${docs.dest}/images" filtering="no">
      <fileset dir="${docs.src}/images">
        <include name="**/*.gif"/>
        <include name="**/*.jpeg"/>
        <include name="**/*.jpg"/>
        <include name="**/*.png"/>
        <include name="**/*.svg"/>
      </fileset>
    </copy>

<!-- Not needed, as css file is only present in docs directory anyway
    <echo>*** Copying CSS files</echo>
    <copy    todir="${docs.dest}" filtering="no">
      <fileset dir="${docs.src}">
        <include name="**/*.css"/>
      </fileset>
    </copy>
-->
  	
  	<antcall target="remove-ws"></antcall>
  	
  </target>

  <!-- Process output to remove the trailing blank spaces -->
  <target name="remove-ws">
  	<echo>*** Removing extra white-space</echo>
  	<replace dir="${docs.dest}" token=" />" value="/>">
  		<include name="**/*.html"/>
  	</replace>
  </target>

  <target name="javadocs"
   description="Javadocs is a noop">

    <echo>
        This project has no Java code, therefore, it has no
        javadocs. This target is here to make Gumpy happy.
    </echo>
  </target>

  <target name="CheckXSL" description="Check site.xsl for tags with more than one attribute">
    <xslt in="${docs.src}/stylesheets/site.xsl" 
         out="${docs.src}/stylesheets/dummy.tmp" 
       style="${docs.src}/stylesheets/sitechk.xsl"/>
    <delete file="${docs.src}/stylesheets/dummy.tmp"/>
  	    <xslt in="${docs.src}/stylesheets/news2content.xsl" 
  	         out="${docs.src}/stylesheets/dummy.tmp" 
  	       style="${docs.src}/stylesheets/sitechk.xsl"/>
  	    <delete file="${docs.src}/stylesheets/dummy.tmp"/>
  </target>

  <target name="_gump_run" if="gump.run">
    <echo level="info">
	 Gump properties for this run
	  gump.run            = ${gump.run}
	  date.projectfile    = ${date.projectfile}
	  version.projectfile = ${version.projectfile}
     </echo>
     <echoproperties prefix="ant"/>
     <echoproperties prefix="gump"/>
     <echoproperties prefix="os"/>
     <echo message="HACK: trying to fix xsl:include error"/>
     <delete file="news2content.xsl"/>
     <copy todir=".">
        <fileset file="${docs.src}/stylesheets/common.xsl"/>
     </copy>
     <mkdir dir="xsltslt-1.2.1"/>
     <copy todir="xsltslt-1.2.1">
     	<fileset dir="${docs.src}/stylesheets/xsltslt-1.2.1"/>
     </copy>
  </target>
	
</project>
