<!--
  ~ JBoss, Home of Professional Open Source.
  ~ Copyright 2010, Red Hat, Inc., and individual contributors
  ~ as indicated by the @author tags. See the copyright.txt file in the
  ~ distribution for a full listing of individual contributors.
  ~
  ~ This is free software; you can redistribute it and/or modify it
  ~ under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2.1 of
  ~ the License, or (at your option) any later version.
  ~
  ~ This software is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  ~ Lesser General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public
  ~ License along with this software; if not, write to the Free
  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  -->

<project name="module-repository-lib" xmlns:artifact="antlib:org.apache.maven.artifact.ant">

    <property name="src.dir" value="src"/>
    <property name="module.repo.src.dir" value="${src.dir}/main/resources/modules/system/layers/base"/>
    <property name="output.dir" value="target/jboss-as-${jboss.as.release.version}"/>
    <property name="module.repo.output.dir" value="${output.dir}/modules/system/layers/base"/>
    <property name="module.xml" value="module.xml"/>

    <property name="bundle.repo.output.dir" value="${output.dir}/bundles/system/layers/base"/>


    <taskdef name="jandex" classname="org.jboss.jandex.JandexAntTask" />

    <macrodef name="module-def">
        <attribute name="name"/>
        <attribute name="slot" default="main"/>
        <attribute name="src" default="${module.repo.src.dir}" />
        <element name="resources" implicit="yes" optional="yes"/>

        <sequential>
            <echo message="Initializing module -> @{name}"/>
            <!-- Figure out the correct module path -->
            <define-module-dir name="@{name}" slot="@{slot}"/>

            <delete dir="${module.repo.output.dir}/${current.module.path}" quiet="true" />

            <!-- Make the module output directory -->
            <mkdir dir="${module.repo.output.dir}/${current.module.path}"/>

            <!-- Copy the module.xml and other stuff to the output directory, skipping binaries -->
            <copy todir="${module.repo.output.dir}/${current.module.path}">
                <filterset>
                    <filter token="project.version" value="${project.version}" />
                </filterset>
                <fileset dir="@{src}/${current.module.path}">
                    <include name="**"/>
                    <exclude name="**/*.png"/>
                    <exclude name="**/*.jpg"/>
                    <exclude name="**/*.gif"/>
                    <exclude name="**/*.pdf"/>
                    <exclude name="**/*.ico"/>
                </fileset>
            </copy>
            <!-- Copy the binaries binaries -->
            <copy todir="${module.repo.output.dir}/${current.module.path}">
                <fileset dir="@{src}/${current.module.path}">
                    <include name="**/*.png"/>
                    <include name="**/*.jpg"/>
                    <include name="**/*.gif"/>
                    <include name="**/*.pdf"/>
                    <include name="**/*.ico"/>
                </fileset>
            </copy>

            <!-- Process the resource -->
            <resources/>

            <!-- Some final cleanup -->
            <replace file="${module.repo.output.dir}/${current.module.path}/${module.xml}">
                <replacetoken>
                    <![CDATA[
        <!-- Insert resources here -->]]></replacetoken>
                <replacevalue>
                </replacevalue>
            </replace>

        </sequential>
    </macrodef>

    <macrodef name="bundle-def">
        <attribute name="name"/>
        <attribute name="slot" default="main"/>
        <element name="resources" implicit="yes" optional="yes"/>

        <sequential>
            <echo message="Initializing bundle -> @{name}"/>
            <!-- Figure out the correct bundle path -->
            <define-bundle-dir name="@{name}" slot="@{slot}" />

            <!-- Make the bundle output director -->
            <mkdir dir="${bundle.repo.output.dir}/${current.bundle.path}"/>

            <!-- Process the resource -->
            <resources/>

        </sequential>
    </macrodef>

    <macrodef name="maven-bundle" >
        <attribute name="group"/>
        <attribute name="artifact"/>

        <sequential>
            <!-- Copy the jar to the bundle dir -->
            <copy todir="${bundle.repo.output.dir}/${current.bundle.path}" failonerror="true">
                <fileset file="${@{group}:@{artifact}:jar}"/>
                <mapper type="flatten" />
            </copy>
        </sequential>
    </macrodef>
    
    <scriptdef name="define-module-dir" language="javascript">
        <attribute name="name"/>
        <attribute name="slot"/>
        <![CDATA[
            name = attributes.get("name");
            name = name.split(".").join("/");
            if (name) {
              self.log("Use JDK8 method to build module names");
            } else {
              name = attributes.get("name");
              name = name.replace(".", "/");
              self.log("Use JDK7 method to build module names");
            }
            project.setProperty("current.module.path", name + "/" + attributes.get("slot"));
        ]]>
    </scriptdef>

    <scriptdef name="define-bundle-dir" language="javascript">
        <attribute name="name"/>
        <attribute name="slot"/>
        <![CDATA[
            name = attributes.get("name");
            name = name.split(".").join("/");
            if (name) {
              self.log("Use JDK8 method to build module names");
            } else {
              name = attributes.get("name");
              name = name.replace(".", "/");
              self.log("Use JDK7 method to build module names");
            }
            project.setProperty("current.bundle.path", name + "/" + attributes.get("slot"));
        ]]>
    </scriptdef>
    
    <macrodef name="maven-resource" >
        <attribute name="group"/>
        <attribute name="artifact"/>
        <attribute name="jandex" default="false" />

        <sequential>
            <!-- Copy the jar to the module dir -->
            <copy todir="${module.repo.output.dir}/${current.module.path}" failonerror="true">
                <fileset file="${@{group}:@{artifact}:jar}"/>
                <mapper type="flatten" />
            </copy>

            <basename file="${@{group}:@{artifact}:jar}" property="resourcename.@{group}.@{artifact}"/>
            <!-- Generate the Jandex Index -->
            <jandex run="@{jandex}" newJar="true" >
                <fileset dir="${module.repo.output.dir}/${current.module.path}" />
            </jandex>
            <!-- Update the resource entry in module.xml -->
            <define-resource-root path="${resourcename.@{group}.@{artifact}}" jandex="@{jandex}"/>
            <replace file="${module.repo.output.dir}/${current.module.path}/${module.xml}">
                <replacefilter token="&lt;!-- Insert resources here --&gt;" value="${current.resource.root}&#10;        &lt;!-- Insert resources here --&gt;"/>
            </replace>
        </sequential>
    </macrodef>

    <macrodef name="maven-resource-with-classifier" >
        <attribute name="group"/>
        <attribute name="artifact"/>
        <attribute name="classifier"/>
        <attribute name="jandex" default="false" />

        <sequential>
            <!-- Copy the jar to the module dir -->
            <copy todir="${module.repo.output.dir}/${current.module.path}" failonerror="true">
                <fileset file="${@{group}:@{artifact}:jar:@{classifier}}"/>
                <!-- http://jira.codehaus.org/browse/MANTRUN-159 -->
                <mapper type="flatten" />
            </copy>

            <basename file="${@{group}:@{artifact}:jar:@{classifier}}" property="resourcename.@{group}.@{artifact}.@{classifier}"/>

            <!-- Update the resource entry in module.xml -->
            <define-resource-root path="${resourcename.@{group}.@{artifact}.@{classifier}}"/>
            <replace file="${module.repo.output.dir}/${current.module.path}/${module.xml}">
                <replacefilter token="&lt;!-- Insert resources here --&gt;" value="${current.resource.root}&#10;        &lt;!-- Insert resources here --&gt;"/>
            </replace>
        </sequential>
    </macrodef>

    <macrodef name="extract-native-jar" >
        <attribute name="group"/>
        <attribute name="artifact"/>
        <sequential>
        <unzip src="${@{group}:@{artifact}:jar}" dest="${module.repo.output.dir}/${current.module.path}">
           <patternset>
               <include name="lib/**"/>
           </patternset>
       </unzip>
        </sequential>
    </macrodef>

    <scriptdef name="define-resource-root" language="javascript">
        <attribute name="path"/>
        <attribute name="jandex"/>
        <![CDATA[
            path = attributes.get("path");
            root = "<resource-root path=\"" + path + "\"/>";
            if(path.indexOf('${') != -1) {
                throw "Module resource root not found, make sure it is listed in build/pom.xml" + path;
            }
            if(attributes.get("jandex") == "true" ) {
                root = root + "\n\t<resource-root path=\"" + path.replace(".jar","-jandex.jar") + "\"/>";
            }
            project.setProperty("current.resource.root", root);
        ]]>
    </scriptdef>

</project>
