<!--
    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="distribution" 
         xmlns:ivy="antlib:org.apache.ivy.ant">
  <!-- a sample common ant build file, used for ivy multi-project tutorial
       feel free to copy and adapt it to your own needs
       Note that the only targets specific to ivy are:
       load-ivy
       resolve
       report
       ivy-new-version
       publish
       publish-local
       
       All other targets are usual ant based targets, which could have been written
       in a build not depending at all on ivy: 
       resolve constructs a lib directory based upon ivy dependencies, and then the lib dir 
       is used as in any classical ant build
  -->
  
  <property name="custom.dir" value="" />
  <property file="${distribution.dir}/build.properties"/>
  <property name="compile.target" value="1.6" />
  <property name="compile.debug" value="true" />
  <property name="compile.source" value="1.6" />
  <property name="compile.encoding" value="utf8" />
  <!-- ================================= 
       target: load-ivy         
       this target is not necessary if you put ivy.jar in your ant lib directory
       if you already have ivy 2.0 in your ant lib, you can simply remove this
       target
       ================================= -->
  

  <path id="lib.path.id">
    <fileset dir="${lib.dir}">
      <include name="*.jar"/>
    </fileset>
  </path>
  <path id="run.path.id">
    <path refid="lib.path.id" />
    <path location="${classes.dir}" />
  </path>
  
  <!-- setup ivy default configuration with some custom info -->
  <property name="ivy.local.default.root" value="${repository.dir}/local"/>
  <property name="ivy.shared.default.root" value="${repository.dir}/shared"/>

  <property name="ivy.cache.dir" value="${distribution.dir}/build/ivy2cache" />

  <property name="ivy.default.ivy.user.dir" value="${distribution.dir}/build/ivy2home" />

  <property name="ivy.default.ivy.user.dir" value="${distribution.dir}/build/ivy2home" />

  <!-- here is how we would have configured ivy if we had our own ivysettings file -->
  <!-- <ivy:settings file="${distribution.dir}/ivysettings.xml" /> -->

  <!-- ================================= 
       target: check-architecture
       ================================= -->
  <target name="check-architecture">
    <!-- check os -->
    <exec dir="." executable="uname" outputproperty="os">
    <arg line="-s"/>
    </exec>

    <!-- check target hardware -->
    <exec dir="." executable="uname" outputproperty="machine">
    <arg line="-m"/>
    </exec>

    <!-- select the corresponding Ivy configuration -->
    <property name="ivy.configurations" value="${os}.${machine}" />
    <echo message="os: ${os}, target architecture: ${machine}" />
  </target>

  <!-- ================================= 
       target: resolve              
       ================================= -->
  <target name="resolve" depends="clean-lib, check-architecture" description="--> resolve and retrieve dependencies with ivy">
    <mkdir dir="${lib.dir}"/> <!-- not usually necessary, ivy creates the directory IF there are dependencies -->
    
    <!-- the call to resolve is not mandatory, retrieve makes an implicit call if we don't -->
    <ivy:resolve file="${ivy.file}" conf="${ivy.configurations}"/>
    <ivy:retrieve pattern="${lib.dir}/[artifact]_[revision].[ext]" type="jar,sh" conf="${ivy.configurations}"/>
    <ivy:retrieve pattern="${lib.dir}/[artifact].[ext]" type="native" conf="${ivy.configurations}"/>
  </target>
  
  <!-- ================================= 
       target: report              
       ================================= -->
  <target name="report" depends="resolve" description="--> generates a report of dependencies">
    <ivy:report todir="${build.dir}"/>
  </target>
  
  <!-- ================================= 
       target: compile              
       ================================= -->
  <target name="compile" depends="resolve" description="--> compile the project">
    <mkdir dir="${classes.dir}" />
    <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="lib.path.id" debug="${compile.debug}" target="${compile.target}" source="${compile.source}" encoding="${compile.encoding}"/>
  </target>

  <available file="${tests.dir}" property="tests.dir.present"/>
  <!-- ================================= 
       target: compile-tests              
       ================================= -->
  <target name="compile-tests" depends="resolve" description="--> compile the tests for the project" if="tests.dir.present">
    <mkdir dir="${tests.classes.dir}" />
    <javac srcdir="${tests.dir}" destdir="${tests.classes.dir}" classpathref="lib.path.id" debug="${compile.debug}" target="${compile.target}" source="${compile.source}" encoding="${compile.encoding}">
      <classpath>
	<pathelement path="${distribution.dir}/externals/junit-4.10.jar" />
      </classpath>
    </javac>
  </target>

  <!-- ================================= 
       target: tests              
       ================================= -->
  <target name="test" depends="compile-tests" description="--> run the tests for the project" if="tests.dir.present">
    <mkdir dir="${tests.reports.dir}" />
    
    <junit printsummary="yes" haltonfailure="yes" fork="true" dir="${basedir}">
      <env key="LD_LIBRARY_PATH" path="${lib.dir}:${env.LD_LIBRARY_PATH}"/>
      <env key="DYLD_LIBRARY_PATH" path="${lib.dir}:${env.DYLD_LIBRARY_PATH}"/>
      <classpath>
	<pathelement path="${tests.classes.dir}" />
	<pathelement path="${distribution.dir}/externals/junit-4.10.jar" />
      </classpath>

      <classpath refid="lib.path.id" />
      
      <formatter type="plain"/>
      
      <batchtest fork="yes" todir="${tests.reports.dir}">
	<fileset dir="${tests.dir}">
	  <include name="**/*Test*.java"/>
	  <exclude name="**/Abstract*.java"/>
	</fileset>
      </batchtest>
    </junit>
  </target>

  <!-- ================================= 
       target: run
       ================================= -->
  <target name="run" depends="version, compile" description="--> compile and run the project">
    <java classpathref="run.path.id" classname="${main.class.name}"/>
  </target>

  <target name="ivy-new-version" unless="ivy.new.revision">
    <!-- default module version prefix value -->
    <property name="module.version.prefix" value="${module.version.target}-dev-b" />
    
    <!-- asks to ivy an available version number -->
    <ivy:info file="${ivy.file}" />
    <ivy:buildnumber 
	organisation="${ivy.organisation}" module="${ivy.module}" 
	revision="${module.version.prefix}" defaultBuildNumber="1" revSep=""/>
  </target>

  <target name="local-version">
    <tstamp>
      <format property="now" pattern="yyyyMMddHHmmss"/>
    </tstamp>
    <property name="ivy.new.revision" value="${module.version.target}-${now}"/>
  </target>
  
  <target name="version" depends="ivy-new-version">
    <!-- create version file in classpath for later inclusion in jar -->
    <mkdir dir="${classes.dir}"/>
    <echo message="version=${ivy.new.revision}" file="${classes.dir}/${ant.project.name}.properties" append="false" />

    <!-- load generated version properties file -->
    <property file="${classes.dir}/${ant.project.name}.properties" />
  </target>

  <!-- ================================= 
       target: jar              
       ================================= -->

  
  <target name="jar" depends="version, compile" description="--> make a jar file for this project">
    <jar destfile="${build.dir}/${ant.project.name}_${version}.jar">
      <fileset dir="${classes.dir}" />
      <fileset dir="${resources.dir}" erroronmissingdir="false" />
      <manifest>
	<attribute name="Built-By" value="${user.name}"/>
	<attribute name="Build-Version" value="${version}" />
      </manifest>
    </jar>
  </target>


  <!-- ================================= 
       target: update              
       ================================= -->
  <target name="update" depends="jar" description="--> make the jar file available to bip compiler">
    <copy file="${build.dir}/${ant.project.name}_${version}.jar"
          todir="${distribution.dir}/build/bipc-latest/lib/${custom.dir}"/>
  </target>

  <!-- ================================= 
       target: publish              
       ================================= -->
  <target name="publish" depends="clean-build, jar" description="--> publish this project in the ivy repository">
    <ivy:publish artifactspattern="${build.dir}/[artifact]_[revision].[ext]" 
		 resolver="shared"
		 pubrevision="${version}"
		 status="release"
		 />
    <echo message="project ${ant.project.name} released with version ${version}" />
  </target>

  <!-- ================================= 
       target: publish-local              
       ================================= -->
  <target name="publish-local" depends="clean-build, local-version, jar, check-architecture" description="--> publish this project in the local ivy repository">
    <ivy:publish artifactspattern="${build.dir}/[artifact]_[revision].[ext]" 
		 resolver="local"
		 pubrevision="${version}"
		 pubdate="${now}"
		 status="integration"
		 forcedeliver="true"
         conf="${ivy.configurations}"
		 />
    <echo message="project ${ant.project.name} published locally with version ${version}" />
  </target>

  <!-- ================================= 
       target: clean-local              
       ================================= -->
  <target name="clean-local"
	  description="--> cleans the local repository for the current module">
    <ivy:info file="${ivy.file}" />
    <delete dir="${ivy.local.default.root}/${ivy.organisation}/${ivy.module}"/>
  </target>

  <!-- ================================= 
       target: clean-lib              
       ================================= -->
  <target name="clean-lib" description="--> clean the project libraries directory (dependencies)">
    <delete includeemptydirs="true" dir="${lib.dir}"/>
  </target>

  <!-- ================================= 
       target: clean-build              
       ================================= -->
  <target name="clean-build" description="--> clean the project built files">
    <delete includeemptydirs="true" dir="${build.dir}"/>
  </target>

  <!-- ================================= 
       target: clean              
       ================================= -->
  <target name="clean" depends="clean-build, clean-lib" description="--> clean the project" />
</project>
