
<project name="ehcache" default="build" xmlns:artifact="urn:maven-artifact-ant">


    <!--
    Most build functions are now in Maven:

    What's left:
    1. Single Page Guide and Book. Run "mvn compile site" first.

    -->

    <!-- Load Maven antlib and make pom.xml available as maven.project -->
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
        <classpath>
            <pathelement location="tools/maven-ant-tasks-2.0.7.jar"/>
        </classpath>
    </typedef>

    <artifact:pom file="pom.xml" id="maven.project"/>

    <!-- Load local environment overrides -->
    <property environment="env"/>
    <!-- Default properties -->
    <property file="build.properties"/>

    <property name="name" value="${maven.project.artifactId}"/>
    <property name="version" value="${maven.project.version}"/>

    <artifact:dependencies filesetId="dependency.fileset" pathId="dependency.classpath"
                           verbose="false">
        <pom refid="maven.project"/>
    </artifact:dependencies>

    <artifact:dependencies filesetId="compile.dependency.fileset"
                           pathId="compile.dependency.classpath" useScope="compile">
        <pom refid="maven.project"/>
    </artifact:dependencies>

    <!-- Set up project.class.path -->
    <path id="project.class.path">
        <path refid="dependency.classpath"/>
    </path>


    <property name="classpath" refid="project.class.path"/>

    <!-- Requres LaTeX and netpbm. Use this from maven because it needs javadoc generated-->
    <target name="book" description="Build book">
        <copy file="src/site/apt/.aptconvert" todir="${env.HOME}" overwrite="true"/>
        <mkdir dir="${build.dir}/book"/>
        <copy todir="${build.dir}/book/images" overwrite="true">
            <fileset dir="src/site/resources/images"/>
        </copy>
        <copy todir="${build.dir}/book/images" overwrite="true">
            <fileset dir="src/site/resources/documentation/images"/>
        </copy>
        <copy todir="${build.dir}/book" overwrite="true">
            <fileset dir="src/site/apt/documentation">
                <include name="*.apt"/>
            </fileset>
            <fileset dir="src/site/apt">
                <include name="features.apt"/>
                <include name="samples.apt"/>
            </fileset>
            <fileset dir="src/site/resources/book"/>
        </copy>

        <replace dir="${build.dir}/book" token=".jpg" includes="*.apt"/>
        <replace dir="${build.dir}/book" token=".gif" includes="*.apt"/>
        <replace dir="${build.dir}/book" token=".png" includes="*.apt"/>

        <property name="extension" value="pdf"/>
        <antcall target="create_book" inheritall="true"/>

        <exec executable="pdftk" newenvironment="false" dir="${build.dir}/book" spawn="false">
            <arg value="blank_title_page.pdf"/>
            <arg value="EhcacheUserGuide.pdf"/>
            <arg value="cat"/>
            <arg value="output"/>
            <arg value="TempEhcacheUserGuide.pdf"/>
        </exec>
        <move file="${build.dir}/book/TempEhcacheUserGuide.pdf" tofile="${build.dir}/book/EhcacheUserGuide.pdf"/>

    </target>


    <target name="singlepageguide" description="Target for the Single Page HTML User Guide">
        <mkdir dir="${build.dir}/book"/>
        <copy todir="${build.dir}/book/images" overwrite="true">
            <fileset dir="src/site/resources/images"/>
        </copy>
        <copy todir="${build.dir}/book/images" overwrite="true">
            <fileset dir="src/site/resources/documentation/images"/>
        </copy>
        <copy todir="${build.dir}/book" overwrite="true">
            <fileset dir="src/site/resources/css"/>
        </copy>
        <copy todir="${build.dir}/book" overwrite="true">
            <fileset dir="src/site/apt/documentation">
                <include name="*.apt"/>
            </fileset>
            <fileset dir="src/site/apt">
                <include name="features.apt"/>
                <include name="samples.apt"/>
            </fileset>
        </copy>

        <replace dir="${build.dir}/book" token=".jpg" includes="*.apt"/>
        <replace dir="${build.dir}/book" token=".gif" includes="*.apt"/>
        <replace dir="${build.dir}/book" token=".png" includes="*.apt"/>

        <antcall target="create_singlepageguide" inheritall="true"/>

        <copy file="${build.dir}/book/EhcacheUserGuide.html" todir="${build.dir}/site"/>
        <copy file="src/site/resources/images/apt_toc.gif" todir="${build.dir}/site"/>
    </target>

    <macrodef name="create_artifact">
        <attribute name="extension" default="NOT SET"/>
        <element name="some-tasks" optional="yes"/>
        <sequential>
            <copy file="src/site/apt/.aptconvert" todir="${env.HOME}"/>
            <exec executable="aptconvert" newenvironment="false" dir="${build.dir}/book"
                  spawn="false" failifexecutionfails="true" failonerror="true">
                <arg value="EhcacheUserGuide.@{extension}"/>
                <arg value="preface.apt"/>
            </exec>

        </sequential>
    </macrodef>

    <target name="prepare-book-resources">
        <antcall target="copy-ydoc"/>
    </target>

    <!-- Produce the ydocs images for the book and singlepage guide. Relies on Maven javadoc.  -->
    <target name="copy-ydoc" description="Creates the javadocs">
        <copy todir="${build.dir}/book/apidocs">
            <fileset dir="${build.dir}/site/apidocs"/>
        </copy>
    </target>

    <target name="copy_files" description="Copies the config files to the site">
        <copy todir="${build.dir}/site">
            <fileset dir="src/main/config"/>
        </copy>
    </target>

    <target name="copy_image_files" description="Copies the config files to the site">
        <copy overwrite="true" todir="${build.dir}/generated-site/pdf/EhcacheUserGuide/images">
            <fileset dir="src/site/resources/images/"/>
            <fileset dir="src/site/resources/documentation/images/"/>
        </copy>
    </target>

    <target name="show_toc" description="Show TOC. Understood by site plugin but not by Doxis book generator">
        <echo>Showing toc</echo>
        <replace dir="src/site/apt/documentation" token="~~%{toc|fromDepth=2|toDepth=3}" value="%{toc|fromDepth=2|toDepth=3}"/>
    </target>


    <!--If the site build fails this step will be missed. Make sure you don't check in the uncommented TOC-->
    <target name="hide_toc" description="Hide TOC. Understood by site plugin but not by Doxis book generator">
        <echo>Hiding toc</echo>
        <replace dir="src/site/apt/documentation" token="%{toc|fromDepth=2|toDepth=3}" value="~~%{toc|fromDepth=2|toDepth=3}"/>
    </target>


    <target name="unhide_html" description="extra site processing">

        <echo>Copying config files</echo>
        <copy file="${src.config.dir}/ehcache.xml" todir="${build.dir}/site"/>
        <copy file="${src.config.dir}/ehcache.xsd" todir="${build.dir}/site"/>

        <echo>Updating index.html</echo>
        <replace file="${build.dir}/site/index.html">
            <replacetoken><![CDATA[Ehcache Distributed Cache]]></replacetoken>
            <replacevalue><![CDATA[
            Ehcache Distributed Cache <br><font size="-1" color="black">Performance At Any Scale</font></h2>
            ]]></replacevalue>
        </replace>

        <echo>Updating repo</echo>
        <replace file="${build.dir}/site/source-repository.html">
            <replacetoken><![CDATA[ehcache-core]]></replacetoken>
            <replacevalue><![CDATA[ehcache]]></replacevalue>
        </replace>

        <!--Add icons wherever they appear-->
        <echo>Updating icons</echo>
        <replace dir="${build.dir}/site">
            <include name="**/*.html"/>
            <replacetoken><![CDATA[_new_]]></replacetoken>
            <replacevalue><![CDATA[<img src="images/new.gif" alt="New feature">]]></replacevalue>
        </replace>
        <replace dir="${build.dir}/site">
            <include name="**/*.html"/>
            <replacetoken><![CDATA[terracotta_community_login]]></replacetoken>
            <replacevalue>
                <![CDATA[<a href="http://www.terracotta.org/account/login"><img src="images/terracotta_community_login.gif" alt="Terracotta Community Login Required"></a>]]></replacevalue>
        </replace>
        <!--<replace dir="${build.dir}/site">-->
        <!--<include name="**/*.html"/>-->
        <!--<replacetoken><![CDATA[_new_]]></replacetoken>-->
        <!--<replacevalue><![CDATA[<img src="images/new.gif" alt="New feature">]]></replacevalue>-->
        <!--</replace>-->

    </target>

    <target name="create_singlepageguide" depends="prepare-book-resources">
        <create_artifact extension="html"/>
    </target>


    <target name="create_book" depends="prepare-book-resources">
        <create_artifact extension="pdf"/>
    </target>


    <macrodef name="maven">
        <attribute name="options" default=""/>
        <attribute name="goal"/>
        <attribute name="basedir"/>
        <attribute name="resultproperty" default="maven.result"/>
        <element name="args" implicit="true" optional="true"/>
        <sequential>
            <java classname="org.codehaus.classworlds.Launcher" fork="true"
                  dir="@{basedir}" resultproperty="@{resultproperty}">
                <jvmarg value="-Xmx512m"/>
                <classpath>
                    <fileset dir="${maven.home}/boot">
                        <include name="*.jar"/>
                    </fileset>
                    <fileset dir="${maven.home}/lib">
                        <include name="*.jar"/>
                    </fileset>
                </classpath>
                <sysproperty key="classworlds.conf" value="${maven.home}/bin/m2.conf"/>
                <sysproperty key="maven.home" value="${maven.home}"/>
                <arg line="--batch-mode @{options} @{goal}"/>
            </java>
        </sequential>
    </macrodef>

    <property name="maven.home" value="/Users/gluck/work/apache-maven-2.2.1"/>

    <target name="start_terracotta" description="Starts the Terracotta Server">
        <maven basedir="${basedir}"
               goal="tc:start"
               resultproperty="maven.build.result"
                />
    </target>

    <target name="stop_terracotta" description="Stops the Terracotta Server">
        <property name="maven.home" value="/Users/gluck/work/apache-maven-2.2.1"/>
        <maven basedir="${basedir}"
               goal="tc:stop"
               resultproperty="maven.build.result"
                />
    </target>

    <!--<artifact:mvn mavenHome="../../apache-ant-1.7.0" pom="pom.xml">-->
    <!--<arg value="tc:start"/>-->
    <!--</artifact:mvn>-->


</project>
