<?xml version="1.0" encoding="UTF-8"?>
<!--
The contents of this file are subject to the terms of the Common Development
and Distribution License (the License). You may not use this file except in
compliance with the License.

You can obtain a copy of the License at http://www.netbeans.org/cddl.html
or http://www.netbeans.org/cddl.txt.

When distributing Covered Code, include this CDDL Header Notice in each file
and include the License file at http://www.netbeans.org/cddl.txt.
If applicable, add the following below the CDDL Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"

The Original Software is NetBeans. The Initial Developer of the Original
Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
Microsystems, Inc. All Rights Reserved.
-->
<project name="run" basedir=".">

    <target name="-init">
        <property name="run.jdkhome" location="${nbjdk.home}"/>
        <condition property="test.user.dir" value="${suite.dir}/build/testuserdir">
            <isset property="suite.dir"/>
        </condition>
        <property name="test.user.dir" location="build/testuserdir"/>
        <property name="install" location="${netbeans.dest.dir}"/>
        <property name="run.args.common" value='--userdir "${test.user.dir}" -J-Dnetbeans.logger.console=true -J-Dorg.netbeans.CLIHandler.fast.random=true -J-ea'/>
        <!-- In case run from a suite module, to get app.name and/or branding.token: -->
        <property file="${suite.dir}/nbproject/private/private.properties"/>
        <property file="${suite.dir}/nbproject/project.properties"/>
        <property name="run.app.exe" value="bin\nb.exe"/>
        <property name="run.app.sh" value="bin/netbeans"/>
        <property name="run.exe.candidate" location="${install}\${run.app.exe}"/>
        <property name="run.sh.candidate" location="${install}/${run.app.sh}"/>
         <!-- #63976: if disabling things, better to treat as a platform-based app -->
        <condition property="app.name" value="test">
            <or>
                <and>
                    <isset property="disabled.modules"/>
                    <length string="${disabled.modules}" when="greater" length="0"/>
                </and>
                <and>
                    <isset property="enabled.clusters"/>
                    <length string="${enabled.clusters}" when="greater" length="0"/>
                </and>
                <and>
                    <isset property="disabled.clusters"/>
                    <length string="${disabled.clusters}" when="greater" length="0"/>
                </and>
                <!-- #66793: also treat as a platform-based app if using platform and have no branding -->
                <not>
                    <available file="${run.exe.candidate}"/>
                </not>
                <not>
                    <available file="${run.sh.candidate}"/>
                </not>
            </or>
        </condition>
    </target>

    <target name="-prepare-as-app" depends="-init" unless="app.name">
        <property name="run.exe" location="${run.exe.candidate}"/>
        <property name="run.sh" location="${run.sh.candidate}"/>
        <mkdir dir="${test.user.dir}/etc"/>
        <echo file="${test.user.dir}/etc/netbeans.conf">netbeans_extraclusters="${cluster}"</echo>
        <property name="run.args.prepared" value=""/>
    </target>

    <target name="-prepare-as-platform" depends="-init" if="app.name">
        <property name="disabled.modules" value=""/>
        <property name="enabled.clusters" value=""/>
        <property name="disabled.clusters" value=""/>
        <pathconvert property="run.platform.update.tracking">
            <path>
                <fileset dir="${netbeans.dest.dir}">
                    <include name="**/update_tracking/*.xml"/>
                </fileset>
            </path>
        </pathconvert>
        <pathconvert property="run.platform.clusters.duplicates" pathsep=",">
            <path>
                <fileset dir="${netbeans.dest.dir}">
                    <selector>
                        <custom classpath="${harness.dir}/tasks.jar" classname="org.netbeans.nbbuild.ModuleSelector">
                            <param name="excludeModules" value="${disabled.modules}"/>
                            <param name="includeClusters" value="${enabled.clusters}"/>
                            <param name="excludeClusters" value="${disabled.clusters}"/>
                            <param name="updateTrackingFiles" value="${run.platform.update.tracking}"/>
                        </custom>
                    </selector>
                </fileset>
            </path>
            <mapper type="regexp" from="[/\\]([^/\\]+)[/\\]config[/\\]Modules[/\\]" to="\1"/> <!-- #71849 -->
        </pathconvert>
        <fail message="Must have at least one cluster from the platform">
            <!-- Otherwise ${run.platform.clusters} gets filled with nonsense. -->
            <condition>
                <equals arg1="${run.platform.clusters.duplicates}" arg2=""/>
            </condition>
        </fail>
        <pathconvert property="run.platform.clusters">
            <path>
                <pathelement location="${cluster}"/>
                <dirset dir="${netbeans.dest.dir}" includes="${run.platform.clusters.duplicates}" excludes="platform*"/>
            </path>
        </pathconvert>
        <pathconvert property="platform.cluster">
            <path>
                <dirset dir="${install}" includes="platform*"/>
            </path>
        </pathconvert>
        <property name="run.exe" location="${platform.cluster}\lib\nbexec.exe"/>
        <property name="run.sh" location="${platform.cluster}/lib/nbexec"/>
        <property name="branding.token" value="-"/> <!-- fallback value if not set -->
        <property name="run.args.prepared" value='--branding ${branding.token} --clusters "${run.platform.clusters}"'/>
    </target>

    <target name="run" depends="-prepare-as-app,-prepare-as-platform">
        <property name="run.args" value=""/>
        <property name="run.args.extra" value=""/>
        <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows 2003 Windows XP Windows NT (unknown) Windows Vista" executable="${run.exe}" failonerror="yes">
            <arg value="--jdkhome"/>
            <arg file="${run.jdkhome}"/>
            <arg line="${run.args.common}"/>
            <arg line="${run.args.prepared}"/>
            <arg line="${run.args}"/>
            <arg line="${run.args.extra}"/>
        </exec>
        <exec os="Linux SunOS Solaris Mac OS X" dir="." executable="sh" failonerror="yes">
            <arg value="${run.sh}"/>
            <arg value="--jdkhome"/>
            <arg file="${run.jdkhome}"/>
            <arg line="${run.args.common}"/>
            <arg line="${run.args.prepared}"/>
            <arg line="${run.args}"/>
            <arg line="${run.args.extra}"/>
        </exec>
    </target>

    <target name="debug">
        <fail unless="netbeans.home">This target only works when run from inside the NetBeans IDE.</fail>
        <nbjpdastart name="NetBeans" addressproperty="debug.port" transport="dt_socket">
            <classpath>
                <fileset dir="${netbeans.dest.dir}">
                    <include name="**/*.jar"/>
                </fileset>
                <fileset dir="${cluster}">
                    <include name="**/*.jar"/>
                </fileset>
            </classpath>
        </nbjpdastart>
        <property name="debug.pause" value="n"/>
        <property name="debug.args" value="-J-Xdebug -J-Xnoagent -J-Xrunjdwp:transport=dt_socket,suspend=${debug.pause},server=n,address=${debug.port}"/>
        <antcall target="run">
            <param name="run.args" value="${debug.args}"/>
        </antcall>
    </target>
    
    <target name="profile" description="Profile a project in the IDE.">
        <fail unless="netbeans.home">This target only works when run from inside the NetBeans IDE.</fail>
        <nbprofiledirect 
            jvmargsprefix="-J" 
            jvmargsproperty="profiler.jvmargs" 
            mainclass="org.netbeans.Main"
        >
            <classpath>
                <fileset dir="${netbeans.dest.dir}">
                    <include name="**/*.jar"/>
                </fileset>
                <fileset dir="${cluster}">
                    <include name="**/*.jar"/>
                </fileset>
            </classpath>
            <rootspath>
                <fileset dir="${cluster}">
                    <include name="**/*.jar"/>
                </fileset>
            </rootspath>
        </nbprofiledirect>
        
        <antcall target="run">
            <param name="run.args" value='"${profiler.jvmargs}"'/>
        </antcall>
    </target>
    
</project>
