<?xml version="1.0" encoding="UTF-8"?>

<!--
    ** Copyright (c) 2004-2017, KNOPFLERFISH project
    ** All rights reserved.
    **
    ** Redistribution and use in source and binary forms, with or without
    ** modification, are permitted provided that the following conditions
    ** are met:
    **
    ** - Redistributions of source code must retain the above copyright notice,
    **   this list of conditions and the following disclaimer.
    **
    ** - Redistributions in binary form must reproduce the above copyright
    **   notice, this list of conditions and the following disclaimer in
    **   the documentation and/or other materials provided with the
    **   distribution.
    **
    ** - Neither the name of the KNOPFLERFISH project nor the names of its
    **   contributors may be used to endorse or promote products derived
    **   from this software without specific prior written permission.
    **
    ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    ** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    ** COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    ** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    ** OF THE POSSIBILITY OF SUCH DAMAGE.
  -->


<project name="knopflerfish" default="default">

  <!-- The default version of this build -->
  <tstamp>
    <format property="build.time"
            pattern="yyMMddHHmm"/>
    <format property="distrib_bintray.tstamp"
            pattern="yyyy-MM-dd'T'HH:mm"
            locale="en"/>
  </tstamp>
  <!-- The current Git state -->
  <exec executable="git" outputproperty="git.status">
    <arg value="status"/>
    <arg value="-s"/>
  </exec>
  <exec executable="git" outputproperty="git.tag" errorproperty="git.error">
    <arg value="describe"/>
    <arg value="--tags"/>
  </exec>
  <condition property="git.pristine">
    <equals arg1="${git.status}" arg2=""/>
  </condition>

  <!-- Set version for travis tag builds -->
  <property name="travis.suffix" value=""/>
  <condition property="version"
             value="${travis.tag}${travis.suffix}">
    <and>
      <isset property="travis.tag"/>
      <not>
        <equals arg1="${travis.tag}" arg2=""/>
      </not>
    </and>
  </condition>

  <!-- Release build assumed if we have a N.N.N version and pristine Git dir -->
  <condition property="release.build">
    <and>
      <isset property="version"/>
      <isset property="git.pristine"/>
      <matches pattern="^[0-9]+\.[0-9]+\.[0-9]+$" string="${version}"/>
      <equals arg1="${version}" arg2="${git.tag}"/>
    </and>
  </condition>

  <!-- OSGi do not allow . in qualifiers -->
  <loadresource property="git.tag.osgi">
    <propertyresource name="git.tag"/>
    <filterchain>
      <tokenfilter>
        <filetokenizer/>
        <replacestring from="." to="_"/>
      </tokenfilter>
    </filterchain>
  </loadresource>
  <!-- Use Git tag if directory is pristine, otherwise a time-stamp -->
  <condition property="version.suffix"
             value="kf_${git.tag.osgi}"
             else="${build.time}">
    <and>
      <isset property="git.pristine"/>
      <isset property="git.tag.osgi"/>
      <not>
        <equals arg1="${git.tag.osgi}" arg2=""/>
      </not>
    </and>
  </condition>

  <!-- Set default bundle suffix if not a release build -->
  <condition property="bundle.version.suffix"
             value=""
             else="${version.suffix}">
    <isset property="release.build"/>
  </condition>

  <property name="version.base"       value="6.1.0.snapshot"/>
  <condition property="version"
             value="${version.base}"
             else="${version.base}-${version.suffix}">
    <equals arg1="${version.suffix}" arg2=""/>
  </condition>

  <property name="kf.groupversion"	value="kf6" />

  <!-- The release date of this release -->
  <!-- *** Un-comment and set here on release branch. *** -->
  <!-- Defaults to current date, set here if another value is wanted. -->
  <!-- property name="release.date"     value="2017-02-08"/-->

  <!-- The previous official release version for this build -->
  <property name="version.previous"   value="6.1.0"/>

  <!-- The URL to the root of the git-repository -->
  <property name="git.repo.url"
            value="https://github.com/knopflerfish/knopflerfish.org"/>

  <!-- The git tag / branch that this build is made on. Used for viewing -->
  <!-- E.g., "trunk", "tags/2.3.2", "branches/kf_1_support". -->
  <property name="git.browse.url"    value="${git.repo.url}/tree/${git.tag}"/>

  <!-- The kind of build, used on the index page for the release. -->
  <property name="build.kind" value="release build"/>

  <!-- Root directory of the current knopflerfish source tree. -->
  <dirname  property="knopflerfish.dir"  file="${ant.file.knopflerfish}"/>

  <!-- The root directory to place distrib build stuff in. -->
  <property name="distrib.out.dir"
            location="out"/>
  <!-- A directory to place intermediate files for a distrib build in. -->
  <property name="distrib.tmp.dir"
            location="${distrib.out.dir}/tmp"/>
  <property name="distrib.sources.file"
            location="${distrib.out.dir}/sources_dirs.txt"/>
  <property name="distrib.exported.file"
            location="${distrib.out.dir}/exported_pkgs.txt"/>

  <!-- Where to place the results of a distrib build in. -->
  <property name="distrib.name"
	    value="distrib_${version}" />
  <property name="distrib.dir"
            location="${distrib.out.dir}/${distrib.name}"/>
  <property name="distrib.doc.dir"
            location="${distrib.dir}/docs"/>
  <property name="distrib.javadoc.dir"
            location="${distrib.doc.dir}/javadoc"/>
  <property name="distrib.jardocs.dir"
            location="${distrib.doc.dir}/jars"/>
  <property name="distrib.bundledoc.dir"
            location="${distrib.doc.dir}/bundledoc"/>
  <property name="distrib.mvn.repo.dir"
            location="${distrib.dir}/maven2"/>
  <property name="distrib.mvn.remote.repo.dir"
            location="${distrib.tmp.dir}/remote_maven2"/>
  <property name="distrib.mvn.remote.repo"
	    value="file:///${distrib.mvn.remote.repo.dir}"/>
  <property name="distrib.sdk.name"
	    value="knopflerfish_osgi_sdk_${version}" />
  <property name="distrib.sdk.file"
            location="${distrib.dir}/knopflerfish_osgi_sdk_${version}.jar"/>
  <property name="distrib.tiny.file"
            location="${distrib.dir}/knopflerfish_osgi_tiny_${version}.zip"/>
  <property name="distrib.osgi.dir"
            location="${distrib.dir}/osgi"/>
  <property name="distrib.junit_grunt.dir"
            location="${distrib.dir}/junit_grunt"/>

  <!-- Current date is default for the relesase.date property -->
  <tstamp>
    <format property="release.date"
            pattern="yyyy-MM-dd"/>
  </tstamp>

  <!-- The URL that this release will be available on after installation. -->
  <property name="base.url" value="http://www.knopflerfish.org/releases"/>

  <property name="jarunpacker.dir" location="tools/jarunpacker"/>

  <!-- Load support for KF bundle related ant tasks. -->
  <property name="ant.dir" location="ant"/>
  <import   file="${ant.dir}/bundletasks.xml"/>


  <target name="default"
         description="Builds entire platform except test bundles" >
    <ant dir="${jarunpacker.dir}" target="jar"/>
    <ant dir="osgi"               target="default" />
  </target>

  <target name="all"
          description="Builds entire platform including optional and test bundles" >
    <ant dir="${ant.dir}"          target="all"/>
    <ant dir="${jarunpacker.dir}"  target="jar"/>
    <ant dir="osgi"                target="all"/>
  </target>


  <target name="clean"
          description="Removes all generated files"
          depends="distrib_clean">
    <delete dir="repo"/>
    <ant    dir="osgi"               target="clean"/>
    <ant    dir="${jarunpacker.dir}" target="clean"/>
    <ant    dir="ant"                target="clean"/>
    <ant    dir="tools/android"      target="clean"/>
  </target>

  <target name="version"
          description="Echos the current Knopflerfish version to stdout.">
    <echo message="Version: ${version}"/>
  </target>


  <!-- Remove dirs created by the distrib targets. -->
  <target name="distrib_clean">
    <delete dir="${distrib.dir}"/>
    <delete dir="${distrib.out.dir}"/>
    <ant dir="ant" target="clean"/>
  </target>

  <!-- Create top-level directories used by the distrib target. -->
  <target name="distrib_mkdirs">
    <mkdir dir="${distrib.out.dir}"/>
    <mkdir dir="${distrib.dir}"/>
  </target>

  <target name="distrib_build_framework">
    <mkdir dir="${distrib.osgi.dir}"/>

    <!-- Note: This will build the bundle_tasks.jar -->
    <ant dir="${osgi.dir}/framework" inheritall="false">
      <property name="compact" value="true"/>
      <property name="jarfile_compact"
                location="${distrib.osgi.dir}/framework_compact.jar"/>
      <property name="version" value="${version}"/>
      <property name="bmfa.Knopflerfish-Version" value="${version}"/>
      <property name="root.out.dir" location="${distrib.tmp.dir}"/>
      <property name="generate.javadoc.data" value="true"/>
      <property name="exported.file" location="${distrib.exported.file}"/>
      <property name="sources.file"  location="${distrib.sources.file}"/>
      <property name="bundle.build.source.jar" value="true"/>
      <property name="source.jarfile"
                location="${distrib.osgi.dir}/framework-source.jar"/>
      <property name="bundle.build.javadoc.jar" value="true"/>
      <property name="javadoc.jarfile"
                location="${distrib.osgi.dir}/framework-javadoc.jar"/>
      <property name="version.suffix"  value="${bundle.version.suffix}"/>
    </ant>
    <ant dir="${osgi.dir}/framework" inheritall="false">
      <property name="jarfile"
                location="${distrib.osgi.dir}/framework.jar"/>
      <property name="jars.dir" location="${distrib.osgi.dir}/jars"/>
      <property name="version" value="${version}"/>
      <property name="bmfa.Knopflerfish-Version" value="${version}"/>
      <property name="root.out.dir" location="${distrib.tmp.dir}"/>
      <property name="generate.javadoc.data" value="true"/>
      <property name="exported.file" location="${distrib.exported.file}"/>
      <property name="sources.file"  location="${distrib.sources.file}"/>
      <property name="bundle.build.source.jar" value="true"/>
      <property name="source.jarfile"
                location="${distrib.osgi.dir}/framework-source.jar"/>
      <property name="bundle.build.javadoc.jar" value="true"/>
      <property name="javadoc.jarfile"
                location="${distrib.osgi.dir}/framework-javadoc.jar"/>
      <property name="version.suffix"  value="${bundle.version.suffix}"/>
    </ant>
  </target>

  <target name="distrib_build_bundles">
    <mkdir dir="${distrib.osgi.dir}/jars"/>

    <ant dir="${osgi.dir}/bundles"
         target="all"
         inheritall="false">
      <property name="bmfa.Knopflerfish-Version" value="${version}"/>
      <property name="jars.dir" location="${distrib.osgi.dir}/jars"/>
      <property name="root.out.dir"  location="${distrib.tmp.dir}"/>
      <property name="generate.javadoc.data" value="true"/>
      <property name="exported.file" location="${distrib.exported.file}"/>
      <property name="sources.file"  location="${distrib.sources.file}"/>
      <property name="bundle.build.source.jar"   value="true"/>
      <property name="bundle.build.javadoc.jar"  value="true"/>
      <property name="bundle.version.suffix"  value="${bundle.version.suffix}"/>
    </ant>
  </target>

  <target name="distrib_build_bundles_opt">
    <mkdir dir="${distrib.osgi.dir}/jars"/>

    <ant dir="${osgi.dir}/bundles_opt"
         target="all"
         inheritall="false">
      <property name="bmfa.Knopflerfish-Version" value="${version}"/>
      <property name="jars.dir" location="${distrib.osgi.dir}/jars"/>
      <property name="root.out.dir"  location="${distrib.tmp.dir}"/>
      <property name="generate.javadoc.data" value="true"/>
      <property name="exported.file" location="${distrib.exported.file}"/>
      <property name="sources.file"  location="${distrib.sources.file}"/>
      <property name="bundle.build.source.jar"   value="true"/>
      <property name="bundle.build.javadoc.jar"  value="true"/>
      <property name="bundle.version.suffix"  value="${bundle.version.suffix}"/>
    </ant>
  </target>

  <target name="distrib_build_bundles_examples">
    <mkdir dir="${distrib.osgi.dir}/example_jars"/>

    <ant dir="${osgi.dir}/bundles_examples"
         target="all"
         inheritall="false">
      <property name="bmfa.Knopflerfish-Version" value="${version}"/>
      <property name="ant.dir"       location="${ant.dir}"/>
      <property name="osgi.jars.dir" location="${distrib.osgi.dir}/jars"/>
      <property name="jars.dir" location="${distrib.osgi.dir}/example_jars"/>
      <property name="root.out.dir"  location="${distrib.tmp.dir}"/>
      <property name="generate.javadoc.data" value="false"/>
      <property name="exported.file" location="${distrib.exported.file}"/>
      <property name="sources.file"  location="${distrib.sources.file}"/>
      <property name="bundle.build.source.jar"   value="true"/>
      <property name="bundle.build.javadoc.jar"  value="true"/>
      <property name="bundle.version.suffix"  value="${bundle.version.suffix}"/>
    </ant>
  </target>

  <target name="distrib_build_bundles_test">
    <ant dir="${osgi.dir}/bundles_test"
         target="all"
         inheritall="false">
      <property name="bmfa.Knopflerfish-Version" value="${version}"/>
      <property name="ant.dir"       location="${ant.dir}"/>
      <property name="osgi.jars.dir" location="${distrib.osgi.dir}/jars"/>
      <property name="jars.dir"      location="${distrib.osgi.dir}/test_jars"/>
      <property name="root.out.dir"  location="${distrib.tmp.dir}"/>
    </ant>
  </target>

  <!-- Build tools and all bundles included in the KF SDK distribution. -->
  <!-- The resulting bundles are placed in ${distrib.osgi.dir}/jars -->
  <target name="distrib_build">
    <mkdir dir="${distrib.osgi.dir}"/>

    <ant dir="${jarunpacker.dir}"  target="jar">
      <property name="root.out.dir"  location="${distrib.tmp.dir}"/>
    </ant>
    <antcall target="distrib_build_framework"/>
    <antcall target="distrib_build_bundles"/>
    <antcall target="distrib_build_bundles_opt"/>
    <antcall target="distrib_build_bundles_test"/>
    <antcall target="distrib_build_bundles_examples"/>
  </target>

  <target name="distrib_xargs">
    <mkdir dir="${distrib.osgi.dir}"/>
    <delete dir="${distrib.osgi.dir}" includes="*.xargs"/>

    <ant dir="${osgi.dir}" target="xargs">
      <property name="xargs.out.dir"  location="${distrib.osgi.dir}"/>
      <property name="xargs.base.dir" location="${distrib.osgi.dir}"/>
      <property name="jars.dir"      location="${distrib.osgi.dir}/jars"/>
      <property name="gosg.jars"     value="file:jars/"/>
      <property name="base.url"      value="${base.url}"/>
      <property name="version"       value="${version}"/>
      <property name="root.out.dir"  location="${distrib.tmp.dir}"/>
    </ant>
    <copy file="${osgi.dir}/props.xargs" todir="${distrib.osgi.dir}"/>
  </target>

  <target name="distrib_repoindex">
    <mkdir dir="${distrib.osgi.dir}/jars"/>

    <ant dir="${osgi.dir}" target="repoindex">
      <property name="jars.dir"      location="${distrib.osgi.dir}/jars"/>
      <property name="version"       value="${version}"/>
    </ant>
  </target>

  <target name="distrib_javadoc" depends="bundle_tasks">
    <ant dir="${osgi.dir}" target="javadoc">
      <property name="javadoc.out.dir" location="${distrib.javadoc.dir}"/>
      <property name="exported.file"   location="${distrib.exported.file}"/>
      <property name="sources.file"    location="${distrib.sources.file}"/>
      <property name="version"         value="${version}"/>
      <property name="ant.dir"         location="${ant.dir}"/>
      <property name="framework.dir"   location="${osgi.dir}/framework"/>
      <property name="osgi.dir"        location="${osgi.dir}"/>
      <property name="root.out.dir"    location="${distrib.tmp.dir}"/>
    </ant>
  </target>

  <target name="distrib_jardocs" depends="bundle_tasks">
    <mkdir dir="${distrib.jardocs.dir}"/>
    <mkdir dir="${distrib.javadoc.dir}"/>

    <property name="bundle.list.header"
              value="Knopflerfish OSGi ${version} Bundles"/>

    <bundlehtml templateHTMLDir="${ant.dir}/html_template"
                outDir="${distrib.jardocs.dir}"
                listHeader="${bundle.list.header}"
                javadocRelPath="../javadoc"
                includeSourceFiles="false"
                includeSourceFileRepositoryLinks="true"
                rootDir="${knopflerfish.dir}"
                repositoryURL="${git.browse.url}/">
      <fileset dir="${distrib.osgi.dir}/jars">
        <include name="**/*.jar"/>
        <exclude name="**/*-source.jar"/>
        <exclude name="**/*-javadoc.jar"/>
      </fileset>
      <!-- Include framework.jar to get a provider of org.osgi.framework. -->
      <fileset dir="${distrib.osgi.dir}">
        <include name="framework.jar"/>
      </fileset>
    </bundlehtml>
  </target>

  <target name="distrib_mvn_repo" depends="bundle_tasks">
    <mkdir dir="${distrib.mvn.repo.dir}/org/knopflerfish"/>

    <bundleMvnAnt templateAntFile="${ant.dir}/ant_templates/mvn_repo.xml"
                  repoDir="${distrib.mvn.repo.dir}"
		  mergeRepoDir="${distrib.mvn.remote.repo.dir}"
                  outDir="${distrib.tmp.dir}"
                  buildFile="toMvn.xml"
                  dependencyManagementFile="KF-${version}_dependencyManagement.xml"
		  groupVersion="${kf.groupversion}"
                  version="${version}"
                  settingsFile="${knopflerfish.dir}/tools/mvnrepo/settings.xml">
      <!-- Include framework.jar -->
      <fileset dir="${distrib.osgi.dir}">
        <include name="framework.jar"/>
      </fileset>
      <!-- and all bunldes in the jars-directory -->
      <fileset dir="${distrib.osgi.dir}/jars">
        <include name="**/*.jar"/>
        <exclude name="**/*-source.jar"/>
        <exclude name="**/*-javadoc.jar"/>
      </fileset>
    </bundleMvnAnt>

    <!-- Time to execute the generated build file. -->
    <ant dir="${distrib.tmp.dir}" antfile="toMvn.xml"/>
  </target>

  <target name="distrib_bundle_doc" depends="bundle_tasks">
    <mkdir dir="${distrib.bundledoc.dir}"/>

    <property name="bundledoc.list.header"
              value="Knopflerfish OSGi ${version}"/>

    <!-- Get documentation from the bundles -->
    <ant dir="${osgi.dir}/framework"      target="framework_doc">
      <property name="jarfile"      location="${distrib.osgi.dir}/framework.jar"/>
      <property name="docs.dir"     location="${distrib.bundledoc.dir}"/>
      <property name="root.out.dir" location="${distrib.tmp.dir}"/>
    </ant>
    <ant dir="${osgi.dir}/bundles"      target="bundle_doc">
      <property name="jars.dir"     location="${distrib.osgi.dir}/jars"/>
      <property name="docs.dir"     location="${distrib.bundledoc.dir}"/>
      <property name="root.out.dir" location="${distrib.tmp.dir}"/>
    </ant>
    <ant dir="${osgi.dir}/bundles_opt"  target="bundle_doc">
      <property name="jars.dir"     location="${distrib.osgi.dir}/jars"/>
      <property name="docs.dir"     location="${distrib.bundledoc.dir}"/>
      <property name="root.out.dir" location="${distrib.tmp.dir}"/>
    </ant>

    <!-- Generate user doc navigation page -->
    <bundle_user_doc_navigate
       outdir="${distrib.bundledoc.dir}"
       docdir="${distrib.bundledoc.dir}"
       tofile="bundledoc_list.html"
       template="htdocs/html_templates/bundle_doc/bundledoc_list.html"
       title="${bundledoc.list.header}"
       />

    <!-- Add navigation and header pages from the templates -->
    <copy todir="${distrib.bundledoc.dir}">
      <fileset dir="htdocs/html_templates/bundle_doc">
        <include name="*.html" />
        <exclude name="bundledoc_list.html"/>
      </fileset>
      <filterset begintoken="@" endtoken="@">
        <filter token="VERSION"      value="${version}"/>
      </filterset>
    </copy>
  </target>

  <!-- General distribution documentation, mostly html -->
  <target name="distrib_docs" depends="bundle_tasks, util_tasks">
    <mkdir dir="${distrib.doc.dir}"/>

    <property name="htdocs.out"     location="${distrib.doc.dir}"/>

    <property name="htdocs.dir"     location="htdocs"/>
    <property name="htdocs.ht.dir"  location="${htdocs.dir}/html_templates"/>
    <property name="htdocs.hs.dir"  location="${htdocs.dir}/html_src"/>

    <property name="template.html"  location="${htdocs.ht.dir}/template.html"/>
    <property name="formatted.html" location="${htdocs.ht.dir}/formatted.html"/>
    <property name="license.html"   location="${htdocs.hs.dir}/license.html"/>

    <tstamp>
      <format property="distrib_docs.tstamp.year" pattern="yyyy" locale="en"/>
    </tstamp>

    <copy todir="${distrib.dir}">
      <fileset dir=".">
        <include name="README.txt"/>
        <include name="NOTICE.txt"/>
        <include name="LICENSE.txt"/>
        <include name="changelog.txt"/>
      </fileset>
      <filterset begintoken="@" endtoken="@">
        <filter token="YEAR"         value="${distrib_docs.tstamp.year}"/>
        <filter token="VERSION"      value="${version}"/>
        <filter token="DOWNLOAD_URL" value="${base.url}/${version}"/>
      </filterset>
    </copy>

    <copy todir="${distrib.doc.dir}">
      <fileset dir="${htdocs.dir}">
        <include name="css/knopflerfish.css"/>
        <include name="css/kf_man.css"/>
        <include name="images/**"/>
      </fileset>
    </copy>

    <mkdir dir="${distrib.doc.dir}/examples/cpa"/>
    <copy todir="${distrib.doc.dir}/examples/cpa">
      <fileset dir="${osgi.dir}/bundles_examples/cpa">
        <include name="**/*"/>
      </fileset>
    </copy>

    <ant dir="${htdocs.dir}" target="all"/>

  </target>

  <target name="distrib_docs_tutorial">
    <mkdir dir="${distrib.doc.dir}/tutorials"/>
    <copy todir="${distrib.doc.dir}/tutorials">
      <fileset dir="docs">
        <include name="kf_osgi_tutorial/**"/>
        <exclude name="*.class"/>
      </fileset>
    </copy>
  </target>

  <target name="distrib_kf_tests" depends="bundle_tasks">
    <delete dir="${distrib.junit_grunt.dir}"/>
    <!-- Ok to use "run-kf-tests-bare" here since the build is clean.-->
    <ant dir="osgi" target="run-kf-tests-bare">
      <property name="junit.out.dir"   location="${distrib.junit_grunt.dir}"/>
      <property name="run.test.secure" value="true"/>
      <property name="jars.dir"        location="${distrib.osgi.dir}/jars"/>
      <property name="test.jars.dir" location="${distrib.osgi.dir}/test_jars"/>
      <property name="xargs.out.dir"   location="${distrib.osgi.dir}"/>
      <property name="xargs.base.dir"  location="${distrib.osgi.dir}"/>
      <property name="run.test.dir"    location="${distrib.osgi.dir}"/>
      <property name="root.out.dir"    location="${distrib.tmp.dir}"/>
    </ant>
  </target>

  <target name="distrib_sdk">
    <echo message="building ${distrib.sdk.file}"/>

    <delete file="${distrib.sdk.file}"/>
    <jar destfile="${distrib.sdk.file}"
         compress="true" filesonly="true" duplicate="fail">
      <manifest>
        <attribute name="Main-class"
                   value="org.knopflerfish.tools.jarunpacker.Main"/>
        <attribute name="jarunpacker-destdir"
                   value="knopflerfish_osgi_${version}"/>
        <attribute name="jarunpacker-opendir"
                   value="$${destdir}/osgi"/>
        <attribute name="jarunpacker-licensepath"
                   value="/LICENSE.txt"/>
        <attribute name="knopflerfish-version"
                   value="${version}"/>
      </manifest>

      <zipfileset dir="${distrib.dir}" prefix="">
        <include name="LICENSE.txt"/>
        <include name="NOTICE.txt"/>
        <include name="README.txt"/>
        <include name="release_notes.html"/>
        <include name="release_notes.md"/>
      </zipfileset>

      <zipfileset dir="${distrib.doc.dir}" prefix="docs">
        <include name="**/*"/>
        <exclude name="**/doc.properties"/>
      </zipfileset>

      <zipfileset dir="${distrib.osgi.dir}" prefix="osgi">
        <exclude name="**/*-source.jar"/>
        <exclude name="**/*-javadoc.jar"/>
        <exclude name="osgi/test_jars/openssl.config"/>
        <exclude name="osgi/test_jars/test.castore"/>
        <exclude name="osgi/test_jars/test.keystore"/>
        <exclude name="osgi/test_jars/testCA"/>
        <include name="**/*"/>
      </zipfileset>

      <zipfileset dir="." prefix="">
        <exclude name="**/*.jbf"/>
        <exclude name="**/out/**"/>
        <exclude name="**/*.class"/>

        <include name="ant/**"/>
        <exclude name="ant/classes/**"/>
        <exclude name="ant/lib/bindex.jar"/>

        <include name="tools/android/**"/>

        <!--<include name="build.xml"/>-->

        <include name="changelog.txt"/>

        <include name="osgi/*.xargs.in"/>
        <include name="osgi/kf2"/>
        <include name="osgi/annotations/**"/>
        <include name="osgi/ee/**"/>
        <include name="osgi/build.xml"/>

        <include name="osgi/framework/**"/>

        <include name="osgi/bundles/build.xml"/>

        <include name="osgi/bundles/cm/cm/**"/>
        <include name="osgi/bundles/cm/cm_cmd/**"/>

        <include name="osgi/bundles/log/**"/>
        <include name="osgi/bundles/http/**"/>
        <include name="osgi/bundles/sslj2sp/**"/>
        <include name="osgi/bundles/device/build.xml"/>
        <include name="osgi/bundles/device/device/**"/>
        <include name="osgi/bundles/device/basicdriverlocator/**"/>
        <include name="osgi/bundles/useradmin/**"/>

        <include name="osgi/bundles/jsdk/**"/>

        <include name="osgi/bundles/threadio/**"/>
        <include name="osgi/bundles/command/**"/>

        <include name="osgi/bundles/console/**"/>
        <include name="osgi/bundles/frameworkcommands/**"/>
        <include name="osgi/bundles/logcommands/**"/>
        <include name="osgi/bundles/util/**"/>
        <include name="osgi/bundles/xml/build.xml"/>
        <include name="osgi/bundles/xml/xml/**"/>
        <include name="osgi/bundles/xml/crimson/**"/>
        <include name="osgi/bundles/xml/kxml/**"/>
        <include name="osgi/bundles/xml/xerces/**"/>
        <include name="osgi/bundles/xml/xalan/**"/>
        <include name="osgi/bundles/desktop/**"/>
        <include name="osgi/bundles/wireadmin/**"/>
        <include name="osgi/bundles/position/**"/>
        <include name="osgi/bundles/measurement/**"/>
        <include name="osgi/bundles/metatype/**"/>
        <include name="osgi/bundles/repository/**"/>
        <include name="osgi/bundles/resolver/**"/>
        <include name="osgi/bundles/promise/**"/>

        <include name="osgi/bundles/component/**"/>
        <include name="osgi/bundles/scrcommands/**"/>
        <include name="osgi/bundles/event/**"/>
        <include name="osgi/bundles/io/**"/>

        <include name="osgi/bundles/prefs/**"/>
        <include name="osgi/bundles/upnp/**"/>
        <include name="osgi/bundles/provisioning/**"/>

        <include name="osgi/bundles/annotations/**"/>
        <include name="osgi/bundles/applicationadmin/**"/>
        <include name="osgi/bundles/blueprint/**"/>
        <include name="osgi/bundles/coordinator/**"/>
        <include name="osgi/bundles/deploymentadmin/**"/>
        <include name="osgi/bundles/dmt/**"/>
        <include name="osgi/bundles/foreignapplication/**"/>
        <include name="osgi/bundles/monitor/**"/>
        <include name="osgi/bundles/namespace/**"/>
        <include name="osgi/bundles/remoteserviceadmin/**"/>
        <include name="osgi/bundles/serviceloader/**"/>
        <include name="osgi/bundles/subsystem/**"/>

        <include name="osgi/bundles/dal/**"/>

        <include name="osgi/bundles/demos/**"/>

        <include name="osgi/bundles/trayicon/build.xml"/>
        <include name="osgi/bundles/trayicon/readme.txt"/>
        <include name="osgi/bundles/trayicon/trayicon/**"/>
        <include name="osgi/bundles/trayicon/trayicon_fw/**"/>
        <include name="osgi/bundles/remotefw/**"/>

        <include name="osgi/bundles_opt/build.xml"/>
        <include name="osgi/bundles_opt/httpclient_connector/**"/>
        <include name="osgi/bundles_opt/classpatcher/**"/>
        <include name="osgi/bundles_opt/commons-logging/**"/>
        <include name="osgi/bundles_opt/desktop_awt/**"/>
        <include name="osgi/bundles_opt/desktop_displayers/**"/>
        <include name="osgi/bundles_opt/dirdeployer/**"/>
        <include name="osgi/bundles_opt/jini/**"/>
        <include name="osgi/bundles_opt/junit/**"/>
        <include name="osgi/bundles_opt/serial/**"/>
        <include name="osgi/bundles_opt/soap/**"/>

        <include name="osgi/bundles_test/**"/>
        <exclude name="osgi/bundles_test/regression_tests/*/resources/*_test*.jar"/>

        <include name="osgi/bundles_examples/**"/>
      </zipfileset>

      <!-- Add required files excluded in the fileset above -->
      <zipfileset dir="." prefix="">
        <include name="osgi/bundles_test/regression_tests/framework_test/res1/**/*.class"/>
        <include name="osgi/bundles_test/regression_tests/framework_test/resources/subdir/**/*.class"/>
      </zipfileset>

      <zipfileset dir="${distrib.tmp.dir}/jarunpacker/classes">
        <include name="**"/>
      </zipfileset>

      <zipfileset dir="${jarunpacker.dir}/resources">
        <include name="**"/>
      </zipfileset>

      <zipfileset dir="${jarunpacker.dir}/">
        <include name="strings.properties"/>
      </zipfileset>
    </jar>
  </target>

  <target name="distrib_tiny">
    <echo message="building ${distrib.tiny.file}"/>

    <property name="distrib_tiny.prefix"
              value="knopflerfish_osgi_tiny_${version}"/>

    <delete file="${distrib.tiny.file}"/>
    <zip destfile="${distrib.tiny.file}" compress="true">
      <zipfileset dir="${distrib.dir}" prefix="${distrib_tiny.prefix}">
        <include name="LICENSE.txt"/>
        <include name="NOTICE.txt"/>
        <include name="README.txt"/>
        <include name="release_notes.html"/>
      </zipfileset>

      <zipfileset dir="${distrib.dir}" prefix="${distrib_tiny.prefix}">
        <include name="osgi/framework_compact.jar"/>
        <include name="osgi/remote-init.xargs"/>
        <include name="osgi/props.xargs"/>
      </zipfileset>

      <zipfileset dir="." prefix="${distrib_tiny.prefix}">
        <include name="osgi/kf2"/>
      </zipfileset>
    </zip>
  </target>

  <!-- Creates an HTML index page for a release build.-->
  <target name="distrib_index" depends="bundle_tasks">
    <tstamp>
      <format property="distrib_index.tstamp"
              pattern="yyyy-MM-dd HH:mm"
              locale="en"/>
    </tstamp>
    <tstamp>
      <format property="distrib_index.tstamp.year"
              pattern="yyyy"
              locale="en"/>
    </tstamp>

    <byteformatter file="${distrib.sdk.file}" unit="B"
                   property="distrib.sdk.file.length"/>
    <byteformatter file="${distrib.tiny.file}" unit="B"
                   property="distrib.tiny.file.length"/>

    <copy file="htdocs/html_src/release_page.html.in"
          tofile="${distrib.dir}/index.html">
    </copy>
    <replace file="${distrib.dir}/index.html">
      <replacefilter token="@VERSION@"
                     value="${version}"/>
      <replacefilter token="@DATE@"
                     value="${distrib_index.tstamp}"/>
      <replacefilter token="@YEAR@"
                     value="${distrib_index.tstamp.year}"/>
      <replacefilter token="@REPO_URL@"
                     value="${git.repo.url}"/>
      <replacefilter token="@TAG@"
                     value="${git.tag}"/>
      <replacefilter token="@BUILD_KIND@"
                     value="${build.kind}"/>
      <replacefilter token="@DISTRIB_SDK_FILE_LENGTH@"
                     value="${distrib.sdk.file.length}"/>
      <replacefilter token="@DISTRIB_TINY_FILE_LENGTH@"
                     value="${distrib.tiny.file.length}"/>
    </replace>
  </target>


  <target name="distrib"
          description="Builds all full Knopflerfish SDK distribution">
    <antcall target="distrib_clean"/>
    <antcall target="distrib_mkdirs"/>
    <antcall target="distrib_build"/>
    <antcall target="distrib_xargs"/>
    <antcall target="distrib_repoindex"/>
    <antcall target="distrib_javadoc"/>
    <antcall target="distrib_jardocs"/>
    <antcall target="distrib_bundle_doc"/>
    <antcall target="distrib_docs_tutorial"/>
    <antcall target="distrib_docs"/>
    <antcall target="distrib_kf_tests"/>
    <antcall target="distrib_sdk"/>
    <antcall target="distrib_tiny"/>
    <antcall target="distrib_index"/>
    <antcall target="distrib_mvn_repo"/>
    <echo message="Build result placed in ${distrib.dir} and repo"/>
  </target>


  <!-- Creates a Bintray configuration file and a matching archive of a release build.-->
  <target name="distrib_bintray" depends="distrib">
    <copy file="tools/bintray/template_bintray.cfg"
          tofile="${distrib.out.dir}/bintray.cfg">
    </copy>
    <replace file="${distrib.out.dir}/bintray.cfg">
      <replacefilter token="@VERSION@"
                     value="${version}"/>
      <replacefilter token="@DATE@"
                     value="${distrib_bintray.tstamp}"/>
      <replacefilter token="@REPO_URL@"
                     value="${git.repo.url}"/>
      <replacefilter token="@TAG@"
                     value="${git.tag}"/>
      <replacefilter token="@BUILD_KIND@"
                     value="${build.kind}"/>
    </replace>
    <copy file="tools/bintray/bin/travis_credentials.sh"
          tofile="${distrib.out.dir}/.travis_credentials.sh">
    </copy>
    <copy file="tools/bintray/bin/travis_upload.sh"
          tofile="${distrib.out.dir}/.travis_upload.sh">
    </copy>
    <replace file="${distrib.out.dir}/.travis_upload.sh">
      <replacefilter token="@VERSION@"
                     value="${version}"/>
      <replacefilter token="@DISTRIB_NAME@"
                     value="${distrib.name}"/>
      <replacefilter token="@SDK_NAME@"
                     value="${distrib.sdk.name}"/>
    </replace>
    <chmod perm="u+x">
      <fileset dir="${distrib.out.dir}">
	<include name=".travis_*.sh" />
      </fileset>
    </chmod>
  </target>


  <!--|
      | The program used to check for broken links is the W3C checklink program:
      | http://validator.w3.org/docs/checklink.html
      |
      | It must be locally installed as it checks the generated docs on the local
      | file system. Only tested on OS X so far.
      |
      | The program is perl based. Please note the special step that needs to be
      | taken to enable SSL/HTTPS support
      |
    -->
  <target name="docs.checklink"
          description="Checks that all links are valid in the generated html documents">
    <exec executable="checklink"
          output="${distrib.out.dir}/checklink.out">
      <arg value="-qr"/>
      <arg value="-X"/>
      <arg value=".*/docs/javadoc/index.html.*"/>
      <arg value="${distrib.doc.dir}/index.html"/>
    </exec>
  </target>
 
</project>
