<?xml version="1.0"?>

<project
  xmlns:http="jelly:http"
  xmlns:j="jelly:core"
  xmlns:maven="jelly:maven"
  xmlns:u="jelly:util"
  xmlns:x="jelly:xml">

  <goal name="plugin" description="Build a plugin jar and install to the maven.local.repo">

    <!-- Plugins are just a jar. -->
    <attainGoal name="jar:jar"/>

    <copy file="${maven.build.dir}/${maven.final.name}.jar"
      todir="${maven.repo.local}/maven/jars" />

  </goal>

  <goal name="plugin:install" prereqs="plugin"
    description="Install the plugin jar, prepare Maven to expand it locally and clear caches">

    <copy file="${maven.build.dir}/${maven.final.name}.jar"
      todir="${maven.home}/plugins"/>

    <delete verbose="false">
      <fileset dir="${maven.home}/plugins">
        <include name="${pom.artifactId}-*.jar" />
        <exclude name="${maven.final.name}.jar" />
      </fileset>
    </delete>

    <delete includeEmptyDirs="true" verbose="false">
      <fileset dir="${maven.plugin.unpacked.dir}">
        <include name="*.cache"/>
        <include name="**/.processed" />
        <include name="${pom.artifactId}-*/**" />
        <exclude name="${maven.final.name}.jar" />
      </fileset>
    </delete>

  </goal>

  <goal name="plugin:deploy" prereqs="plugin"
    description="Install a specific unpacked plugin but no new jar">

    <mkdir dir="${maven.plugin.unpacked.dir}/${maven.final.name}"/>
    <unzip src="${maven.build.dir}/${maven.final.name}.jar"
      dest="${maven.plugin.unpacked.dir}/${maven.final.name}"/>

    <echo message="" >
  Warning: Contents of ${maven.home}/plugins/${maven.final.name}.jar does not match
  the unpacked contents of ${maven.plugin.unpacked.dir}/${maven.final.name} directory.
    </echo>

  </goal>


  <!-- generate documentation -->

  <!-- generate docs that are usually missing -->
  <goal name="plugin:generate-docs"
    description="Generate navigation, goals and properties docs">
    <attainGoal name="plugin:generate-navigation" />
    <attainGoal name="plugin:generate-goals" />
    <attainGoal name="plugin:generate-properties" />
  </goal>

  <!-- generate a skeletal navigation.xml for the plugin specified
       by the 'plugin' variable -->
  <goal name="plugin:generate-navigation"
    description="Generate navigation.xml for the plugin">

    <j:set var="skip" value="false" />
    <j:set var="fileName">${maven.docs.src}/navigation.xml</j:set>
    <u:available file="${fileName}">
      <echo>Skipping file as '${fileName}' already exists</echo>
      <j:set var="skip" value="true" />
    </u:available>

    <j:if test="${!skip}">
      <mkdir dir="${maven.docs.src}" />
      <echo>Generating file '${fileName}'</echo>
      <j:file name="${fileName}" prettyPrint="true">
        <j:import file="${plugin.resources}/templates/navigation.jelly" inherit="true"/>
      </j:file>
    </j:if>
  </goal>

  <!-- generate a skeletal goals.xml for the plugin specified -->
  <goal name="plugin:generate-goals"
    description="Generate goals.xml for the plugin">

    <j:set var="skip" value="false" />
    <j:set var="fileName">${maven.docs.src}/goals.xml</j:set>
    <u:available file="${fileName}">
      <echo>Skipping file as '${fileName}' already exists</echo>
      <j:set var="skip" value="true" />
    </u:available>

    <j:if test="${!skip}">
      <j:set var="pluginScript" value="false" />
      <u:available file="${basedir}/plugin.jelly">
        <j:set var="pluginScript" value="true" />
      </u:available>

      <mkdir dir="${maven.docs.src}" />

      <j:if test="${!pluginScript}">
        <!-- no plugin.jelly - generate a file for empty goals -->
        <echo>Generating 'empty' goals file '${fileName}'</echo>
        <j:set var="template" value="${plugin.resources}/templates/goals-empty.jelly" />
      </j:if>

      <j:if test="${pluginScript}">
        <!-- generate docs based on plugin.jelly -->
        <u:file name="${basedir}/plugin.jelly" var="script" />
        <x:parse xml="${script}" var="def"/>
        <echo>Generating file '${fileName}'</echo>
        <j:set var="template" value="${plugin.resources}/templates/goals.jelly"/>
      </j:if>

      <j:file name="${fileName}" prettyPrint="true">
        <j:import file="${template}" inherit="true"/>
      </j:file>

    </j:if>
  </goal>

  <!-- generate properties documentation -->
  <goal name="plugin:generate-properties"
    description="Generate properties.xml for the plugin">


    <j:set var="skip" value="false" />
    <j:set var="fileName">${maven.docs.src}/properties.xml</j:set>
    <u:available file="${fileName}">
      <echo>Skipping file as '${fileName}' already exists</echo>
      <j:set var="skip" value="true" />
    </u:available>

    <j:if test="${!skip}">
      <j:set var="props" value="false"/>
      <j:set var="propsFileName">${basedir}/plugin.properties</j:set>
      <u:available file="${propsFileName}">
        <j:set var="props" value="true" />
      </u:available>

      <j:if test="${!props}">
        <echo>Generating 'empty' properties file '${fileName}'</echo>
        <j:set var="template" value="${plugin.resources}/templates/properties-empty.jelly"/>
      </j:if>

      <j:if test="${props}">
        <echo>Generating file '${fileName}'</echo>
        <j:scope>
          <u:properties file="${propsFileName}" />
          <j:set var="vars" value="${context.variables}" scope="parent"/>
        </j:scope>
        <j:set var="template" value="${plugin.resources}/templates/properties.jelly"/>
      </j:if>

      <mkdir dir="${maven.docs.src}" />

      <j:file name="${fileName}" prettyPrint="true">
        <j:import file="${template}" inherit="true"/>
      </j:file>

    </j:if>
  </goal>
  
  <!-- download a plugin from a remote repo -->
  <goal name="plugin:download">
    <maven:param-check value="${artifactId}" fail="true" message="'artifactId' must be specified"/>
    <maven:param-check value="${groupId}" fail="true" message="'groupId' must be specified"/>
    <maven:param-check value="${version}" fail="true" message="'version' must be specified"/>
    <j:set var="repoList">${maven.repo.remote}</j:set>
    <u:tokenize var="repos" delim=",">${repoList.trim()}</u:tokenize>
    <j:forEach var="repo" items="${repos}">
      <echo>repo is '${repo}'</echo>
      <u:file var="localPlugin" 
        name="${maven.home}/plugins/${artifactId}-${version}.jar" />
      <j:if test="${!localPlugin.exists()}">
        <j:set var="remoteFile"
          value="${repo}/${groupId}/jars/${artifactId}-${version}.jar" />
        <echo>trying to download ${remoteFile}</echo>
        <http:get uri="${remoteFile}" var="outputVar" />
      </j:if>
    </j:forEach>
  </goal>
</project>
