<?xml version="1.0" encoding="utf-8" ?>
 <!--
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 -->
<project name="etch-helloworld-example-java" basedir="." default="help" >
    <description>HelloWorld Example Java</description>
    <property name="Etch.basedir" location="${basedir}/../../.." />
    <import file="${Etch.basedir}/build-support/etch.includes.xml" />

    <!-- TODO better computation of etch.home, etch.version -->
    <property environment="env" />

    <!-- compute ETCH_HOME -->
    <property name="build.ETCH_HOME" location="${Etch.basedir}/target/Installers/dist" />
    <condition property="etch.home" value="${env.ETCH_HOME}" >
        <isset property="${env.ETCH_HOME}" />
    </condition>
    <condition property="etch.home" value="${build.ETCH_HOME}" >
        <not><isset property="etch.home" /></not>
    </condition>

    <property name="Etch.dependency.junit.jar"
        location="${default.junit.lib}/junit-4.3.1.jar" />

    <property name="Etch.buildSupportDirectory" location="${Etch.basedir}/build-support" />

    <!-- compute etch.version -->
    <condition property="etch.version" value="${Etch.version}" >
        <not><isset property="etch.version" /></not>
    </condition>

    <condition property="etch.longversion" value="${Etch.longversion}" >
        <not><isset property="etch.longversion" /></not>
    </condition>

    <!-- import etch task -->
    <taskdef
        onerror="report"
        resource="org/apache/etch/tools/ant/etch.xml"
        classpath="${etch.home}/lib/apache-etch-ant-plugin-${etch.longversion}.jar" />

    <!-- set properties -->
    <property name="src"               location="${basedir}/src" />
    <property name="example-base"      location="${basedir}/.." />
    <property name="main-src-java"     location="${src}/main" />
    <property name="test-src-java"     location="${src}/test" />
    <property name="target"            location="${basedir}/target" />
    <property name="dist-src"          location="${target}/dist-src" />
    <property name="generated-sources" location="${target}/generated-sources" />
    <property name="gen-src-java"      location="${generated-sources}/java" />
    <property name="bin"               location="${target}/bin" />
    <property name="classes"           location="${target}/classes" />
    <property name="classes-test"      location="${target}/classes-test" />
    <property name="test-results"      location="${target}/test-results" />
    <property name="dist"              location="${Etch.basedir}/target/Installers/dist" />

    <!-- initialize -->
    <target name="init" >
        <mkdir dir="${target}" />
        <mkdir dir="${dist-src}" />
        <mkdir dir="${generated-sources}" />
        <mkdir dir="${gen-src-java}" />
        <mkdir dir="${bin}" />
        <mkdir dir="${classes}" />
        <mkdir dir="${classes-test}" />
        <mkdir dir="${test-results}" />
    </target>

    <!-- build the server -->
    <target name="generate-sources" depends="init" >
        <!-- generate sources -->
        <echo>Generating Java Sources...</echo>
        <etch home="${etch.home}"
              file="${example-base}/etch/HelloWorld.etch"
              binding="java"
              outputDir="${gen-src-java}" />
    </target>

    <property name="etch-java-runtime-name"
        value="apache-etch-java-runtime-${etch.longversion}.jar" />

    <property name="Etch.dependency.java.runtime"
        location="${etch.home}/binding-java/lib/${etch-java-runtime-name}" />

    <target name="build-java" depends="generate-sources" >
        <!-- compile sources -->

        <javac target="1.5" destdir="${classes}" >
            <src path="${gen-src-java}" />
            <src path="${main-src-java}" />
            <classpath>
                <pathelement location="${Etch.dependency.java.runtime}" />
            </classpath>
        </javac>

        <javac target="1.5" destdir="${classes-test}" >
            <src path="${gen-src-java}" />
            <src path="${main-src-java}" />
            <src path="${test-src-java}" />
            <classpath>
                <pathelement location="${Etch.dependency.java.runtime}" />
                <pathelement location="${Etch.dependency.junit.jar}" />
            </classpath>
        </javac>

        <!-- Create Jar -->
        <jar jarfile="${bin}/helloworld.jar" >
            <manifest>
                <attribute name="Class-Path" value="${etch-java-runtime-name}" />
            </manifest>
            <fileset dir="${classes}" >
                <include name="**/*" />
            </fileset>
        </jar>

        <!-- copy in dependencies -->
        <copy todir="${bin}" file="${Etch.dependency.java.runtime}" />
    </target>


    <target name="build" depends="build-java" />

    <!-- run unit tests -->

    <target name="test-java" depends="build-java" unless="build.skip.tests">
        <!-- Run Unit Tests -->
        <junit printsummary="yes" haltonfailure="no" dir="${classes-test}"
            errorProperty="build.tests.fail" failureProperty="build.tests.fail" >
            <classpath>
                <pathelement location="${classes-test}" />
                <pathelement location="${Etch.dependency.java.runtime}" />
                <pathelement location="${Etch.dependency.junit.jar}" />
            </classpath>
            <formatter type="xml" />
            <batchtest fork="true" todir="${test-results}" >
                <fileset dir="${test-src-java}" >
                    <include name="**/Test*.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="test" depends="build,test-java,done" >
    </target>

    <target name="done" depends="build" >
        <echo>
     Build Java Complete!
     ======================
     To run java example, open a cmd window and run these commands:

     > cd java\target\bin
     > start java -cp helloworld.jar org.apache.etch.examples.helloworld.MainHelloWorldListener
     > start java -cp helloworld.jar org.apache.etch.examples.helloworld.MainHelloWorldClient

     You can mix and match the various clients and listeners.
        </echo>
    </target>

    <!-- build everything -->
    <target name="init-debug" >
        <property name="build.target" value="Debug" />
    </target>

    <target name="init-release" >
        <property name="build.target" value="Release" />
    </target>

   <target name="dist-src" >
       <copy file="dist-build.xml" tofile="${dist-src}/build.xml" overwrite="true" >
           <filterset refid="Etch.buildTokens" />
       </copy>

        <copy todir="${dist-src}/src/main" overwrite="true" >
            <fileset dir="${main-src-java}" >
                <exclude name="**/.svn/**" />
                <include name="**/*.java" />
            </fileset>
        </copy>

        <copy todir="${dist}/examples/helloworld/etch" overwrite="true" >
            <fileset dir="${example-base}/etch" >
                <exclude name="**/.svn/**" />
                <include name="**/*.etch" />
            </fileset>
        </copy>

        <copy todir="${dist}/examples/helloworld/java" overwrite="true" >
            <fileset dir="${dist-src}" />
        </copy>
    </target>

    <target name="debug" depends="dist-src,init-debug,test" />
    <target name="release" depends="dist-src,init-release,test" />

    <!-- clean -->
    <target name="clean" >
        <delete dir="${target}" quiet="true" />
    </target>
</project>
