<?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:ant="jelly:ant"
  xmlns:maven="jelly:maven"
  xmlns:deploy="deploy"
  xmlns:util="jelly:util">

  <!-- ================================================================== -->
  <!-- D I S T R I B U T I O N S                                          -->
  <!-- ================================================================== -->

  <goal
    name="dist:prepare-bin-filesystem"
    prereqs="xdoc:init, jar:jar, javadoc:generate"
    description="Builds the binary distribution file system.">
    
    <!--
    
    This is the directory where everything is copied to so that it can
    be archived.
    
    -->
    
    <ant:delete dir="${maven.dist.bin.assembly.dir}"/>
    <ant:mkdir dir="${maven.dist.bin.assembly.dir}"/>

    <ant:echo>
      +-------------------------------------------------------+
      | C R E A T I N G  B I N A R Y  D I S T R I B U T I O N |
      +-------------------------------------------------------+
    </ant:echo>

    <ant:copy todir="${maven.dist.bin.assembly.dir}">
      <ant:fileset dir=".">
        <ant:include name="README.txt"/>
        <ant:include name="LICENSE*"/>
      </ant:fileset>
    </ant:copy>

    <!-- Copy Jars -->
    <ant:copy todir="${maven.dist.bin.assembly.dir}">
      <ant:fileset dir="${maven.build.dir}">
        <ant:include name="${maven.final.name}.jar"/>
      </ant:fileset>
    </ant:copy>

    <!-- Copy documentation -->
    <j:set var="docsDest" value="${maven.docs.dest}"/>
    <ant:copy todir="${maven.dist.bin.assembly.dir}/docs">
      <ant:fileset dir="${docsDest}">
        <ant:include name="**"/>
      </ant:fileset>
    </ant:copy>
  </goal>

  <goal
    name="dist:prepare-src-filesystem"
    description="Builds the source distribution file system.">

    <!-- S O U R C E  D I S T R I B U T I O N -->

    <ant:delete dir="${maven.dist.src.assembly.dir}" />
    <ant:mkdir dir="${maven.dist.src.assembly.dir}" />

    <ant:echo>
      +-------------------------------------------------------+
      | C R E A T I N G  S O U R C E  D I S T R I B U T I O N |
      +-------------------------------------------------------+
    </ant:echo>

    <ant:copy todir="${maven.dist.src.assembly.dir}">
      <ant:fileset dir=".">
        <ant:include name="README.txt"/>
        <ant:include name="LICENSE*"/>
        <ant:include name="project.properties"/>
        <ant:include name="maven.xml"/>
        <ant:include name="project.xml"/>
      </ant:fileset>
    </ant:copy>

    <ant:available property="maven.dist.build.xml.avail"
               file="${basedir}/build.xml"/>

    <j:choose>
      <j:when test="${maven.dist.build.xml.avail}">
        <ant:copy todir="${maven.dist.src.assembly.dir}" file="${basedir}/build.xml"/>
      </j:when>
      <j:otherwise>
        <j:if test="${sourcesPresent == 'true'}">
          <attainGoal name="ant:generate-build"/>
          <ant:move 
            file="${basedir}/build.xml"
            tofile="${maven.dist.src.assembly.dir}/build.xml"/>
        </j:if>
      </j:otherwise>
    </j:choose>

    <!-- Copy Source -->
    <util:available file="${maven.src.dir}">
      <ant:copy todir="${maven.dist.src.assembly.dir}/src">
        <ant:fileset dir="${maven.src.dir}" />
      </ant:copy>
    </util:available>
  </goal>

  <goal name="dist"
    prereqs="dist:build"
    description="Build a complete distribution.">
  </goal>
        
  <goal
    name="dist:build"
    prereqs="dist:build-bin,dist:build-src"
    description="Build a complete distribution.">
  </goal>

  <goal
    name="dist:build-setup"
    description="Set up directories for a distribution build">
    <ant:delete dir="${maven.dist.dir}"/>
    <ant:mkdir dir="${maven.dist.dir}"/>
  </goal>

  <goal
    name="dist:build-bin"
    prereqs="dist:build-setup,dist:prepare-bin-filesystem"
    description="Build the binary distribution.">

    <!-- Create a tar.gz file -->
    <ant:tar longfile="gnu" tarfile="${maven.dist.dir}/${maven.final.name}.tar">
      <ant:tarfileset dir="${maven.dist.bin.archive.dir}"/>
    </ant:tar>

    <ant:gzip 
      zipfile="${maven.dist.dir}/${maven.final.name}.tar.gz"
      src="${maven.dist.dir}/${maven.final.name}.tar"
    />
    
    <ant:delete file="${maven.dist.dir}/${maven.final.name}.tar"/>

    <!-- Create a zip file -->
    <ant:zip zipfile="${maven.dist.dir}/${maven.final.name}.zip">
      <ant:zipfileset dir="${maven.dist.bin.archive.dir}"/>
    </ant:zip>
  </goal>

  <goal
    name="dist:build-src"
    prereqs="dist:build-setup,dist:prepare-src-filesystem"
    description="Build the source distribution.">
 
    <!-- Create a tar.gz file -->
    <ant:tar longfile="gnu" tarfile="${maven.dist.dir}/${maven.final.name}-src.tar">
      <ant:tarfileset dir="${maven.dist.src.archive.dir}"/>
    </ant:tar>

    <ant:gzip 
      zipfile="${maven.dist.dir}/${maven.final.name}-src.tar.gz" 
      src="${maven.dist.dir}/${maven.final.name}-src.tar"
    />
    
    <ant:delete file="${maven.dist.dir}/${maven.final.name}-src.tar"/>

    <!-- Create a zip file -->
    <ant:zip zipfile="${maven.dist.dir}/${maven.final.name}-src.zip">
      <ant:zipfileset dir="${maven.dist.src.archive.dir}"/>
    </ant:zip>

  </goal>

  <!-- ================================================================== -->
  <!-- D E P L O Y  D I S T R I B U T I O N                               -->
  <!-- ================================================================== -->

  <goal name="dist:deploy" description="Deploy a distribution">

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

    <!--
    
    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="dist:build"/>

    <ant:tar tarfile="distributions.tar" basedir="${maven.dist.dir}"/>
    <ant:gzip zipfile="distributions.tar.gz" src="distributions.tar"/>
    <ant:delete file="distributions.tar"/>

    <deploy:artifact
      artifact="distributions.tar.gz"
      type="distributions"
      assureDirectoryCommand="mkdir -p"
      siteCommand="cd @deployDirectory@;${maven.dist.gunzip.executable} distributions.tar.gz;${maven.dist.tar.executable} xUvf distributions.tar;chmod -R g+u *;rm distributions.tar"
    />
  
  </goal>


  <goal name="dist:deploy-bin" description="Deploy a binary distribution">

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

    <attainGoal name="dist:build-bin"/>

    <deploy:artifact
      artifact="${maven.dist.dir}/${maven.final.name}.tar.gz"
      type="distributions"
      assureDirectoryCommand="mkdir -p"
      siteCommand="cd @deployDirectory@;chmod -R g+u *;"
    />

  </goal>

  <goal name="dist:deploy-src" description="Deploy a source distribution">

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

    <attainGoal name="dist:build-src"/>

    <deploy:artifact
      artifact="${maven.dist.dir}/${maven.final.name}-src.tar.gz"
      type="distributions"
      assureDirectoryCommand="mkdir -p"
      siteCommand="cd @deployDirectory@;chmod -R g+u *;"
    />

  </goal>

  <!-- ================================================================== -->
  <!-- D E P L O Y  S N A P S H O T  D I S T R I B U T I O N              -->
  <!-- ================================================================== -->

  <maven:snapshot project="${pom}"/>    

  <goal name="dist:deploy-snapshot" description="Deploy a snapshot distribution">
    
    <maven:user-check user="${maven.username}"/>
    
    <j:set var="maven.final.name" value="${snapshotSignature}"/>
    <ant:echo>Building snapshot distribution: ${maven.final.name}</ant:echo>
        
    <attainGoal name="dist:deploy"/>
    
  </goal>

</project>
