<?xml version="1.0" encoding="UTF-8"?>
<project name="Kraken" default="release" basedir="..">
    <description>Kraken Build</description>

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

    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="${basedir}/build/lib/ant-contrib.jar"/>
        </classpath>
    </taskdef>


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

    <property name="version.major" value="1"/>
    <property name="version.minor" value="1"/>
    <property name="version.revision" value="3"/>
    <property name="version.extra" value="beta3"/> <!-- For 'beta' or 'alpha' -->
    <property name="dist.prefix" value="kraken"/>

    <property file="${basedir}/build/build.properties"/>
    <property name="openfire.home" value="${basedir}/../openfire"/>
    <property name="kraken.home" value="${basedir}"/>
    <property name="target.dir" value="${basedir}/target"/>
    <property name="release.dir" value="${target.dir}/release"/>
    <property name="work.dir" value="${basedir}/work"/>

    <!-- 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>

    <tstamp>
        <format property="builddate" pattern="MMM dd, yyyy"/>
     </tstamp>

    <property name="target.tmp.dir" value="${release.dir}/tmp/${dist.prefix}_${version.filename}"/>
    <property name="plugins.dir" value="${target.tmp.dir}/plugins"/>
    <property name="doc.dir" value="${target.tmp.dir}/documentation"/>
    <property name="database.dir" value="${target.tmp.dir}/database"/>


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

    <!-- init ================================================================================== -->
    <target name="init">
        <tstamp/>
        <!-- Check for Ant 1.6 -->
        <antcall target="-check-ant"/>
        <!-- Check for openfire -->
        <antcall target="-check-openfire"/>
        <condition property="windows">
	        <os family="windows" />
 	    </condition>
	    <condition property="unix">
	        <os family="unix" />
        </condition>
        <condition property="solaris">
            <os name="SunOS"/>
        </condition>
        <condition property="linux">
            <os name="Linux"/>
        </condition>
        <condition property="mac">
            <os name="Mac OS X"/>
        </condition>
    </target>

    <target name="installer" depends="jar" description="Releases Kraken">

        <ant antfile="build/build.xml" dir="${openfire.home}" inheritall="false" inheritrefs="false" target="installer">
            <property name="installer.app_short_name" value="kraken"/>
            <property name="dist.prefix" value="kraken"/>
            <property name="ant.version" value="${ant.version}" />
        </ant>

    </target>

    <target name="dist.src" depends="jar" description="Source Release of GKraken">

        <ant antfile="build/build.xml" dir="${openfire.home}" inheritall="false" inheritrefs="false" target="dist.src">
            <property name="installer.app_short_name" value="kraken"/>
            <property name="dist.prefix" value="kraken"/>
            <property name="ant.version" value="${ant.version}" />
        </ant>

    </target>

    <target name="jar" depends="init" description="Deploys Kraken">
        <!-- Copy Kraken to the openfire plugin directory -->
        <mkdir dir="${openfire.home}/src/plugins/kraken"/>

        <copy todir="${openfire.home}/src/plugins/kraken">
            <fileset dir="${kraken.home}" includes="**/*.java"/>
            <filterset>
                <filter token="DATE" value="${builddate}"/>
            </filterset>
        </copy>

        <copy todir="${openfire.home}/src/plugins/kraken">
            <fileset dir="${kraken.home}" includes="**/*" excludes="**/*.java,build" />
        </copy>

        <ant antfile="build/build.xml" dir="${openfire.home}" inheritall="false" inheritrefs="false" >
            <target name="openfire"/>
            <target name="plugin"/>
            <property name="ant.version" value="${ant.version}" />
            <property name="plugin" value="kraken"/>
        </ant>

        <!-- Start the copying -->
        <mkdir dir="${target.dir}"/>

        <!-- Copy the Kraken plugin to the plugin directory -->
        <copy todir="${plugins.dir}">
            <fileset dir="${openfire.home}/work/plugins" includes="kraken.jar"/>
        </copy>

    </target>

    <target name="release" depends="jar" description="Creates a release of Kraken">
        <echo>release: ${release.dir}</echo>
        <property name="release.basename" value="kraken" />
        <mkdir dir="${release.dir}/${release.basename}" />
        <copy todir="${release.dir}/${release.basename}">
            <fileset dir="${kraken.home}" includes="*.html" />
            <fileset dir="${plugins.dir}/.." includes="plugins/*.*" />
        </copy>
        <zip basedir="${release.dir}"
             file="${release.dir}/kraken_plugin_${version.filename}.zip"
             includes="${release.basename}/**/*.*" />
        <tar basedir="${release.dir}"
             destfile="${release.dir}/kraken_plugin_${version.filename}.tar.gz"
             includes="${release.basename}/**/*.*"
             compression="gzip" />
    </target>

    <!-- clean ================================================================================= -->
    <target name="clean" description="Cleans up all build-generated output">
        <!-- Delete the Kraken target directory -->
        <delete dir="${target.dir}"/>
        <!-- Delete the Kraken work directory -->
        <delete dir="${work.dir}"/>
        <!-- Delete the Kraken plugin in the openfire plugin directory -->
        <delete dir="${openfire.home}/src/plugins/kraken"/>
        <!-- Clean openfire -->
        <ant antfile="build/build.xml" dir="${openfire.home}" inheritall="false" inheritrefs="false" target="clean-plugins">
            <property name="ant.version" value="${ant.version}" />   
        </ant>
    </target>

    <!-- clean-all ============================================================================= -->
    <target name="clean-all" depends="clean" >
        <!-- Clean openfire target folder -->
        <ant antfile="build/build.xml" dir="${openfire.home}" inheritall="false" inheritrefs="false" target="clean">
            <property name="ant.version" value="${ant.version}" />
        </ant>
    </target>

    <target name="clean.dist.folders" >
        <!-- Removes the expanded release folders, makes for easier automation of builds -->
        <delete dir="${release.dir}/tmp" />
        <delete dir="${release.dir}/kraken" />
        
        <ant antfile="build/build.xml" dir="${openfire.home}" inheritall="false" inheritrefs="false" target="clean.dist.folders">
            <property name="installer.app_short_name" value="kraken"/>
            <property name="dist.prefix" value="kraken"/>
            <property name="ant.version" value="${ant.version}" />
        </ant>
    </target>    


    <!-- ======================================================================================= -->
    <!-- "PRIVATE" TARGETS                                                                       -->
    <!-- ======================================================================================= -->

    <!-- -check-ant ============================================================================ -->
    <target name="-check-ant">
        <condition property="ant.not.ok" value="true">
            <not>
                <or>
                    <contains string="${ant.version}" substring="1.6"/>
                    <contains string="${ant.version}" substring="1.7"/>
                    <contains string="${ant.version}" substring="1.8"/>
                </or>
            </not>
        </condition>
        <condition property="java.not.ok" value="true">
            <not>
                <or>
                    <contains string="${ant.java.version}" substring="1.5"/>
                    <contains string="${ant.java.version}" substring="1.6"/>
                </or>
            </not>
        </condition>
        <fail if="ant.not.ok" message="Must use Ant 1.6 or higher to build."/>
        <fail if="java.not.ok" message="Must use JDK 1.5.x or higher to build Openfire"/>
    </target>

    <!-- -check-openfire ====================================================================== -->
    <target name="-check-openfire">
        <available file="${openfire.home}/build/build.xml" property="openfire.not.ok"/>
        <fail unless="openfire.not.ok" message="Must point to openfire SVN project root directory (openfire.home)"/>
    </target>

</project>

