<?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="suite-jnlp" basedir=".">

    <!-- if jnlp.codebase is provided make both the app and the platform relative to it -->
    <condition property="jnlp.codebase.app" value="${jnlp.codebase}app/" >
        <isset property="jnlp.codebase" />
    </condition>
    <!-- if not specified, assign all codebases to value suitable for jnlp-servlet.jar -->
    <property name="jnlp.codebase" value="$$$$codebase" />
    <property name="jnlp.codebase.app" value="$$$$codebase" />

    <target name="-check-for-master.jnlp">
        <available file="master.jnlp" property="master.jnlp.exists"/>
    </target>
    <target name="jnlp-init-generate-master" depends="-check-for-master.jnlp" unless="master.jnlp.exists">
        <!-- XXX ought to try to avoid generating a fixed codebase at this time (WAR build replaces it though) -->
        <echo file="master.jnlp"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="${jnlp.codebase}">
  <information>
      <title>$${app.title}</title>
      <vendor>$${app.title} vendor</vendor>
      <description>$${app.name} application</description>
      <icon href="$${app.icon}"/>
  </information>
  <security><all-permissions/></security>
  <resources>
<!-- The following line will be replaced with an automatically generated list of resources: -->    
<!--$${jnlp.resources}-->
  </resources>
  <resources os="Mac OS X"> 
      <property name="netbeans.user" value="$${user.home}/Library/Application Support/$${app.name}"/>
  </resources>
  <application-desc>
    <argument>--branding</argument>
    <argument>$${branding.token}</argument>
  </application-desc>
</jnlp>  
]]></echo>
    </target>
    
    <target name="jnlp-init" depends="jnlp-init-generate-master">
        <fail unless="app.name">Must have set at least an application name ('app.name')</fail>
        
        <property name="jnlp.dest.dir" location="build/jnlp"/>
        <property name="jnlp.master.dir" location="build/tmp/master-jnlp"/>
        <mkdir dir="${jnlp.master.dir}"/>
        
        <property name="disabled.modules" value=""/>
        <property name="enabled.clusters" value=""/>
        <property name="disabled.clusters" value=""/>
        
        <fileset id="jnlp.included.modules" dir="${netbeans.dest.dir}">
            <and>
                <or>
                    <filename name="*/modules/**/*.jar"/>
                    <filename name="*/lib/**/*.jar"/>
                    <filename name="*/core/**/*.jar"/>
                </or>
                <not>
                    <filename name="**/update/**/*.jar"/>
                </not>
                <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}"/>
                </custom>
            </and>
        </fileset>
    </target>

    <property name="jnlp.signjar.keystore" location="build/default.keystore"/>
    <property name="jnlp.signjar.alias" value="jnlp"/>
    <property name="jnlp.signjar.password" value="netbeans"/>
    <available property="jnlp.signjar.keystore.exists" file="${jnlp.signjar.keystore}"/>
    <target name="jnlp-generate-keystore" depends="jnlp-init" unless="jnlp.signjar.keystore.exists">
        <property name="jnlp.signjar.vendor" value="CN=A NetBeans Friend, OU=NetBeans, O=netbeans.org, C=US"/>
        
        <mkdir dir="${jnlp.signjar.keystore}/../"/>
        <echo message="Going to create default keystore in ${jnlp.signjar.keystore}"/>
        <genkey 
            alias="${jnlp.signjar.alias}"
            keystore="${jnlp.signjar.keystore}"
            storepass="${jnlp.signjar.password}"
            dname="${jnlp.signjar.vendor}"
        />
    </target>

    <target name="jnlp-generate-platform" depends="jnlp-generate-platform-repository,jnlp-generate-platform-master"/>
    
    <target name="jnlp-generate-platform-repository" depends="jnlp-generate-keystore,jnlp-init" unless="jnlp.platform.codebase">
        <condition property="jnlp.platform.codebase.own" value="${jnlp.codebase}netbeans/" >
            <not>
                <equals arg1="${jnlp.codebase}" arg2="$$$$codebase" />
            </not>
        </condition>
        <property name="jnlp.platform.codebase.own" value="$$$$codebase"/>
        <property name="jnlp.platform.codebase" value="netbeans/"/>
        <mkdir dir="${jnlp.dest.dir}/netbeans"/>
        
        <makejnlp 
            alias="${jnlp.signjar.alias}" 
            keystore="${jnlp.signjar.keystore}" 
            storepass="${jnlp.signjar.password}"
            dir="${jnlp.dest.dir}/netbeans/"
            codebase="${jnlp.platform.codebase.own}"
            verify="true"
            verifyexcludes="lib/nbexec.cmd,lib/nbexec,lib/nbexec.exe,core/ide.ks,LICENSE.txt,DISTRIBUTION.txt,THIRDPARTYLICENSEREADME.txt,docs/swing-layout-1.0-doc.zip,docs/html40.zip,core/org-netbeans-modules-utilities-cli.jar,docs/org/netbeans/modules/usersguide/ide.css"
        >
            <modules refid="jnlp.included.modules"/>
        </makejnlp>
    </target>
    
    <target name="jnlp-generate-platform-master" depends="jnlp-generate-platform-repository">
        <makemasterjnlp 
            dir="${jnlp.master.dir}"
            codebase="${jnlp.platform.codebase}"
        >
            <modules refid="jnlp.included.modules"/>
        </makemasterjnlp>
    </target>
    
    <target name="build" 
            depends="build-jnlp-nowar"
            description="Build JNLP files and signed JARs for all modules in the suite."
    >
        <property name="jnlp.servlet.jar" location="${nbjdk.home}/sample/jnlp/servlet/jnlp-servlet.jar"/>
        <fail message="Point -Djnlp.servlet.jar to your jnlp-servlet.jar, usually in $$JDKHOME/sample/jnlp/servlet/jnlp-servlet.jar">
            <condition>
                <not>
                    <available file="${jnlp.servlet.jar}"/>
                </not>
            </condition>
        </fail>
        
        <echo file="build/tmp/web.xml"><![CDATA[
<web-app>
 <servlet>
    <servlet-name>JnlpDownloadServlet</servlet-name>
    <servlet-class>jnlp.sample.servlet.JnlpDownloadServlet</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>JnlpDownloadServlet</servlet-name>
    <url-pattern>*.jnlp</url-pattern>
 </servlet-mapping>
</web-app>         
]]></echo>
        <mkdir dir="dist"/>
        <war basedir="${jnlp.dest.dir}" destfile="dist/${app.name}.war" webxml="build/tmp/web.xml">
            <zipfileset dir="${jnlp.servlet.jar}/.." file="${jnlp.servlet.jar}" prefix="WEB-INF/lib"/>
        </war>
    </target>

    <target name="build-jnlp-local" depends="build-jnlp-nowar">
        <property name="build.jnlp.local.dir" location="dist/jnlp/local"/>
        <mkdir dir="${build.jnlp.local.dir}"/>
        <mkdir dir="${build.jnlp.local.dir}/netbeans/"/>
        <mkdir dir="${build.jnlp.local.dir}/app/"/>
        
        <copy todir="${build.jnlp.local.dir}">
            <fileset dir="build/jnlp">
                <include name="*.jnlp"/>
            </fileset>
            <filterchain>
                <replacestring from="$$$$codebase" to="file:${build.jnlp.local.dir}"/><!-- XXX potential SPACE_IN_PATH bug -->
            </filterchain>
        </copy>
        <copy todir="${build.jnlp.local.dir}/netbeans/">
            <fileset dir="build/jnlp/netbeans">
                <include name="*.jnlp"/>
            </fileset>
            <filterchain>
                <replacestring from="$$$$codebase" to="file:${build.jnlp.local.dir}/netbeans/"/><!-- XXX potential SPACE_IN_PATH bug -->
            </filterchain>
        </copy>
        <copy todir="${build.jnlp.local.dir}/app/">
            <fileset dir="build/jnlp/app">
                <include name="*.jnlp"/>
            </fileset>
            <filterchain>
                <replacestring from="$$$$codebase" to="file:${build.jnlp.local.dir}/app/"/><!-- XXX potential SPACE_IN_PATH bug -->
            </filterchain>
        </copy>
        <copy todir="${build.jnlp.local.dir}">
            <fileset dir="build/jnlp">
                <exclude name="**/*.jnlp"/>
            </fileset>
        </copy>
    </target>
    
    <target name="build-jnlp-nowar" depends="jnlp-init,jnlp-generate-keystore,jnlp-generate-platform">
        <mkdir dir="${jnlp.dest.dir}/app"/>
        <subant target="jnlp" buildpath="${modules.sorted}" inheritrefs="false" inheritall="false">
            <property name="jnlp.dest.dir" value="${jnlp.dest.dir}/app"/>
            <property name="jnlp.master.dir" value="${jnlp.master.dir}"/>
            <property name="jnlp.master.codebase" value="app/"/>
            <property name="jnlp.codebase" value="${jnlp.codebase.app}"/>
            <property name="jnlp.signjar.alias" value="${jnlp.signjar.alias}"/>
            <property name="jnlp.signjar.keystore" location="${jnlp.signjar.keystore}"/>
            <property name="jnlp.signjar.password" value="${jnlp.signjar.password}"/>
        </subant>

        <mkdir dir="${jnlp.dest.dir}/branding"/>
        <copy todir="${jnlp.dest.dir}/branding" flatten="true">
            <fileset dir="${cluster}">
                <include name="**/locale/*.jar"/>
            </fileset>
        </copy>
        
        <signjar
            alias="${jnlp.signjar.alias}" 
            keystore="${jnlp.signjar.keystore}" 
            storepass="${jnlp.signjar.password}" 
            signedjar="${jnlp.dest.dir}/app"
        >
            <fileset dir="${jnlp.dest.dir}/branding">
                <include name="*_${branding.token}.jar"/>
            </fileset>
        </signjar>
        
        
        <signjar 
            alias="${jnlp.signjar.alias}" 
            keystore="${jnlp.signjar.keystore}" 
            storepass="${jnlp.signjar.password}" 
            jar="${harness.dir}/jnlp/jnlp-launcher.jar"
            signedjar="${jnlp.dest.dir}/startup.jar" 
        />
        
        <pathconvert pathsep="${line.separator}" property="jnlp.branding.jars">
            <path>
                <fileset dir="${jnlp.dest.dir}/branding">
                    <include name="*_${branding.token}.jar"/>
                </fileset>
            </path>
            <mapper type="regexp" from="^.*[/\\]([^/\\]+\.jar)" to='    &lt;jar href="branding/\1"/&gt;'/>
        </pathconvert>
        
        <echo file="${jnlp.master.dir}/resources.xml"><![CDATA[
    <j2se version="1.4+"/>
    <jar href="startup.jar"/>
    <property name="netbeans.user" value="$${user.home}/.nbapp-${app.name}"/>
    ${jnlp.branding.jars}
]]></echo>
        <concat append="true" destfile="${jnlp.master.dir}/resources.xml">
            <fileset dir="${jnlp.master.dir}">
                <include name="*.ref"/>
            </fileset>
        </concat>
        
        <loadfile property="jnlp.resources" srcfile="${jnlp.master.dir}/resources.xml"/>
        
        <copy file="${app.icon}" tofile="${jnlp.dest.dir}/master.png" failonerror="false"/>
        <copy file="master.jnlp" tofile="${jnlp.dest.dir}/master.jnlp">
            <filterchain>
                <replacestring from="&lt;!--$${jnlp.resources}--&gt;" to="${jnlp.resources}"/>
                <replacestring from="$${jnlp.resources}" to="${jnlp.resources}"/>
                <replacestring from="$${app.name}" to="${app.name}"/>
                <replacestring from="$${app.title}" to="${app.title}"/>
                <replacestring from="$${app.icon}" to="master.png"/>
                <replacestring from="$${branding.token}" to="${branding.token}"/>
            </filterchain>
        </copy>
        <echo>Your JNLP file is generated at ${jnlp.dest.dir}/master.jnlp</echo>
    </target>
    
    <target name="run" depends="build-jnlp-local" description="Executes this suite as JNLP application">
        <condition property="run.javaws" value="${nbjdk.home}/jre/javaws/javaws">
            <available file="${nbjdk.home}/jre/javaws/javaws"/>
        </condition>
        <condition property="run.javaws" value="${nbjdk.home}/javaws/javaws">
            <available file="${nbjdk.home}/javaws/javaws"/>
        </condition>
        <property name="run.javaws" value="javaws"/>
        <property name="run.args" value=""/>
        <exec executable="${run.javaws}">
            <!-- XXX 1.5+: <arg value="-wait"/> -->
            <arg file="${build.jnlp.local.dir}/master.jnlp"/>
            <env key="JAVAWS_VM_ARGS" value="${run.args}"/>
        </exec>
    </target>
    
    <target name="debug" depends="build-jnlp-local" description="Executes this suite as JNLP application under debugger">
        <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="-Xdebug -Xnoagent -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="-check-for-jnlp-userdir">
        <property name="jnlp.userdir" location="${user.home}/.nbapp-${app.name}"/>
        <condition property="have.jnlp.userdir">
            <and>
                <isset property="app.name"/>
                <available file="${jnlp.userdir}" type="dir"/>
            </and>
        </condition>
    </target>
    <target name="-clean-jnlp-userdir" depends="-check-for-jnlp-userdir" if="have.jnlp.userdir">
        <property name="jnlp.userdir.bak" location="${jnlp.userdir}.bak"/>
        <delete dir="${jnlp.userdir.bak}" failonerror="false"/>
        <move file="${jnlp.userdir}" tofile="${jnlp.userdir.bak}" failonerror="false"/>
    </target>
    <target name="clean" depends="-clean-jnlp-userdir" description="Make sure default JNLP user directory is removed so run-jnlp will not get old test settings."/> <!-- #64249 -->
    
</project>
