<?xml version="1.0"?>

<project name="vaadin-push" basedir="." default="publish-local"
    xmlns:ivy="antlib:org.apache.ivy.ant">
    <description>
        Meta package which defines dependencies needed for push
    </description>
    <include file="../build.xml" as="vaadin" />
    <include file="../common.xml" as="common" />

    <property name="module.name" value="vaadin-push" />
    <property name="module.symbolic" value="com.vaadin.push" />
    <property name="result.dir" location="result" />
    <property name="temp.dir" location="${result.dir}/temp" />
    <property name="jquery.unpack" location="${temp.dir}/jquery" />
    <property name="vaadinPush.js" location="${result.dir}/js/VAADIN/vaadinPush.js" />
    <property name="vaadinPush.debug.js"
        location="${result.dir}/js/VAADIN/vaadinPush.debug.js" />

    <!-- Keep the version number in sync with ivy.xml, server/src/com/vaadin/server/Constants.java -->
    <property name="atmosphere.runtime.version" value="2.2.7.vaadin1" />
    <property name="jquery.js" location="lib/jquery/jquery-1.11.0.js" />

    <path id="classpath.compile.custom" />

    <union id="jar.includes">
        <fileset dir="${result.dir}/js">
            <include name="VAADIN/vaadinPush.js" />
            <include name="VAADIN/vaadinPush.js.gz" />
            <include name="VAADIN/vaadinPush.debug.js" />
            <include name="VAADIN/vaadinPush.debug.js.gz" />
        </fileset>
    </union>

    <target name="vaadinPush.js">
        <mkdir dir="${result.dir}/js/VAADIN" />
        <ivy:resolve log="download-only" file="ivy.xml"
            conf="push.js" />
        <ivy:cachepath pathid="atmosphere.jquery.deps"
            conf="push.js" />

        <delete dir="${temp.dir}" />
        <copy flatten="true" tofile="${temp.dir}/jquery.war">
            <path refid="atmosphere.jquery.deps" />
        </copy>

        <!-- Unzip to temp dir -->
        <unzip src="${temp.dir}/jquery.war" dest="${jquery.unpack}">
            <patternset>
                <include name="**/jquery.atmosphere.js" />
            </patternset>
            <mapper type="flatten" />
        </unzip>
        <loadfile srcfile="${jquery.js}" property="jquery.js.contents" />
        <loadfile srcfile="${jquery.unpack}/jquery.atmosphere.js"
            property="jquery.atmosphere.js.contents" />

        <loadfile
            srcfile="${vaadin.basedir}/WebContent/VAADIN/vaadinPush.js.tpl"
            property="vaadinPush.js.contents">
            <filterchain>
                <replacetokens begintoken="@" endtoken="@">
                    <token key="jquery.js" value="${jquery.js.contents}" />
                    <token key="jquery.atmosphere.js" value="${jquery.atmosphere.js.contents}" />
                </replacetokens>
            </filterchain>
        </loadfile>

        <!-- Non-obfuscated version for debugging -->
        <echo file="${vaadinPush.debug.js}">${vaadinPush.js.contents}</echo>

        <!-- Minify -->
        <ivy:retrieve organisation="com.yahoo.platform.yui"
            module="yuicompressor" revision="2.4.7" inline="true" type="jar"
            pattern="${result.dir}/compressor.jar" />
        <java jar="${result.dir}/compressor.jar" fork="true">
            <arg value="-v" />
            <arg value="-o" />
            <arg file="${vaadinPush.js}" />
            <arg file="${vaadinPush.debug.js}" />
        </java>
        
        <!-- Gzipped versions -->
        <gzip src="${vaadinPush.js}" destfile="${vaadinPush.js}.gz" />
        <gzip src="${vaadinPush.debug.js}" destfile="${vaadinPush.debug.js}.gz" />
    </target>

    <target name="jar" depends="vaadinPush.js">
        <antcall target="common.jar">
            <param name="require-bundle"
                value="com.vaadin.external.atmosphere.runtime;bundle-version=&quot;${atmosphere.runtime.version}&quot;;visibility:=reexport" />
            <reference torefid="extra.jar.includes" refid="jar.includes" />
        </antcall>
    </target>

    <target name="publish-local" depends="jar">
        <antcall target="common.publish-local" />
    </target>

    <target name="clean">
        <antcall target="common.clean" />
    </target>
    <target name="checkstyle">
    </target>

    <target name="test" depends="checkstyle">
    </target>
</project>