<!--
 -
 - 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>

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

    <condition property="suffix" value="${module.suffix}" else="">
        <isset property="module.suffix"/>
    </condition>

    <condition property="prefix" value="${module.prefix}" else="">
        <isset property="module.prefix"/>
    </condition>

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

    <property name="dist.module.name" value="${prefix}${module.name}${suffix}" />
      
    <property name="module.jar" location="${module.dir}/${dist.module.name}.jar"/>
    <property name="module.launcher"
              location="${module.dir}/${module.name}${launcher.sfx}"/>
    <property name="module.manifest"
              location="${module.dir}/${dist.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.root.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="${dist.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}/${bin.dir}"/>
        <copy todir="${destdir}/${bin.dir}" failonerror="false">
            <fileset dir="${src.bin}"/>
        </copy>        
        <chmod dir="${destdir}/${bin.dir}" perm="ugo+rx" includes="**/*"/>
    </target>

    <target name="build" depends="compile">
        <antcall target="dist-bin">
            <param name="destdir" value="${build.root.dir}"/>
        </antcall>
        <copy todir="${build.root.dir}/${etc.dir}" failonerror="false">
            <fileset dir="${module.etc}" excludes="**/*.conf"/>
        </copy>
        <copy todir="${build.root.dir}/${etc.dir}" failonerror="false">
            <fileset dir="${build.root.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">

        <copy todir="${module.classes}">
            <fileset dir="${resources.root.dir}">
                <include name="${metainf.dir}/**"/>
            </fileset>
        </copy>
        <copy todir="${module.classes}/${metainf.dir}">
             <fileset dir="${project.root}">
                <include name="LICENSE.txt"/>
                <include name="NOTICE.txt"/>
            </fileset>
        </copy>

        <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}"/>
            <attribute name="X-Compile-Source-JDK" value="${module.vm.version}"/>                        
        </manifest>
    </target>

    <target name="manifest-lib" unless="module.main">
        <manifest file="${module.manifest}">
            <attribute name="Class-Path" value="${module.manifest.classpath}"/>
            <attribute name="X-Compile-Source-JDK" value="${module.vm.version}"/>                                    
        </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>

    <condition property="module.vm.version" value="${module.vm.version}" else="1.4">
          <isset property="module.vm.version"/>
    </condition>    

    <condition property="dist.destlib.dir" value="${dist.dest.dir}/${lib.dir}" else="${dist.lib}">
         <isset property="dist.dest.dir"/>
    </condition>

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


    <condition property="dist.destdir" value="${dist.dest.dir}" else="${dist.dir}">
        <isset property="dist.dest.dir"/>
    </condition>

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

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

</project>