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

<project name="webstart-build" default="webstart" basedir="..">

    <property file="nbproject/webstart.properties"/>

    <!-- Build the webstart tree -->
    <target name="webstart" depends="init,jar">

        <!-- 1.  Set the JAR signing script names -->
        <condition property="signpack" value="nbproject/signpack.bat"
                   else="nbproject/signpack">
            <os family="windows"/>
        </condition>
        <condition property="genvar" value="nbproject/genvar.bat"
                   else="nbproject/genvar">
            <os family="windows"/>
        </condition>

        <!-- 2. Create webstart directories -->
        <mkdir dir="${webstart.dist.dir}/unpacked"/>
        <mkdir dir="${webstart.dist.dir}/packed"/>

        <!-- 3. Copy the source files, replacing any tokens -->
        <copy todir="${webstart.dist.dir}">
            <fileset dir="${webstart.src.dir}"/>
            <filterset>
                <filter token="WEBSTART_JNLP_URL"
                        value="${webstart.jnlp.url}"/>
                <filter token="WEBSTART_HOME_URL"
                        value="${webstart.home.url}"/>
            </filterset>
        </copy>

        <!-- 4. Copy in all the java JAR files -->
        <copy todir="${webstart.dist.dir}/unpacked" flatten="true">
            <fileset dir="${dist.dir}" includes="**/*.jar"/>
        </copy>

        <!-- 5. Create packed and signed versions of the JAR files -->
        <apply executable="${signpack}" dest="${webstart.dist.dir}/packed"
               type="file" resolveexecutable="true" failonerror="true"
               ignoremissing="false">
            <fileset dir="${webstart.dist.dir}/unpacked" includes="*.jar"/>
            <mapper type="glob" from="*.jar" to="*.jar.pack.gz"/>
            <srcfile/>
            <targetfile/>
            <arg value="${keystore.location}"/>
            <arg value="${keystore.password}"/>
            <arg value="${keystore.alias}"/>
        </apply>

        <!-- 6. Sign all the unpacked JAR files -->
        <signjar
            alias="${keystore.alias}"
            keystore="${keystore.location}"
            storepass="${keystore.password}" >
            <fileset dir="${webstart.dist.dir}" includes="**/*.jar" />
        </signjar>

        <!-- 7. Create the Apache type mapping files -->
        <apply executable="${genvar}" type="file" relative="true"
               dir="${webstart.dist.dir}" dest="${webstart.dist.dir}"
               resolveexecutable="true" failonerror="true"
               ignoremissing="false">
            <fileset dir="${webstart.dist.dir}/unpacked" includes="*.jar"/>
            <flattenmapper/>
        </apply>

        <!-- 8. Convert to Unix line endings -->
        <fixcrlf srcdir="${webstart.dist.dir}" eol="lf" eof="remove"
                 includes=".htaccess,*.jnlp,*.var"/>

    </target>

    <!-- Delete the webstart dist tree -->
    <target name="-post-clean" depends="init">
        <delete dir="${webstart.dist.dir}"/>
    </target>

</project>
