<?xml version="1.0" encoding="UTF-8"?>
<project name="JOnAS Documentation"
         default="all"
         xmlns:m2="urn:maven-artifact-ant"
         basedir=".">

  <!-- Import common rules -->

  <property file="build.properties" />
  <property file="${user.home}/proxy.properties" />

  <property name="tmp.dir" value="output/tmp" />
  <property name="doc.dir" value="src/docbook" />
  <property name="resources.dir" value="src/resources" />
  <property name="images.dir" value="${resources.dir}/images" />
  <property name="pom.dir" value="src/pom" />
  <!-- conditions -->
  <condition property="isWindows">
	 <os family="windows"/>
  </condition>

  <condition property="isUnix">
	 <os family="unix"/>
   </condition>

  <!-- m2:* elements loading -->
  <path id="build.lib.path">
    <fileset dir="tools">
      <include name="maven-ant-tasks-*.jar" />
    </fileset>
  </path>
  <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
           uri="urn:maven-artifact-ant">
    <classpath refid="build.lib.path" />
  </typedef>

  <target name="init" depends="easybeans_download_doc, cmi_download_doc">
    <mkdir dir="${tmp.dir}" />
    <copy-images destdir="${tmp.dir}" />
  </target>


  <!-- - - - - - - - - - - - -->
  <!-- Ant Macro definitions -->
  <!-- - - - - - - - - - - - -->

  <!-- ============================================================ -->
  <!--                    Generation macros                         -->
  <!-- ============================================================ -->
  <macrodef name="generate-html"
            description="Generate HTML from the docbook input file">
    <attribute name="destdir"/>
    <attribute name="file" />
    <attribute name="lang" />
    <sequential>

      <copy-images destdir="@{destdir}" />

      <xslt-process file="@{file}"
                    output="@{destdir}/@{lang}/html/@{file}.html"
                    style="${resources.dir}/html.xsl"
                    lang="@{lang}" />
      <copy todir="@{destdir}/@{lang}/html">
        <fileset dir="${resources.dir}">
          <include name="*.css" />
        </fileset>
      </copy>
      <check-generated file="@{destdir}/@{lang}/html/@{file}.html" />

    </sequential>
  </macrodef>

  <macrodef name="generate-chunk"
            description="Generate HTML Chunks from the docbook input file">
    <attribute name="destdir" />
    <attribute name="file" />
    <attribute name="lang" />
    <sequential>

      <copy-images destdir="@{destdir}" />

      <xslt-process file="@{file}"
                    output="@{destdir}/@{lang}/chunk/@{file}/index.html"
                    style="${resources.dir}/chunk.xsl"
                    lang="@{lang}" />
      <copy todir="@{destdir}/@{lang}/chunk/@{file}">
        <fileset dir="${resources.dir}">
          <include name="*.css" />
        </fileset>
      </copy>
      <check-generated file="@{destdir}/@{lang}/chunk/@{file}/index.html" />

    </sequential>
  </macrodef>

  <macrodef name="generate-pdf"
            description="Generate FO &amp; PDF from the docbook input file">
    <attribute name="destdir" />
    <attribute name="file" />
    <attribute name="lang" />
    <sequential>

      <!-- Firstly, generate FO files -->
      <property name="fo.output" value="${tmp.dir}/@{lang}/fo/@{file}.fo" />

      <xslt-process file="@{file}"
                    output="${tmp.dir}/@{lang}/fo/@{file}.fo"
                    style="${resources.dir}/fo.xsl"
                    lang="@{lang}" />
      <copy todir="${tmp.dir}/@{lang}/fo">
        <fileset dir="${resources.dir}">
          <include name="*.css" />
        </fileset>
      </copy>
      <check-generated file="${tmp.dir}/@{lang}/fo/@{file}.fo" />

      <!-- Then PDF if everything was OK -->
      <mkdir dir="@{destdir}/@{lang}/pdf" />
      <java classname="org.apache.fop.cli.Main" fork="true" maxmemory="256m">
        <classpath>
          <fileset dir="${fop.dir}">
            <include name="build/fop.jar" />
            <include name="lib/*.jar" />
            <include name="lib/*.zip" />
          </fileset>
        </classpath>
        <arg line="-d -fo ${tmp.dir}/@{lang}/fo/@{file}.fo" />
        <arg line="-pdf @{destdir}/@{lang}/pdf/@{file}.pdf" />
        <jvmarg line="-Dhttp.proxyHost=${proxyHost} -Dhttp.proxyPort=${proxyPort} -Dhttp.proxySet=${proxySet}" />
      </java>
      <check-generated file="@{destdir}/@{lang}/pdf/@{file}.pdf" />

    </sequential>
  </macrodef>

  <!-- ============================================================ -->
  <!--                   Distribution macros                        -->
  <!-- ============================================================ -->

  <macrodef name="generate-distribution">
    <attribute name="file" />
    <attribute name="lang" default="doc-en" />
    <sequential>
      <echo>==========================================================</echo>
      <echo> Generating @{file}</echo>
      <echo>  * formats: PDF &amp; HTML &amp; HTML Chunks</echo>
      <echo>  * language: @{lang}</echo>
      <echo>==========================================================</echo>
      <generate-html file="@{file}" lang="@{lang}" destdir="output/dist" />
      <generate-chunk file="@{file}" lang="@{lang}" destdir="output/dist" />
      <generate-pdf file="@{file}" lang="@{lang}" destdir="output/dist" />
    </sequential>
  </macrodef>

  <macrodef name="generate-local">
    <attribute name="file" />
    <attribute name="lang" default="doc-en" />
    <sequential>
      <echo>==========================================================</echo>
      <echo> Generating @{file}</echo>
      <echo>  * formats: PDF &amp; HTML</echo>
      <echo>  * language: @{lang}</echo>
      <echo>==========================================================</echo>
      <generate-html file="@{file}" lang="@{lang}" destdir="output/local" />
      <generate-pdf file="@{file}" lang="@{lang}" destdir="output/local" />
    </sequential>
  </macrodef>

  <!-- ============================================================ -->
  <!--                       Misc. macros                           -->
  <!-- ============================================================ -->

  <macrodef name="copy-images"
            description="Copy documentation images at the desired location">
    <attribute name="destdir" description="Base destination directory" />
    <sequential>
      <!-- images -->
      <mkdir dir="@{destdir}/resources/images" />
      <copy todir="@{destdir}/resources/images">
        <fileset dir="${images.dir}">
          <exclude name="**/*.svg" />
        </fileset>
        <fileset dir="olddoc/images" />
      </copy>
      <!-- callouts -->
      <copy todir="@{destdir}/resources/images/callouts">
        <fileset dir="${images.dir}/callouts">
          <include name="*.png" />
          <include name="*.gif" />
        </fileset>
      </copy>
      <!-- bamboo -->
      <copy todir="@{destdir}/resources/images/bamboo">
        <fileset dir="${images.dir}/bamboo">
          <include name="*.png" />
          <include name="*.gif" />
        </fileset>
      </copy>
      <!-- cmi -->
      <mkdir dir="@{destdir}/doc-en/html/cmi/images" />
      <copy todir="@{destdir}/doc-en/html/cmi/images">
        <fileset dir="src/docbook/doc-en/cmi/images">
          <include name="*.png" />
          <include name="*.gif" />
        </fileset>
      </copy>
      <copy todir="@{destdir}/doc-en/fo/cmi/images">
          <fileset dir="src/docbook/doc-en/cmi/images">
            <include name="*.png" />
            <include name="*.gif" />
          </fileset>
      </copy>
    </sequential>
  </macrodef>

  <!-- transform given ${input.file} with the ${style.file} style
       using xsltproc
       xsltproc is faster and treats xinclude directive -->
  <macrodef name="xslt-process"
            description="Transform the input XML files">
    <attribute name="file" description="Base name of the file to proces" />
    <attribute name="output" description="File or directory" />
    <attribute name="style" description="XSL style sheet" />
    <attribute name="lang" description="Language" />
    <sequential>
      <echo level="debug">xsltproc - file:@{file}</echo>
      <echo level="debug">xsltproc - output:@{output}</echo>
      <echo level="debug">xsltproc - style.file:@{style}</echo>
      <exec executable="xsltproc" failonerror="true" >
        <arg line="--stringparam  navig.graphics 0 " />
        <arg line="--stringparam fop.extensions 0 " />
        <arg line="--stringparam docbook ${docbook.xsl.dir} " />
        <arg line="-o @{output}" />
        <arg line="--xinclude" />
        <arg value="@{style}" />
        <arg value="${doc.dir}/@{lang}/@{file}.xml" />
      </exec>
    </sequential>
  </macrodef>

  <macrodef name="check-generated"
            description="Fails the build if the expected file was not generated">
    <attribute name="file" description="Filename to check" />
    <sequential>
      <condition property="@{file}.generation.success">
        <available file="@{file}" />
      </condition>
      <fail message="Checked file was not generated: @{file}" unless="@{file}.generation.success" />
    </sequential>
  </macrodef>

  <!-- ============================================================ -->
  <!--                       Build targets                          -->
  <!-- ============================================================ -->

  <target name="copylocal">
    <copy-images destdir="output/local" />
  </target>

  <target name="copydist">
    <copy-images destdir="output/dist" />
    <!-- copy start index for english doc: index.html -->
    <copy todir="output/dist/doc-en/html">
      <fileset file="src/docbook/doc-en/index.html" />
    </copy>
  </target>

  <!-- ============================================================ -->
  <!--  Build the guides independently                              -->
  <!-- ============================================================ -->

  <target name="command" description="generates the command guide" depends="init">
    <generate-distribution file="command_guide"
                           lang="doc-en" />
  </target>

  <target name="admin" description="generates the administration guide" depends="init">
    <generate-distribution file="administration_guide"
                           lang="doc-en" />
  </target>

  <target name="deployment-plan" description="generates the deployment plan guide" depends="init">
      <generate-distribution file="deployment-plans_guide"
                             lang="doc-en" />
  </target>

  <target name="deployer" description="generates the deployer guide" depends="init">
    <generate-distribution file="deployer_guide"
                           lang="doc-en" />
  </target>

  <target name="started" description="generates the getting started guide" depends="init">
    <generate-distribution file="getting_started_guide"
                           lang="doc-en" />
  </target>

  <target name="migration" description="generates the migration guide" depends="init">
    <generate-distribution file="migration_guide"
                           lang="doc-en" />
  </target>

  <target name="bamboo" description="generates the continuous_integration_guide" depends="init">
    <generate-distribution file="continuous_integration_guide"
                           lang="doc-en" />
  </target>

  <target name="howto_firewall" description="generates howto firewall" depends="init">
    <generate-distribution file="howto_firewall"
                           lang="doc-en" />
  </target>

  <target name="howto_tracking_jdbc_leaks" description="generates howto_track_jdbc_connectionleaks" depends="init">
    <generate-distribution file="howto_track_jdbc_connectionleaks"
                           lang="doc-en" />
  </target>

  <target name="howto_mbeans" description="generates howto mbeans" depends="init">
    <generate-distribution file="howto_mbeans"
                           lang="doc-en" />
  </target>

  <target name="howto_services" description="generates howto services" depends="init">
    <generate-distribution file="howto_services"
                           lang="doc-en" />
  </target>

  <target name="howto_services_on_demand" description="generates howto services on demand" depends="init">
    <generate-distribution file="howto_services_on_demand"
                           lang="doc-en" />
  </target>

  <target name="howto_win32service" description="generates howto win32service" depends="init">
    <generate-distribution file="howto_win32_service"
                           lang="doc-en" />
  </target>

  <target name="howto_distributedJoram" description="generates howto distributedJoram" depends="init">
    <generate-distribution file="howto_distributed_joram"
                           lang="doc-en" />
  </target>

  <target name="howto_bnd" description="generates howto Bnd" depends="init">
    <generate-distribution file="howto_install_jdbc_driver"
                           lang="doc-en" />
  </target>

  <target name="howto_cdi" description="generates howto cdi" depends="init">
    <generate-distribution file="howto_use_cdi"
                           lang="doc-en" />
  </target>

  <target name="howto_jaxrs" description="generates howto jaxrs" depends="init">
    <generate-distribution file="howto_use_jaxrs"
                           lang="doc-en" />
  </target>

  <target name="anttasks" description="generates anttasks manual " depends="init">
    <generate-distribution file="ant_tasks"
                           lang="doc-en" />
  </target>

  <target name="config" description="generate the configuration guide" depends="init">
    <generate-distribution file="configuration_guide"
                           lang="doc-en" />
  </target>

  <target name="j2eepg" description="generate the j2ee programmer's guide" depends="init">
    <generate-distribution file="j2eeprogrammerguide"
                           lang="doc-en" />
  </target>

  <target name="ejb2pg" description="generate the ejb2 programmer's guide" depends="init">
    <generate-distribution file="ejb2_programmer_guide"
                           lang="doc-en" />
  </target>

  <target name="ejb3pg" description="generate the ejb3 programmer's guide" depends="init">
    <generate-distribution file="ejb3_programmer_guide"
                           lang="doc-en" />
  </target>

  <target name="webpg" description="generate Web Application programmer's guide" depends="init">
    <generate-distribution file="web_pg"
                           lang="doc-en" />
  </target>

  <target name="connector" description="J2EE Connector programmer's guide" depends="init">
    <generate-distribution file="connector_pg"
                           lang="doc-en" />
  </target>

  <target name="clustering" description="JOnAS Clustering Guide" depends="init">
    <generate-distribution file="clustering_guide"
                           lang="doc-en" />
  </target>

  <target name="developer" description="JOnAS Developer Guide" depends="init">
    <generate-distribution file="developerguide"
                           lang="doc-en" />
  </target>

  <target name="client" description="J2EE Client programmer's guide" depends="init">
    <generate-distribution file="j2eeclient_pg"
                           lang="doc-en" />
  </target>

  <target name="ear" description="J2EE Application Deployer's Guide" depends="init">
    <generate-distribution file="eardeploy"
                           lang="doc-en" />
  </target>

  <target name="version" description="version's Guide" depends="init">
    <generate-distribution file="version"
                           lang="doc-en" />
    </target>

  <target name="jaxws" description="JAXWS Developer's Guide" depends="init">
    <generate-distribution file="jaxws_developer_guide"
                           lang="doc-en" />
    </target>

  <target name="essai" description="generate essai for tests" depends="init">
    <generate-local file="essai"
                    lang="doc-en" />
  </target>

  <target name="jonasdoctutorial" depends="init">
    <generate-local file="howtoJOnASDocBook"
                    lang="doc-fr" />
  </target>

  <!-- ============================================================ -->
  <!-- Generates a set of documents at once                         -->
  <!-- ============================================================ -->

  <target name="howto"
          description="build all howto documents"
          depends="howto_firewall, howto_services, howto_tracking_jdbc_leaks, howto_mbeans, howto_win32service, howto_services_on_demand, howto_distributedJoram, howto_bnd, howto_cdi, howto_jaxrs" />

  <target name="guides"
          description="build all guides"
          depends="ejb2pg, ejb3pg, webpg, config, bamboo, migration, connector, ear, client, j2eepg, started, command, anttasks, admin, deployment-plan, clustering, developer, jaxws">
    <!--
      Extra  dependencies currently not activated:
      * clustering
      * deployer
      -->
  </target>

  <target name="olddoc">
    <copy todir="output/dist/doc-en/html">
      <fileset dir="olddoc">
        <include name="*.html" />
      </fileset>
    </copy>
    <copy todir="output/dist/doc-en/html/howto">
      <fileset dir="olddoc/howto" />
    </copy>
  </target>

  <target name="dist" description="Build the doc tree" depends="init, copydist, guides, howto">
  </target>

  <target name="local" description="Build the doc tutorial" depends="init, copylocal">
    <antcall target="jonasdoctutorial" />
  </target>

  <target name="doc_tutorial" description="Build the doc tutorial" depends="local">
  </target>

  <!-- build the war that will be put in the jonas server distrib -->
  <target name="war" depends="init, dist">
    <mkdir dir="output/war" />
    <war file="output/war/jonas-doc-en.war" webxml="${resources.dir}/WEB-INF/web.xml">
      <fileset dir="output/dist">
        <exclude name="**/*.pdf" />
      </fileset>
      <fileset dir="${resources.dir}">
        <include name="WEB-INF/jonas-web.xml" />
      </fileset>
      <fileset file="${resources.dir}/index.html"/>
    </war>
  </target>

  <target name="all" depends="init, war, local, install" />

  <target name="clean" description="delete generated files">
    <delete dir="output" />
    <!-- Do not forget the included documentation of
         external projects -->
    <delete dir="src/docbook/doc-en/easybeans" />
    <delete dir="src/docbook/doc-en/cmi" />
  </target>

  <!-- ============================================================ -->
  <!--                     Maven 2 targets                          -->
  <!-- ============================================================ -->

  <target name="update" depends="init">
    <copy todir="${tmp.dir}">
      <fileset dir="${pom.dir}" includes="*.pom" />
    </copy>
    <replace dir="${tmp.dir}"
             token="@VERSION@"
             value="${product.version}" />
    <m2:pom id="jonas-doc-en.pom" file="${tmp.dir}/jonas-doc-en.pom" />
  </target>

  <target name="install" depends="update, war" description="Install artifacts">
    <m2:install file="output/war/jonas-doc-en.war">
      <m2:pom refid="jonas-doc-en.pom" />
    </m2:install>
  </target>

  <target name="deploy" depends="install" description="Deploy artifacts">
    <m2:install-provider artifactId="wagon-ssh" version="1.0-alpha-7" />
    <m2:deploy file="output/war/jonas-doc-en.war" pomRefId="jonas-doc-en.pom" />
  </target>

  <!-- ============================================================ -->
  <!-- Download & Unpack external documentations                    -->
  <!-- ============================================================ -->

  <target name="easybeans_download_doc">
    <m2:dependencies filesetId="dependency.fileset" useScope="runtime">
      <m2:remoteRepository url="http://maven.ow2.org/maven2-snapshot" id="ow2.snapshot">
        <snapshots enabled="true"/>
      </m2:remoteRepository>
      <m2:remoteRepository url="http://maven.ow2.org/maven2" id="ow2.release">
        <releases enabled="true"/>
      </m2:remoteRepository>
      <dependency groupId="org.ow2.easybeans.doc" artifactId="easybeans-doc" version="${easybeans.version}"/>
    </m2:dependencies>

    <delete dir="src/docbook/doc-en/easybeans" />
    <mkdir dir="src/docbook/doc-en/easybeans" />
    <copy todir="src/docbook/doc-en/easybeans">
      <fileset refid="dependency.fileset" />
        <mapper type="flatten" />
    </copy>
    <unjar dest="src/docbook/doc-en/easybeans">
      <fileset dir="src/docbook/doc-en/easybeans">
        <include name="**/*.jar"/>
      </fileset>
    </unjar>
    <delete>
      <fileset dir="src/docbook/doc-en/easybeans">
        <include name="**/*.jar"/>
      </fileset>
    </delete>
  </target>

  <target name="cmi_download_doc">
    <m2:dependencies filesetId="dependency.fileset" useScope="runtime">
      <m2:remoteRepository url="http://maven.ow2.org/maven2-snapshot" id="ow2.snapshot">
        <snapshots enabled="true"/>
      </m2:remoteRepository>
      <m2:remoteRepository url="http://maven.ow2.org/maven2" id="ow2.release">
        <releases enabled="true"/>
      </m2:remoteRepository>
        <dependency groupId="org.ow2.cmi.doc" artifactId="cmi-doc" version="${cmi.version}"/>
    </m2:dependencies>

    <delete dir="src/docbook/doc-en/cmi" />
    <mkdir dir="src/docbook/doc-en/cmi" />
    <copy todir="src/docbook/doc-en/cmi">
      <fileset refid="dependency.fileset" />
      <mapper type="flatten" />
    </copy>
    <unjar dest="src/docbook/doc-en/cmi">
      <fileset dir="src/docbook/doc-en/cmi">
        <include name="**/*.jar"/>
      </fileset>
    </unjar>
    <delete>
      <fileset dir="src/docbook/doc-en/cmi">
        <include name="**/*.jar"/>
      </fileset>
    </delete>
  </target>

</project>

