<project name="build.dist">
    <import file="../../build.xml"/>

    <!-- Builds all jars and packages jar and 3rd party libraries into one jar -->
    <target name="dist.binary" depends="check.app.version, jar" description="Play Binary Distro">
        <copy todir="${dest.dir}">
            <fileset dir=".">
                <include name="LICENSE*"/>
                <include name="README"/>
            </fileset>
            <fileset dir="${libraries.dir}">
                <include name="iText*.jar"/>
                <include name="xml*.jar"/>
            </fileset>
        </copy>
        <zip basedir="${dest.dir}" zipfile="build/${app.name}-${app.version}.zip">
            <include name="core-renderer.jar"/>
            <include name="core-renderer-minimal.jar"/>
            <include name="iText*.jar"/>
            <include name="xml*.jar"/>
            <include name="demos/**"/>
            <include name="LICENSE*"/>
            <include name="README"/>
        </zip>
        <checksum file="build/${app.name}-${app.version}.zip"
                  forceOverwrite="yes"
                  fileext=".md5.txt"
                />
        <checksum file="build/${app.name}-${app.version}.zip"
                  algorithm="SHA"
                  forceOverwrite="yes"
                  fileext=".asc.txt"
                />
    </target>

    <!-- 
    Builds a ZIP file of all source for distribution, with 3rd party libraries
    and XHTML samples included
    -->
    <target name="dist.src" depends="check.app.version, init" description="Play Src Distro">
        <zip basedir="${basedir}" zipfile="${dest.dir}/${app.name}-${app.version}-src.zip">
            <include name="LICENSE*"/>
            <include name="README"/>
            <include name="build.xml"/>
            <include name="lib/**"/>
            <include name="etc/build/**"/>
            <include name="src/**"/>

            <!-- restricting to those demos we still maintain -->
            <include name="demos/about/**"/>
            <include name="demos/browser/**"/>
            <include name="demos/docbook/**"/>
            <include name="demos/samples/**"/>
            <include name="demos/splash/**"/>
            <include name="demos/svg/**"/>

            <include name="doc/guide/users-guide-${app.version}.*"/>
            <include name="doc/guide/**/*.png"/>
            <include name="doc/guide/**/*.css"/>
        </zip>
        <checksum file="${dest.dir}/${app.name}-${app.version}-src.zip"
                  forceOverwrite="yes"
                  fileext=".md5.txt"
                />
        <checksum file="${dest.dir}/${app.name}-${app.version}-src.zip"
                  algorithm="SHA"
                  forceOverwrite="yes"
                  fileext=".asc.txt"/>
    </target>

    <!--
        <target name="dist.all" depends="dist.binary, dist.jnlp, dist.src, docs" description="build complete distro set">
    -->
    <target name="dist.jnlp" depends="check.app.version" description="build complete distro set">
        <property file="local.build.properties"/>
        <delete dir="${dist.dir}/demo_repo"/>
        <mkdir dir="${dist.dir}/demo_repo"/>
        <mkdir dir="${dist.dir}/demo_repo/packed"/>
        <mkdir dir="${dist.dir}/demo_repo/unpacked"/>
        <mkdir dir="${dist.dir}/${app.version}"/>
        <delete dir="${dist.dir}/demo"/>
        <mkdir dir="${dist.dir}/${app.version}/demo"/>
        <mkdir dir="${dist.dir}/${app.version}/demo/packed"/>
        <mkdir dir="${dist.dir}/${app.version}/demo/unpacked"/>

        <!-- demos -->
        <!--   shared distributables across all demos -->
        <copy todir="${dist.dir}/${app.version}/demo">
            <fileset dir="${src.packaging.dir}">
                <include name="flyingsaucer.png"/>
            </fileset>
        </copy>
        <copy todir="${dist.dir}/${app.version}/demo/unpacked" file="${dest.dir}/core-renderer.jar"/>
        <copy todir="${dist.dir}/demo_repo/unpacked">
            <fileset dir="${libraries.dir}">
                <include name="xml*.jar"/>
            </fileset>
            <fileset dir="${libraries.dev.dir}">
                <include name="looks-2.1.4.jar"/>
            </fileset>
            <fileset dir="${libraries.dev.dir}">
                <include name="svgSalamander.jar"/>
            </fileset>
        </copy>
        <pack200 src="${dest.dir}/core-renderer.jar"
                 destfile="${dist.dir}/${app.version}/demo/packed/core-renderer.jar.pack.gz"
                 gzipoutput="true"/>
        <pack200 src="${libraries.dir}/xml-apis-xerces-2.9.1.jar"
                 destfile="${dist.dir}/demo_repo/packed/xml-apis-xerces-2.9.1.jar.pack.gz"
                 gzipoutput="true"/>
        <pack200 src="${libraries.dev.dir}/looks-2.1.4.jar"
                 destfile="${dist.dir}/demo_repo/packed/looks-2.1.4.jar.pack.gz"
                 gzipoutput="true"/>
        <pack200 src="${libraries.dev.dir}/svgSalamander.jar"
                 destfile="${dist.dir}/demo_repo/packed/svgSalamander.jar.pack.gz"
                 gzipoutput="true"/>

        <copy file="${src.packaging.dir}/template.jar.var"
              tofile="${dist.dir}/${app.version}/demo/core-renderer.jar.var"/>
        <replace file="${dist.dir}/${app.version}/demo/core-renderer.jar.var"
                 token="FILENAME"
                 value="core-renderer"/>
        <copy tofile="${dist.dir}/${app.version}/demo/.htaccess"
              file="${src.packaging.dir}/apache-jnlp-config.htaccess"/>


        <copy file="${src.packaging.dir}/template.jar.var"
              tofile="${dist.dir}/demo_repo/xml-apis-xerces-2.9.1.jar.var"/>
        <replace file="${dist.dir}/demo_repo/xml-apis-xerces-2.9.1.jar.var"
                 token="FILENAME"
                 value="xml-apis-xerces-2.9.1.jar"/>

        <copy file="${src.packaging.dir}/template.jar.var"
              tofile="${dist.dir}/demo_repo/looks-2.1.4.jar.var"/>
        <replace file="${dist.dir}/demo_repo/looks-2.1.4.jar.var" token="FILENAME" value="looks-2.1.4.jar"/>
        <copy tofile="${dist.dir}/demo_repo/.htaccess"
              file="${src.packaging.dir}/apache-jnlp-config.htaccess"/>

        <copy file="${src.packaging.dir}/template.jar.var"
              tofile="${dist.dir}/demo_repo/svgSalamander.jar.var"/>
        <replace file="${dist.dir}/demo_repo/svgSalamander.jar.var" token="FILENAME" value="svgSalamander.jar"/>
        <copy tofile="${dist.dir}/demo_repo/.htaccess"
              file="${src.packaging.dir}/apache-jnlp-config.htaccess"/>

        <!-- browser demo -->
        <m.dist.demo demoname="browser" />
        <m.dist.demo demoname="aboutbox" />
        <m.dist.demo demoname="docbook" />
        <m.dist.demo demoname="svg" />

    </target>
    <macrodef name="m.dist.demo">
        <attribute name="demoname"/>
        <sequential>
            <mkdir dir="${dist.dir}/${app.version}/demo/@{demoname}/packed"/>
            <mkdir dir="${dist.dir}/${app.version}/demo/@{demoname}/unpacked"/>
            <copy todir="${dist.dir}/${app.version}/demo/@{demoname}/unpacked">
                <fileset dir="${dest.dir}">
                    <include name="@{demoname}.jar"/>
                </fileset>
            </copy>
            <pack200 src="${dest.dir}/@{demoname}.jar"
                     destfile="${dist.dir}/${app.version}/demo/@{demoname}/packed/@{demoname}.jar.pack.gz"
                     gzipoutput="true"/>
            <copy file="${src.packaging.dir}/template.jar.var"
                  tofile="${dist.dir}/${app.version}/demo/@{demoname}/@{demoname}.jar.var"/>
            <replace file="${dist.dir}/${app.version}/demo/@{demoname}/@{demoname}.jar.var"
                     token="FILENAME"
                     value="@{demoname}.jar"/>

            <copy todir="${dist.dir}/${app.version}/demo/@{demoname}"
                  file="${src.packaging.dir}/@{demoname}/@{demoname}_demo.jnlp"/>
            <replace file="${dist.dir}/${app.version}/demo/@{demoname}/@{demoname}_demo.jnlp"
                     token="DEMO_CODEBASE"
                     value="${dist.demo.path}"/>
            <copy tofile="${dist.dir}/${app.version}/demo/@{demoname}/.htaccess"
                  file="${src.packaging.dir}/apache-jnlp-config.htaccess"/>
        </sequential>
    </macrodef>

    <target name="dist.all" depends="check.app.version, clean, dist.binary, dist.src"
            description="build complete distro set">
        <delete dir="${dist.dir}"/>
        <mkdir dir="${dist.dir}/demo_repo"/>
        <mkdir dir="${dist.dir}/demo_repo/packed"/>
        <mkdir dir="${dist.dir}/demo_repo/unpacked"/>
        <mkdir dir="${dist.dir}/${app.version}"/>
        <mkdir dir="${dist.dir}/${app.version}/demo"/>
        <mkdir dir="${dist.dir}/${app.version}/demo/packed"/>
        <mkdir dir="${dist.dir}/${app.version}/demo/unpacked"/>
        <mkdir dir="${dist.dir}/${app.version}/demo/browser/packed"/>
        <mkdir dir="${dist.dir}/${app.version}/demo/browser/unpacked"/>
        <mkdir dir="${dist.dir}/${app.version}/downloads"/>
        <mkdir dir="${dist.dir}/${app.version}/doc"/>

        <antcall target="dist.jnlp" />

        <!-- downloads -->
        <copy todir="${dist.dir}/${app.version}/downloads">
            <fileset dir="${dest.dir}">
                <include name="flyingsaucer-*.zip"/>
                <!-- include MD5 and SHA hash files -->
                <include name="flyingsaucer-*.zip.*"/>
            </fileset>
        </copy>

        <!-- users guide -->
        <copy todir="${dist.dir}/${app.version}/doc/guide">
            <fileset dir="${guide}">
                <include name="*.html"/>
                <include name="*.pdf"/>
            </fileset>
        </copy>

        <!-- javadoc -->
        <tar destfile="${dist.dir}/${app.version}/javadoc-full.tar.gz" compression="gzip">
            <fileset dir="${dest.javadoc.full.dir}"/>
        </tar>
        <tar destfile="${dist.dir}/${app.version}/javadoc-user.tar.gz" compression="gzip">
            <fileset dir="${dest.javadoc.user.dir}"/>
        </tar>
    </target>
</project>
