<?xml version="1.0" encoding="UTF-8"?>
<!--

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.

-->

<!--
  This is the base build script for an nbi group package.
  
  It is not intended to be used directly, but to be imported into derivative
  implementation scripts, which will provide properties' values that are 
  specific the concrete group implementation. The derivative scripts may also
  provide some additional functionality that is required by their respective
  group implementations.
  
  This script exposes five primary targets: 'init', 'clean', 'checkout', 
  'build' and 'release'; as well as two aggregate targets: 'build-all', 
  which tranlates into 'init', 'clean', 'checkout', 'build', and 
  'release-all' - 'init', 'clean', 'checkout', 'build', 'release'. For the 
  detailed description of the functionality of these targets, consult the 
  corresponding documentation.
-->
<project name="group" default="usage" basedir=".">
    <!-- a default value for the ${common.dir} property, which allows the user 
         to see the usage information is he calls this script directly and not
         via a derivative (which is the proper way to do this) -->
    <property name="common.dir" value="${basedir}"/>
    
    <!-- import the common properties -->
    <property file="${common.dir}/common.properties"/>
    
    <!-- import properties specific to the group package's build script -->
    <property file="${common.dir}/group.properties"/>
    
    <!-- import the common target library -->
    <import file="common.xml" optional="false"/>
    
    <!-- ============================================================================
      Initialization
    ============================================================================= -->
    
    <!--
      Initializes the group package's build script.
      
      This target overrides the '-init' target in common.xml, providing the 
      custom functionality that is required for the group package's build 
      script initialization.
      
      In addition to the standard initialization routine, this target defines
      several custom ant tasks, which are specific to the group package's build 
      script.
      
      This target is not intended to be called directly.
    -->
    <target name="-init" depends=".init">
        <!-- define custom ant tasks -->
        <taskdef 
            name="sizeof" 
            classname="org.netbeans.installer.infra.build.ant.SizeOf" 
            classpath="${custom.tasks.cls}"/>
        <taskdef 
            name="md5" 
            classname="org.netbeans.installer.infra.build.ant.Md5" 
            classpath="${custom.tasks.cls}"/>
        <taskdef 
            name="load-locales" 
            classname="org.netbeans.installer.infra.build.ant.LoadLocales" 
            classpath="${custom.tasks.cls}"/>
        <taskdef 
            name="group-descriptor" 
            classname="org.netbeans.installer.infra.build.ant.GroupDescriptor" 
            classpath="${custom.tasks.cls}"/>
        
        <if property="release.to.server" value="true">
            <taskdef
                name="release-package-server"
                classname="org.netbeans.installer.infra.build.ant.ReleasePackage"
                classpath="${custom.tasks.cls}"/>
        </if>
        
        <if property="release.to.server" value="false">
            <taskdef 
                name="release-package-filesystem"
                classname="org.netbeans.installer.infra.build.ant.registries.AddPackage"
                classpath="${custom.tasks.cls}"/>
        </if>
    </target>
    
    <!-- ============================================================================
      Clean-up
    ============================================================================= -->
    
    <!--
      Cleans the current working directory and the distributives directory.
      
      This target overrides the '-clean' target in common.xml, providing the 
      custom functionality that is required for correctly cleaning the group 
      package.
      
      In addition to the standard behavior the group package distributive file 
      is removed from the distributives directory.
      
      This target is not intended to be called directly.
    -->
    <target name="-clean" depends=".clean">
        <!-- remove the group's distributive file from the distributives 
             directory -->
        <delete file="${dist.dir}/${group.dist.file.name}"/>
    </target>
    
    <!-- ============================================================================
      Check-out
    ============================================================================= -->
    
    <!--
      The functionality of the checkout procedure for the group package is 
      indentical to the standard one, thus there is not need to override the 
      '-checkout' target, whosew default implementation is present in 
      common.xml.
    -->
    
    <!-- ============================================================================
      Build
    ============================================================================= -->
    
    <!--
      Builds the group package.
      
      This target overrides the '-build' target in common.xml, providing the 
      custom functionality that is required for correctly building the group 
      package.
      
      The build process for the grup package consists of four steps.
      1) The group's icon is copied from its location to the group's package 
         directory structure and some metadata is calculated
      
      2) The group's localizing bundle is loaded and the group package 
         descriptor is generated
      
      3) The manifest for the group package is generated
      
      4) The package directory structure is jarred, digitally signed and moved 
         to the distributives directory
      
      This target is not intended to be called directly.
    -->
    <target name="-build" depends=".build">
        <!-- copy the group's icon to the soon-to-be package directory 
             structure and calculate some metadata for it -->
        <copy file="${group.icon}" 
              tofile="${package.dir}/${group.icon.path}"/>
        <sizeof file="${package.dir}/${group.icon.path}" 
                property="group.icon.size"/>
        <md5 file="${package.dir}/${group.icon.path}" 
             property="group.icon.md5"/>
        <set 
            property="group.icon.correct.uri" 
            value="${group.icon.uri}"/>
        
        <!-- load the group's localizing bundle and store the locales list to 
             ${group.locales.list} -->
        <load-locales basename="${group.bundle}" list="group.locales.list"/>
        
        <!-- generate the group package descriptor -->
        <group-descriptor file="${package.dir}/${package.descriptor.path}"/>
        
        <!-- create the package jar file and sign it -->
        <jar basedir="${package.dir}"
             destfile="${work.dir}/${group.dist.file.name}" 
             compress="true">
            <manifest>
                <attribute 
                    name="Product-Descriptor" 
                    value="${package.descriptor.path}"/>
            </manifest>
        </jar>
        
        <!-- copy the distributive to the distributives directory -->
        <move file="${work.dir}/${group.dist.file.name}" 
              todir="${dist.dir}"/>
        <if property="jarsigner.enabled" value="true">
            <signjar jar="${dist.dir}/${group.dist.file.name}" 
                     keystore="${jarsigner.keystore}"
                     storepass="${jarsigner.storepass}"
                     alias="${jarsigner.alias}"
                     maxmemory="${jarsigner.xmx}"/>
	</if>
    </target>
    
    <!-- ============================================================================
      Release
    ============================================================================= -->
    
    <!--
      Releases the group package to the registries server.
      
      This target overrides the '-release' target in common.xml, providing the 
      custom functionality that is required for correctly releasing the group 
      package.
      
      It is assumed that the group package has already been built, but a 
      dependency on the 'build' target is not defined, as it may be desirable 
      to build once and then release several times to different registries 
      servers.
      
      This target is not intended to be called directly.
    -->
    <target name="-release">
        <if property="release.to.server" value="true">
            <echo message="Release URL is ${release.url}"/>
            <echo message="Archive is ${dist.dir}/${group.dist.file.name}"/>
            
            <release-package-server
                url="${release.url}"
                registry="${release.registry}"
                uid="${release.parent.uid}"
                version="${release.parent.version}"
                archive="${dist.dir}/${group.dist.file.name}"/>
        </if>
        <if property="release.to.server" value="false">
            <echo message="Registry root is ${release.registry.dir}"/>
            <echo message="Archive is ${dist.dir}/${group.dist.file.name}"/>
            
            <release-package-filesystem
                root="${release.registry.dir}"
                uid="${release.parent.uid}"
                version="${release.parent.version}"
                archive="${dist.dir}/${group.dist.file.name}"/>
        </if>
    </target>
    
    <!-- ============================================================================
      Default
    ============================================================================= -->
    
    <!--
      The default target. 
      
      Does nothing except printing the usage information.
    -->
    <target name="usage">
        <echo>
            This is the base build script for nbi group package and is not 
            intended to be called directly. In order to create a derivative 
            implementation script, see 
            ${basedir}/.templates/group-template.xml
        </echo>
    </target>
</project>
