<?xml version="1.0" encoding="UTF-8"?>
<!--
   ** Copyright (c) 2003-2015, 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="bundletasks" basedir="." default="default">

  <dirname  property="ant.dir" file="${ant.file.bundletasks}"/>

  <property name="ant.lib"       location="${ant.dir}/lib"/>
  <property name="antclasses"    location="${ant.dir}/classes"/>
  <property name="osgi.dir"      location="${ant.dir}/../osgi"/>
  <property name="framework.dir" location="${osgi.dir}/framework"/>


  <!-- ASM bytecode manipulation and analysis framework -->
  <property name="asm.version" value="3.2"/>
  <property name="asm.jar"
	    location="${osgi.dir}/framework/libs/asm-${asm.version}.jar"/>
  <path id="asm.path">
    <pathelement location="${osgi.dir}/framework/libs/asm-${asm.version}.jar"/>
  </path>
  
  <property name="asm-all.version"	value="4.0"/>
  <property name="asm-all.jar"		value="asm-all-${asm-all.version}.jar" />
  <path id="asm-all.path">
    <pathelement location="${ant.lib}/${asm-all.jar}" />
  </path>

  <!-- Download and install repoindex -->

  <property name="bndtools.download.url"
            value="https://raw.githubusercontent.com/bndtools/bundle-hub/master/" />
  
  <property name="org.osgi.impl.bundle.repoindex.cli.jar"
            value="org.osgi.impl.bundle.repoindex.cli-0.0.4.jar"/>
  <property name="org.osgi.impl.bundle.repoindex.cli.download.url"
            value="${bndtools.download.url}/org.osgi.impl.bundle.repoindex.cli/"/>

  <property name="org.osgi.impl.bundle.repoindex.ant.jar"
            value="org.osgi.impl.bundle.repoindex.ant-0.0.1.jar"/>
  <property name="org.osgi.impl.bundle.repoindex.ant.download.url"
            value="${bndtools.download.url}/org.osgi.impl.bundle.repoindex.ant/" />

  <target name="install_repoindex">
    <get src="${org.osgi.impl.bundle.repoindex.cli.download.url}/${org.osgi.impl.bundle.repoindex.cli.jar}"
         dest="${ant.lib}/${org.osgi.impl.bundle.repoindex.cli.jar}"
         verbose="false"
         skipexisting="true"/>
    <get src="${org.osgi.impl.bundle.repoindex.ant.download.url}/${org.osgi.impl.bundle.repoindex.ant.jar}"
         dest="${ant.lib}/${org.osgi.impl.bundle.repoindex.ant.jar}"
         verbose="false" skipexisting="true"/>
  </target>

  <macrodef name="repoindex"
            description="Generates repository XML description.">
    <attribute name="name"
               description="The name of the repository to create."/>
    <attribute name="dir"
               description="The directory to index."/>
    <attribute name="stylesheet"
               default="${ant.dir}/xsl/repository2html.xsl"
               description="Path to xsl stylesheet."/>

    <sequential>
      <path id="repoindex.path.pre">
        <pathelement location="org.osgi.impl.bundle.repoindex.cli-N.N.N.jar"/>
        <pathelement location="repoindex_kf_all-N.N.N.jar"/>
      </path>
      <bundle_locator classPathRef="repoindex.path.pre"
                      newClassPathId="repoindex.path"
                      failOnMissingBundles="true">
        <fileset dir="${ant.dir}/lib">
          <include name="*.jar"/>
          <exclude name="*-source.jar"/>
          <exclude name="*-javadoc.jar"/>
        </fileset>
        <!-- Search the local repository first (where jars are placed) -->
        <fileset dir="${jars.dir}/repoindex_kf">
          <exclude name="**/*-source.jar"/>
          <exclude name="**/*-javadoc.jar"/>
          <include name="**/*.jar"/>
        </fileset>
        <!-- Search the local default repository second -->
        <fileset dir="${osgi.jars.dir}/repoindex_kf">
          <exclude name="**/*-source.jar"/>
          <exclude name="**/*-javadoc.jar"/>
          <include name="**/*.jar"/>
        </fileset>
        <!-- Search the default repository third -->
        <fileset dir="${osgi.dir}/jars/repoindex_kf">
          <exclude name="**/*-source.jar"/>
          <exclude name="**/*-javadoc.jar"/>
          <include name="**/*.jar"/>
        </fileset>
        <!-- Search the repository to index fourth -->
        <fileset dir="@{dir}">
          <exclude name="**/*-source.jar"/>
          <exclude name="**/*-javadoc.jar"/>
          <include name="**/*.jar"/>
        </fileset>
      </bundle_locator>

      <local name="repoindex.jars.line"/>
      <fileset id="repoindex.jars" dir="@{dir}">
        <include name="**/*.jar"/>
        <exclude name="**/*-source.jar"/>
        <exclude name="**/*-javadoc.jar"/>
      </fileset>
      <pathconvert refid="repoindex.jars" property="repoindex.jars.line" pathsep=" "/>
      <java classname="org.osgi.impl.bundle.bindex.cli.Index"
            fork="yes"
            dir="@{dir}">
        <classpath refid="repoindex.path"/>
        <arg value="--pretty"/>
        <arg value="-n"/>
        <arg value="@{name}"/>
        <arg line="${repoindex.jars.line}"/>
      </java>

      <copy file="@{stylesheet}"
            tofile="@{dir}/repository2html.xsl"/>
      <replace file="@{dir}/index.xml">
        <replacetoken><![CDATA[<?xml version='1.0' encoding='utf-8'?>]]></replacetoken>
        <replacevalue><![CDATA[<?xml version='1.0' encoding='utf-8'?>
        <?xml-stylesheet type='text/xsl' href='repository2html.xsl'?>]]></replacevalue>
      </replace>
    </sequential>
  </macrodef>

  <property name="proguard.version" value="5.2"/>
  <property name="proguard-anttask.jar"
            value="proguard-anttask-${proguard.version}.jar"/>
  <property name="proguard-base.jar"
            value="proguard-base-${proguard.version}.jar"/>
  <property name="maven.url" value="http://repo1.maven.org/maven2"/>
  <property name="proguard.url" value="${maven.url}/net/sf/proguard"/>
  <property name="proguard-anttask.url"
            value="${proguard.url}/proguard-anttask/${proguard.version}/${proguard-anttask.jar}"/>
  <property name="proguard-base.url"
            value="${proguard.url}/proguard-base/${proguard.version}/${proguard-base.jar}"/>

  <target name="install_proguard">
    <get src="${proguard-base.url}"
         dest="${ant.lib}/${proguard-base.jar}"
         verbose="false"
         skipexisting="true"/>
    <get src="${proguard-anttask.url}"
         dest="${ant.lib}/${proguard-anttask.jar}"
         verbose="false"
         skipexisting="true"/>
    <taskdef resource="proguard/ant/task.properties">
      <classpath>
        <pathelement location="${ant.lib}/${proguard-base.jar}"/>
        <pathelement location="${ant.lib}/${proguard-anttask.jar}"/>
      </classpath>
    </taskdef>
  </target>

  <property name="pegdown.version"	value="1.6.0"/>
  <property name="pegdown.jar"		value="pegdown-${pegdown.version}.jar" />
  <property name="pegdown.url"		value="http://repo1.maven.org/maven2/org/pegdown/pegdown/${pegdown.version}/${pegdown.jar}" />

  <property name="parboiled-core.version"	value="1.1.7"/>
  <property name="parboiled-core.jar"		value="parboiled-core-${parboiled-core.version}.jar" />
  <property name="parboiled-core.url"		value="http://repo1.maven.org/maven2/org/parboiled/parboiled-core/${parboiled-core.version}/${parboiled-core.jar}" />
  <property name="parboiled-java.version"	value="1.1.7"/>
  <property name="parboiled-java.jar"		value="parboiled-java-${parboiled-java.version}.jar" />
  <property name="parboiled-java.url"		value="http://repo1.maven.org/maven2/org/parboiled/parboiled-java/${parboiled-java.version}/${parboiled-java.jar}" />

  <property name="asm-all.url"		value="http://central.maven.org/maven2/org/ow2/asm/asm-all/${asm-all.version}/${asm-all.jar}" />

  <target name="install_pegdown">
    <get src="${pegdown.url}"
	 dest="${ant.lib}/${pegdown.jar}"
	 verbose="true"
	 skipexisting="true" />
    <get src="${parboiled-core.url}"
	 dest="${ant.lib}/${parboiled-core.jar}"
	 verbose="true"
	 skipexisting="true" />
    <get src="${parboiled-java.url}"
	 dest="${ant.lib}/${parboiled-java.jar}"
	 verbose="true"
	 skipexisting="true" />
    <get src="${asm-all.url}"
	 dest="${ant.lib}/${asm-all.jar}"
	 verbose="true"
	 skipexisting="true" />
  </target>
  
  <!-- Compile and pack the Knopflerfish bundle tasks.  -->
  <property name="bundle_tasks.jar" location="${ant.lib}/bundle_tasks.jar"/>
  <property name="kf_ant_util_tasks.jar" location="${ant.lib}/kf_ant_util_tasks.jar"/>

  <property name="osgi_annotation.jar" location="${osgi.dir}/annotations/osgi.annotation-6.0.1.jar"/>

  <path id="bundle_tasks.path">
    <pathelement location="${antclasses}"/>
    <pathelement location="${bundle_tasks.jar}"/>
    <pathelement location="${osgi_annotation.jar}"/>
    <path refid="asm.path"/>
  </path>

  <path id="util_tasks.path">
    <pathelement location="${antclasses}"/>
    <path refid="asm-all.path"/>
    <pathelement location="${ant.lib}/pegdown-1.6.0.jar"/>
    <pathelement location="${ant.lib}/parboiled-core-1.1.7.jar"/>
    <pathelement location="${ant.lib}/parboiled-java-1.1.7.jar"/>
    <path refid="asm-all.path"/>
  </path>

  <target name="chkBundleTaskSrc">
    <condition property="bundleTasksBuild.notRequired">
      <not>
        <and>
          <available type="dir"  file="${ant.dir}/src"/>
          <available type="dir"  file="${framework.dir}/src"/>
          <available type="file" file="${asm.jar}"/>
        </and>
      </not>
    </condition>
  </target>


  <!-- This check is needed to be able to avoid rebuilding -->
  <!-- when properties used in the manifest has changed.   -->
  <target name="chkBundleTasksBuild" unless="bundleTasksBuild.notRequired">
    <condition property="bundleTasksBuild.notRequired">
      <uptodate>
        <srcfiles dir="${ant.dir}/src"
                  includes="org/knopflerfish/ant/**"/>
        <srcfiles dir="${framework.dir}/src"
                  includes="org/osgi/framework/Version.java,
                            org/osgi/framework/Constants.java"/>
        <mapper type="merge" to="${bundle_tasks.jar}"/>
      </uptodate>
    </condition>
  </target>

  <target name="build_util_tasks"
	  depends="install_pegdown">
    <mkdir dir="${antclasses}"/>
    <javac destdir="${antclasses}"
           classpathref="util_tasks.path"
           includeAntRuntime="true"
           debug="on">
      <src path="${ant.dir}/src"/>
      <include name="org/knopflerfish/ant//taskdefs/util/**"/>
      <include name="org/knopflerfish/ant//taskdefs/bundle/FileUtil.java"/>
    </javac>

    <jar destfile="${kf_ant_util_tasks.jar}"
         compress="true"
         update="false"
         filesonly="true">
      <manifest>
        <attribute name="Implementation-Vendor" value="Knopflerfish"/>
        <attribute name="Implementation-Title"
                   value="Knopflerfish Bundle Tasks"/>
        <attribute name="Knopflerfish-Version" value="${version}"/>
      </manifest>
      <zipfileset dir="${ant.dir}/src"
		  includes="org/knopflerfish/ant/taskdefs/util/*.xml"/>
      <zipfileset dir="${antclasses}"
		  includes="org/knopflerfish/ant/taskdefs/util/**,
			    org/knopflerfish/ant/taskdefs/bundle/FileUtil.class" />
    </jar>
  </target>

  <target name="build_bundle_tasks"
          depends="chkBundleTaskSrc,chkBundleTasksBuild"
          unless="bundleTasksBuild.notRequired">
    <mkdir dir="${antclasses}"/>
    <javac destdir="${antclasses}"
           classpathref="bundle_tasks.path"
           includeAntRuntime="true"
           debug="on">
      <src path="${ant.dir}/src"/>
      <!-- org.osgi.framework.Version is used by BundleLocator. -->
      <src path="${framework.dir}/src"/>
      <include name="org/knopflerfish/ant/taskdefs/bundle/**"/>
      <include name="org/osgi/framework/Constants.java"/>
      <include name="org/osgi/framework/Version.java"/>
      <include name="org/osgi/framework/VersionRange.java"/>
    </javac>

    
    <jar destfile="${bundle_tasks.jar}"
         compress="true"
         update="false"
         filesonly="true">
      <manifest>
        <attribute name="Implementation-Vendor" value="Knopflerfish"/>
        <attribute name="Implementation-Title"
                   value="Knopflerfish Bundle Tasks"/>
        <attribute name="Knopflerfish-Version" value="${version}"/>
      </manifest>
      <zipfileset dir="${ant.dir}/src"
                  includes="**/antlib.xml"/>
      <zipfileset dir="${antclasses}"/>
      <zipfileset src="${asm.jar}" includes="org/**"/> 
    </jar>
  </target>

  <target name="build_ant_tasks"
	  depends="build_bundle_tasks, build_util_tasks" />

  <target name="define_bundle_tasks"
          description="Defines the Knopflerfish bundle tasks">
    <taskdef resource="org/knopflerfish/ant/taskdefs/bundle/antlib.xml">
      <classpath>
        <pathelement location="${bundle_tasks.jar}"/>
      </classpath>
    </taskdef>
  </target>

  <target name="define_util_tasks"
          description="Defines the Knopflerfish utl tasks">
    <taskdef resource="org/knopflerfish/ant/taskdefs/util/antlib.xml">
      <classpath>
        <pathelement location="${kf_ant_util_tasks.jar}"/>
	<pathelement location="${ant.lib}/pegdown-1.6.0.jar"/>
	<pathelement location="${ant.lib}/parboiled-core-1.1.7.jar"/>
	<pathelement location="${ant.lib}/parboiled-java-1.1.7.jar"/>
	<pathelement location="${ant.lib}/asm-all-4.0.jar"/>
      </classpath>
    </taskdef>
  </target>
  
  <target name="bundle_tasks"
          description="Builds and defines the Knopflerfish bundle tasks"
          depends="build_bundle_tasks,
                   define_bundle_tasks">
  </target>

  <target name="util_tasks"
          description="Builds and defines the Knopflerfish util tasks"
          depends="build_util_tasks,
                   define_util_tasks">
  </target>

  <target name="default" depends="bundle_tasks"/>

</project>
