<?xml version="1.0" encoding="UTF-8"?>

<project default="jar" xmlns:j="jelly:core" xmlns:maven="jelly:maven" xmlns:deploy="deploy" xmlns:ant="jelly:ant">
    <j:set var="dist.name" value="${pom.artifactId}-${pom.currentVersion}"/>
    <j:set var="dist.dir" value="target/dist"/>

    <goal name="setclasspath">
        <path id="test.classpath">
            <path refid="maven.dependency.classpath"/>
        </path>
    </goal>


    <preGoal name="java:compile">
        <copy file="config/sandesha.properties" todir="${maven.build.dir}/classes"/>
        <copy file="config/log4j.properties" todir="${maven.build.dir}/classes"/>
        <copy file="config/commons-logging.properties" todir="${maven.build.dir}/classes"/>
        <copy file="config/WSRMPolicy.xml" todir="${maven.build.dir}/classes"/>
    </preGoal>

    <goal name="samples:compile" prereqs="java:compile">
        <ant:property name="dir.samples" value="samples"/>
        <ant:mkdir dir="${maven.build.dir}/sample-classes"/>
        <copy file="samples/client-config.wsdd" todir="${maven.build.dir}/sample-classes"/>
        <copy file="samples/server-config.wsdd" todir="${maven.build.dir}/sample-classes"/>
        <ant:javac srcdir="${dir.samples}" destdir="${maven.build.dir}/sample-classes" debug="on">
            <classpath>
                <pathelement location="${maven.build.dest}"/>
                <path refid="maven.dependency.classpath"/>
            </classpath>
        </ant:javac>
    </goal>

    <postGoal name="jar">
        <ant:mkdir dir="${basedir}/target/lib"/>
        <deploy:copy-deps todir="${basedir}/target/lib"/>
        <copy file="lib/xerces.jar" todir="${basedir}/target/lib"/>
    </postGoal>

    <preGoal name="interop.jar">
        <copy file="config/sandesha.properties" todir="${maven.build.dir}/classes"/>
        <copy file="config/client-config.wsdd" todir="${maven.build.dir}/classes"/>
        <copy file="config/server-config.wsdd" todir="${maven.build.dir}/classes"/>
        <copy file="config/log4j.properties" todir="${maven.build.dir}/classes"/>
        <copy file="config/commons-logging.properties" todir="${maven.build.dir}/classes"/>
        <copy file="config/WSRMPolicy.xml" todir="${maven.build.dir}/classes"/>
        <ant:javac srcdir="interop" destdir="${maven.build.dir}/classes" includes="**/*.java" debug="${maven.compile.debug}" optimize="${optimize}" deprecation="${maven.compile.deprecation}">
            <classpath>
                <pathelement location="${maven.build.dest}"/>
                <path refid="maven.dependency.classpath"/>
            </classpath>
        </ant:javac>
    </preGoal>

    <!-- to transforme htmls to xdocs -->
    <preGoal name="xdoc:jelly-transform">
        <attainGoal name="html2xdoc"/>
    </preGoal>

    <goal name="interop.jar" prereqs="java:compile">
        <ant:property name="sandesha.jar.name" value="Sandesha-interop.jar"/>
        <ant:mkdir dir="${maven.build.dir}/dist/jar"/>
        <ant:jar basedir="${maven.build.dir}/classes" destfile="${sandesha.jar.name}"/>
    </goal>

    <goal name="samples.jar" prereqs="samples:compile">
        <ant:property name="samples.jar.name" value="Sandesha-samples.jar"/>
        <ant:jar destfile="${maven.build.dir}/${samples.jar.name}">
            <fileset dir="${maven.build.dir}/sample-classes"/>
        </ant:jar>
    </goal>

    <!-- ================================================================ -->
    <!--- Create the Java Docs -->
    <!-- ================================================================ -->

    <goal name="javadoc-gen">
        <ant:javadoc packagenames="org.apache.sandesha.*" defaultexcludes="yes" destdir="target/apidocs" author="true" version="true" use="true" windowtitle="Sandesha API">
            <ant:sourcepath>
                <ant:pathelement location="src"/>
            </ant:sourcepath>
        </ant:javadoc>
    </goal>

    <goal name="clean">
        <delete dir="target"/>
        <ant:delete>
            <ant:fileset dir=".">
                <ant:include name="**/axis.log"/>
                <ant:include name="**/junit*.properties"/>
                <ant:include name="**/temp.properties"/>
            </ant:fileset>
        </ant:delete>
    </goal>

    <!-- ================================================================ -->
    <!--- Create the Binary Distribution -->
    <!-- ================================================================ -->

    <goal name="dist-bin" prereqs="clean,java:compile,javadoc-gen,jar">
        <property name="bin.dist.dir" value="target/dist-bin/Sandesha-${pom.currentVersion}-bin"/>
        <ant:mkdir dir="${bin.dist.dir}"/>
        <ant:mkdir dir="${bin.dist.dir}/docs"/>
        <ant:mkdir dir="${bin.dist.dir}/lib"/>
        <ant:mkdir dir="${bin.dist.dir}/samples"/>
        <ant:mkdir dir="${bin.dist.dir}/docs/api"/>

        <ant:copy todir="${bin.dist.dir}/docs/api">
            <ant:fileset dir="target/apidocs/">
                <ant:include name="**"/>
            </ant:fileset>
        </ant:copy>

        <ant:copy todir="${bin.dist.dir}/docs">
            <ant:fileset dir="xdocs">
                <ant:include name="**"/>
            </ant:fileset>
        </ant:copy>

        <ant:copy todir="${bin.dist.dir}/lib" flatten="true">
            <ant:fileset dir="target/lib">
                <ant:include name="*.jar"/>
            </ant:fileset>
        </ant:copy>

        <ant:copy file="target/Sandesha-${pom.currentVersion}.jar" todir="${bin.dist.dir}"/>

        <ant:copy todir="${bin.dist.dir}/samples">
            <ant:fileset dir="samples">
                <ant:include name="*.xml"/>
                <ant:include name="*.wsdd"/>
                <ant:include name="*.properties"/>
            </ant:fileset>
        </ant:copy>

        <ant:copy todir="${bin.dist.dir}/samples">
            <ant:fileset dir="samples"/>
        </ant:copy>


        <attainGoal name="samples.jar"/>
        <copy file="${maven.build.dir}/Sandesha-samples.jar" todir="${bin.dist.dir}/samples"/>

        <ant:copy todir="${bin.dist.dir}/">
            <ant:fileset dir=".">
                <ant:include name="*.html"/>
                <ant:include name="*.txt"/>
            </ant:fileset>
        </ant:copy>

        <mkdir dir="target/dist"/>

        <ant:zip file="${dist.dir}/${dist.name}-bin.zip" basedir="target/dist-bin"/>
        <ant:tar tarfile="target/${dist.name}-bin.tar" basedir="target/dist-bin"/>
        <gzip src="target/${dist.name}-bin.tar" zipfile="${dist.dir}/${dist.name}-bin.tar.gz"/>
        <ant:delete dir="target/dist-bin"/>
        <ant:delete file="target/${dist.name}-bin.tar"/>
    </goal>


    <!-- ================================================================ -->
    <!--- Create the Source Distribution -->
    <!-- ================================================================ -->

    <goal name="dist-src">
        <mkdir dir="target/dist"/>
        <ant:zip file="${dist.dir}/${dist.name}-src.zip">
            <ant:fileset dir=".">
                <ant:include name="**"/>
                <ant:exclude name="**/target/**"/>
                <ant:exclude name="**/.cvs/**"/>
                <ant:exclude name="**/bin/**"/>
                <ant:exclude name=".*"/>
                <ant:exclude name="**/*.license"/>
            </ant:fileset>
        </ant:zip>
        <ant:tar tarfile="target/${dist.name}-src.tar" longfile="gnu">
            <ant:tarfileset dir=".">
                <ant:include name="**"/>
                <ant:exclude name="**/target/**"/>
                <ant:exclude name="**/.cvs/**"/>
                <ant:exclude name="**/bin/**"/>
                <ant:exclude name=".*"/>
                <ant:exclude name="**/*.license"/>
            </ant:tarfileset>
        </ant:tar>
        <gzip src="target/${dist.name}-src.tar" zipfile="${dist.dir}/${dist.name}-src.tar.gz"/>
        <ant:delete file="target/${dist.name}-src.tar"/>
    </goal>


</project>
