<?xml version="1.0" encoding="iso-8859-1"?>

<!--
    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 1997-2018 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://oss.oracle.com/licenses/CDDL+GPL-1.1
    or LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<project name="modules" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant"> 

    <!-- An importer of modules.xml MUST make sure that the init target is called. 
	 The importer must also have a copy maven-ant-tasks-2.1.1.jar available.
	 TODO: where should we put maven-ant-tasks?
	 -->

    <target name="init">
        <!-- set up the maven ant tasks -->
        <property name="maven.ant.file"         value="maven-ant-tasks-2.1.1.jar"/>
        <path id="maven-ant-tasks.classpath" path="${basedir}/lib/${maven.ant.file}"/>
        <typedef resource="org/apache/maven/artifact/ant/antlib.xml" 
            uri="antlib:org.apache.maven.artifact.ant" 
            classpathref="maven-ant-tasks.classpath">
        </typedef>

        <!-- wagon-svn is needed to push artifacts to maven -->
        <artifact:install-provider artifactId="wagon-svn"
         groupId="org.jvnet.wagon-svn" version="1.7">
            <artifact:remoteRepository id="java.net.maven2.repository"
             url="http://download.java.net/maven/2/" />
        </artifact:install-provider> 

        <!-- We need bnd internally -->
        <artifact:dependencies pathId="bnd.classpath">
            <remoteRepository id="bnd.repository"
             url="http://www.aqute.biz/repo"/>
            <dependency groupId="biz.aQute" 
                artifactId="bnd"
                version="0.0.401" scope="compile"/>
        </artifact:dependencies>
    </target>

    <!-- Useful for getting dependencies placed into a directory.
	 Also creates @{name}.classpath, which can be used to reference
	 the dependency. 
    -->

    <macrodef name="get-and-copy-dependencies">
        <attribute name="name" default=""/>
        <attribute name="repository" default=""/>
        <attribute name="destDir" default=""/>
        <element name="dependency-list" optional="no"/>
        <sequential>
            <echo message="get-and-copy-dependencies: name=@{name} repository=@{repository} destDir=@{destDir}"/>
            <artifact:dependencies versionsId="@{name}.versions"
                filesetId="@{name}.fileset" pathId="@{name}.classpath">
               <remoteRepository id="@{name}.repid" url="@{repository}"/>
               <dependency-list/>
            </artifact:dependencies>

            <mkdir dir="@{destDir}"/>
            <copy todir="@{destDir}">
                <fileset refid="@{name}.fileset"/>
                <mapper classpathref="maven-ant-tasks.classpath" 
                    classname="org.apache.maven.artifact.ant.VersionMapper"
                    from="${@{name}.versions}" to="flatten"/>
            </copy>
        </sequential>
    </macrodef>

    <!-- ================== OSGi bundle support ======================= -->

    <!-- internal use only -->

    <scriptdef name="make-bundle-file" language="javascript">
	<attribute name="version"/>
	<attribute name="outfile"/>
        <attribute name="bundlename"/>
	<element name="directive" classname="org.apache.tools.ant.types.Parameter"/>
	<![CDATA[
	    function openFile( name ) {
		var fos = new java.io.FileOutputStream( name ) ;
		var osw = new java.io.OutputStreamWriter( fos ) ;
		var writer = new java.io.BufferedWriter( osw ) ;
		return writer ;
	    }

	    function writeString( file, str ) {
	        file.write( str, 0, str.length ) ;
		file.newLine() ;
	    }

	    function convert( object ) {
		if (object==null) {
		    return null ;
		}

		var res = object.toString() ;
		if (res.startsWith( "[" )) {
		    var flag = true ;
		    res = "" ;
		    for (var ctr=0; ctr<object.length; ctr++) {
		        if (flag) {
			    flag = false ;
			} else {
			    res += ", " ;
			}
			res += convert( object[ctr] ) ;
		    }
		} 
		return res ;
	    }

	    var version = attributes.get("version") ; 
	    var outfile = attributes.get("outfile") ; 
	    var bundlename = attributes.get("bundlename") ; 

	    // Write out the bnd control file

	    var file = openFile( outfile ) ;

	    try {
		writeString( file, "Bundle-Version=" + version ) ;
                writeString( file, "Bundle-SymbolicName=" + bundlename ) ;
		var directives = elements.get("directive") ;
		for (var ctr = 0; ctr<directives.size(); ctr++) {
		    var directive = directives.get(ctr) ;
		    var type = directive.getType() ;
		    var name = directive.getName() ;
		    var value = directive.getValue() ;
		    // self.log( "directives[" + ctr + "]:" ) ;
		    // self.log( "\ttype=" + directive.getType() ) ;
		    // self.log( "\tname=" + directive.getName() ) ;
		    // self.log( "\tvalue=" + convert( directive.getValue() ) ) ;
		    var content = null ;
		    if (name==null) {
			content = value ;
		    } else {
			content = project.references.get( name ) ;
			if (content == null) {
			    throw new Error( "Content for type " + type + " is null" ) ; 
			} else {
			    content = content.getIncludePatterns( project ) ;
			}
		    }
		    // self.log( "\tcontent=" + convert( content ) ) ;
		    writeString( file, type + ": " + convert( content ) ) ;
		}
	    } finally {
		file.close() ;
	    }
	]]>
    </scriptdef>

    <!-- Macro to use for generating OSGi bundles.
         version: A string giving a standard OSGi version.
         classpath: The actual value of the classpath
         outDir: The name of the directory that should contain the generated bundle
         outFile: The name of the generated bundle WITHOUT the .jar suffix

	 <directive-list must contain <directive> elements, where each
	 <directive> element has the following attributes:
	     type: the name (like Export-Package) that is used in the bnd file (REQUIRED)
             One of name and value is required:
	     name: A reference to the value, which must be a 
                patternset of java package names.
	     value: the actual string value
    -->

    <macrodef name="make-bundle">
	<attribute name="version"/>
	<attribute name="classpath"/>
	<attribute name="outDir"/>
	<attribute name="outFile"/>
        <attribute name="bundlename"/>
	<element name="directive-list"/>
	<sequential>
	    <make-bundle-file version="@{version}" outfile="@{bundlename}.bfile" bundlename="@{bundlename}">
		<directive-list/>
	    </make-bundle-file>
	
	    <!-- Use bnd to generate the bundle jar file. -->

	    <java classpathref="bnd.classpath" classname="aQute.bnd.main.bnd" 
		fork="true" failonerror="true">

		<arg value="build"/>
		<arg value="-noeclipse"/>
		<arg value="-classpath"/>
		<arg value="@{classpath}"/>
		<arg value="-output"/>
		<arg value="@{outDir}/@{outFile}"/>
		<arg value="@{bundlename}.bfile"/>
	    </java>

	    <delete file="@{bundlename}.bfile"/>
	</sequential>
    </macrodef>	

    <!-- Making poms and pushing artifacts to maven 
	 Several names are built from the artifactId:
	 @{artifactId}.pom: the internal reference to the (in memory) POM file.
	 @{artifactId}.pom.xml: the name of the POM file written to an output directory.
	 @{artifactId}.jar: the name of the jar file for the artifact.
     -->

    <macrodef name="make-pom">
	<attribute name="outdir"/>
	<attribute name="groupId"/>
	<attribute name="artifactId"/>
	<attribute name="version"/>
	<attribute name="description"/>
	<attribute name="url"/>

	<element name="element-list" optional="true"/>

	<sequential>
	    <artifact:pom 
		id="@{artifactId}.pom" 
		groupId="@{groupId}"
		artifactId="@{artifactId}" 
		version="@{version}">
		<organization
		    name="Oracle"
		    url="http://www.oracle.com"/>
		<element-list/>
	    </artifact:pom>
	    <artifact:writepom pomRefId="@{artifactId}.pom"
		file="@{outdir}/@{artifactId}.pom.xml"
		trim="false"/>
	</sequential>
    </macrodef>

    <macrodef name="push-to-local-maven">
	<attribute name="outdir"/>
        <attribute name="module"/>
        <sequential>
            <echo message="Releasing module @{module} to local repository"/>

	    <artifact:pom id="@{module}.pom" file="@{outdir}/@{module}.pom.xml"/>
            <property environment="env"/>
            <artifact:install file="@{outdir}/@{module}.jar">
                <pom refid="@{module}.pom"/>
            </artifact:install>
        </sequential>
    </macrodef>

    <macrodef name="push-to-maven">
	<attribute name="outdir"/>
        <attribute name="module"/>
        <attribute name="destination"/>
        <sequential>
            <echo message="Releasing module @{module} to @{destination}"/>

	    <artifact:pom id="@{module}.pom" file="@{outdir}/@{module}.pom.xml"/>
            <artifact:deploy file="@{outdir}/@{module}.jar">
                <remoteRepository url="@{destination}">
                    <authentication id="java.net.maven2.repository"/> <!-- this is java.net specific -->
                </remoteRepository>
                <pom refid="@{module}.pom"/>
            </artifact:deploy>
        </sequential>
    </macrodef>
</project>
