<?xml version="1.0"?>
<!-- 
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * 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
  xmlns:j="jelly:core"
  xmlns:maven="jelly:maven">

  <!-- ================================================================== -->
  <!-- S I T E  G E N E R A T I O N                                       -->
  <!-- ================================================================== -->
  
  <goal name="site:generate" prereqs="site"/>

  <goal name="site:init" prereqs="xdoc:init">
    <j:set var="docsDest" value="${maven.docs.dest}"/>
  </goal>
  
  
  <goal
    name="site"
    description="Generate the web site">

    <attainGoal name="site:run-reports"/>
    <attainGoal name="xdoc"/>

  </goal>
  
  <goal name="site:run-reports" prereqs="xdoc:register-reports">
    <maven:pluginVar var="reports" plugin="maven-xdoc-plugin" property="reports" />
  
    <j:forEach var="report" items="${reports}">
      <j:set var="reportName" value="${report.get('pluginName')}"/>
      <j:if test="${!empty(reportName)}">
        <echo>Generating the ${report.get('name')}...</echo>
        <attainGoal name="${reportName}:report"/>
      </j:if>
    </j:forEach>
  </goal>

  <!-- ================================================================== -->
  <!-- S I T E  D E P L O Y                                               -->
  <!-- ================================================================== -->

  <goal 
    name="site:deploy" 
    description="deploy the generated site docs">

    <!--
     |
     | Generate the site documentation if it has not already been generated.
     | We need some markers or timestamps for this so we don't repeat work
     | if it's not necessary.
     |
    -->

    <attainGoal name="site"/>

    <!--
     |
     | Deploy the site using the maven.site.deploy.method property (right
     | now possible variants are 'fs' and 'ssh')
     |
    -->

    <attainGoal name="site:${maven.site.deploy.method}deploy"/>
  </goal>
  
  <!-- ================================================================== -->
  <!-- S I T E  F T P  D E P L O Y                                        -->
  <!-- Deploys the site using FTP                                         -->
  <!-- ================================================================== -->

  <goal name="site:ftpdeploy"
    prereqs="site:init"
    description="Deploy the generated site docs using ftp">

    <maven:user-check user="${maven.username}"/>

    <echo>
      siteAddress = ${pom.siteAddress}
      siteDirectory = ${pom.siteDirectory}
      siteUser = ${maven.username}
    </echo>

    <!-- FTP to the server -->
    <ftp server="${pom.siteAddress}"
    	 userid="${maven.username}"
    	 password="${maven.password}"
    	 remotedir="${pom.siteDirectory}">
         <fileset dir="${docsDest}" includes="**/*.*"/>
    </ftp>

  </goal>  

  <!-- ================================================================== -->
  <!-- S I T E  S S H  D E P L O Y                                        -->
  <!-- Deploys the site using SSH                                         -->
  <!-- ================================================================== -->

  <goal name="site:sshdeploy" 
    prereqs="site:init"
    description="Deploy the generated site docs using ssh">

    <maven:user-check user="${maven.username}"/>

    <echo>
      siteAddress = ${pom.siteAddress}
      siteDirectory = ${pom.siteDirectory}
      siteUser = ${maven.username}
    </echo>

    <!-- This needs to taken from the project properties -->
    <property name="maven.homepage" value="${pom.siteDirectory}"/>

    <tar tarfile="${maven.build.dir}/${maven.final.name}-site.tar" basedir="${docsDest}"/>
    <gzip zipfile="${maven.build.dir}/${maven.final.name}-site.tar.gz" src="${maven.build.dir}/${maven.final.name}-site.tar"/>
    <delete file="${maven.build.dir}/${maven.final.name}-site.tar"/>

    <!-- Make sure the destination directory exists before trying to copy -->
    <exec dir="." executable="${maven.ssh.executable}">
      <arg line="${maven.ssh.args} -l ${maven.username} ${pom.siteAddress} 'mkdir -p ${maven.homepage}'"/>
    </exec>

    <exec dir="${maven.build.dir}" executable="${maven.scp.executable}">
      <arg line="${maven.scp.args} ${maven.final.name}-site.tar.gz ${maven.username}@${pom.siteAddress}:${maven.homepage}"/>
    </exec>

    <exec dir="." executable="${maven.ssh.executable}">
      <arg line="${maven.ssh.args} -l ${maven.username} ${pom.siteAddress} 'cd ${maven.homepage};${maven.site.gunzip.executable} ${maven.final.name}-site.tar.gz;${maven.site.tar.executable} xUvf ${maven.final.name}-site.tar;chmod -R g+u * .;rm ${maven.final.name}-site.tar'"/>
    </exec>

    <delete file="${maven.build.dir}/${maven.final.name}-site.tar.gz"/>

  </goal>

  <!-- ================================================================== -->
  <!-- S I T E  F S D E P L O Y                                           -->
  <!-- Deploys the site by copying to another file system                 -->
  <!-- ================================================================== -->

  <goal name="site:fsdeploy" 
    prereqs="site:init"
    description="Deploy the generated site by copying to the site directory">

    <echo>
      siteAddress = ${pom.siteAddress}
      siteDirectory = ${pom.siteDirectory}
    </echo>

    <!-- copy the site to the directory specified in the project.xml file -->

    <copy todir="${pom.siteDirectory}">
      <fileset dir="${docsDest}">
        <include name="**"/>
      </fileset>
    </copy>

  </goal>

  <goal name="site:war" description="Create a WAR File from the generated site"
    prereqs="site:init, site">
    <mkdir dir="${docsDest}/WEB-INF" />
    <war destfile="${maven.build.dir}/${maven.final.name}-site.war"
      webxml="${plugin.resources}/web.xml">
      <fileset dir="${docsDest}" />
    </war>
  </goal>

  <goal name="site:ear" prereqs="site:war"
    description="Create an EAR File from the generated site">

    <j:set var="docType1" value="&lt;" />
    <j:set var="docType2">!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN' 'http://java.sun.com/j2ee/dtds/application_1_2.dtd'</j:set>
    <j:set var="docType3" value="&gt;" />
    <mkdir dir="${maven.build.dir}/${plugin.artifactId}" />
    <j:set var="warName" value="${maven.final.name}-site.war" />
    
    <copy file="${plugin.resources}/application.xml"
      tofile="${maven.build.dir}/${plugin.artifactId}/application.xml"/>
      
    <replace file="${maven.build.dir}/${plugin.artifactId}/application.xml">
      <replacefilter token="@name@" value="${pom.artifactId}"/>
      <replacefilter token="@shortDescription@"
        value="${pom.shortDescription}"/>
      <replacefilter token="@warName@" value="${warName}" />
      <replacefilter token="@contextRoot@" value="${maven.final.name}" />
    </replace>
    
    <ear destfile="${maven.build.dir}/${maven.final.name}-site.ear"
      appxml="${maven.build.dir}/${plugin.artifactId}/application.xml">
      <fileset dir="${maven.build.dir}" includes="${maven.final.name}-site.war" />
    </ear>
  </goal>
</project>
