<!-- ============================================================ -->
<!--                    xargs file building                       -->
<!-- ============================================================ -->


<project name="xargs" basedir=".">

  <!-- Users of this macro should call the check_openssl target before
       calling this macro to ensure that the property openssl_exists
       is correctly set. -->
  <macrodef name="xargs"
	    description="Generates xargs-file from template.">
    <attribute name="template"
	       description="Location of the template xargs file to use."/>
    <attribute name="xargs"
	       description="Location (path) of the xargs to create."/>
    <attribute name="gosgJars"
	       description="Semicolon separated URL-path to replace $(GOSG_JARS) with. Also used to search for bundles."
	       default="file:jars/;file:test_jars/"/>
    <attribute name="repositoryXmlUrls"
	       description="Comma separated list of OSGi Repository xml urls to replace $(REPOSITORY_XML_URLS) with."
	       default="file:jars/index.xml,http://www.knopflerfish.org/releases/current/osgi/jars/index.xml"/>
    <attribute name="baseDir"
	       description="Base directory for relative file URLs in the gosgJars path."
	       default=""/>
    <attribute name="testJars"
	       description="Replacement value for $(TESTJARS); path to the root directory of the respository holding test bundles."
	       default="$${user.dir}/test_jars"/>
    <attribute name="xargsOsTemplate"
	       description="Location of file with replacement for $(OS_ARGS)."
	       default=""/>
    <attribute name="headless"
	       description="If set to 'true' then $(AWT) is replaced with the comment char otherwise it is replaced with an empty string."
	       default="false"/>
    <attribute name="outDir"
	       description="Directory to place intermediate build files in."
	       default="out"/>
    <element name="bundles"
             optional="true"
             description="Place holder element that specifies file-sets to search for bundles to use when expanding symbolic bundle names in the template file."/>
    <element name="replacefilters"
             optional="true"
             description="Additional replacefilter elements applied to the resulting xargs file."/>

    <sequential>
      <delete file="@{xargs}"/>

      <loadresource property="xargs.os.args" failonerror="false" quiet="true">
	<file file="@{xargsOsTemplate}"/>
      </loadresource>
      <!-- Set default value if file did not exist. -->
      <property name="xargs.os.args" value=""/>

      <mkdir dir="@{outDir}"/>
      <local name="bundleVersionFilter"/>
      <property name="bundleVersionFilter"
		location="@{outDir}/bundleVersionsXargs.props"/>
      <delete file="${bundleVersionFilter}"/>

      <bundle_locator replacefilterfile="${bundleVersionFilter}"
		      bundlePath="@{gosgJars}"
		      baseDir="@{baseDir}">
	<bundles/>
      </bundle_locator>

      <local name="do.cpa.test"/>
      <condition property="do.cpa.test" value="" else="#">
	<and>
          <isset property="openssl_exist"/>
          <!-- JRE 1.5 form Sun is broken -->
          <not><equals arg1="${ant.java.version}" arg2="1.5"/></not>
	</and>
      </condition>

      <local name="headless"/>
      <condition property="headless" value="#" else="">
          <equals arg1="@{headless}" arg2="true"/>
      </condition>

      <copy file="@{template}" tofile="@{xargs}"/>
      <replace file="@{xargs}"
               replacefilterfile="${bundleVersionFilter}">
	<replacefilter token="$(GOSG_JARS)"    value="@{gosgJars}"/>
    <replacefilter token="$(REPOSITORY_XML_URLS)"    value="@{repositoryXmlUrls}"/>
	<replacefilter token="$(TESTJARS)"     value="@{testJars}"/>
	<replacefilter token="$(VERSION)"      value="${version}"/>
	<replacefilter token="$(OS_ARGS)"      value="${xargs.os.args}"/>
	<replacefilter token="$(DO_CPA_TEST)"  value="${do.cpa.test}"/>
	<replacefilter token="$(AWT)"          value="${headless}"/>
	<replacefilters/>
      </replace>
    </sequential>
  </macrodef>

  <target name="check_openssl" unless="check_openssl.done">
    <exec executable="openssl"
          failifexecutionfails="false"
          resultproperty="openssl_return_code">
      <arg value="version" />
    </exec>
    <condition property="openssl_exist">
      <isset property="openssl_return_code"/>
    </condition>

    <property name="check_openssl.done" value="true"/>
  </target>

</project>
