<?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:define="jelly:define"
  xmlns:deploy="deploy"
  xmlns:util="jelly:util"
  xmlns:log="jelly:log">
  
  <!--
   |
   | FIX ME: It is the additional siteCommand options that is causing the
   | deploy:artifact tag to hang indefinitely.
   |
   -->
  
  <!-- ================================================================== -->
  <!-- D E P L O Y  A R T I F A C T  T A G                                -->
  <!-- ================================================================== -->
  <!-- The deploy-artifact tag is a dynamic tag that allows the           -->
  <!-- consistent deployment of artifacts generated by Maven.             -->
  <!--                                                                    -->
  <!-- The following parameters can be used with the deploy-artifact      -->
  <!-- tag by setting the appropriate attribute when invoking the tag:    -->
  <!--                                                                    -->
  <!-- @param artifact                                                    -->
  <!-- @param type                                                        -->
  <!-- @param assureDirectoryCommand                                      -->
  <!-- ================================================================== -->
  
  <define:taglib uri="deploy">
    <define:tag name="artifact">

      <!--
        ||
        || If the ${maven.username} value isn't set then the deployment
        || process fails.
        ||
      -->
      
      <j:set var="username" value='${context.getVariable("maven.username")}'/>

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

      <j:set var="copier" value='${context.getVariable("maven.scp.executable")}'/>
      <j:set var="commander" value='${context.getVariable("maven.ssh.executable")}'/>

      <j:set var="distributionSiteX" value="${pom.distributionSite}X"/>
      <j:choose>
        <j:when test="${distributionSiteX != 'X'}">
          <j:set var="siteAddress" value="${pom.distributionSite}"/>
          <j:set var="siteDirectory" value="${pom.distributionDirectory}"/>
        </j:when>
        <j:otherwise>
          <j:set var="siteAddress" value='${context.getVariable("maven.repo.central")}'/>
          <j:set var="siteDirectory" value='${context.getVariable("maven.repo.central.directory")}'/>
        </j:otherwise>
      </j:choose>
      
      <j:set var="typeX" value="${type}X"/>
      <j:choose>
        <j:when test="${typeX != 'X'}">
          <j:set 
            var="resolvedDirectory" 
            value="${siteDirectory}/${pom.artifactDirectory}/${type}"
          />
        </j:when>
        <j:otherwise>
          <j:set 
            var="resolvedDirectory" 
            value="${siteDirectory}/${pom.artifactDirectory}"
          />
        </j:otherwise>
      </j:choose>

      <ant:echo>
        Moving ${artifact} to the ${resolvedDirectory} on ${siteAddress}
      </ant:echo>
      
      <!--
        ||
        || Make sure the destination directory exists.
        ||
      -->
      
      <j:set var="assureDirectoryCommandX" value="${assureDirectoryCommand}X"/>
      <j:if test="${assureDirectoryCommandX != 'X'}">
        <ant:exec dir="." executable="${commander}">
          <ant:arg line="${maven.ssh.args} -l ${username} ${siteAddress} '${assureDirectoryCommand} ${resolvedDirectory}'"/>
        </ant:exec>
      </j:if>
      
      <ant:exec dir="." executable="${copier}">
        <ant:arg line="${maven.scp.args} ${artifact} ${username}@${siteAddress}:${resolvedDirectory}"/>
      </ant:exec>
    
      <!--
        ||
        || Execute a command on the server if one is specified.
        ||
      -->
    
      <j:set var="siteCommandX" value="${siteCommand}X"/>
      <j:if test="${siteCommandX != 'X'}">
        
        <j:useBean var="strings" class="org.apache.commons.lang.StringUtils"/>
        
        <j:set 
          var="siteCommand" 
          value='${strings.replace(siteCommand, "@deployDirectory@", resolvedDirectory)}'
        />
        
        <ant:echo>
          Executing ${siteCommand} with the username ${username} on ${siteAddress}
        </ant:echo>
        
        <ant:exec dir="." executable="${commander}">
          <ant:arg line="${maven.ssh.args} -l ${username} ${siteAddress} '${siteCommand}'"/>
        </ant:exec>

      </j:if>
    
    </define:tag>
  
    <!-- ================================================================== -->
    <!-- C O P Y  D E P S                                                   -->
    <!-- ================================================================== -->
    
    <define:tag name="copy-deps">
      <!--
       |
       | If a POM descriptor has been specified then use it.
       |
       -->
      <j:if test="${projectDescriptor != null}">
        <maven:pom var="pom" projectDescriptor="${projectDescriptor}"/>
      </j:if>
    
      <j:set var="mavenRepoLocal" value='${context.getVariable("maven.repo.local")}'/>
      <ant:mkdir dir="${todir}"/>
      <ant:copy todir="${todir}" flatten="true">
        <!-- grab list of excludedItems up front -->
        <j:if test="${excludes != ''}">
          <!-- The excludes are a list of dep ids -->
          <util:tokenize var="excludeItems" delim=",">${excludes}</util:tokenize>
        </j:if>
        
        <!-- 
         !   for each artifact, see if it is excluded. If not, add a fileset
         !   to the copy
         !--> 
        <j:forEach var="lib" items="${pom.artifacts}">
          <j:set var="excluded" value="false"/>
          <j:if test="${excludes != ''}">
            <!-- The excludes are a list of dep ids, check if this id is in the list -->
            <j:forEach var="exclude" items="${excludeItems}">
              <!-- the exclude could be either the short name (e.g. artifactId), or the id -->
              <j:if test="${(lib.dependency.id == exclude) or (lib.dependency.artifactId == exclude)}">
                <j:set var="excluded" value="true"/>
              </j:if>
            </j:forEach>
          </j:if>
          <j:if test="${!excluded}">
            <ant:fileset dir="${lib.file.parent}">
              <ant:include name="${lib.file.name}"/>
            </ant:fileset>
          </j:if>
        </j:forEach>
      </ant:copy>
    </define:tag>

  </define:taglib>

</project>
