<!--
 -
 - 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="module">

  <import file="common.xml"/>

  <map property="module" value="${basedir}" split="${path.separator}">
    <globmapper from="${project.root}${file.separator}*" to="*"/>
  </map>

  <map property="module.name" value="${module}">
    <filtermapper>
        <replacestring from="${file.separator}" to="-"/>
    </filtermapper>
  </map>	
	
  <echo message="Running ant for module : ${module}" level="info"/>

  <property file="${project.root}/build.deps"/>

  <property name="module.build"        location="${build}/${module}"/>
  <property name="module.classes"      location="${module.build}/classes"/>
  <property name="module.instrumented" location="${module.build}/classes-instrumented"/>
  <property name="module.precompiled"  location="${module.build}/src"/>
  <property name="module.api"          location="${module.build}/api"/>
  <property name="module.test.classes" location="${module.build}/test/classes"/>
  <property name="module.results"      location="${build.results}/${module}"/>
  <property name="module.failed"       location="${module.results}/FAILED"/>
  <property name="module.src"          location="src/main/java"/>
  <property name="module.test.src"     location="src/test/java"/>
  <property name="module.bin"          location="bin"/>
  <property name="module.etc"          location="etc"/>
    
  <property name="module.namever"              value="${project.name}-${module.name}-${project.version}"/>
  <property name="module.release.base"	       value="${basedir}/release"/>
  <property name="module.release"              value="${module.release.base}/${module.namever}"/>
  <property name="module.release.lib"	       value="${module.release}/lib"/>
  <property name="module.release.zip"          location="${module.release.base}/${module.namever}.zip"/>
  <property name="module.release.tar"          location="${module.release.base}/${module.namever}.tar"/>
  <property name="module.release.tgz"          location="${module.release.base}/${module.namever}.tar.gz"/>
  <property name="module.release.bz2"          location="${module.release}/${module.namever}.tar.bz2"/>

  <property name="module.qpid.jar"     location="${module.release.lib}/qpid-all.jar"/>
  <basename property="qpid.jar.name"  file="${module.qpid.jar}"/>

  <property name="module.coverage"     location="${module.build}/coverage"/>
  <property name="cobertura.datafile"  location="${module.instrumented}/cobetura.ser"/>
  
  <available property="module.test.src.exists" file="${module.test.src}"/>
  <available property="module.etc.exists" file="${module.etc}"/>
  <available property="module.bin.exists" file="${module.bin}"/>

  <indirect name="module.libs"         variable="${module.name}.libs"/>
  <indirect name="module.test.libs"    variable="${module.name}.test.libs"/>

  <path id="module.libs">
    <filelist dir="${project.root}" files="${module.libs}"/>
  </path>

  <path id="module.test.libs">
    <filelist dir="${project.root}" files="${module.test.libs}"/>
  </path>

  <path id="module.src.path">
    <pathelement location="${module.src}"/>
    <pathelement location="${module.precompiled}"/>
  </path>

  <condition property="module.jar"
             value="${build.plugins}/${project.name}-${module.name}-${project.version}.jar"
             else="${build.lib}/${project.name}-${module.name}-${project.version}.jar">
    <and>
      <isset property="module.plugin"/>
      <istrue value="${module.plugin}"/>
    </and>
  </condition>

  <property name="module.test.jar"
            location="${build.lib}/${project.name}-${module.name}-tests-${project.version}.jar"/>

  <!-- module.depends and module.test.depends are supplied by the importing file -->
  <property name="module.depends"       value=""/>
  <property name="module.test.depends"  value=""/>
  <property name="module.test.excludes" value=""/>

  <map property="module.depends.path" value="${module.depends}" join="${path.separator}">
    <globmapper from="*" to="${build}/*/classes"/>
  </map>

  <map property="module.test.depends.path" value="${module.test.depends}" join="${path.separator}">
    <globmapper from="*" to="${build}/*/classes"/>
  </map>

  <path id="module.class.path">
    <pathelement location="${module.classes}"/>
    <pathelement path="${module.depends.path}"/>
    <path refid="module.libs"/>
  </path>

  <path id="module.test.path">
    <pathelement path="${module.test.classes}"/>
    <path refid="module.class.path"/>
    <pathelement path="${module.test.depends.path}"/>
    <path refid="module.test.libs"/>
  </path>

  <property name="javac.deprecation" value="off"/>

  <target name="debug">
    <echo-prop name="basedir"/>
    <echo-prop name="project.root"/>
    <echo-prop name="module"/>
    <echo-prop name="module.libs"/>
    <echo-prop name="module.test.libs"/>
    <echo-prop name="module.name"/>
    <echo-prop name="module.jar"/>
    <echo-prop name="module.depends"/>
    <echo-prop name="module.depends.path"/>
    <echo-prop name="module.test.depends"/>
    <echo-prop name="module.test.depends.path"/>
    <echo-path refid="module.src.path"/>
    <echo-path refid="module.class.path"/>
    <echo-path refid="module.test.path"/>
  </target>

  <target name="prepare">
    <mkdir dir="${build.bin}"/>
    <mkdir dir="${build.etc}"/>
    <mkdir dir="${build.lib}"/>
    <mkdir dir="${build.results}"/>
    <mkdir dir="${build.data}"/>
    <mkdir dir="${build.plugins}"/>
    <mkdir dir="${module.classes}"/>
    <mkdir dir="${module.precompiled}"/>
    <mkdir dir="${module.api}"/>
    <mkdir dir="${module.test.classes}"/>
    <mkdir dir="${module.results}"/>
  </target>

  <target name="precompile"/>

  <target name="compile" depends="prepare,precompile" description="compile sources">

    <echo message="Targeting : ${java.target}" level="info"/>

    <javac source="${java.source}" target="${java.target}" 
           destdir="${module.classes}" debug="on"
           deprecation="${javac.deprecation}">
      <compilerarg line="${javac.compiler.args}"/>
      <src refid="module.src.path"/>
      <classpath refid="module.class.path"/>
    </javac>

    <!-- copy any non java src files into the build tree, e.g. log4j.properties -->
    <copy todir="${module.classes}" verbose="true">
      <fileset dir="${module.src}">
        <exclude name="**/*.java"/>
        <exclude name="**/package.html"/>
      </fileset>
    </copy>
  </target>

  <target name="precompile-tests" if="module.test.src.exists"/>

  <target name="compile-tests" depends="compile,precompile-tests" if="module.test.src.exists"
          description="compilte unit tests">
    <javac target="${java.target}" source="${java.source}"
           destdir="${module.test.classes}" debug="on"
           deprecation="${javac.deprecation}"
           srcdir="${module.test.src}">
      <classpath refid="module.test.path"/>
    </javac>

    <!-- copy any non java src files into the build tree, e.g. log4j.properties -->
    <copy todir="${module.test.classes}" verbose="true">
      <fileset dir="${module.test.src}">
        <exclude name="**/*.java"/>
        <exclude name="**/package.html"/>
      </fileset>
    </copy>
  </target>

  <condition property="config" value="${profile}.testprofile" else="default.testprofile">
    <and>
      <isset property="profile"/>
      <available file="${project.root}/${profile}.testprofile" type="file"/>
    </and>
  </condition>


  <property file="${project.root}/${config}"/>
  <property file="${project.root}/default.testprofile"/>

  <condition property="dontruntest" value="dontruntest" else="runtest">
         <contains substring="${module.name}" string="${exclude.modules}" />
  </condition>

  <target name="test" depends="build,compile-tests" if="module.test.src.exists"
         unless="${dontruntest}" description="execute unit tests">

    <delete file="${module.failed}"/>

    <echo message="Using config:${config}" level="info"/>
    <junit fork="${test.fork}" maxmemory="${test.mem}" reloading="no"
           haltonfailure="${haltonfailure}" haltonerror="${haltonerror}"
           failureproperty="test.failures" printsummary="on" timeout="600000" >

      <jvmarg value="${jvm.args}"/>

      <sysproperty key="amqj.logging.level" value="${amqj.logging.level}"/>
      <sysproperty key="amqj.protocol.logging.level" value="${amqj.protocol.logging.level}"/>
      <sysproperty key="log4j.debug" value="${log4j.debug}"/>
      <sysproperty key="root.logging.level" value="${root.logging.level}"/>
      <sysproperty key="log4j.configuration" value="${log4j.configuration}"/>
      <sysproperty key="java.naming.factory.initial" value="${java.naming.factory.initial}"/>
      <sysproperty key="java.naming.provider.url" value="${java.naming.provider.url}"/>
      <sysproperty key="broker" value="${broker}"/>
      <sysproperty key="broker.clean" value="${broker.clean}"/>
      <sysproperty key="broker.version" value="${broker.version}"/>
      <sysproperty key="broker.ready" value="${broker.ready}" />
      <sysproperty key="test.output" value="${module.results}"/>
      <syspropertyset>
        <propertyref prefix="test"/>
      </syspropertyset>
      <syspropertyset>
        <propertyref prefix="profile"/>
      </syspropertyset>
      <syspropertyset>
        <propertyref prefix="javax.net.ssl"/>
      </syspropertyset>
      <sysproperty key="max_prefetch" value ="${max_prefetch}"/>
      <sysproperty key="example.plugin.target" value="${project.root}/build/lib/plugins"/>
      <sysproperty key="QPID_EXAMPLE_HOME" value="${project.root}/build"/>
      <sysproperty key="QPID_HOME" value="${project.root}/build"/>
      <sysproperty key="QPID_WORK" value="${project.root}/build/work"/>

      <formatter type="plain"/>
      <formatter type="xml"/>

      <classpath refid="module.test.path"/>

      <batchtest fork="${test.fork}" todir="${module.results}">
        <fileset dir="${module.test.src}" excludes="${module.test.excludes}">
          <include name="**/${test}.java"/>
        </fileset>
      </batchtest>
    </junit>

    <antcall target="touch-failed"/>

    <condition property="failed">
      <and>
        <isfalse value="${test.failures.ignore}"/>
        <available file="${module.failed}"/>
      </and>
    </condition>

    <fail if="failed" message="TEST SUITE FAILED"/>

  </target>

  <target name="touch-failed" if="test.failures">
    <touch file="${module.failed}"/>
    <touch file="${build.failed}"/>
  </target>

  <target name="copy-bin" if="module.bin.exists" description="copy bin directory if it exists to build tree">
    <copy todir="${build.bin}" failonerror="false">
      <fileset dir="${module.bin}"/>
    </copy>
    <chmod dir="${build.bin}" perm="ugo+rx" includes="**/*"/>
  </target>

  <target name="copy-bin-release" if="module.bin.exists" description="copy dependencies into module release">
    <copy todir="${module.release}/bin" failonerror="true">
      <fileset dir="${module.bin}" />
    </copy>
    <chmod dir="${module.release}/bin" perm="ugo+rx" includes="**/*"/>
  </target>

  <target name="copy-etc" if="module.etc.exists" description="copy etc directory if it exists to build tree">
    <copy todir="${build.etc}" failonerror="false">
      <fileset dir="${module.etc}"/>
    </copy>
  </target>

  <target name="copy-etc-release" if="module.etc.exists" description="copy etc directory if it exists to build tree">
    <copy todir="${module.release}/etc" failonerror="false" flatten="true">
        <fileset dir="${module.etc}"/>
    </copy>
  </target>

  <target name="postbuild" description="run after a build"/>

  <target name="build" depends="jar,jar-tests,libs,copy-bin,copy-etc,postbuild" description="compile and copy resources into build tree"/>
  <target name="jar.manifest" depends="compile" if="module.manifest">
    <jar destfile="${module.jar}" basedir="${module.classes}" manifest="${module.manifest}"/>
  </target>

  <target name="jar.nomanifest" depends="compile" unless="module.manifest">
    <jar destfile="${module.jar}" basedir="${module.classes}">
       <metainf dir="${project.root}/resources/" />
    </jar>
  </target>

  <target name="jar" depends="jar.manifest,jar.nomanifest" description="create jar"/>

  <target name="jar-tests" depends="compile-tests" description="create unit test jar">
    <jar destfile="${module.test.jar}" basedir="${module.test.classes}"/>
  </target>

  <target name="libs" description="copy dependencies into build tree">
    <copy todir="${build.lib}" failonerror="false" flatten="true">
      <fileset dir="${project.root}" includes="${module.libs}"/>
    </copy>
  </target>

  <map property="module.depends.jars" value="${module.depends}" join=",">
    <globmapper from="*" to="${project.name}-*-${project.version}.jar"/>
  </map>

 
  <target name="libs-release" description="copy dependencies into module release">
    <!-- Copy the module dependencies -->
    <copy todir="${module.release}" failonerror="true">
      <fileset dir="${basedir}${file.separator}.." includes="${module.libs}"/>
    </copy>
    <!-- Copy the jar for this module -->
    <copy todir="${module.release}/lib" failonerror="true">
        <fileset file="${module.jar}"/>
        <fileset dir="${build.lib}" includes="${module.depends.jars}"/>
    </copy>
  </target>

  <target name="resources" description="copy resources into build tree">
    <copy todir="${build}" failonerror="false" flatten="true">
      <fileset dir="${basedir}${file.separator}.." includes="${resources}"/>
    </copy>
  </target>

  <target name="resources-release" description="copy resources into module release">
    <copy todir="${module.release}" failonerror="false" flatten="true">
      <fileset dir="${resources}" excludes="META-INF"/>
    </copy>
  </target>

  <uptodate property="doc.done" targetfile="${module.api}/index.html">
    <srcfiles dir="${module.src}" includes="**/*.java"/>
  </uptodate>

  <target name="doc" depends="prepare" unless="doc.done" description="generate api-doc">
    <javadoc destdir="${module.api}" sourcepathref="module.src.path"
             classpathref="module.class.path" packagenames="*"/>
  </target>
  
  <target name="release-bin-prepare">
    <mkdir dir="${module.release}"/>
    <available property="module.release.exists" file="${module.release}"/>
  </target>

  <target name="check-module-manifest">
    <uptodate property="module-manifest.done" targetfile="${qpid.jar}">
      <srcfiles dir="${build.lib}" includes="**/*.jar" excludes="**/${qpid.jar.name}"/>
    </uptodate>
  </target>

  <target name="module-manifest" depends="check-module-manifest" unless="module-manifest.done">
    <path id="class.path">
      <fileset dir="${module.release.lib}" >
        <include name="*.jar"/>
        <exclude name="${qpid.jar.name}"/>
      </fileset>
    </path>
    <pathconvert property="qpid.jar.classpath" pathsep=" " dirsep="/">
      <path refid="class.path"/>
      <globmapper from="${module.release.lib}${file.separator}*" to="*"/>
    </pathconvert>

    <jar destfile="${module.qpid.jar}">
      <manifest>
        <attribute name="Class-Path" value="${qpid.jar.classpath}"/>
      </manifest>
      <metainf dir="${project.root}/resources/"/>
    </jar>

    <touch file="${module.qpid.jar}"/>
  </target>


  <target name="zip-release"  depends="build-release-bin" description="build module release archive">
    <zip destfile="${module.release.zip}">
      <zipfileset dir="${module.release}" prefix="${module.namever}" filemode="755">
        <include name="bin/*"/>
	<exclude name="bin/*.txt"/>
      </zipfileset>

      <zipfileset dir="${module.release}" prefix="${module.namever}" filemode="644">
        <include name="bin/*.txt"/>
      </zipfileset>

      <zipfileset dir="${module.release}" prefix="${module.namever}" excludes="${module.release.excludes}" filemode="644" dirmode="755">
        <exclude name="bin/**"/>
	    <exclude name="**/*.class"/>          
      </zipfileset>
    </zip>
  </target>

  <target name="bundle" depends="jar">
   <taskdef resource="aQute/bnd/ant/taskdef.properties" classpath="${project.root}/lib/bnd-0.0.249.jar"/>
   <echo message="Bundling ${build}/lib/${module.namever}.jar with ${module.src}/${module}.bnd"/>
   <bnd 
      classpath="${build}/lib/${module.namever}.jar"
      eclipse="false" 
      failok="false" 
      exceptions="true"
      output="${build}/lib/${module.namever}-osgi.jar"
      files="${module.src}/${module}.bnd"/>
  </target>    

    <target name="tar-release" depends="zip-release" description="build release archive">
      <tar destfile="${module.release.tar}" longfile="gnu" >
        <zipfileset src="${module.release.zip}"/>
      </tar>
    </target>

    <target name="gzip-release" depends="tar-release" description="build release archive">
      <gzip src="${module.release.tar}" destfile="${module.release.tgz}"/>
    </target>

    <target name="bzip2-release" depends="tar-release" description="build release archive">
      <bzip2 src="${module.release.tar}" destfile="${module.release.bz2}"/>
    </target>

    <target name="doc-release" description="no-op override if a doc step is requried "/>


    <target name="build-release-bin" depends="release-bin-prepare,libs-release,copy-bin-release,
                                              copy-etc-release,doc-release,resources-release,release-bin-other,module-manifest"
            description="Task that includes all tasks required to create a module binary release"/>

    <!-- ,zip-release,gzip-release -->
  <target name="release-bin-tasks" depends="gzip-release" description="build all release archives except .bz2"/>

  <target name="release-bin-all-tasks" depends="bzip2-release" description="build all release archives"/>

  <!-- Dummy targets to no-op for most modules. Override if a module package is required -->
  <target name="release-bin-other" description="Override if there is tasks required for the module bin release to occur last"/>
  <target name="release-bin" description="Override and depend on release-bin-tasks to generate"/>
  <target name="release-bin-all" description="Override and depend on release-bin-tasks to generate"/>

  <target name="clean" description="remove build artifacts">
    <delete dir="${module.build}"/>
    <delete dir="${module.results}"/>
    <delete dir="${module.release.base}"/>
    <delete dir="${module.instrumented}"/>
  </target>

  <target name="instrument" depends="cobertura-init">
    <cobertura-instrument todir="${module.instrumented}"
                          datafile="${cobertura.datafile}">
      <fileset dir="${module.classes}">
        <include name="**/*.class"/>
      </fileset>
      <fileset dir="${module.test.classes}">
        <include name="**/*.class"/>
      </fileset>      
    </cobertura-instrument>
  </target>

  <target name="cover-test" depends="instrument">
    <mkdir dir="${build.coveragereport}" />
    <junit fork="yes" forkmode="once" maxmemory="${test.mem}" reloading="no"
           haltonfailure="${haltonfailure}" haltonerror="${haltonerror}"
           failureproperty="test.failures" printsummary="on" timeout="600000" >

      <sysproperty key="amqj.logging.level" value="${amqj.logging.level}"/>
      <sysproperty key="amqj.protocol.logging.level" value="${amqj.protocol.logging.level}"/>
      <sysproperty key="log4j.debug" value="${log4j.debug}"/>
      <sysproperty key="root.logging.level" value="${root.logging.level}"/>
      <sysproperty key="log4j.configuration" value="${log4j.configuration}"/>
      <sysproperty key="java.naming.factory.initial" value="${java.naming.factory.initial}"/>
      <sysproperty key="java.naming.provider.url" value="${java.naming.provider.url}"/>
      <sysproperty key="broker" value="${broker}"/>
      <sysproperty key="broker.clean" value="${broker.clean}"/>
      <sysproperty key="broker.version" value="${broker.version}"/>
      <sysproperty key="broker.ready" value="${broker.ready}" />
      <sysproperty key="test.excludes" value="${test.excludes}"/>
      <sysproperty key="test.excludesfile" value="${test.excludesfile}"/>
      <sysproperty key="test.output" value="${module.results}"/>
      <sysproperty key="max_prefetch" value ="${max_prefetch}"/>
      <sysproperty key="example.plugin.target" value="${project.root}/build/lib/plugins"/>
      <sysproperty key="QPID_EXAMPLE_HOME" value="${project.root}/build"/>
      <sysproperty key="QPID_HOME" value="${project.root}/build"/>

      <sysproperty key="net.sourceforge.cobertura.datafile"
		file="${cobertura.datafile}" />
      
      <formatter type="plain"/>
      <formatter type="xml"/>

      <classpath path="${module.instrumented}"/>
      <classpath>
        <fileset dir="${build}">
	  <include name="**/classes-instrumented/*.class"/>
	</fileset>
      </classpath>
      <classpath refid="module.test.path"/>
      <classpath refid="cobertura.classpath"/>      

      <batchtest todir="${module.results}">
        <fileset dir="${module.test.src}" excludes="${module.test.excludes}">
          <include name="**/${test}.java"/>
        </fileset>
      </batchtest>
    </junit>
  </target>

  <target name="coverage-report" depends="cobertura-init">
      <echo message="${cobertura.datafile}"/>
      <cobertura-report format="html"
                      destdir="${module.coverage}"
		      datafile="${cobertura.datafile}">
      <fileset dir="${module.src}" includes="**/*.java" />
    </cobertura-report>
  </target>
  
</project>
