<project 
  xmlns:j="jelly:core" 
  xmlns:maven="jelly:maven"
  xmlns:util="jelly:util"
  xmlns:define="jelly:define"
  xmlns:deploy="deploy"
  xmlns:sea="sea"
  xmlns:velocity="jelly:velocity"
  xmlns:runner="runner">

  <!-- ===================================================================== -->
  <!-- D E P L O Y  P L E X U S                                              -->
  <!-- ===================================================================== -->
  <!-- Deploys Plexus into a self-contained directory that allows you        -->
  <!-- to run Plexus easily.                                                 -->
  <!-- ===================================================================== -->

  <!--
   |
   | We need a way to customize the configuration process.
   |
   -->


  <!--
   |
   | Plexus Dynamic TagLibarary
   |
   | o Create a plexus project.
   | o Create a runtime based on a configuration.
   | o Update the runtime based on an updated configuration.
   | o Generate a reactor for component builds.
   | o Generate a stub component with testing facilities.
   | o Leave hooks for processes to allow for things like insertion of webapps
   |   or other artifacts that are the product of another build.
   | o Generate reports on the state of the overall project.
   |
   -->
  
  <define:taglib uri="runner">

    <!--
     |
     | @base
     | @conf
     | @confDir
     | @modelPom
     | @main
     | @options
     |
     -->

    <define:tag name="deploy">

      <j:set 
        var="templatePath" 
        value="${pom.getPluginContext('maven-runner-plugin').getVariable('plugin.dir')}/plugin-resources/templates"/>
      
      <j:set 
        var="launcherConfiguration" 
        value="classworlds.conf"/>
      
      <j:set 
        var="launcherConfigurationTemplate" 
        value="classworlds.vm"/>

      <runner:create-directory-structure
        base="${base}"/>

      <runner:assemble-dependencies
        plexusPomFile="${plexusPomFile}"
        plexusCoreLibDirectory="${base}/lib"/>

      <runner:create-classworlds-configuration
        main="${main}"
        options="${options}"
        templatePath="${templatePath}"
        classWorldsConfiguration="${base}/conf/${launcherConfiguration}"
        classWorldsConfigurationTemplate="${launcherConfigurationTemplate}"/>

      <runner:create-launcher-scripts
        templatePath="${templatePath}"
        unixScript="${base}/bin/plexus.sh"
        windowsScript="${base}/bin/plexus.bat"/>

      <runner:package-java-runtime
        jreDirectory="${base}/jre"/>
      
    </define:tag>

    <define:tag name="create-directory-structure">
      
      <delete dir="${base}"/>
    
      <mkdir dir="${base}/bin"/>
      <mkdir dir="${base}/conf"/>
      <mkdir dir="${base}/lib"/>
    
    </define:tag>

    <define:tag name="assemble-dependencies">
      
      <deploy:copy-deps 
        todir="${plexusCoreLibDirectory}"
        excludes="junit"/>
      
      <copy
        file="${maven.repo.local}/classworlds/jars/classworlds-1.0-beta-5.jar"
        todir="${plexusCoreLibDirectory}"/>
        
      <copy
        file="${basedir}/target/${maven.final.name}.jar"
        todir="${plexusCoreLibDirectory}"/>        
      
    </define:tag>

    <!--
     |
     | @plexusPom
     | @templatePath
     | @classWorldsConfiguration
     | @classWorldsConfigurationTemplate
     | 
     -->
    <define:tag name="create-classworlds-configuration">

      <echo>
        @templatePath = ${templatePath}
        @classWorldsConfiguration = ${classWorldsConfiguration}
        @classWorldsConfigurationTemplate = ${classWorldsConfigurationTemplate}
      </echo>

      <velocity:merge
        name="${classWorldsConfiguration}"
        basedir="${templatePath}"
        template="${classWorldsConfigurationTemplate}"/>
    
    </define:tag>

    <!--
     |
     | @templatePath
     | @unixScript
     | @windowsScript
     | 
     -->
    <define:tag name="create-launcher-scripts">

      <echo>
        @templatePath = ${templatePath}
        @unixScript = ${unixScript}
        @windowsScript = ${windowsScript}
      </echo>

      <velocity:merge
        name="${unixScript}"
        basedir="${templatePath}"
        template="plexus.vm"/>

      <velocity:merge
        name="${windowsScript}"
        basedir="${templatePath}"
        template="plexus-bat.vm"/>

      <chmod file="${unixScript}" perm="+x"/>
    
    </define:tag>

    <!--
     |
     | Include the JRE if specified. This is useful for applications
     | that need to be completely self-contained.
     |
     -->
    <define:tag name="package-java-runtime">
     
      <j:if test="${jre == 'true'}">
        <exec 
          executable="cp">
          <arg value="-r"/>
          <arg value="${java.home}"/>
          <arg value="${jreDirectory}"/>
        </exec>
      </j:if>
    
    </define:tag>
    
  </define:taglib>

</project>
