<?xml version="1.0"?>

<project
  xmlns:j="jelly:core"
  xmlns:i="jelly:interaction"
  xmlns:ant="jelly:ant" 
  xmlns:define="jelly:define"
  xmlns:u="jelly:util"
  xmlns:genapp="genapp">

  <define:taglib uri="genapp">
    <!--
     |
     | @template
     |
     -->
    <define:tag name="generate">
      <!-- Get the template -->
      <!-- create a temporary variable with no dots in the name to get around a jexl bug -->
      <j:set var="presettemplate" value="${template}"/>
      <j:if test="${empty(presettemplate)}">
        <i:ask question="${maven.genapp.prompt.template}" answer="template" default="${maven.genapp.default.template}"/>
      </j:if>
      
      <!-- If template is in the user's home directory, use it -->    
      <u:available file="${user.home}/.maven/template/${template}">
        <j:set var="maven.genapp.template.dir" value="${user.home}/.maven/template/${template}"/>
      </u:available>
      
      <j:set var="currenttemplatedir" value="${maven.genapp.template.dir}"/>
      <j:if test="${empty(currenttemplatedir)}">
        <!-- The template was not found in user.home, check in the plugin resources -->
        <u:available file="${plugin.resources}/${template}">
          <j:set var="maven.genapp.template.dir" value="${plugin.resources}/${template}"/>
        </u:available>
      </j:if>
      
      <j:set var="currenttemplatedir" value="${maven.genapp.template.dir}"/>
      <j:if test="${empty(currenttemplatedir)}">
        <fail>
          Unable to locate a template named ${template}
        </fail>
      </j:if>
      
      <!-- Ensure that the template has a template-resources directory and use it -->
      <u:available file="${maven.genapp.template.dir}/template-resources">
        <!-- Set resources dir to the template resources directory -->
        <j:set var="maven.genapp.resources" value="${maven.genapp.template.dir}/template-resources"/>
      </u:available>
  
      <!-- Check that we have a template-resources directory, if not the template is broken -->
      <j:set var="currenttemplateresources" value="${maven.genapp.resources}"/>
      <j:if test="${empty(currenttemplateresources)}">
        <fail>
          Found a template in ${maven.genapp.template.dir}, but it was broken.
        </fail>
      </j:if>
      
      <!-- Load template properties if available -->
      <u:available file="${maven.genapp.template.dir}/template.properties">
        <u:properties file="${maven.genapp.template.dir}/template.properties"/>
      </u:available>
      
      <!-- Ensure we have a project id, name, and package -->
      
      <j:set var="presetid" value="${maven.genapp.template.id}"/>
      <j:if test="${empty(presetid)}">
        <i:ask question="${maven.genapp.prompt.id}" answer="maven.genapp.template.id" default="${maven.genapp.default.id}"/>
      </j:if>
      
      <j:set var="presetname" value="${maven.genapp.template.name}"/>
      <j:if test="${empty(presetname)}">
        <i:ask question="${maven.genapp.prompt.name}" answer="maven.genapp.template.name" default="${maven.genapp.default.name}"/>
      </j:if>
      
      <j:set var="presetpackage" value="${maven.genapp.template.package}"/>
      <j:if test="${empty(presetpackage)}">
        <i:ask question="${maven.genapp.prompt.package}" answer="maven.genapp.template.package" default="${maven.genapp.default.package}"/>
      </j:if>
      
      <!-- Turn the specified package into a path -->
      <u:replace var="appPath" oldChar="." newChar="/" value="${maven.genapp.template.package}"/>
      
      <!-- We want to substitute in the id, name, and package name. -->
      <filter token="ID" value="${maven.genapp.template.id}"/>
      <filter token="NAME" value="${maven.genapp.template.name}"/>
      <filter token="PACKAGE" value="${maven.genapp.template.package}"/>
  
      <!-- split the list of dirs that need repackaged -->
      <u:tokenize var="maven.genapp.repackage.split" delim=",">${maven.genapp.repackage}</u:tokenize>
  
      <!-- copy resources that need repackaged, eg java files -->
      <j:forEach var="res" items="${maven.genapp.repackage.split}">
        <ant:copy todir="${basedir}/src/${res}/${appPath}" 
              filtering="true">
          <ant:fileset dir="${maven.genapp.resources}/src/${res}" />
        </ant:copy>      
      </j:forEach>
  
      <!-- copy resources that just need filtered, eg project.xml -->
      <ant:copy todir="${basedir}" filtering="true">      
        <ant:fileset dir="${maven.genapp.resources}">
          <j:forEach var="res" items="${maven.genapp.filter}">
            <include name="${res}"/>
          </j:forEach>
        </ant:fileset>
      </ant:copy>
  
      <!-- copy rest of app unfiltered -->
      <ant:copy todir="${basedir}">      
        <ant:fileset dir="${maven.genapp.resources}">
          <ant:exclude name="src/java"/>
          <ant:exclude name="src/test"/>
          <j:forEach var="res" items="${maven.genapp.filter}">
            <exclude name="${res}"/>
          </j:forEach>
          <j:forEach var="res" items="${maven.genapp.repackage.split}">
            <exclude name="src/${res}/**"/>
          </j:forEach>
        </ant:fileset>
      </ant:copy>
      
      <!-- Run the specific jelly script for the template -->
      <u:available file="${maven.genapp.template.dir}/template.jelly">
        <echo>Use template.jelly file present in ${maven.genapp.template.dir}.</echo>
        <j:import file="${maven.genapp.template.dir}/template.jelly" inherit="true"/> 
      </u:available>
    </define:tag>
  </define:taglib>
  
  <goal 
    name="genapp" 
    description="Generate Application based on a template">

    <genapp:generate template="${maven.genapp.template}"/>
  </goal>

</project>
