<?xml version="1.0"?>
<project name="project">

    <target name="getMajorMinorVersion">
        <script language="javascript">
            <![CDATA[

                  // getting the value
                  buildnumber = project.getProperty("unqualifiedVersion");
                  index = buildnumber.lastIndexOf(".");
                  counter = buildnumber.substring(0, index);
            project.setProperty("majorMinorVersion",counter);

              ]]>
        </script>
    </target>

    <target name="test_getMajorMinor" depends="getMajorMinorVersion">
        <echo message="majorMinorVersion: ${majorMinorVersion}"/>
    </target>

    <!--
    site.label      The name/title/label of the created composite site
    unqualifiedVersion  The version without any qualifier replacement
    buildQualifier    The build qualifier
  -->
    <target name="compute.child.repository.data" depends="getMajorMinorVersion">
        <property name="full.version" value="${unqualifiedVersion}" />

        <property name="software.download.area" location="${user.home}/p2.repositories" />
        <property name="updates.dir" value="updates" />

        <property name="site.composite.name" value="${site.label} ${majorMinorVersion}" />
        <property name="main.site.composite.name" value="${site.label} All Versions" />

    	<!--
        <property name="main.composite.repository.directory" location="${software.download.area}/${updates.dir}" />
      -->
        <property name="main.composite.repository.directory" location="${software.download.area}" />

        <property name="composite.repository.directory" value="${main.composite.repository.directory}/${majorMinorVersion}" />
        <property name="child.repository" value="${full.version}" />
        <property name="child.repository.directory" value="${composite.repository.directory}/${child.repository}/" />
    </target>

    <target name="p2.composite.add" depends="compute.child.repository.data">
        <property name="source.repository" location="${project.build.directory}/repository"/>

        <echo message=" " />

        <echo message="Source repository path: ${source.repository}" />

        <echo message="Copying to ${child.repository.directory}..." />

        <mkdir dir="${child.repository.directory}"/>
        <copy todir="${child.repository.directory}" overwrite="true">
            <fileset dir="${source.repository}" />
        </copy>

        <add.composite.repository.internal
      composite.repository.location="${composite.repository.directory}"
      composite.repository.name="${site.composite.name}"
      composite.repository.child="${child.repository}"
    />

        <add.composite.repository.internal
      composite.repository.location="${main.composite.repository.directory}"
      composite.repository.name="${main.site.composite.name}"
      composite.repository.child="${majorMinorVersion}"
    />

    </target>

    <!-- = = = = = = = = = = = = = = = = =
          macrodef: add.composite.repository.internal          
         = = = = = = = = = = = = = = = = = -->
    <macrodef name="add.composite.repository.internal">
        <attribute name="composite.repository.location" />
        <attribute name="composite.repository.name" />
        <attribute name="composite.repository.child" />
        <sequential>

            <echo message=" " />
            <echo message="Composite repository       : @{composite.repository.location}" />
            <echo message="Composite name             : @{composite.repository.name}" />
            <echo message="Adding child repository    : @{composite.repository.child}" />

            <p2.composite.repository>
                <repository compressed="false" location="@{composite.repository.location}" name="@{composite.repository.name}" />
                <add>
                    <repository location="@{composite.repository.child}" />
                </add>
            </p2.composite.repository>

            <echo file="@{composite.repository.location}/p2.index">version=1
metadata.repository.factory.order=compositeContent.xml,\!
artifact.repository.factory.order=compositeArtifacts.xml,\!
</echo>

        </sequential>
    </macrodef>


</project>
