<?xml version="1.0"?>

<!--
    This software is published under the terms of the Apache License, Version 2.0,
    a copy of which is included in this distribution.
-->

<!--
    Build Requirements:

        * Ant 1.7.1 or higher (including optional tasks)
        * JDK 1.7 or higher
        * jUnit in your Ant or Java classpath
-->

<project name="Openfire XMPP Server" default="openfire" basedir="..">

    <description>
        Openfire build script.
    </description>

    <!-- ======================================================================================= -->
    <!-- GLOBAL PROPERTIES                                                                       -->
    <!-- ======================================================================================= -->

    <property name="version.major" value="4"/>
    <property name="version.minor" value="3"/>
    <property name="version.revision" value="0"/>
    <property name="version.extra" value="alpha"/>    <!-- For 'beta' or 'alpha' -->

    <property name="javac.source" value="1.7"/>
    <property name="javac.target" value="1.7"/>
    <property name="dist.prefix" value="openfire"/>

    <property file="${basedir}/build/build.properties"/>

    <property name="src.dir" value="${basedir}/src"/>
    <property name="src.java.dir" value="${src.dir}/java"/>
    <property name="src.i18n.dir" value="${src.dir}/i18n"/>
    <property name="src.test.dir" value="${src.dir}/test"/>
    <property name="src.test.java.dir" value="${src.test.dir}/java"/>
    <property name="docs.dir" value="${basedir}/documentation"/>
    <property name="web.dir" value="${src.dir}/web"/>
    <property name="spank.dir" value="${src.dir}/spank"/>
    <property name="lib.ant.dir" value="${basedir}/build/lib/ant"/>
    <property name="lib.build.dir" value="${basedir}/build/lib"/>
    <property name="lib.build.installer.dir" value="${basedir}/build/installer"/>
    <property name="lib.merge.dir" value="${lib.build.dir}/merge"/>
    <property name="lib.dist.dir" value="${lib.build.dir}/dist"/>
    <property name="lib.web.dir" value="${web.dir}/WEB-INF/lib"/>
    <property name="i18n.basename" value="openfire_i18n"/>
    <property name="i18n.default.locale" value="en"/>

    <property name="work.dir" value="${basedir}/work"/>
    <property name="temp.build.dir" value="${work.dir}/tempbuild"/>
    <property name="target.i18n.dir" value="${temp.build.dir}/i18n"/>

    <property name="target.dir" value="${basedir}/target"/>
    <property name="target.openfireHome" value="${target.dir}/openfire"/>

    <property name="compile.dir" value="${work.dir}/classes"/>
    <property name="jar.name" value="openfire.jar"/>
    <property name="jar.starter.name" value="startup.jar"/>
    <property name="jar.dest.dir" value="${work.dir}/lib"/>
    <property name="resources.dir" value="${src.dir}/resources"/>

    <property name="overlay.dir" value="${basedir}/custom"/>
    <property name="overlay.src.dir" value="${overlay.dir}/source"/>
    <property name="overlay.web.dir" value="${overlay.dir}/webapp"/>
    <property name="overlay.lib.dir" value="${overlay.dir}/lib"/>
    <property name="overlay.conf.dir" value="${overlay.dir}/conf"/>
    <property name="overlay.i18n.dir" value="${overlay.dir}/i18n"/>
    <property name="overlay.resources.dir" value="${overlay.dir}/resources"/>
    <property name="overlay.compile.dir" value="${work.dir}/overlay/classes"/>

    <property name="javadoc.src.dir" value="${src.dir}/javadoc"/>
    <property name="javadoc.dest.dir" value="${work.dir}/javadoc"/>
    <property name="jspc.jsp.src.dir" value="${work.dir}/jspc/jsp"/>
    <property name="jspc.dest.dir" value="${work.dir}/jspc"/>
    <property name="jspc.java.dest.dir" value="${work.dir}/jspc/java"/>
    <property name="jspc.classes.dest.dir" value="${work.dir}/jspc/classes"/>
    <property name="jspc.jar.name" value="admin-jsp.jar"/>

    <property name="test.dest.dir" value="${work.dir}/test"/>
    <property name="test.classes.dest.dir" value="${test.dest.dir}/classes"/>
    <property name="test.resources.dest.dir" value="${test.dest.dir}/resources"/>
    <property name="test.results.dest.dir" value="${test.dest.dir}/results"/>

    <property name="plugin.src.dir" value="${src.dir}/plugins"/>
    <property name="plugin.dev.dir" value=""/>
    <!-- Set by a developer as alt plugin location -->
    <property name="plugin.dev.dest.dir" value="${work.dir}/plugins-dev"/>
    <property name="plugin.dest.dir" value="${work.dir}/plugins"/>
    <property name="nonrelease.plugin.dir" value="${basedir}/plugins-tmp"/>
    <property name="halt.on.plugin.error" value="true"/>
    <property name="webapp.dest.dir" value="${work.dir}/webapp"/>

    <property name="release.dest.dir" value="${target.dir}/release"/>
    <property name="package.dest.dir" value="${release.dest.dir}"/>
    <property name="overwrite" value="false"/>
    <property name="deploy.jar.dir" value="${jar.dest.dir}"/>
    <property name="anttools.src.dir" value="${src.dir}/tools"/>
    <property name="anttools.target.dir" value="${work.dir}/tools"/>
    <property name="copy.dbscripts" value="true"/>

    <property name="installer.install4j.home" value="/home/bamboo/install4j"/>
    <property name="installer.src" value="${basedir}/build/installer"/>
    <property name="installer.dest.dir" value="${release.dest.dir}"/>
    <property name="installer.install4j.srcfile" value="${installer.src}/openfire.install4j"/>
    <property name="installer.app_name" value="Openfire"/>
    <property name="installer.app_short_name" value="openfire"/>
    <property name="installer.product_name" value="openfire"/>
    <property name="installer.application_id" value="6886-9911-0474-3571"/>
    <property name="installer.unix_install_dir" value="openfire"/>
    <property name="installer.windows_install_dir" value="Openfire"/>
    <property name="installer.publisher" value="Ignite Realtime RTC Community"/>
    <property name="installer.publisher_url" value="www.igniterealtime.org"/>
    <property name="installer.file_prefix" value="${installer.app_short_name}"/>
    <property name="installer.release_root_path" value="${release.dest.dir}"/>

    <property name="license.file.path" value="${docs.dir}/dist"/>

    <!-- For auto-running and debugging -->
    <property name="debug" value="false"/>

    <path id="ant.dependencies">
        <fileset dir="${lib.ant.dir}" includes="*.jar" />
        <fileset dir="${ant.home}/lib" includes="ant.jar" />
    </path>

    <!-- Reference DTDs and XSDs on the classpath, to prevent them from being looked up online. -->
    <xmlcatalog id="dtds">
        <!-- Provided on the classpath (by Jetty/Jasper) -->
        <entity publicId="http://java.sun.com/xml/ns/j2ee" location="javax/servlet/jsp/resources/jsp_2_0.xsd"/>
        <entity publicId="http://java.sun.com/xml/ns/javaee" location="javax/servlet/jsp/resources/jsp_2_1.xsd"/>
        <entity publicId="http://java.sun.com/xml/ns/javaee" location="javax/servlet/jsp/resources/jsp_2_2.xsd"/>
        <entity publicId="http://xmlns.jcp.org/xml/ns/javaee" location="javax/servlet/jsp/resources/jsp_2_3.xsd"/>
        <entity publicId="http://java.sun.com/JSP/Page" location="javax/servlet/jsp/resources/jspxml_2_0.xsd"/>
        <entity publicId="http://java.sun.com/xml/ns/j2ee" location="javax/servlet/jsp/resources/web-jsptaglibrary_2_0.xsd"/>
        <entity publicId="http://java.sun.com/xml/ns/javaee" location="javax/servlet/jsp/resources/web-jsptaglibrary_2_1.xsd"/>
        <dtd publicId="http://java.sun.com/JSP/Page" location="javax/servlet/jsp/resources/jspxml_2_0.dtd"/>
        <dtd publicId="-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" location="javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd"/>

        <!-- Provided in the lib folder -->
        <dtd publicId="-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" location="${lib.ant.dir}/web-app_2_3.dtd"/>
    </xmlcatalog>

    <!-- ======================================================================================= -->
    <!-- GLOBAL TASKDEFs                                                                         -->
    <!-- ======================================================================================= -->

    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath refid="ant.dependencies" />
    </taskdef>
    <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask">
        <classpath refid="ant.dependencies" />
    </taskdef>
    <taskdef name="propertymerge" classname="org.jugbb.ant.propertymerge.PropertyMergeTask">
        <classpath refid="ant.dependencies" />
    </taskdef>
    <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
        <classpath refid="ant.dependencies" />
    </taskdef>

    <!-- ======================================================================================= -->
    <!-- PATHs / PATTERNSETs / FILTERSETs                                                        -->
    <!-- ======================================================================================= -->

    <available type="dir"  file="${overlay.dir}"                property="overlay.dir.found"/>
    <available type="dir"  file="${overlay.src.dir}"            property="overlay.src.dir.found"/>
    <available type="dir"  file="${overlay.web.dir}"            property="overlay.web.dir.found"/>
    <available type="dir"  file="${overlay.lib.dir}"            property="overlay.lib.dir.found"/>
    <available type="dir"  file="${overlay.conf.dir}"           property="overlay.conf.dir.found"/>
    <available type="dir"  file="${overlay.i18n.dir}"           property="overlay.i18n.dir.found"/>
    <available type="dir"  file="${overlay.resources.dir}"      property="overlay.resources.dir.found"/>

    <!-- When overlay is used the content of "lib.merge.dir" needs to be combined with the content of "overlay.lib.dir"
         Here, "libs.to.merge" is created which will always refer to the correct combination. "libs.to.merge" should be
         used instead of "lib.merge.dir" and/or "overlay.lib.dir" from this point onwards.
      -->
    <if>
        <equals arg1="${overlay.lib.dir.found}" arg2="true"/>
        <then>
            <path id="libs.to.merge">
                <fileset dir="${lib.merge.dir}"   includes="**/*.jar"/>
                <fileset dir="${overlay.lib.dir}" includes="**/*.jar"/>
            </path>
        </then>
        <else>
            <path id="libs.to.merge">
                <fileset dir="${lib.merge.dir}"   includes="**/*.jar"/>
            </path>
        </else>
    </if>

    <path id="javadoc.dependencies">
        <fileset dir="${lib.build.dir}" includes="*.jar" excludes="junit.jar"/>
        <path refid="libs.to.merge" />
        <fileset dir="${lib.dist.dir}" includes="mail.jar, jdic.jar"/>
        <fileset dir="${lib.dist.dir}" includes="bcpg-jdk15on.jar, bcpkix-jdk15on.jar, bcprov-jdk15on.jar"/>
    </path>

    <path id="compile.dependencies">
        <path refid="ant.dependencies"/>
        <path refid="javadoc.dependencies"/>
    </path>

    <path id="jspc.dependencies">
        <path refid="compile.dependencies"/>
        <path refid="ant.dependencies"/>
        <fileset dir="${lib.web.dir}" includes="*.jar"/>
    </path>

    <path id="test.dependencies">
        <path refid="compile.dependencies"/>
        <fileset dir="${jar.dest.dir}" includes="${jar.name}"/>
        <fileset dir="${lib.build.dir}" includes="junit.jar"/>
        <fileset dir="${lib.build.dir}" includes="hamcrest-core.jar"/>
        <fileset dir="${lib.dist.dir}" includes="slf4j-log4j12.jar"/>
        <!-- <fileset dir="${ant.home}/lib" includes="clover.jar" /> -->
    </path>

    <path id="plugin.dependencies">
        <path refid="javadoc.dependencies"/>
        <fileset dir="${jar.dest.dir}" includes="${jar.name}"/>
    </path>

    <patternset id="compile.sources">
        <include name="**/*.java"/>
    </patternset>

    <patternset id="test.sources">
        <include name="**/*Test.java"/>
        <exclude name="**/CheckChainTrustedTest.java" if="skipResourceIntensiveTests"/>
    </patternset>

    <patternset id="web.sources">
        <include name="**/*.jsp"/>
        <include name="**/*.jar"/>
        <include name="**/*.tld"/>
        <include name="**/*.jspf"/>
        <include name="**/*.html"/>
        <include name="**/*.css"/>
        <include name="**/*.gif"/>
        <include name="**/*.png"/>
        <include name="**/favicon.ico"/>
        <include name="**/*.js"/>
    </patternset>

    <presetdef name="of.javac">
        <javac encoding="utf8" source="${javac.source}" target="${javac.target}" includeAntRuntime="no" debug="on"/>
    </presetdef>

    <!-- ======================================================================================= -->
    <!-- TARGETs                                                                                 -->
    <!-- ======================================================================================= -->

    <!-- git.revision ========================================================= 
         http://stackoverflow.com/questions/2974106
    -->
    <available file=".git" type="dir" property="git.present"/>

    <target name="git.revision" description="Store git revision in ${repository.version}" if="git.present">
        <exec executable="git" outputproperty="git.revision" failifexecutionfails="false" errorproperty="">
            <arg value="rev-parse"/>
            <arg value="--short"/>
            <arg value="HEAD"/>
        </exec>
        <condition property="repository.version" value="${git.revision}" else="unknown">
            <and>
                <isset property="git.revision"/>
                <length string="${git.revision}" trim="yes" length="0" when="greater"/>
            </and>
        </condition>
        <echo message="repository.version set to '${repository.version}'" />
    </target>

    <!-- init ================================================================================== -->
    <target name="init">

        <!-- Check for min build requirements -->

        <condition property="ant.not.ok" value="true">
            <not>
                <antversion atleast="1.7.1"/>
            </not>
        </condition>
        <condition property="java.not.ok" value="true">
            <not>
                <or>
                    <contains string="${ant.java.version}" substring="1.7"/>
                    <contains string="${ant.java.version}" substring="1.8"/>
                    <contains string="${ant.java.version}" substring="1.9"/>
                </or>
            </not>
        </condition>
        <fail if="ant.not.ok" message="Must use Ant 1.7.1 or higher to build Openfire"/>
        <fail if="java.not.ok" message="Must use JDK 1.7 or higher to build Openfire"/>

        <tstamp/>
        <tstamp>
            <format property="builddate" pattern="MM/dd/yyyy"/>
        </tstamp>
        <tstamp>
            <format property="dailybuild.tstamp" pattern="yyyy-MM-dd" locale="en"/>
        </tstamp>

        <mkdir dir="${work.dir}"/>

        <!-- Setup the full version property correctly -->
        <if>
            <equals arg1="${version.extra}" arg2=""/>
            <then>
                <property name="version"
                          value="${version.major}.${version.minor}.${version.revision}"/>
                <property name="version.filename"
                          value="${version.major}_${version.minor}_${version.revision}"/>
            </then>
            <else>
                <property name="version"
                          value="${version.major}.${version.minor}.${version.revision}.${version.extra}"/>
                <property name="version.filename"
                          value="${version.major}_${version.minor}_${version.revision}_${version.extra}"/>
            </else>
        </if>
    </target>

    <!-- compile =============================================================================== -->
    <target name="compile" depends="init" description="Compiles Openfire app code">

        <mkdir dir="${compile.dir}"/>
        <of.javac destdir="${compile.dir}">
            <src path="${src.java.dir}"/>
            <patternset refid="compile.sources"/>
            <classpath refid="compile.dependencies"/>
        </of.javac>

        <!-- The Ant 'javac' tasks does not allow one to use more than one source directory and simultaneously exclude
             duplicates from one. As a work-around, the overlay-sources are compiled in a second iteration, overwriting
             any compiled files from the first iteration.
        -->
        <if>
            <equals arg1="${overlay.src.dir.found}" arg2="true"/>
            <then>
                <mkdir dir="${overlay.compile.dir}"/>
                <of.javac destdir="${overlay.compile.dir}">
                    <src path="${overlay.src.dir}"/>
                    <patternset refid="compile.sources"/>
                    <classpath>
                        <path location="${compile.dir}"/>
                        <path refid="compile.dependencies"/>
                    </classpath>
                </of.javac>

                <!-- By copying/overwriting overlays directly into compile.dir, all tasks executed after this one need
                     not consider both compile.dir and overlay.compile.dir (which simplifies further tasks considerably).
                -->
                <copy todir="${compile.dir}" overwrite="true">
                    <fileset dir="${overlay.compile.dir}"/>
                </copy>
            </then>
        </if>
    </target>

    <!-- i18n ================================================================================== -->
    <!-- Note, this is a "private" target - no need to call it externally -->
    <target name="-i18n">

        <mkdir dir="${target.i18n.dir}"/>

        <if>
            <equals arg1="${overlay.i18n.dir.found}" arg2="true"/>
            <then>
                <!-- Overlay files exist for i18n: append/override the files that ship with Openfire. -->
                <foreach target="-i18n-overlay" param="i18n-propertyfile">
                    <path>
                        <fileset dir="${src.i18n.dir}">
                            <include name="${i18n.basename}*.properties"/>
                        </fileset>
                    </path>
                </foreach>
            </then>
            <else>
                <!-- No overlay for i18n: simply copy all files. -->
                <copy todir="${target.i18n.dir}">
                    <fileset dir="${src.i18n.dir}" includes="*.properties"/>
                </copy>
            </else>
        </if>

        <!--
            Java has trouble reading translations from UTF-8 files. To work around that,
            this plugin ensures that Java-compatible encoding is used.
            See https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle
          -->
        <native2ascii encoding="UTF8" src="${target.i18n.dir}" dest="${target.i18n.dir}/encoded" />
        <move todir="${target.i18n.dir}" overwrite="true">
            <fileset dir="${target.i18n.dir}/encoded"/>
        </move>

        <!-- Auto generates a default base i18n file -->
        <copy file="${target.i18n.dir}/${i18n.basename}_${i18n.default.locale}.properties"
              tofile="${target.i18n.dir}/${i18n.basename}.properties"/>

    </target>

    <!-- Note, this is a "private" target - no need to call it externally -->
    <target name="-i18n-overlay">
        <basename property="filename" file="${i18n-propertyfile}"/>
        <if>
            <available file="${overlay.i18n.dir}/${filename}"/>
            <then>
                <propertymerge
                        inputpropertyfile="${src.i18n.dir}/${filename}"
                        mergepropertyfile="${overlay.i18n.dir}/${filename}"
                        outputpropertyfile="${target.i18n.dir}/${filename}"
                        ignoreMissingMergeFile="true" />
            </then>
            <else>
                <copy file="${src.i18n.dir}/${filename}" tofile="${target.i18n.dir}/${filename}"/>
            </else>
        </if>
    </target>

    <target name="plugins-dev" depends="anttasks">
        <!-- Setup Openfire -->
        <ant antfile="${basedir}/build/build.xml" dir="${basedir}" target="openfire"
             inheritAll="false" inheritRefs="false"/>

        <copy file="${java.home}/../lib/tools.jar" todir="${jar.dest.dir}"></copy>

        <!-- Retrieve each plugin -->
        <!-- Get a list of subdirs of the main plugins dir. This tells us which plugins to make.
        <subdirinfo dir="${plugin.src.dir}" property="dirlist" ifexists="plugin.xml" except="admin"/>

        <for list="${dirlist}" param="plugin" trim="true">
            <sequential>
                <xmltask source="${plugin.src.dir}/@{plugin}/plugin.xml" dest="${plugin.src.dir}/@{plugin}/plugins2.xml">
                    <xmlcatalog refid="dtds"/>
                    <insert path="/plugin/minOpenfireVersion[last()]"  position="after">
                        <![CDATA[
                          <development>
                            <webRoot>
                              ${java.home}
                            </webRoot>
                            <classesDir>
                              XXXXXXXXX
                            </classesDir>
                          </development>
                        ]]>
                      </insert>
                </xmltask>
                <jar jarfile="c:\\test\\@{plugin}.jar">
                    <fileset dir="${plugin.src.dir}/@{plugin}" includes="*.xml"/>
                </jar>
            </sequential>
        </for>
        -->

    </target>

    <!-- openfire =================================================================================== -->
    <target name="openfire" depends="compile, jspc, -i18n"
            description="Compiles and generates runnable target/openfire folder">
        <mkdir dir="${jar.dest.dir}"/>
        <!-- Make main Openfire jar -->
        <jar jarfile="${jar.dest.dir}/${jar.name}" index="true" duplicate="preserve">
            <fileset dir="${compile.dir}" includes="**/*.class" excludes="org/jivesoftware/openfire/starter/ServerStarter*.class,org/jivesoftware/openfire/launcher/*.class"/>
            <fileset dir="${target.i18n.dir}" includes="*.properties"/>
            <fileset dir="${overlay.resources.dir}/jar" includes="**" erroronmissingdir="false"/>
            <fileset dir="${resources.dir}/jar" includes="**"/>
            <archives>
                <zips>
                    <path refid="libs.to.merge"/>
                </zips>
            </archives>
            <manifest>
                <attribute name="Built-By" value="Jive Software (www.igniterealtime.org)"/>
            </manifest>
        </jar>

        <!-- Make startup jar -->
        <jar jarfile="${jar.dest.dir}/${jar.starter.name}">
            <fileset dir="${compile.dir}">
                <include name="org/jivesoftware/openfire/starter/ServerStarter*.class"/>
                <include name="org/jivesoftware/openfire/launcher/*.class"/>
                <include name="org/jivesoftware/openfire/starter/JiveClassLoader*.class"/>
            </fileset>
            <fileset dir="${lib.build.installer.dir}/images" includes="**/*.gif"/>
            <fileset dir="${lib.build.installer.dir}/images" includes="**/*.png"/>
            <manifest>
                <attribute name="Main-Class"
                           value="org.jivesoftware.openfire.starter.ServerStarter"/>
                <attribute name="Built-By" value="Jive Software (www.igniterealtime.org)"/>
            </manifest>
        </jar>
        <!-- Copy application dependent files -->
        <copy todir="${jar.dest.dir}">
            <fileset dir="${lib.dist.dir}" includes="*.*"/>
        </copy>

        <!-- Copy in JSP support -->
        <copy todir="${jar.dest.dir}">
            <fileset dir="${lib.build.dir}" includes="org.apache.taglibs.taglibs-standard-impl.jar"/>
            <fileset dir="${lib.build.dir}" includes="org.apache.taglibs.taglibs-standard-spec.jar"/>
            <fileset dir="${lib.build.dir}" includes="org.eclipse.jetty.apache-jsp.jar"/>
            <fileset dir="${lib.build.dir}" includes="org.eclipse.jetty.orbit.org.eclipse.jdt.core.jar"/>
            <fileset dir="${lib.build.dir}" includes="org.mortbay.jasper.apache-el.jar"/>
            <fileset dir="${lib.build.dir}" includes="org.mortbay.jasper.apache-jsp.jar"/>
        </copy>

        <!-- Copy in jasper runtime jar -->
        <copy todir="${jar.dest.dir}">
            <fileset dir="${lib.build.dir}" includes="jasper-runtime.jar"/>
            <fileset dir="${lib.build.dir}" includes="jasper-compiler.jar"/>
        </copy>

        <!-- Update/create target/openfire directory -->
        <antcall target="openfireHome"/>
    </target>

    <!-- run =================================================================================== -->
    <target name="run" description="Starts Openfire inline to the build process.">
        <available file="${target.openfireHome}" type="dir" property="openfire.found"/>
        <fail unless="openfire.found"
              message="Can't find openfire, run 'ant openfire' or specify with -Dtarget.openfireHome."/>

        <condition property="run.debug"
                   value="-Xdebug -Xint -server -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000">
            <isset property="debug"/>
        </condition>

        <property name="run.spawn" value="false"/>

        <java jar="${target.openfireHome}/lib/startup.jar" fork="true" spawn="${run.spawn}"
              dir="${target.openfireHome}/bin">
            <jvmarg line="${run.debug} -DopenfireHome=${target.openfireHome}"/>
        </java>
    </target>


    <!-- run.test.init ========================================================================= -->
    <target name="run.test.init">
        <delete dir="${target.openfireHome}"/>

        <property name="openfireHome.preconfigured" value="true"/>

        <property name="run.spawn" value="true"/>
    </target>

    <!-- run.test ============================================================================== -->
    <!--    Starts Openfire inline to the build process, with a clean pre-configured openfireHome -->
    <target name="run.test" depends="run.test.init,openfireHome,run">
        <echo>Openfire server started.</echo>
    </target>

    <!-- javadoc =============================================================================== -->
    <target name="javadoc" depends="init" description="Produces Openfire Javadocs">
        <mkdir dir="${javadoc.dest.dir}"/>
        <antcall target="-javadoc-impl"/>
    </target>

    <target name="-javadoc-impl" unless="no.javadoc">

        <!-- Starting with Java8 Javadoc checks for valid html. We disable it only for Java8 because older verions doesn't know the property -->
        <condition property="javadoc.additionalparams" value="-Xdoclint:none">
            <contains string="${ant.java.version}" substring="1.8"/>
        </condition>
        <condition property="javadoc.additionalparams" value="">
            <not>
              <contains string="${ant.java.version}" substring="1.8"/>
            </not>
        </condition>
        <!-- Run javadoc over all source code -->
        <javadoc
                packagenames="org.jivesoftware.*, org.xmpp.*"
                destdir="${javadoc.dest.dir}"
                windowtitle="Openfire ${version} Javadoc"
                overview="${src.java.dir}/overview.html"
                failonerror="yes"
        Encoding="UTF-8"
                additionalparam="${javadoc.additionalparams}"
                >
            <sourcepath>
                <path location="${src.java.dir}"/>
            </sourcepath>
            <doctitle>
                <![CDATA[<font face="arial,helvetica,sans-serif">Openfire ${version} Javadoc</font>]]></doctitle>
            <header><![CDATA[<b>Openfire ${version} Javadoc</b>]]></header>
            <bottom><![CDATA[<i>Copyright &copy; 2003-2008 Jive Software.</i>]]></bottom>
            <classpath>
                <path refid="javadoc.dependencies"/>
            </classpath>
            <link offline="true" href="http://docs.oracle.com/javase/7/docs/api/"
                  packagelistLoc="${javadoc.src.dir}/jdk15"/>
        </javadoc>
    </target>

    <!-- jspc ================================================================================== -->
    <target name="jspc" depends="compile" description="Compiles all JSP pages in the admin console">
        <mkdir dir="${jspc.dest.dir}"/>
        <mkdir dir="${jspc.java.dest.dir}"/>
        <mkdir dir="${jspc.classes.dest.dir}"/>
        <mkdir dir="${webapp.dest.dir}"/>

        <antcall target="-jspc-impl"/>
    </target>

    <target name="-jspc-impl" unless="no.jspc">

        <!-- In a different approach to 'overlay' as compared with the 'compile' task, this task first creates a new
             directory that combines of the original files, overwritten by the 'overlay' files before processing
             occurs. This approach is simpler, but requires quite some disk operations (and is therefor not used in the
             'compile' task).
        -->
        <mkdir dir="${jspc.jsp.src.dir}"/>

        <copy todir="${jspc.jsp.src.dir}">
            <fileset dir="${web.dir}" includes="**/*"/>
        </copy>

        <if>
            <equals arg1="${overlay.web.dir.found}" arg2="true"/>
            <then>
                <copy todir="${jspc.jsp.src.dir}" overwrite="true">
                    <fileset dir="${overlay.web.dir}" includes="**/*"/>
                </copy>
            </then>
        </if>

        <!-- Generate .java files from the .jsp files. -->
        <java classname="org.apache.jasper.JspC" fork="true">
            <classpath>
                <pathelement location="${java.home}/../lib/tools.jar"/>
                <pathelement path="${compile.dir}"/>
                <path refid="jspc.dependencies"/>
            </classpath>
            <arg line="-uriroot '${jspc.jsp.src.dir}'"/>
            <arg line="-d '${jspc.java.dest.dir}'"/>
            <arg line="-p org.jivesoftware.openfire.admin"/>
            <arg line="-webinc '${jspc.dest.dir}/web.partial.xml'"/>
        </java>

        <!-- Compile the .java files-->
        <of.javac destdir="${jspc.classes.dest.dir}">
            <include name="org/jivesoftware/openfire/admin/**/*.java"/>
            <src path="${jspc.java.dest.dir}"/>
            <classpath>
                <pathelement path="${compile.dir}"/>
                <path refid="jspc.dependencies"/>
            </classpath>
        </of.javac>

        <!-- Update the web.xml to include the servlet and servlet mapping defs from jspc -->
        <loadfile property="servlet-xml" srcFile="${jspc.dest.dir}/web.partial.xml" failonerror="false" />
        <copy file="${web.dir}/WEB-INF/web.xml"
              toFile="${webapp.dest.dir}/WEB-INF/web.xml"
              overwrite="true">
            <filterset begintoken="&lt;!--@@" endtoken="@@--&gt;">
                <filter token="JSPC-SERVLETS" value="${servlet-xml}"/>
            </filterset>
        </copy>

        <!-- When there is a web.xml in the overlay use that. -->
        <if>
            <available file="${overlay.web.dir}/WEB-INF/web.xml" />
            <then>
                <copy file="${overlay.web.dir}/WEB-INF/web.xml"
                      toFile="${webapp.dest.dir}/WEB-INF/web.xml"
                      overwrite="true">
                    <filterset begintoken="&lt;!--@@" endtoken="@@--&gt;">
                        <filter token="JSPC-SERVLETS" value="${servlet-xml}"/>
                    </filterset>
                </copy>
            </then>
        </if>

        <!-- Copy over other WEB-INF files. -->
        <copy todir="${webapp.dest.dir}/WEB-INF" overwrite="true">
            <fileset dir="${web.dir}/WEB-INF">
                <include name="**/*"/>
                <exclude name="web.xml"/>
            </fileset>
        </copy>

        <!-- When there are WEB-INF overlay files, use those. -->
        <if>
            <equals arg1="${overlay.web.dir.found}" arg2="true"/>
            <then>
                <copy todir="${webapp.dest.dir}/WEB-INF" overwrite="true">
                    <fileset dir="${overlay.web.dir}/WEB-INF">
                        <include name="**/*"/>
                        <exclude name="web.xml"/>
                    </fileset>
                </copy>
            </then>
        </if>

        <!-- Create a jar of compiled servlets -->
        <jar jarfile="${jspc.dest.dir}/${jspc.jar.name}">
            <fileset dir="${jspc.classes.dest.dir}" includes="**/*.class"/>
        </jar>

    </target>

    <!-- test ================================================================================== -->
    <target name="test" depends="openfire" description="Compiles and runs test cases">
        <!-- Compile all test code -->
        <mkdir dir="${test.classes.dest.dir}"/>
        <of.javac destdir="${test.classes.dest.dir}">
            <src path="${src.test.java.dir}"/>
            <patternset refid="test.sources"/>
            <classpath>
                <path refid="test.dependencies"/>
                <pathelement path="${compile.dir}"/>
            </classpath>
        </of.javac>

        <!-- Copy resources -->
        <copy todir="${test.dest.dir}">
            <fileset dir="${src.test.dir}" includes="resources/**/*.*"/>
        </copy>

        <!-- Run jUnit -->
        <mkdir dir="${test.results.dest.dir}"/>
        <junit printsummary="yes" haltonfailure="yes" fork="yes" dir="${test.dest.dir}" includeantruntime="false">
        <sysproperty key="openfireHome" value="${target.openfireHome}"/>

            <!-- We must add our own classes to the classpath for testing -->
            <classpath>
                <path refid="test.dependencies"/>
                <pathelement path="${test.dest.dir}"/>
                <pathelement path="${test.classes.dest.dir}"/>
                <pathelement path="${test.resources.dest.dir}"/>
            </classpath>

            <formatter type="xml"/>

            <batchtest todir="${test.results.dest.dir}">
                <fileset dir="${src.test.java.dir}">
                    <patternset refid="test.sources"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <!-- openfireHome ========================================================================== -->
    <target name="openfireHome" unless="openfireHome.no.update">
        <!-- Set this to true to enable a pre-setup openfireHome directory -->
        <property name="openfireHome.preconfigured" value="false"/>
        <property name="openfireHome.no.plugins" value="false"/>

        <mkdir dir="${target.openfireHome}"/>

        <!-- Copy conf dir -->
        <copy todir="${target.openfireHome}">
            <fileset dir="${src.dir}" includes="conf/**/*.*"></fileset>
        </copy>

        <if>
            <equals arg1="${overlay.conf.dir.found}" arg2="true"/>
            <then>
                <copy todir="${target.openfireHome}" overwrite="true">
                    <fileset dir="${overlay.conf.dir}" includes="**/*"/>
                </copy>
            </then>
            <else><echo>No custom config here </echo></else>
        </if>

        <!-- Copy database scripts -->
        <if>
            <equals arg1="${copy.dbscripts}" arg2="true"/>
            <then>
                <copy todir="${target.openfireHome}/resources">
                    <fileset dir="${src.dir}" includes="database/**/*.sql"></fileset>
                </copy>
            </then>
        </if>

        <!-- Copy security resources -->
        <copy todir="${target.openfireHome}/resources">
            <fileset dir="${src.dir}">
                <include name="security/*"/>
                <include name="security/*.*"/>
            </fileset>
        </copy>

        <!-- Copy native authentication files -->
        <copy todir="${target.openfireHome}/resources">
            <fileset dir="${resources.dir}">
                <include name="nativeAuth/**"/>
            </fileset>
        </copy>
        <copy todir="${target.openfireHome}/resources">
            <fileset dir="${overlay.resources.dir}" erroronmissingdir="false">
                <include name="nativeAuth/**"/>
            </fileset>
        </copy>

        <!-- Copy lib dir -->
        <copy todir="${target.openfireHome}/lib">
            <fileset dir="${jar.dest.dir}" includes="*.*"></fileset>
        </copy>

        <!-- Overwrite config with build/openfireHome dir, if requested -->
        <if>
            <equals arg1="${openfireHome.preconfigured}" arg2="true"/>
            <then>
                <!-- Copy pre-configured openfireHome dir -->
                <copy todir="${target.openfireHome}">
                    <fileset dir="${basedir}/build/openfireHome" includes="**/*.*"/>
                </copy>
            </then>
        </if>

        <!-- Copy admin Plugin -->
        <antcall target="adminPlugin">
            <param name="plugin.dest.dir" value="${target.openfireHome}/plugins"/>
        </antcall>

        <antcall target="spank">
            <param name="plugin.dest.dir" value="${target.openfireHome}/plugins"/>
        </antcall>

        <!-- Copy compiled plugins if we need to -->
        <if>
            <and>
                <equals arg1="${openfireHome.no.plugins}" arg2="false"/>
                <available file="${plugin.dest.dir}" />
            </and>
            <then>
                <copy todir="${target.openfireHome}/plugins" failonerror="false">
                    <fileset dir="${plugin.dest.dir}"/>
                </copy>
            </then>
        </if>

        <!-- Copy bin dir -->
        <copy todir="${target.openfireHome}">
            <fileset dir="${src.dir}" includes="bin/**/*"/>
        </copy>
        <!-- Make stuff under bin executable -->
        <chmod perm="+x">
            <fileset dir="${target.openfireHome}/bin" includes="**/*">
                <exclude name="**/*.bat"/>
                <exclude name="**/*.rc"/>
                <exclude name="**/*-sysconfig"/>
            </fileset>
        </chmod>
        <fixcrlf srcdir="${target.openfireHome}/bin" eol="lf" eof="remove" includes="*.sh,extra/*"/>

        <!-- Create a logs dir in the binary release -->
        <mkdir dir="${target.openfireHome}/logs"/>
    </target>

    <!-- adminPlugin =========================================================================== -->
    <target name="adminPlugin">
        <!-- Copy admin plugin source (if any) -->
        <copy todir="${plugin.dest.dir}">
            <fileset dir="${src.dir}/plugins" includes="admin/*.*"/>
        </copy>

        <!-- Copy admin plugin resources -->
        <copy todir="${plugin.dest.dir}/admin/webapp">
            <!-- All web resources minus jsp's and jspf's - those are precompiled -->
            <fileset dir="${web.dir}">
                <patternset refid="web.sources"/>
                <exclude name="**/*.jsp"/>
                <exclude name="**/*.jspf"/>
            </fileset>
        </copy>

        <!-- Copy admin plugin overlay resources -->
        <if>
            <equals arg1="${overlay.web.dir.found}" arg2="true"/>
            <then>
                <copy todir="${plugin.dest.dir}/admin/webapp" overwrite="true">
                    <!-- All web resources minus jsp's and jspf's - those are precompiled -->
                    <fileset dir="${overlay.web.dir}">
                        <patternset refid="web.sources"/>
                        <exclude name="**/*.jsp"/>
                        <exclude name="**/*.jspf"/>
                    </fileset>
                </copy>
            </then>
        </if>

        <!-- Copy other admin WEB-INF stuff -->
        <copy todir="${plugin.dest.dir}/admin/webapp">
            <fileset dir="${webapp.dest.dir}"/>
        </copy>

        <!-- Copy in pre-compiled JSP jar -->
        <copy todir="${plugin.dest.dir}/admin/webapp/WEB-INF/lib" overwrite="${overwrite}">
            <fileset dir="${jspc.dest.dir}" includes="${jspc.jar.name}"/>
        </copy>

    </target>

    <target name="spank">
        <mkdir dir="${target.openfireHome}/resources/spank"/>
        <copy todir="${target.openfireHome}/resources/spank">
            <!-- All web resources minus jsp's and jspf's - those are precompiled -->
            <fileset dir="${spank.dir}">
                <patternset refid="web.sources"/>
                <exclude name="**/*.jsp"/>
                <exclude name="**/*.jspf"/>
                <include name="**/*.xml"/>
                <include name="**/*.swf"/>
            </fileset>
        </copy>
    </target>
    
    <!-- dist.init ============================================================================== -->
    <target name="dist.init" >
        <!-- ie: openfire_src -->
        <property name="release.name.src" value="${dist.prefix}_src"/>
        <!-- ie: openfire_src_2_1_2 -->
        <property name="release.fullname.src" value="${dist.prefix}_src_${version.filename}"/>
        <property name="package.dest.filename.zip" value="${package.dest.dir}/${release.fullname.src}.zip" />
        <property name="package.dest.filename.tar" value="${package.dest.dir}/${release.fullname.src}.tar" />
        <property name="package.dest.filename.tar.gz" value="${package.dest.filename.tar}.gz" />
        <property name="release.src.out.dir" value="${release.dest.dir}/${release.name.src}"/>

        <!-- ie: openfire -->
        <property name="release.name" value="${dist.prefix}"/>
        <!-- ie: openfire_2_1_2 -->
        <property name="release.fullname" value="${dist.prefix}_${version.filename}"/>
        <property name="release.out.dir" value="${release.dest.dir}/${release.name}"/>
    </target>    

    <target name="clean.dist.folders" depends="init, dist.init">
        <!-- Removes the expanded release folders, makes for easier automation of builds -->
        <delete dir="${release.src.out.dir}"/>
        <delete dir="${release.out.dir}"/>
    </target>    

    <!-- dist.src =============================================================================== -->
    <target name="dist.src" depends="init, dist.init, javadoc, -i18n"
            description="Creates a source distribution package">

        <mkdir dir="${release.dest.dir}"/>

        <!-- Create a new prop for the final release dirs then create the dirs -->
        <mkdir dir="${release.src.out.dir}"/>
        <!-- Copy dist docs, use filtering  -->
        <copy todir="${release.src.out.dir}">
            <fileset dir="${docs.dir}/dist" includes="*.*" excludes="LICENSE.html"/>
            <filterset>
                <filter token="builddate" value="${builddate}"/>
                <filter token="version" value="${version}"/>
            </filterset>
        </copy>
        <!-- Copy the license -->
        <copy todir="${release.src.out.dir}" file="${license.file.path}/LICENSE.html"/>
        <!-- Copy text docs -->
        <copy todir="${release.src.out.dir}/documentation">
            <fileset dir="${docs.dir}" includes="**/*.html,**/*.txt"/>
            <filterset>
                <filter token="version" value="${version}"/>
            </filterset>
        </copy>
        <!-- Copy rest of docs -->
        <copy todir="${release.src.out.dir}/documentation">
            <fileset dir="${docs.dir}" excludes="**/*.html,**/*.txt"/>
        </copy>
        <!-- Copy Javadocs -->
        <copy todir="${release.src.out.dir}/documentation/docs/javadoc">
            <fileset dir="${javadoc.dest.dir}"/>
        </copy>
        <!-- Copy non-java source -->
        <copy todir="${release.src.out.dir}/src">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>

        <!-- Copy java source, stripping non-distributable files and replacing DATE strings -->
        <copy todir="${release.src.out.dir}/src">
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
            <filterset>
                <filter token="DATE" value="${builddate}"/>
            </filterset>
        </copy>

        <if>
            <available file="${work.dir}/plugins-dev/enterprise/target/lib" />
            <then>
                <copy todir="${release.src.out.dir}/src/plugins/enterprise/lib" failonerror="false">
                    <fileset dir="${work.dir}/plugins-dev/enterprise/target/lib">
                        <include name="*-lib.jar" />
                    </fileset>
                </copy>
            </then>
        </if>

        <!-- Copy the i18n files to the resources dir. This way they won't be in the jar only -->
        <copy todir="${release.src.out.dir}/resources/i18n">
            <fileset dir="${target.i18n.dir}" includes="*.properties"/>
        </copy>
        <!-- Copy build -->
        <copy todir="${release.src.out.dir}/build">
            <fileset dir="${basedir}/build"/>
        </copy>

        <if>
            <not>
                <isset property="no.package"/>
            </not>
            <then>
                <parallel>
                    <!-- Source Packages -->
                    <zip zipfile="${package.dest.filename.zip}"
                         basedir="${release.src.out.dir}/.."
                         includes="${release.fullname.src}/**/*, ${release.name.src}/**/*"
                            />
                    <tar tarfile="${package.dest.filename.tar.gz}"
                         basedir="${release.src.out.dir}/.."
                         includes="${release.fullname.src}/**/*, ${release.name.src}/**/*"
                         compression="gzip"
                         longfile="gnu"
                            />
                </parallel>
            </then>
        </if>
    </target>

    <!-- dist.bin =============================================================================== -->
    <target name="dist.bin" depends="init, dist.init, plugins, javadoc, enterprise"
            description="Creates a binary distribution package">

        <mkdir dir="${release.out.dir}"/>

        <!-- Make & copy the search plugin. -->
        <copy todir="${release.out.dir}">
            <fileset dir="${work.dir}">
                <include name="plugins/search.jar"/>
            </fileset>
        </copy>

        <!-- Copy all prepare dirs to the binary dist -->
        <antcall target="openfireHome">
            <param name="target.openfireHome" value="${release.out.dir}"/>
            <param name="openfireHome.no.plugins" value="true"/>
        </antcall>

        <!-- Remove the development scripts -->
        <delete file="${release.out.dir}/bin/openfire-dev.bat"/>
        <delete file="${release.out.dir}/bin/openfire-dev.sh"/>

        <!-- Copy dist docs, use filtering  -->
        <copy todir="${release.out.dir}">
            <fileset dir="${docs.dir}/dist" includes="*.*" excludes="LICENSE.html"/>
            <filterset>
                <filter token="builddate" value="${builddate}"/>
                <filter token="version" value="${version}"/>
            </filterset>
        </copy>

        <!-- Copy the license -->
        <copy todir="${release.out.dir}" file="${license.file.path}/LICENSE.html"/>
        <mkdir dir="${release.out.dir}/logs"/>
        <touch file="${release.out.dir}/logs/stderr.out"/>

        <!-- Copy docs -->
        <copy todir="${release.out.dir}/documentation">
            <fileset dir="${docs.dir}/docs"/>
            <filterset>
                <filter token="version" value="${version}"/>
            </filterset>
        </copy>
        <copy todir="${release.out.dir}/documentation/images" filtering="false" overwrite="true">
            <fileset dir="${docs.dir}/docs/images"/>
        </copy>

        <!-- Copy Javadocs -->
        <copy todir="${release.out.dir}/documentation/javadoc">
            <fileset dir="${javadoc.dest.dir}"/>
        </copy>


        <!-- Copy the i18n files to the resources dir. This way they won't be in the jar only -->
        <copy todir="${release.out.dir}/resources/i18n">
            <fileset dir="${target.i18n.dir}" includes="*.properties"/>
        </copy>

        <!-- Package the release -->
        <property name="package.dest.dir" value="${release.dest.dir}"/>

        <if>
            <not>
                <isset property="no.package"/>
            </not>
            <then>
                <parallel>
                    <!-- Binary packages -->
                    <zip zipfile="${package.dest.dir}/${release.fullname}.zip"
                         basedir="${release.out.dir}/.."
                         includes="${release.fullname}/**/*, ${release.name}/**/*"/>
                    <tar tarfile="${package.dest.dir}/${release.fullname}.tar.gz"
                         basedir="${release.out.dir}/.."
                         includes="${release.fullname}/**/*, ${release.name}/**/*"
                         compression="gzip"
                         longfile="gnu"/>
                </parallel>
            </then>
        </if>
    </target>

    <target name="enterprise" if="do.enterprise">
        <!-- Copy enterprise plugins. -->
        <copy todir="${release.out.dir}">
            <fileset dir="${work.dir}">
                <include name="plugins/enterprise.jar"/>
                <include name="plugins/fastpath.jar"/>
                <include name="plugins/webchat.war"/>
            </fileset>
        </copy>
        <!-- Remove GPL libs from distribution -->
        <delete file="${release.out.dir}/lib/mysql.jar"/>
    </target>

    <!-- dailybuild ============================================================================ -->
    <target name="dailybuild" depends="init" description="Creates a daily build release">
        <property name="release.out.name" value="${dist.prefix}_${dailybuild.tstamp}"/>
        <property name="release.fullname" value="${release.out.name}"/>
        <property name="release.src.out.name" value="${dist.prefix}_src_${dailybuild.tstamp}"/>
        <property name="release.fullname.src" value="${release.src.out.name}"/>
        <property name="release.out.dir"
                  value="${release.dest.dir}/dailybuild/${release.out.name}"/>
        <property name="release.src.out.dir"
                  value="${release.dest.dir}/dailybuild/${release.src.out.name}"/>
        <antcall target="dist.src">
            <param name="release.src.out.name" value="${release.src.out.name}"/>
            <param name="release.src.out.dir" value="${release.src.out.dir}"/>
            <param name="package.dest.dir" value="${release.dest.dir}/dailybuild"/>
        </antcall>
        <antcall target="dist.bin">
            <param name="release.out.name" value="${release.out.name}"/>
            <param name="release.out.dir" value="${release.out.dir}"/>
            <param name="package.dest.dir" value="${release.dest.dir}/dailybuild"/>
            <!-- dist.src above does this already, so don't need to do it twice -->
            <param name="no.javadoc" value="true"/>
        </antcall>
    </target>

    <!-- installer ============================================================================= -->
    <target name="installer" description="Creates Openfire installers">
        <condition property="install4j.not.ok" value="true">
            <not>
                <available file="${installer.install4j.home}/bin/install4j.jar"/>
            </not>
        </condition>
        <fail if="install4j.not.ok"
              message="Path to Install4j not set, see build.properties.template file."/>
    
        <antcall target="dist.bin">
            <!-- Don't need package for installer build -->
            <param name="no.package" value="true"/>
        </antcall>

        <antcall target="dist.src">
            <!-- Already built javadoc with dist.bin above -->
            <param name="no.javadoc" value="true"/>
        </antcall>

        <taskdef name="install4j"
                 classname="com.install4j.Install4JTask"
                 classpath="${installer.install4j.home}/bin/ant.jar"/>

        <mkdir dir="${installer.dest.dir}"/>

        <!-- Install4j doesn't support extra version info (like beta) correctly, so
             define a special revision number.  -->
        <if>
            <equals arg1="${version.extra}" arg2=""/>
            <then>
                <property name="install4j.revision" value="${version.revision}"/>
            </then>
            <else>
                <property name="install4j.revision" value="${version.revision}.${version.extra}"/>
            </else>
        </if>

        <install4j projectfile="${installer.install4j.srcfile}" destination="${installer.dest.dir}">
            <variable name="RELEASE_DIR" value="${dist.prefix}"/>
            <variable name="VERSION_MAJOR" value="${version.major}"/>
            <variable name="VERSION_MINOR" value="${version.minor}"/>
            <variable name="VERSION_REVISION" value="${install4j.revision}"/>
            <variable name="APP_NAME" value="${installer.app_name}"/>
            <variable name="APP_SHORT_NAME" value="${installer.app_short_name}"/>
            <variable name="PRODUCT_NAME" value="${installer.product_name}"/>
            <variable name="PUBLISHER" value="${installer.publisher}"/>
            <variable name="PUBLISHER_URL" value="${installer.publisher_url}"/>
            <variable name="FILE_PREFIX" value="${installer.app_short_name}"/>
            <variable name="RELEASE_ROOT_PATH" value="${installer.release_root_path}"/>
            <variable name="APPLICATION_ID" value="${installer.application_id}"/>
            <variable name="UNIX_INSTALL_DIR" value="${installer.unix_install_dir}"/>
            <variable name="WINDOWS_INSTALL_DIR" value="${installer.windows_install_dir}"/>
        </install4j>

    </target>
    
    <!-- installer.rpm ======================================================== 
         Build RPM artifiacts, set rpmbuild.arch to build non-x64, for example:
           $ ant installer.rpm -Drpmbuild.arch=noarch
           $ ant installer.rpm -Drpmbuild.arch=i686         
    -->
    <property name="bundle.jre" value="true" />
    <property name="target.work.subdir" value="rpm" />
    <property name="target.rpm" value="${work.dir}/${target.work.subdir}" />
    <property name="rpm.spec.file" value="${basedir}/build/rpm/openfire.spec"/>
    <property name="rpmbuild.arch" value="x86_64" />
    <tstamp>
        <format property="rpm.builddate" pattern="EEE MMM dd yyyy" locale="en"/>
    </tstamp>
    <tstamp>
        <format property="rpm.builddate2" pattern="yyyyMMdd" locale="en"/>
    </tstamp>
    <target name="installer.rpm" depends="init, dist.init, git.revision"
            description="Builds a rpm of openfire.">
        <antcall target="dist.src"/>

        <!-- create empty dirs to allow rpmbuild to do its thing -->
        <mkdir dir="${target.rpm}/SPECS" />
        <mkdir dir="${target.rpm}/SOURCES" />
        <mkdir dir="${target.rpm}/BUILD" />
        <mkdir dir="${target.rpm}/SRPMS" />
        <mkdir dir="${target.rpm}/RPMS" />

        <!-- Setup the RPM versioning correctly, so one can update from 
             a alpha,beta,rc build to GA 
             For General Releases we get x.y.z-1
             For Alpha builds we get     x.y.z-0.1.{YYYYMMDD}alpha
             For Beta/RC builds we get   x.y.z-0.2.(beta|rc)
        -->
        <property name="rpmbuild.version"
                  value="${version.major}.${version.minor}.${version.revision}"/>
        <if>
            <equals arg1="${version.extra}" arg2=""/>
            <then>
                <property name="rpmbuild.release"
                          value="1"/>
            </then>
        <elseif>
            <equals arg1="${version.extra}" arg2="alpha"/>
            <then>
                <property name="rpmbuild.release"
                          value="0.1.${rpm.builddate2}${version.extra}"/>
            </then>
        </elseif>
        <else>
            <property name="rpmbuild.release"
                      value="0.2.${version.extra}"/>        	
        </else>
        </if>
        <copy tofile="${target.rpm}/SPECS/openfire.spec" file="${rpm.spec.file}" />
        <!-- Save the user from themself, if noarch is the target arch, ignore 
             copying the JRE as well -->
        <if>
            <and>
            <equals arg1="${bundle.jre}" arg2="true"/>
            <not>
                <equals arg1="${rpmbuild.arch}" arg2="noarch" />
            </not>
            </and>
            <then>
                <!-- Include bundled jre -->
                <copy todir="${target.rpm}/SOURCES" file="${jre.bundle.location}" />
            </then>
        </if>
        <copy todir="${target.rpm}/SOURCES/" file="${release.dest.dir}/${release.fullname.src}.tar.gz" />


        <rpm specFile="openfire.spec"
             topDir="${target.rpm}"
             command="-ba --target ${rpmbuild.arch} --define 'JRE_BUNDLE ${jre.bundle.location}' --define 'OPENFIRE_VERSION ${rpmbuild.version}' --define 'OPENFIRE_RELEASE ${rpmbuild.release}' --define 'OPENFIRE_SOURCE ${release.fullname.src}.tar.gz' --define 'OPENFIRE_REPOVERSION ${repository.version}' --define 'OPENFIRE_BUILDDATE ${rpm.builddate}'"
             failOnError="true"
         />

          <!-- copy the rpmbuild result into the release directory! -->
          <copy todir="${release.dest.dir}" >
          <fileset dir="${target.rpm}/SRPMS" />
          <fileset dir="${target.rpm}/RPMS/${rpmbuild.arch}" />
          </copy>
    </target>

    <!-- plugins =============================================================================== -->
    <target name="plugins" description="Builds all plugins" depends="anttasks">
        <antcall target="clean-plugins"/>

        <tstamp>
            <format property="buildJavaDate" pattern="MMM dd, yyyy"/>
        </tstamp>
        <!-- Call jar task, can't do this is as a 'depends' call of this target because
             of the way class loading works for jspc calls.
        -->
        <antcall target="openfire">
            <param name="openfireHome.no.update" value="true"/>
        </antcall>

        <mkdir dir="${plugin.dest.dir}"/>

        <!-- Get a list of subdirs of the main plugins dir. This tells us which plugins to make. Exclude the
             admin plugin (which is created by a different Ant task) as well as the clustering plugin (which
             requires non-free JAR-files to be added to its library path, which most users won't do). -->
        <subdirinfo dir="${plugin.src.dir}" property="dirlist" ifexists="plugin.xml" except="admin,clustering"/>

        <antcall target="-plugins-impl"/>

        <!-- Update/create target/openfire directory -->
        <antcall target="openfireHome"/>
    </target>

    <target name="-plugins-impl" if="dirlist">
        <for list="${dirlist}" param="plugin" trim="true">
            <sequential>
                <buildplugin plugin="@{plugin}" pluginsrc="${plugin.src.dir}"/>
            </sequential>
        </for>
    </target>

    <target name="plugin" description="build one plugin">
        <mkdir dir="${plugin.dest.dir}"/>

        <delete dir="${plugin.dev.dest.dir}/${plugin}"/>
        <delete file="${plugin.dev.dest.dir}/${plugin}.jar"/>
        <delete file="${plugin.dest.dir}/${plugin}.jar"/>
        <buildplugin plugin="${plugin}" pluginsrc="${plugin.src.dir}"/>

        <!-- Update/create target/openfire directory -->
        <antcall target="openfireHome"/>
    </target>

    <!-- buildplugin (MACRO) =================================================================== -->
    <macrodef name="buildplugin">
        <attribute name="plugin"/>
        <attribute name="pluginsrc"/>
        <attribute name="pluginlib" default="${plugin.dev.dest.dir}/@{plugin}/work/lib" />
        <sequential>
            <if>
                <not>
                    <istrue value="${plugin.@{plugin}.skip}"/>
                </not>
                <then>
                    <trycatch property="message_ref" reference="exception_ref">
                        <try>

                            <!-- For each plugin, compile code, make a jar and copy resources. -->
                            <mkdir dir="${plugin.dev.dest.dir}"/>
                            <mkdir dir="${plugin.dev.dest.dir}/@{plugin}"/>
                            <mkdir dir="${plugin.dev.dest.dir}/@{plugin}/target"/>
                            <mkdir dir="${plugin.dev.dest.dir}/@{plugin}/target/classes"/>
                            <mkdir dir="${plugin.dev.dest.dir}/@{plugin}/target/parentlibs"/>

                            <xmlproperty file="${plugin.src.dir}/@{plugin}/plugin.xml" prefix="@{plugin}" keeproot="false"/>
                            <if>
                                <isset property="@{plugin}.parentPlugin"/>
                                <then>
                                    <echo>Parent plugin found: ${@{plugin}.parentPlugin}</echo>
                                    <!--<buildparentplugin parentPlug="${@{plugin}.parentPlugin}" plugin="@{plugin}" pluginsrc="@{pluginsrc}" pluginlib="@{pluginlib}"/>-->
                                    <buildplugin plugin="${@{plugin}.parentPlugin}" pluginsrc="${plugin.src.dir}"/>

                                    <!-- Copy parent plugin code + dependencies into our own plugin directory. -->
                                    <echo>Parent plugin built. Copying its resources and continuing with build for child plugin.</echo>
                                    <copy todir="${plugin.dev.dest.dir}/@{plugin}/target/parentlibs">
                                        <fileset dir="${plugin.dev.dest.dir}/${@{plugin}.parentPlugin}/target/parentlibs/" includes="**/*.jar" />
                                    </copy>
                                    <copy todir="${plugin.dev.dest.dir}/@{plugin}/target/parentlibs">
                                        <fileset dir="${plugin.dev.dest.dir}/${@{plugin}.parentPlugin}/target/lib/" includes="**/*.jar" />
                                    </copy>

                                </then>
                            </if>

                            <path id="plugin.classpath.source">
                                <path refid="plugin.dependencies"/>
                                <fileset dir="${plugin.dev.dest.dir}/@{plugin}/target/parentlibs" includes="**/*.jar"/>
                                <fileset dir="@{pluginsrc}/@{plugin}" includes="lib/*.jar"/>
                            </path>

                            <!-- Compile plugin source code -->
                            <of.javac destdir="${plugin.dev.dest.dir}/@{plugin}/target/classes" classpathref="plugin.classpath.source">
                                <src path="@{pluginsrc}/@{plugin}/src/java"/>
                            </of.javac>

                            <mkdir dir="${plugin.dev.dest.dir}/@{plugin}/target/lib" />

                            <!-- Searchs source of a plugin and makes a <plugin>-lib.jar that contains compiled versions of all
                                 non-distributable source -->
                            <mkdir dir="@{pluginlib}/source" />
                            <mkdir dir="@{pluginlib}/classes" />

                            <!-- copy the source we want to compile -->
                            <copy todir="@{pluginlib}/source">
                                <fileset dir="@{pluginsrc}/@{plugin}/src/java" includes="**/*.java" />
                                <filterset>
                                    <filter token="DATE" value="${buildJavaDate}"/>
                                </filterset>
                            </copy>

                            <if>
                                <not><uptodate>
                                    <srcfiles dir="@{pluginlib}/source" includes="**/*.java"/>
                                    <mapper type="merge" to="@{pluginlib}/@{plugin}-lib.jar"/>
                                </uptodate></not>
                                <then>
                                    <of.javac destdir="@{pluginlib}/classes">
                                        <src path="@{pluginlib}/source"/>
                                        <classpath>
                                            <pathelement location="${plugin.dev.dest.dir}/@{plugin}/target/classes"/>
                                            <path refid="plugin.classpath.source"/>
                                        </classpath>
                                    </of.javac>

                                    <jar jarfile="${plugin.dev.dest.dir}/@{plugin}/target/lib/@{plugin}-lib.jar">
                                        <fileset dir="@{pluginlib}/classes" />
                                    </jar>
                                </then>
                            </if>

                            <!-- Make the jar -->
                            <jar jarfile="${plugin.dev.dest.dir}/@{plugin}/target/lib/plugin-@{plugin}.jar">
                                <fileset dir="${plugin.dev.dest.dir}/@{plugin}/target/classes" >
                                    <present present="srconly" targetdir="@{pluginlib}/classes"/>
                                </fileset>
                            </jar>

                            <delete dir="@{pluginlib}/classes" />
                            <delete dir="@{pluginlib}/source" />

                            <available file="@{pluginsrc}/@{plugin}/src/include" type="dir"
                                       property="@{plugin}.include.exists"/>
                            <if>
                                <equals arg1="${@{plugin}.include.exists}" arg2="true"/>
                                <then>
                                    <jar jarfile="${plugin.dev.dest.dir}/@{plugin}/target/lib/plugin-@{plugin}.jar"
                                         update="true">
                                        <fileset dir="@{pluginsrc}/@{plugin}/src/include" includes="**/*.*"/>
                                    </jar>
                                </then>
                            </if>

                            <!-- Copy anything in the plugin's lib dir to the target lib dir -->
                            <if>
                                <available file="@{pluginsrc}/@{plugin}/lib" />
                                <then>
                                    <copy todir="${plugin.dev.dest.dir}/@{plugin}/target/lib" failonerror="false"
                                          overwrite="${overwrite}">
                                        <fileset dir="@{pluginsrc}/@{plugin}/lib" includes="**/*.*"/>
                                    </copy>
                                </then>
                            </if>

                            <!-- Copy web.xml to web-custom.xml -->
                            <if>
                                <available file="@{pluginsrc}/@{plugin}/src/web/WEB-INF" />
                                <then>
                                    <copy todir="${plugin.dev.dest.dir}/@{plugin}/target/web/WEB-INF" failonerror="false"
                                          overwrite="${overwrite}">
                                        <fileset dir="@{pluginsrc}/@{plugin}/src/web/WEB-INF" includes="web.xml"/>
                                        <mapper type="glob" from="web.xml" to="web-custom.xml"/>
                                    </copy>
                                </then>
                            </if>

                            <!-- Copy the plugin.xml and documentation to the target dir, code below assumes it's there -->
                            <mkdir dir="${plugin.dev.dest.dir}/@{plugin}/jar"/>
                            <copy todir="${plugin.dev.dest.dir}/@{plugin}/jar" failonerror="false"
                                  overwrite="${overwrite}">
                                <fileset dir="@{pluginsrc}/@{plugin}" includes="*.xml, *.html, *.gif, *.png, *.ico"/>
                            </copy>

                            <!-- Copy the database and i18n file to the target dir, if they exist. -->
                            <!--<copy todir="${plugin.dev.dest.dir}/@{plugin}/target/database" failonerror="false">-->
                            <!--<fileset dir="@{pluginsrc}/@{plugin}/src/database"/>-->
                            <!--</copy>-->

                            <!-- JSPC any JSP pages. Do this conditionally as there might not be a web dir. -->
                            <available property="plugin.@{plugin}.webdocs.available"
                                       type="dir" file="@{pluginsrc}/@{plugin}/src/web"/>
                            <if>
                                <equals arg1="${plugin.@{plugin}.webdocs.available}" arg2="true"/>
                                <then>

                                    <!-- Continue with JSPC tasks... -->

                                    <!-- Create output dir -->
                                    <mkdir dir="${plugin.dev.dest.dir}/@{plugin}/target/jspc/java"/>
                                    <mkdir dir="${plugin.dev.dest.dir}/@{plugin}/target/jspc/classes"/>

                                    <!-- Copy jsp's from plugin and web.xml from openfire to a temp dir. We'll
                                         not fail on an error since the web dir might not exist.
                                    -->
                                    <copy todir="${plugin.dev.dest.dir}/@{plugin}/target/web"
                                          overwrite="${overwrite}">
                                        <fileset dir="@{pluginsrc}/@{plugin}/src/web">
                                            <exclude name="WEB-INF/web.xml"/>
                                        </fileset>
                                        <fileset dir="${web.dir}">
                                            <include name="WEB-INF/**/*.*"/>
                                            <include name="META-INF/**/*.*"/>
                                            <exclude name="WEB-INF/web.xml"/>
                                            <exclude name="WEB-INF/classes/openfire_init.xml"/>
                                            <exclude name="WEB-INF/tmp/**/*.*"/>
                                            <exclude name="WEB-INF/work/**/*.*"/>
                                        </fileset>
                                    </copy>

                                    <!-- JSP to Java -->
                                    <java classname="org.apache.jasper.JspC" fork="true">
                                        <classpath>
                                            <pathelement location="${java.home}/../lib/tools.jar"/>
                                            <fileset dir="${plugin.dev.dest.dir}/@{plugin}/target" includes="lib/*.jar"/>
                                            <pathelement location="${plugin.dev.dest.dir}/@{plugin}/target/lib/plugin-@{plugin}.jar"/>
                                            <pathelement path="${compile.dir}"/>
                                            <path refid="jspc.dependencies"/>
                                        </classpath>
                                        <arg line="-uriroot '${plugin.dev.dest.dir}/@{plugin}/target/web'"/>
                                        <arg line="-d '${plugin.dev.dest.dir}/@{plugin}/target/jspc/java'"/>
                                        <arg line="-p org.jivesoftware.openfire.plugin.@{plugin}"/>
                                        <arg line="-webxml '${plugin.dev.dest.dir}/@{plugin}/target/jspc/web.xml'"/>
                                    </java>

                                    <!-- Use xmltask to merge the generated web.xml file and a developer one (if any) -->
                                    <available property="plugin.@{plugin}.webxml.available"
                                               type="file" file="@{pluginsrc}/@{plugin}/src/web/WEB-INF/web.xml"/>

                                    <mkdir dir="${plugin.dev.dest.dir}/@{plugin}/target/web/WEB-INF"/>

                                    <if>
                                        <equals arg1="${plugin.@{plugin}.webxml.available}" arg2="true"/>
                                        <then>

                                            <!-- Copy the servlet and servlet-mapping elements from the original web.xml to a temp buffer.
                                                 Note: The original web.xml can only contain one servlet and servlet-mapping -->
                                            <xmltask source="@{pluginsrc}/@{plugin}/src/web/WEB-INF/web.xml">
                                                <xmlcatalog refid="dtds"/>
                                                <copy path="//web-app/servlet[last()]" buffer="foobar"/>
                                                <copy path="//web-app/servlet-mapping[last()]" buffer="foobar2"/>
                                            </xmltask>
                                            <!-- Add the copied servlet and servlet-mapping elements to the generated web.xml -->
                                            <xmltask source="${plugin.dev.dest.dir}/@{plugin}/target/jspc/web.xml"
                                                     dest="${plugin.dev.dest.dir}/@{plugin}/target/web/WEB-INF/web.xml">
                                                <xmlcatalog refid="dtds"/>
                                                <insert path="/web-app/servlet[last()]" buffer="foobar"
                                                        position="after"/>
                                                <insert path="/web-app/servlet-mapping[last()]" buffer="foobar2"
                                                        position="after"/>
                                            </xmltask>

                                        </then>
                                        <else>
                                            <copy todir="${plugin.dev.dest.dir}/@{plugin}/target/web/WEB-INF"
                                                  file="${plugin.dev.dest.dir}/@{plugin}/target/jspc/web.xml"
                                                  overwrite="${overwrite}"/>
                                        </else>
                                    </if>

                                    <!-- Compile java classes -->
                                    <of.javac destdir="${plugin.dev.dest.dir}/@{plugin}/target/jspc/classes">
                                        <src path="${plugin.dev.dest.dir}/@{plugin}/target/jspc/java"/>
                                        <classpath>
                                            <path refid="jspc.dependencies"/>
                                            <pathelement path="${compile.dir}"/>
                                            <fileset dir="${plugin.dev.dest.dir}/@{plugin}/target/lib" includes="*.jar"/>
                                            <fileset dir="${plugin.dev.dest.dir}/@{plugin}/target/parentlibs" includes="**/*.jar"/>
                                        </classpath>
                                    </of.javac>

                                    <!-- Make a jar of compiled jsp classes -->
                                    <jar jarfile="${plugin.dev.dest.dir}/@{plugin}/target/lib/plugin-@{plugin}-jspc.jar">
                                        <fileset dir="${plugin.dev.dest.dir}/@{plugin}/target/jspc/classes"
                                                 includes="**/*.class"/>
                                    </jar>

                                </then>
                            </if>

                            <copy todir="${plugin.dev.dest.dir}/@{plugin}/jar" overwrite="true">
                                <fileset dir="${plugin.dev.dest.dir}/@{plugin}/target">
                                    <include name="lib/*.jar"/>
                                    <include name="web/WEB-INF/web.xml"/>
                                    <include name="web/WEB-INF/web-custom.xml"/>
                                </fileset>
                            </copy>

                            <!-- Copy everything else to Openfire's plugin dir -->
                            <copy todir="${plugin.dev.dest.dir}/@{plugin}/jar" overwrite="true">
                                <fileset dir="@{pluginsrc}/@{plugin}">
                                    <include name="classes/**/*.*"/>
                                </fileset>
                                <fileset dir="@{pluginsrc}/@{plugin}/src">
                                    <include name="database/**/*.sql"/>
                                    <include name="i18n/*.properties"/>
                                    <include name="web/**/*.*"/>
                                    <include name="**/*"/>
                                    <exclude name="web/WEB-INF/web.xml"/>
                                    <exclude name="web/**/*.jsp"/>
                                    <exclude name="web/**/*.jspf"/>
                                    <exclude name="java/**/*.java"/>
                                </fileset>
                            </copy>

                            <!-- Jar the plugin -->
                            <jar jarfile="${plugin.dest.dir}/@{plugin}.jar">
                                <fileset dir="${plugin.dev.dest.dir}/@{plugin}/jar"/>
                            </jar>

                            <!-- Delete the exploded plugin -->
                            <delete dir="${plugin.dev.dest.dir}/@{plugin}/jar"/>

                            <!-- Plugin is done! No need to rebuilt it within the same Ant task -->
                            <property name="plugin.@{plugin}.skip" value="true"/>
                        </try>
                        <catch>
                            <property name="exception" refid="exception_ref" />
                            <property name="message" value="Error building plugin: @{plugin}. Exception:${line.separator}${exception}" />
                            <echo message="${message}" />
                            <fail message="${message}">
                                <condition>
                                    <istrue value="${halt.on.plugin.error}"/>
                                </condition>
                            </fail>
                        </catch>
                    </trycatch>
                </then>
            </if>
        </sequential>

    </macrodef>

    <!-- anttasks ============================================================================== -->
    <target name="anttasks" depends="init">

        <mkdir dir="${anttools.target.dir}"/>
        <mkdir dir="${anttools.target.dir}/classes"/>

        <of.javac destdir="${anttools.target.dir}/classes" includeAntRuntime="yes">
            <src path="${anttools.src.dir}"/>
            <patternset refid="compile.sources"/>
            <classpath>
                <path refid="compile.dependencies"/>
            </classpath>
        </of.javac>

        <jar jarfile="${anttools.target.dir}/ant-subdirtask.jar">
            <fileset dir="${anttools.target.dir}/classes" includes="**/*.class"/>
        </jar>

        <taskdef name="subdirinfo" classname="org.jivesoftware.ant.SubDirInfoTask">
            <classpath>
                <pathelement location="${anttools.target.dir}/ant-subdirtask.jar"/>
            </classpath>
        </taskdef>
    </target>

    <!-- clean ================================================================================= -->
    <target name="clean" description="Cleans up all build-generated output">
        <delete dir="${work.dir}"/>
        <delete dir="${target.dir}"/>

        <!-- Delete the Enterprise plugin in the openfire plugin directory, if this isn't an enterprise build -->
        <if>
            <not><equals arg1="${do.enterprise}" arg2="true"/></not>
            <then>
                <!-- Copy pre-configured openfireHome dir -->
                <delete dir="${basedir}/src/plugins/enterprise" />
            </then>
        </if>
    </target>

    <target name="clean-jspc" description="Cleans up all JSPC output">
        <delete dir="${jspc.dest.dir}"/>
    </target>

    <target name="clean-test" description="Cleans up all compiled test classes">
        <delete dir="${test.classes.dest.dir}"/>
    </target>

    <target name="clean-plugins" description="Cleans up all generated plugins">
        <delete includeemptydirs="true" failonerror="false">
            <fileset dir="${plugin.dest.dir}" excludes="admin/**/*.*"/>
        </delete>
        <delete dir="${plugin.dev.dest.dir}"/>
    </target>

    <target name="clean-plugin" description="Cleans up one generated plugin">
        <delete includeemptydirs="true" failonerror="false">
            <fileset dir="${plugin.dest.dir}/${plugin}.jar" excludes="admin/**/*.*"/>
        </delete>
        <delete dir="${plugin.dev.dest.dir}/${plugin}" failonerror="false"/>
    </target>

    <!-- Mac installer ========================================================================= -->
    <property name="target.osx" value="${work.dir}/osx" />
    <property name="mac.pkg.dir" value="${target.osx}/macpkg"/>
    <property name="mac.dmg.dir" value="${target.osx}/Openfire"/>
    <property name="mac.dmg.file" value="${release.dest.dir}/openfire.dmg"/>
    <property name="mac.template" value="${target.osx}/template"/>
    <property name="mac.prefpane.build" value="${target.osx}/prefPane"/>

    <target name="mac.delete">
        <delete dir="${mac.pkg.dir}" failonerror="false"/>
        <delete dir="${mac.dmg.dir}" failonerror="false"/>
        <delete file="${mac.dmg.file}" failonerror="false"/>
        <delete dir="${mac.template}" failonerror="false"/>
        <delete dir="${mac.prefpane.build}" failonerror="false"/>
    </target>
    
    <target name="mac.prefpane">
    <!-- install will put it in macpkg/Library/PreferencePanes/ as specified in the target settings -->
        <copy todir="${mac.prefpane.build}" >
            <fileset dir="${basedir}/build/osx/openfirePrefPane" />
        </copy>
        <exec executable="/usr/bin/xcodebuild" dir="${mac.prefpane.build}" failonerror="true">
            <arg value="-configuration"/>
            <arg value="Deployment"/>
            <arg value="-target"/>
            <arg value="Openfire"/>
            <arg value="clean"/>
            <arg value="install"/>
        </exec>
    </target>

    <target name="mac.prepare" depends="mac.delete,openfire, mac.prefpane">
        <copy todir="${mac.pkg.dir}/usr/local/openfire">
            <fileset dir="${target.dir}/openfire">
                <exclude name="**/openfired"/>
                <exclude name="**/openfirectl"/>
                <exclude name="**/redhat"/>
                <exclude name="**/embedded-db*"/>
                <exclude name="**/redhat-postinstall.sh"/>
            </fileset>
        </copy>
        <mkdir dir="${mac.pkg.dir}/Library/LaunchDaemons"/>
        <copy file="${basedir}/build/osx/org.jivesoftware.openfire.plist"
              todir="${mac.pkg.dir}/Library/LaunchDaemons"/>
        <mkdir dir="${mac.dmg.dir}"/>
        <mkdir dir="${mac.pkg.dir}/Library/PreferencePanes"/>
        <copy todir="${mac.pkg.dir}/Library/PreferencePanes">
            <fileset dir="${mac.prefpane.build}/build/UninstalledProducts/"/>
        </copy>
        <chmod perm="o+x">
            <fileset dir="${mac.pkg.dir}/Library/PreferencePanes/Openfire.prefPane/Contents/MacOS/">
                <include name="HelperTool"/>
            </fileset>
        </chmod>
        <chmod perm="ug+x">
            <fileset dir="${mac.pkg.dir}/usr/local/openfire/bin">
                <include name="extra/openfire-launchd-wrapper.sh"/>
            </fileset>
            <fileset dir="${basedir}/build/osx/resources"/>
        </chmod>
        <mkdir dir="${mac.template}/.background"/>
        <copy todir="${mac.template}/.background">
            <fileset file="${basedir}/build/osx/dmgBackground.png"/>
        </copy>
    </target>

    <!-- Create a Mac OS X installer -->
    <target name="mac.pkg" depends="mac.prepare">
        <tstamp>
            <format property="copyrightyear" pattern="yyyy"/>
        </tstamp>
        <copy todir="${target.osx}" file="${basedir}/build/osx/Info.plist"/>
        <replace file="${target.osx}/Info.plist">
            <replacefilter token="%VERSION%" value="${version}"/>
            <replacefilter token="%VERSIONMAJOR%" value="${version.major}"/>
            <replacefilter token="%VERSIONMINOR%" value="${version.minor}"/>
            <replacefilter token="%COPYRIGHT%" value="${copyrightyear}"/>
        </replace>
        <copy todir="${target.osx}" file="${basedir}/build/osx/Description.plist"/>
        <exec executable="/Developer/usr/bin/packagemaker">
            <arg value="-build"/>
            <arg value="-f"/>
            <arg value="${mac.pkg.dir}"/>
            <arg value="-i"/>
            <arg value="${target.osx}/Info.plist"/>
            <arg value="-d"/>
            <arg value="${target.osx}/Description.plist"/>
            <arg value="-r"/>
            <arg value="${basedir}/build/osx/resources"/>
            <!--<arg value="-proj"/>
            <arg value="${basedir}/build/osx/openfire.pmproj"/> -->
            <arg value="-p"/>
            <arg value="${mac.template}/Openfire.pkg"/>
            <arg value="-ds"/>
            <arg value="-v"/>
        </exec>
    </target>

    <target name="installer.mac" depends="mac.pkg" description="Creates a Mac OS X package">
        <mkdir dir="${release.dest.dir}"/>
        <exec executable="hdiutil" failonerror="true">
            <arg line="create -srcfolder '${mac.template}' -volname 'Openfire' -fs HFS+ -fsargs '-c c=64,a=16,e=16' -format UDRW '${target.osx}/tmp.dmg'"/>
        </exec>
        <exec executable="hdiutil" failonerror="true">
            <arg line="attach '${target.osx}/tmp.dmg' -readwrite -noverify -noautoopen -noidme -mountpoint '${mac.dmg.dir}'"/>
        </exec>
<!-- OF-386 - commented out since it wasn't working with our Bamboo remote agent // TODO: fix it
        <exec executable="osascript" dir="${basedir}/build/osx/" failonerror="true">
            <arg value="dmg_openfire.scpt"/>
            <arg value="Openfire"/>
            <arg value="images"/>
            <arg value="360"/>
            <arg value="200"/>
            <arg value="450"/>
            <arg value="220"/>
            <arg value="128"/>
        </exec>
-->
        <exec executable="hdiutil" failonerror="true">
            <arg line="detach ${mac.dmg.dir} -quiet -force"/>
        </exec>
        <exec executable="hdiutil" failonerror="true">
            <arg line="convert ${target.osx}/tmp.dmg -format UDZO -imagekey zlib-level=9 -o ${mac.dmg.file}"/>
        </exec>
        <delete file="${target.osx}/tmp.dmg"/>
    </target>

    <!-- Debian package ========================================================================= -->
    <property name="debian.dir" value="${basedir}/build/debian"/>
    <property name="debian.release.dir" value="${release.dest.dir}/debian"/>
    <property name="debian.version" value="${version.major}.${version.minor}.${version.revision}"/>
    <property name="debian.work.dir" value="${work.dir}/debian/${dist.prefix}-${debian.version}"/>
    <tstamp>
        <format property="debian.builddate" pattern="EEE, dd MMM yyyy HH:mm:ss Z" locale="en"/>
    </tstamp>

    <target name="installer.debian" depends="dist.src" description="Creates a Debian package">
        <!-- Copy the sources to some place to work -->
        <copy todir="${debian.work.dir}"> 
            <fileset dir="${release.src.out.dir}">
                <include name="**/*" />
            </fileset>
        </copy>

        <!-- Copy build/debian into workdir -->
        <copy todir="${debian.work.dir}/debian">
            <fileset dir="${debian.dir}/" includes="**/*"/>
            <filterset>
                <filter token="builddate" value="${debian.builddate}"/>
                <filter token="version" value="${debian.version}"/>
            </filterset>
        </copy>

        <!-- Make the copied 'rules' executable -->
        <chmod file="${debian.work.dir}/debian/rules" perm="755" />

        <!-- Create the package -->
        <exec executable="dpkg-buildpackage" dir="${debian.work.dir}" failonerror="true">
            <!-- Allow building without root-privs -->
            <arg line="-rfakeroot"/>
            <!-- Don't sign source -->
            <arg line="-us"/>
            <!-- Don't sign changelog -->
            <arg line="-uc"/>
        </exec>

    <!-- Copy the resulting files into target -->
    <copy todir="${debian.release.dir}">
            <fileset dir="${debian.work.dir}/.." followsymlinks="false">
        <include name="**/*.deb" />
        <include name="**/*.changes" />
        <include name="**/*.dsc" />
        <include name="**/*.gz" />
            </fileset>
        </copy>
    </target>

</project>
