<!--
 -
 - Copyright (c) 2006 The Apache Software Foundation
 -
 - Licensed 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>

  <property name="module.dir" value="${build.dir}/${module}"/>
  <property name="module.classes" value="${module.dir}/${classes.dir}"/>
  <property name="module.precompiled" value="${module.dir}/${src.dir}"/>

  <map property="module.name" value="${module}">
    <filtermapper>
      <replacestring from="${file.separator}" to="-"/>
    </filtermapper>
  </map>


  <property name="module.jar" location="${module.dir}/${module.name}.jar"/>
  <property name="module.launcher"
            location="${module.dir}/${module.name}${launcher.sfx}"/>
  <property name="module.manifest"
            location="${module.dir}/${module.name}.mf"/>
  <property name="module.doc" location="${module.dir}/${doc.dir}"/>
  <property name="module.src" location="${src.dir}"/>
  <property name="module.lib" location="${lib.dir}"/>
  <property name="module.etc" location="${etc.dir}"/>

  <!-- module.depends is supplied by the importing file -->
  <pathconvert property="module.depends.dirs" pathSep=" ">
    <path>
      <dirset dir="${project.root}" includes="${module.depends}"/>
    </path>
    <globmapper from="${project.root}${file.separator}*" to="*"/>
  </pathconvert>

  <map property="module.depends.names" value="${module.depends.dirs}">
    <filtermapper>
      <replacestring from="${file.separator}" to="-"/>
    </filtermapper>
  </map>

  <map property="module.depends.libs" value="${module.depends.dirs}"
       setonempty="false">
    <regexpmapper from="(.*)" to="\1/${lib.dir}/\*\*\/*\.jar" handledirsep="/"/>
  </map>
  <map property="module.depends.classes" value="${module.depends.dirs}" join=":">
    <globmapper from="*" to="${build.dir}/*/${classes.dir}"/>
  </map>

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

  <path id="module.class.path">
    <pathelement location="${module.classes}"/>
    <fileset dir="${module.lib}" includes="**/*.jar"/>
    <pathelement path="${module.depends.classes}"/>
    <fileset dir="${project.root}" includes="${module.depends.libs}"/>
  </path>

  <pathconvert property="module.manifest.jars" pathsep=" " dirsep="/">
    <path>
      <fileset dir="${module.lib}" includes="**/*.jar"/>
    </path>
    <globmapper from="${module.lib}${file.separator}*" to="*"/>
  </pathconvert>

  <map property="module.manifest.deps" value="${module.depends.dirs}">
    <globmapper from="*" to="*${launcher.sfx}"/>
  </map>

  <property name="module.manifest.classpath"
            value="${module.name}.jar ${module.manifest.jars} ${module.manifest.deps}"/>

 <property name="module.test.reports" value="${module.dir}/reports/junit" />

  <target name="debug">
    <echo message="basedir = ${basedir}"/>
    <echo message = "project.root = ${project.root}"/>
    <echo message = "module = ${module}"/>
    <echo message = "module.name = ${module.name}"/>
    <echo message = "module.jar = ${module.jar}"/>
    <echo message = "module.depends = ${module.depends}"/>
    <echo message = "module.depends.libs = ${module.depends.libs}"/>
    <echo message = "module.depends.dirs = ${module.depends.dirs}"/>
    <echo message = "module.depends.classes = ${module.depends.classes}"/>

    <pathconvert property="module.class.path" refid="module.class.path"/>
    <echo message = "module.class.path = ${module.class.path}"/>
    <pathconvert property="pretty.module.class.path"
                 refid="module.class.path"
                 pathsep="${line.separator}|   |-- "/>
    <echo message = "pretty.module.class.path = ${pretty.module.class.path}"/>

    <pathconvert property="module.src.path" refid="module.src.path"/>
    <echo message= "module.src.path = ${module.src.path}"/>
    <pathconvert property="pretty.module.src.path"
                 refid="module.src.path"
                 pathsep="${line.separator}|   |-- "/>
    <echo message = "pretty.module.src.path = ${pretty.module.src.path}"/>
  </target>

  <target name="prepare">
    <mkdir dir="${module.classes}"/>
    <mkdir dir="${module.precompiled}"/>
    <mkdir dir="${module.doc}"/>
    <mkdir dir="${module.test.reports}"/>
  </target>

  <target name="precompile"/>

  <target name="compile" depends="prepare,precompile">
    <javac destdir="${module.classes}" target="${java.target}"
           source="${java.source}" debug="on">
      <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="dist-bin">
    <mkdir dir="${destdir}"/>
    <copy todir="${destdir}" failonerror="false">
      <fileset dir="${src.bin}"/>
    </copy>
    <chmod dir="${destdir}" perm="ugo+rx" includes="**/*"/>
  </target>

  <target name="build" depends="compile">
    <antcall target="dist-bin">
      <param name="destdir" value="${build.dir}/${bin.dir}"/>
    </antcall>
    <copy todir="${build.dir}/${etc.dir}" failonerror="false">
      <fileset dir="${module.etc}" excludes="**/*.conf"/>
    </copy>
    <copy todir="${build.dir}/${etc.dir}" failonerror="false">
      <fileset dir="${build.dir}/${etc.dir}" includes="**/*.dev"/>
      <mapper>
        <globmapper from="*.dev" to="*"/>
      </mapper>
    </copy>
  </target>

  <target name="test"/>

  <target name="testreport"/>

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

  <target name="doc" depends="prepare" unless="doc.done">
    <javadoc destdir="${module.doc}" sourcepathref="module.src.path"
             classpathref="module.class.path" packagenames="*"/>
  </target>

  <target name="jar" depends="build">
    <jar destfile="${module.jar}" basedir="${module.classes}"/>
  </target>

  <target name="manifest-main" if="module.main">
    <manifest file="${module.manifest}">
      <attribute name="Main-Class" value="${module.main}"/>
      <attribute name="Class-Path" value="${module.manifest.classpath}"/>
    </manifest>
  </target>

  <target name="manifest-lib" unless="module.main">
    <manifest file="${module.manifest}">
      <attribute name="Class-Path" value="${module.manifest.classpath}"/>
    </manifest>
  </target>

  <target name="launcher" depends="manifest-main,manifest-lib">
    <jar destfile="${module.launcher}" manifest="${module.manifest}"/>
  </target>

  <condition property="module.nodist">
    <and>
      <isset property="module.dist"/>
      <isfalse value="${module.dist}"/>
    </and>
  </condition>

  <target name="dist-lib" depends="jar,launcher,doc" unless="module.nodist">
    <copy todir="${dist.lib}" file="${module.jar}"/>
    <copy todir="${dist.lib}" file="${module.launcher}"/>
    <copy todir="${dist.lib}" failonerror="false">
      <fileset dir="${module.lib}"/>
    </copy>
    <copy todir="${dist.doc}/${module}">
      <fileset dir="${module.doc}"/>
    </copy>
  </target>

  <target name="dist" depends="dist-lib" unless="module.nodist">
    <antcall target="dist-bin">
      <param name="destdir" value="${dist.bin}"/>
    </antcall>
    <copy todir="${dist.etc}" failonerror="false">
      <fileset dir="${module.etc}" excludes="**/*.dev"/>
    </copy>
  </target>

  <target name="clean">
    <delete dir="${module.dir}"/>
  </target>

</project>
