<?xml version="1.0" encoding="UTF-8"?>
<project default="xar" name="interformat" xmlns:xdb="http://exist-db.org/ant">

  <!-- bind environment variables to 'env'//-->
  <property environment="env" />
  <property file="build.${env.BRANCH}.properties" />

  <!-- default values for building outside CI //-->
  <property name="project.abbrev" value="interformat-local" />
  <property name="project.name" value="http://bdn-edition.de/${project.abbrev}" />
  <property name="project.version" value="1.2.0" />
  <property name="project.title" value="BdN Intermediate Format (Local)" />
  <property name="project.processorversion" value="5.2.0" />

  <!-- generic properties //-->
  <property name="build.dir" value=".build" />
  <property name="test.dir" value="local" />

  <xmlproperty file="expath-pkg.xml.tmpl" />

  <property name="destfile" value="${build.dir}/${project.abbrev}-${project.version}.xar" />

  <target name="xar">
    <echo message="Creating expath-pkg.xml for ${project.abbrev}-${project.version}" />
    <copy file="expath-pkg.xml.tmpl" tofile="expath-pkg.xml" filtering="true" overwrite="true">
      <filterset>
        <filter token="project.version" value="${project.version}" />
        <filter token="project.title" value="${project.title}" />
        <filter token="project.abbrev" value="${project.abbrev}" />
        <filter token="project.name" value="${project.name}" />
        <filter token="project.processorversion" value="${project.processorversion}" />
      </filterset>
    </copy>
    <echo message="Creating ${project.abbrev}-${project.version}.xar" />
    <zip basedir="." destfile="${destfile}" excludes="${build.dir}/**, ${test.dir}/**, .hooks/**, node_modules/**" />
  </target>

  <target name="cleanup">
    <echo message="Deleting ${test.dir}" />
    <delete dir="${test.dir}" />
  </target>

  <target name="test" depends="xar">
    <mkdir dir="${build.dir}" />
    <!-- this path may be and is subject to change! -->
    <echo message="Downloading exist-db" />
    <get src="https://bintray.com/existdb/releases/download_file?file_path=exist-distribution-${project.processorversion}-unix.tar.bz2" dest="${build.dir}/exist-db.tar.bz2" skipexisting="true" />
    <untar src="${build.dir}/exist-db.tar.bz2" dest="${test.dir}" compression="bzip2" />
    <path id="exist_zipped_dir_name">
      <dirset dir="${test.dir}">
        <include name="exist*" />
      </dirset>
    </path>
    <property name="exist-dir" refid="exist_zipped_dir_name" />
    <echo message="Renaming ${exist-dir} to exist-db" />
    <move file="${exist-dir}" tofile="${test.dir}/exist-db" />

    <echo message="Downloading applets" />

    <get src="https://ci.de.dariah.eu/exist-repo/find.zip?abbrev=sade_assets&amp;processor=${project.processorversion}" dest="${test.dir}/eXist-db-${project.processorversion}/autodeploy/sade_assets-latest.xar" ignoreerrors="true" />
    <get src="https://ci.de.dariah.eu/exist-repo/find.zip?abbrev=cv&amp;processor=${project.processorversion}" dest="${test.dir}/eXist-db-${project.processorversion}/autodeploy/codeview-latest.xar" ignoreerrors="true" />
    <get src="https://ci.de.dariah.eu/exist-repo/find.zip?abbrev=fontane-lucene-exist-module&amp;processor=${project.processorversion}" dest="${test.dir}/eXist-db-${project.processorversion}/autodeploy/fontane-lucene-latest.xar" ignoreerrors="true" />

    <get src="http://exist-db.org/exist/apps/public-repo/find.zip?abbrev=markdown&amp;processor=${project.processorversion}" dest="${test.dir}/eXist-db-${project.processorversion}/autodeploy/markdown-latest.xar" ignoreerrors="true" />
    <get src="http://exist-db.org/exist/apps/public-repo/find.zip?abbrev=functx&amp;processor=${project.processorversion}" dest="${test.dir}/eXist-db-${project.processorversion}/autodeploy/functx-latest.xar" ignoreerrors="true" />

    <echo message="Moving  ${project.abbrev}-${project.version}.xar to autodeploy directory" />
    <copy file="${destfile}" todir="${test.dir}/exist-db/autodeploy" />
    <chmod
      dir="${test.dir}/exist-db/bin"
      perm="ugo+rx"
      includes="**/*.sh"
    />
  </target>

</project>