<project name="sparkplug" default="jar" basedir="..">

    <property name="plug.dir" value="${basedir}/."/>
    <property name="plug.lib.dir" value="${plug.dir}/lib"/>

    <property name="classes.dir" value="${basedir}/build/classes"/>
    <property name="src.dir" value="${plug.dir}/src"/>
    <property name="target.dir" value="${plug.dir}/target"/>
    <property name="target.lib.dir" value="${plug.dir}/target/lib"/>
    <property name="jar.file" value="${target.dir}/lib/plugin-classes.jar"/>

    <property name="spark.home" value="${plug.dir}/../spark" />

    <path id="lib.classpath">
        <fileset dir="${plug.lib.dir}" includes="**/*.jar, **/*.zip"/>
        <fileset dir="${spark.home}/lib" includes="**/*.jar, **/*.zip"/>
        <fileset dir="${spark.home}/lib/windows" includes="**/*.jar" />
    </path>

    <target name="clean" description="Cleans all build related output">
        <delete file="${jar.file}"/>
        <delete dir="${classes.dir}"/>
        <delete dir="${target.dir}"/>
    </target>

    <target name="compile" description="Compiles plugin source">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}"
               destdir="${classes.dir}"
               classpathref="lib.classpath"
               source="1.4"
               debug="true"
               target="1.4"/>
        <copy todir="${classes.dir}">
		<fileset dir="${src.dir}" includes="**/*.png"/>
		<fileset dir="${src.dir}" includes="**/*.gif"/>
		<fileset dir="${src.dir}" includes="**/*.jpg"/>
		<fileset dir="${src.dir}" includes="**/*.jpeg"/>
        </copy>
    </target>

    <target name="jar" depends="clean,compile" description="Makes a plugin jar">
        <mkdir dir="${target.dir}" />
        <mkdir dir="${target.lib.dir}"/>

        <copy todir="${target.lib.dir}">
            <fileset file="${plug.lib.dir}/lib" includes="**/*"/>
        </copy>

        <copy todir="${target.dir}">
            <fileset file="${plug.dir}/plugin.xml"/>
        </copy>

        <jar basedir="${classes.dir}" file="${jar.file}" update="false"/>

        <zip zipfile="${plug.dir}/myplugin.jar" basedir="${target.dir}" />
    </target>

    <target name="run" depends="jar" description="Makes a plugin jar and starts Spark with that plugin">
        <copy todir="${basedir}/../spark/plugins"
            file="${plug.dir}/myplugin.jar" />

        <property name="sparklib" value="${basedir}/../spark/lib" />
        <java fork="true" classname="org.jivesoftware.Spark" dir="${basedir}/../spark/bin">
            <classpath>
                <fileset dir="${sparklib}">
                    <include name="**/*.jar" />
                    <include name="**/*.exe" />
                    <include name="**/*.dll" />
                </fileset>
                <pathelement location="${basedir}/../spark/resources" />
            </classpath>
            <jvmarg value="-Dappdir=${basedir}/../spark" />
        </java>
    </target>

</project>
