<?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 product 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 product implementation. The derivative scripts may also
  provide some additional functionality that is required by their respective
  product 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="product" 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 product package's build script -->
    <property file="${common.dir}/product.properties"/>
    
    <!-- import the common target library -->
    <import file="common.xml" optional="false"/>
    
    <!-- ============================================================================
      Initialization
    ============================================================================= -->
    
    <!--
      Initializes the product package's build script.
      
      This target overrides the '-init' target in common.xml, providing the 
      custom functionality that is required for the product package's build 
      script initialization.
      
      In addition to the standard initialization routine, this target defines
      several custom ant tasks, which are specific to the product 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="sum"
            classname="org.netbeans.installer.infra.build.ant.Sum"
            classpath="${custom.tasks.cls}"/>
        <taskdef
            name="package"
            classname="org.netbeans.installer.infra.build.ant.Package"
            classpath="${custom.tasks.cls}"/>
        <taskdef 
            name="load-locales" 
            classname="org.netbeans.installer.infra.build.ant.LoadLocales" 
            classpath="${custom.tasks.cls}"/>
        <taskdef 
            name="product-descriptor" 
            classname="org.netbeans.installer.infra.build.ant.ProductDescriptor" 
            classpath="${custom.tasks.cls}"/>
        <taskdef
            name="absolutize"
            classname="org.netbeans.installer.infra.build.ant.Absolutize"
            classpath="${custom.tasks.cls}"/>
        <taskdef
            name="uri-to-path"
            classname="org.netbeans.installer.infra.build.ant.UriToPath"
            classpath="${custom.tasks.cls}"/>
        <taskdef
            name="n-unzip"
            classname="org.netbeans.installer.infra.build.ant.NativeUnzip"
            classpath="${custom.tasks.cls}"/>
        <taskdef
            name="n-untar"
            classname="org.netbeans.installer.infra.build.ant.NativeUntar"
            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>
        
        <!-- check whether the engine should be built -->
        <condition property="do.build.engine">
            <equals arg1="${build.engine}" arg2="true"/>
        </condition>
        
        <!-- make the engine-related paths absolute -->
        <absolutize property="engine.work.dir"/>
        <absolutize property="engine.dist.dir"/>
        
        <absolutize property="engine.dist.file"/>
        
        <!-- define the javac.classpath for the netbeans project -->
        <property 
            name="nb.custom.parameter" 
            value="-Djavac.classpath=${engine.dist.file}"/>
    </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 product 
      package.
      
      In addition to the standard behavior the product package distributive file 
      is removed from the distributives directory.
      
      This target is not intended to be called directly.
    -->
    <target name="-clean" depends="-clean-engine,.clean">
        <!-- remove the product's distributive file from the distributives 
             directory -->
        <delete file="${dist.dir}/${product.dist.file.name}"/>
    </target>
    
    <!--
      Cleans the engine.
      
      It runs the 'clean' target on the engine's build script.
      
      This target is only executed if the the ${build.engine} property was 
      set to 'true'.
      
      This target is not intended to be called directly.
    -->
    <target name="-clean-engine" depends="init" if="do.build.engine">
        <exec executable="${ant.executable}" 
              dir="${engine.dir}" 
              failonerror="false">
            <env key="JAVA_HOME" value="${java.home}"/>
            <arg value="clean"/>
            
            <arg value="-Dwork.dir=${engine.work.dir}"/>
            <arg value="-Ddist.dir=${engine.dist.dir}"/>
            
            <arg value="-Dbuild.native=false"/>
            
            <arg value="-Ddont.build.custom.tasks=true"/>
            <arg value="-Dcustom.tasks.cls=${custom.tasks.cls}"/>
            
            <arg value="-Dengine.dist.file.name=${engine.dist.file.name}"/>
            
            <arg value="-Denvironment.properties=${environment.properties}"/>
        </exec>
    </target>
    
    <!-- ============================================================================
      Check-out
    ============================================================================= -->
    
    <!--
      The functionality of the checkout procedure for the product 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 product package.
      
      This target overrides the '-build' target in common.xml, providing the 
      custom functionality that is required for correctly building the product 
      package.
      
      The build process for the grup package consists of five steps.
      1) The search-and-replace is run over the product's sources, based on the
         [token] => [value] pairs defined in ${product.params.N.token} and
         ${product.params.N.value}
      
      2) The product's icon is copied from its location to the product's package 
         directory structure and some metadata is calculated
      
      3) The product's localizing bundle is loaded and the product package 
         descriptor is generated
      
      4) The manifest for the product package is generated
      
      5) 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-engine,.build">
        <!-- init the required disk space -->
        <set property="product.disk.space" 
             value="${product.disk.space.modificator}"/>
        
        <!-- init several shorthands -->
        <set 
            property="product.logic.dir" 
            value="${package.dir}/${product.logic.path}"/>
        <set 
            property="product.data.dir" 
            value="${package.dir}/${product.data.path}"/>
        
        <!-- some necessary mkdirs -->
        <mkdir dir="${packaged.data.dir}"/>
        <mkdir dir="${downloads.cache.dir}"/>
        <mkdir dir="${product.logic.dir}"/>
        <mkdir dir="${product.data.dir}"/>
        
        <!-- fetch the configuration logic jars; the first jar is handled 
             separately, if the the ${build.nbproject} has been set to true - 
             we don't need to fetch it, as we ca just copy -->
        <if property="build.nbproject" value="true">
            <set 
                property="i" 
                value="1"/>
            <set 
                property="current.file" 
                value="${product.logic.dir}/${product.logic.file.name}"/>
            
            <copy 
                file="${nbproject.dist.dir}/${nbproject.dist.file.name}"
                tofile="${current.file}"/>
            
            <!-- sign the current jar file -->
	    <if property="jarsigner.enabled" value="true">
                <signjar jar="${current.file}" 
                         keystore="${jarsigner.keystore}"
                         storepass="${jarsigner.storepass}"
                         alias="${jarsigner.alias}"
                         maxmemory="${jarsigner.xmx}"/>
             </if>
            
            <!-- calculate the metadata for the file -->
            <sizeof file="${current.file}" property="product.logic.${i}.size"/>
            <md5 file="${current.file}" property="product.logic.${i}.md5"/>
            <set property="product.logic.${i}.correct.uri"
                 value="resource:${product.logic.path}/${product.logic.file.name}"/>
            
            <property 
                name="start.index" 
                value="2"/>
        </if>
        <property name="start.index" value="1"/>
        
        <for-each from="${start.index}" 
                  to="${product.logic.length}" 
                  property="i">
            <!-- set the properties relevant to the current file -->
            
            <set 
                property="current.file" 
                value="${product.logic.dir}/${product.logic.file.name}"/>

            <condition property="product.logic.${i}.is.local" value="true" else="false">
                <isset property="product.logic.${i}.path"/>
            </condition>
            
            <if property="product.logic.${i}.is.local" value="false">
                <set 
                    property="current.uri" 
                    source="product.logic.${i}.uri"/>
                <get 
                    src="${current.uri}" 
                    dest="${current.file}" 
                    usetimestamp="true"/>
            </if>

            <if property="product.logic.${i}.is.local" value="true">
                <set 
                    property="current.path" 
                    source="product.logic.${i}.path"/>
                <copy
                    file="${current.path}" 
                    tofile="${current.file}" 
                    preservelastmodified="true"/>
            </if>
            
            <!-- sign the current jar file -->
	    <if property="jarsigner.enabled" value="true">
                <signjar 
                    jar="${current.file}" 
                    keystore="${jarsigner.keystore}"
                    storepass="${jarsigner.storepass}"
                    alias="${jarsigner.alias}"
                    maxmemory="${jarsigner.xmx}"/>
            </if>
            
            <!-- calculate the metadata for the file -->
            <sizeof 
                file="${current.file}" 
                property="product.logic.${i}.size"/>
            <md5 
                file="${current.file}" 
                property="product.logic.${i}.md5"/>
            <set 
                property="product.logic.${i}.correct.uri"
                value="resource:${product.logic.path}/${product.logic.file.name}"/>
        </for-each>
        
        <!-- fetch and repackage installation data zips -->
        <set property="current.product.data.length" 
             value="${product.data.length}"/>

        <for-each from="1" to="${current.product.data.length}" property="i">
            <!-- define the path to the current working file and the temporary
                 directory which should be used for repackaging -->
            <set 
                property="current.zip" 
                value="${product.data.${i}.zip}"/>

            <condition property="product.tar.${i}.is.set" value="true" else="false">
		<isset property="product.data.${i}.tar"/>
            </condition>

            <if property="product.tar.${i}.is.set" value="true">
                <set 
                    property="current.tar" 
                    value="${product.data.${i}.tar}"/>
            </if>
            <if property="product.tar.${i}.is.set" value="false">
                <set 
                    property="current.tar" 
                    value="false"/>
            </if>

            <set 
                property="current.file" 
                value="${product.data.dir}/${product.data.file.name}"/>
            <set 
                property="current.temp.dir" 
                value="${work.dir}/temp"/>
            
            <set 
                property="current.packaged.data.file" 
                value="${product.packaged.data.file}"/>
            <set 
                property="current.packaged.data.properties.file" 
                value="${product.packaged.data.properties.file}"/>
            
            <condition property="do.package.data.${i}" value="true">
                <or>
                    <equals arg1="${package.data}" arg2="true"/>
                    <and>
                        <equals 
                            arg1="${package.data}" 
                            arg2="default"/>
                        <not>
                            <and>
                                <available 
                                    file="${current.packaged.data.file}"/>
                                <available 
                                    file="${current.packaged.data.properties.file}"/>
                            </and>
                        </not>
                    </and>
                </or>
            </condition>
            <condition property="do.package.data.${i}" value="false">
                <or>
                    <equals arg1="${package.data}" arg2="false"/>
                    <and>
                        <equals 
                            arg1="${package.data}" 
                            arg2="default"/>
                        <available 
                            file="${current.packaged.data.file}"/>
                        <available 
                            file="${current.packaged.data.properties.file}"/>
                    </and>
                </or>
            </condition>
            
            <!-- if the data needs to be packaged -->
            <if property="do.package.data.${i}" value="true">

                <condition property="product.data.${i}.is.local" value="true" else="false">
                    <isset property="product.data.${i}.path"/>
                </condition>

                <if property="product.data.${i}.is.local" value="false">               
                    <set 
                        property="current.uri" 
                        source="product.data.${i}.uri"/>
                    <uri-to-path 
                        property="current.downloaded.path" 
                        uri="${current.uri}"/>
                    <set 
                        property="current.download" 
                        value="${downloads.cache.dir}/${current.downloaded.path}"/>
               
                    <dirname 
                        property="current.download.${i}.dir" 
                        file="${current.download}"/>
                    <set 
                        property="current.download.dir"
                        source="current.download.${i}.dir"/>
                    <mkdir 
                        dir="${current.download.dir}"/>
                    
                    <get 
                        src="${current.uri}" 
                        dest="${current.download}"
                        usetimestamp="true"/>
                </if>

                <if property="product.data.${i}.is.local" value="true">
                    <set property="current.download"
                         source="product.data.${i}.path"/>
                </if>
                
                <!-- extract the zip file to a temporary directory, if it's a 
                     zip file, or simply move it there if it is the data we 
                     need -->
                <mkdir 
                    dir="${current.temp.dir}"/>
                <if property="current.zip" value="true">
                    <n-unzip
                        src="${current.download}" 
                        dest="${current.temp.dir}"/>
                </if>

                <if property="current.tar" value="true">
                    <n-untar
                        src="${current.download}" 
                        dest="${current.temp.dir}"/>
                </if>

                <condition property="do.copy.data.${i}" value="true">
		    <and>
			<not>
				<equals arg1="${current.zip}" arg2="true"/>
			</not>
			<not>
				<equals arg1="${current.tar}" arg2="true"/>
			</not>
		    </and>
                </condition>

                <if property="do.copy.data.${i}" value="true">
                    <copy file="${current.download}"
                          todir="${current.temp.dir}"/>
                </if>
                
                <!-- call the pre-package hook, to allow derivative scripts to 
                     perform any required operations on the data if they 
                     like -->
                <antcall target="-pre-package">
                    <param name="i" value="${i}"/>
                    <param name="data.directory" value="${current.temp.dir}"/>
                </antcall>
                
                <!-- measure its size and add it to the required disk space -->
                <sizeof 
                    file="${current.temp.dir}" 
                    property="current.disk.space"/>
                <sum 
                    arg1="${product.disk.space}" 
                    arg2="${current.disk.space}" 
                    property="product.disk.space"/>
                
                <!-- repackage (pack200, jar) and build files list -->
                <package 
                    directory="${current.temp.dir}" 
                    file="${current.file}"/>
                
                <!-- if we need to release the packaged data - copy the file 
                     and the original size to the specified directory -->
                <if property="release.packaged.data" value="true">
                    <mkdir 
                        dir="${packaged.data.dir}"/>
                    
                    <copy file="${current.file}" 
                          tofile="${current.packaged.data.file}"/>
                    <echo 
                        message="product.data.${i}.size=${current.disk.space}" 
                        file="${current.packaged.data.properties.file}"/>
                </if>
                
                <!-- remove the temporary directory -->
                <delete dir="${current.temp.dir}" deleteonexit="true"/>
            </if>
            
            <!-- if the data does not need to be packaged -->
            <if property="do.package.data.${i}" value="false">
                <copy 
                    file="${current.packaged.data.file}" 
                    tofile="${current.file}"/>                              
                
                <!-- fetch the real size data -->
                <property 
                    file="${current.packaged.data.properties.file}"/>
                
                <set 
                    property="current.disk.space" 
                    source="product.data.${i}.size"/>
                <sum 
                    arg1="${product.disk.space}" 
                    arg2="${current.disk.space}" 
                    property="product.disk.space"/>
            </if>
            
            <!-- sign the resulting jar file -->
            <if property="jarsigner.enabled" value="true">
                <signjar 
                    jar="${current.file}" 
                    keystore="${jarsigner.keystore}"
                    storepass="${jarsigner.storepass}"
                    alias="${jarsigner.alias}"
                    maxmemory="${jarsigner.xmx}"/>
	    </if>
            
            <!-- calculate the metadata for the file -->
            <sizeof 
                file="${current.file}" 
                property="product.data.${i}.size"/>
            <md5 
                file="${current.file}" 
                property="product.data.${i}.md5"/>
            <set 
                property="product.data.${i}.correct.uri"
                value="resource:${product.data.path}/${product.data.file.name}"/>
        </for-each>
        
        <!-- fetch the icon -->
        <set 
            property="product.icon.file" 
            value="${package.dir}/${product.icon.path}"/>
        
        <copy 
            file="${product.icon}" 
            tofile="${product.icon.file}"/>
        <sizeof 
            file="${product.icon.file}" 
            property="product.icon.size"/>
        <md5 
            file="${product.icon.file}" 
            property="product.icon.md5"/>
        <set 
            property="product.icon.correct.uri" 
            value="${product.icon.uri}"/>
        
        <!-- load product's localizing bundle and generate descriptor -->
        <load-locales 
            basename="${product.bundle}" 
            list="product.locales.list"/>
        <product-descriptor 
            file="${package.dir}/${package.descriptor.path}"/>
        
        <!-- create the distributive jar file and sign it -->
        <set 
            property="product.dist.file" 
            value="${dist.dir}/${product.dist.file.name}"/>
        
        <jar basedir="${package.dir}"
             destfile="${product.dist.file}" 
             compress="true">
            <manifest>
                <attribute 
                    name="Product-Descriptor" 
                    value="${package.descriptor.path}"/>
            </manifest>
        </jar>
        <if property="jarsigner.enabled" value="true">
            <signjar 
                jar="${product.dist.file}" 
                keystore="${jarsigner.keystore}"
                storepass="${jarsigner.storepass}"
                alias="${jarsigner.alias}"
                maxmemory="${jarsigner.xmx}"/>
	</if>
    </target>
    
    <!--
      Builds the engine.
      
      It runs the 'build-all' target on the engine's build script.
      
      This target is only executed if the the ${build.engine} property was 
      set to 'true'.
      
      This target is not intended to be called directly.
    -->
    <target name="-build-engine" depends="init" if="do.build.engine">
        <exec executable="${ant.executable}" 
              dir="${engine.dir}" 
              failonerror="true"
              failifexecutionfails="true">
            <arg value="checkout,build"/>
            
            <arg value="-Dwork.dir=${engine.work.dir}"/>
            <arg value="-Ddist.dir=${engine.dist.dir}"/>
            
            <arg value="-Dbuild.native=false"/>
            
            <arg value="-Ddont.build.custom.tasks=true"/>
            <arg value="-Dcustom.tasks.cls=${custom.tasks.cls}"/>
            
            <arg value="-Dengine.dist.file.name=${engine.dist.file.name}"/>
            
            <arg value="-Denvironment.properties=${environment.properties}"/>
        </exec>
    </target>
    
    <!--
      An empty hook. 
      
      It allows derivative scripts to perform arbitrary manipulations on the 
      data prior to it getting packaged. E.g. the netbeans ide build script 
      might want to remove the 'netbeans' directory and move its contents up one
      level.
      
      A special property - 'data.directory' will be set pointing to the 
      unzipped data.
      
      This target is not intended to be called directly.
    -->
    <target name="-pre-package"/>
    
    <!-- ============================================================================
      Release
    ============================================================================= -->
    
    <!--
      Releases the product 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 product 
      package.
      
      It is assumed that the product 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 ${product.dist.file}"/>
            
            <release-package-server 
                url="${release.url}"
                registry="${release.registry}"
                uid="${release.parent.uid}"
                version="${release.parent.version}"
                platforms="${product.platforms}"
                archive="${product.dist.file}"/>
        </if>
        <if property="release.to.server" value="false">
            <echo message="Registry root is ${release.registry.dir}"/>
            <echo message="Archive is ${product.dist.file}"/>
            
            <release-package-filesystem
                root="${release.registry.dir}"
                uid="${release.parent.uid}"
                version="${release.parent.version}"
                platforms="${product.platforms}"
                archive="${product.dist.file}"/>
        </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 product package and is not 
            intended to be called directly. In order to create a derivative 
            implementation script, see 
            ${basedir}/.templates/product-template.xml
        </echo>
    </target>
</project>
