<?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 nbi engine. 
  
  It is not intended to be used directly, but to be imported into derivative
  implementaion scripts, which will provide properties' values that are 
  specific the concrete engine implementation. The derivative scripts may also
  provide some additional functionality that is required by their respective
  engine 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 
  corresponsing documentation.
-->
<project name="engine" 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 engine's build script -->
    <property file="${common.dir}/engine.properties"/>
    
    <!-- import the common target library -->
    <import file="common.xml" optional="false"/>
    
    <!-- ============================================================================
      Initialization
    ============================================================================= -->
    
    <!--
      Initializes the engine's build script.
      
      This target overrides the '-init' target in common.xml, providing the 
      custom functionality that is required for the engine's build script
      initialization.
      
      In addition to the standard initialization routine, this target defines
      several custom ant tasks, which are specific to the engine's build 
      script.
      
      This target is not intended to be called directly.
    -->
    <target name="-init" depends=".init">
        <!-- define custom ant tasks -->
        <if property="release.to.server" value="true">
            <taskdef
                name="release-engine-server"
                classname="org.netbeans.installer.infra.build.ant.ReleaseEngine"
                classpath="${custom.tasks.cls}"/>
        </if>
        
        <if property="release.to.server" value="false">
            <taskdef 
                name="release-engine-filesystem"
                classname="org.netbeans.installer.infra.build.ant.registries.UpdateEngine"
                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 engine.
      
      In addition to the standard behavior the engine distributive file is 
      removed from the distributives directory.
      
      This target is not intended to be called directly.
    -->
    <target name="-clean" depends=".clean">
        <!-- remove the engine's distributive file from the distributives 
             directory -->
        <delete file="${dist.dir}/${engine.dist.file.name}"/>
    </target>
    
    <!--
      Cleans the native components of the engine.
      
      This target overrides the '.clean-native' target defined in common.xml,
      as the engine's native components slightly differ from the defaults. 
      Besides cleaning the jni libraries, the engine also needs to clean the 
      native launcher stubs.
      
      This target is antcall'ed in a loop over the list of platforms from the 
      '.clean' target in common.xml.
      
      This target is only executed if the the ${build.native} property was set 
      to 'true'.
      
      This target is not intended to be called directly.
    -->
    <target name=".clean-native" if="do.build.native">
        <antcall target=".clean-jni"/>
        <antcall target=".clean-launcher"/>
    </target>
    
    <!--
      Cleans the native launcher stub for the given platform.
      
      This target is antcall'ed as part of the call to the '.clean-native' 
      target, and expects the ${platform} property to contain the code name of
      the platform for which the launcher should be built.
      
      It simply removes the current launcher stub.
      
      This target is only executed if the the ${build.native} property was set 
      to 'true'.
      
      This target is not intended to be called directly.
    -->
    <target name=".clean-launcher" if="do.build.native">
        <!-- does nothing at the moment -->
    </target>
    
    <!-- ============================================================================
      Check-out
    ============================================================================= -->
    
    <!--
      The functionality of the checkout procedure for the engine 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 engine and (optionally) its native components.
      
      This target overrides the '-build' target in common.xml, providing the 
      custom functionality that is required for correctly building the engine.
      
      The build process consists of three steps: first, the native components 
      of the engine (jni libraries and native launcher stubs) are built (if it
      is required), then the engine's netbeans project is compiled and 
      packaged, last the engine jar file is digitally signed and copied to the
      distributives directory.
      
      This target is not intended to be called directly.
    -->
    <target name="-build" depends=".build">
        <!-- copy the built binary to the distributives directory -->
        <copy file="${nbproject.dist.file}" todir="${dist.dir}"/>
        
        <!-- digitally sign the built binary -->
	<if property="jarsigner.enabled" value="true">
                <signjar jar="${dist.dir}/${engine.dist.file.name}" 
                         keystore="${jarsigner.keystore}"
                         storepass="${jarsigner.storepass}"
                         alias="${jarsigner.alias}"
                         maxmemory="${jarsigner.xmx}"/>
	</if>
    </target>
    
    <!--
      Builds the native components of the engine.
      
      This target overrides the '.build-native' target defined in common.xml,
      as the engine's native components slightly differ from the defaults. 
      Besides building the jni libraries, the engine also needs to build the 
      native launcher stubs, which will later be used to create redistributable 
      bundles.
      
      This target is antcall'ed in a loop over the list of platforms from the 
      '.build' target in common.xml.
      
      This target is only executed if the the ${build.native} property was set 
      to 'true'.
      
      This target is not intended to be called directly.
    -->
    <target name=".build-native" if="do.build.native">
        <antcall target=".build-jni"/>
        <antcall target=".build-launcher"/>
        <antcall target=".build-cleaner"/>
    </target>
    
    <!--
      Builds the native launcher stub for the given platform.
      
      This target is antcall'ed as part of the call to the '.build-native' 
      target, and expects the ${platform} property to contain the code name of
      the platform for which the launcher should be built.
      
      It connects to the registered satellite machine for the current platform
      using the 'ssh' utility, checks out the launcher's sources and builds it.
      The built binary is then copied back to the host machine using 'scp'.
      
      This target is only executed if the the ${build.native} property was set 
      to 'true'.
      
      This target is not intended to be called directly.
    -->
    <target name=".build-launcher" if="do.build.native">
        <!-- does nothing at the moment -->
    </target>
    
    <!--
      Builds the native cleaner for locked files for the given platform.
      
      This target is antcall'ed as part of the call to the '.build-native' 
      target, and expects the ${platform} property to contain the code name of
      the platform for which the launcher should be built.
      
      It connects to the registered satellite machine for the current platform
      using the 'ssh' utility, checks out the launcher's sources and builds it.
      The built binary is then copied back to the host machine using 'scp'.
      
      This target is only executed if the the ${build.native} property was set 
      to 'true'.
      
      This target is not intended to be called directly.
    -->
    <target name=".build-cleaner" if="do.build.native">
        <!-- does nothing at the moment -->
    </target>
    
    
    <!-- ============================================================================
      Release
    ============================================================================= -->
    
    <!--
      Releases the engine to the registries server.
      
      This target overrides the '-release' target in common.xml, providing the 
      custom functionality that is required for correctly releasing the engine.
      
      It is assumed that the engine 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}/${engine.dist.file.name}"/>
            
            <release-engine-server 
                url="${release.url}"
                archive="${dist.dir}/${engine.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}/${engine.dist.file.name}"/>
            
            <release-engine-filesystem
                root="${release.registry.dir}"
                archive="${dist.dir}/${engine.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 engine and is not intended to
            be called directly. In order to create a derivative implementation 
            script, see 
            ${basedir}/.templates/engine-template.xml
        </echo>
    </target>
</project>
