<project name="dist-dir" basedir="." default="clean">

   <property name="output.dir" value="${project.build.directory}/${product.name}-${product.version}" />

   <import file="lib.xml" />

   <target name="clean">
      <delete dir="${output.dir}" />
   </target>

   <target name="unpack-server">
      <mkdir dir="${output.dir}"/>
      <move todir="${output.dir}">
         <fileset dir="${project.build.directory}">
            <include name="wildfly-*/**"/>
         </fileset>
         <cutdirsmapper dirs="1"/>
      </move>
   </target>

   <target name="copy-files">
      <!-- Copy the Infinispan Server resource files -->
      <copy todir="${output.dir}">
         <fileset dir="${basedir}/src/main/resources">
            <include name="JBossEULA.txt" />
            <include name="LICENSE.txt" />
            <include name="bin/**" />
            <include name="client/**" />
            <include name="docs/**" />
            <include name="domain/**" />
            <include name="rest/**" />
            <include name="standalone/configuration/**.properties" />
            <include name="README.txt" />
         </fileset>
         <fileset dir="${generated.configs}">
            <include name="standalone/configuration/**.xml" />
            <include name="domain/**" />
            <include name="docs/**" />
         </fileset>
      </copy>

      <mkdir dir="${output.dir}/domain/data/content"/>
      <mkdir dir="${output.dir}/standalone/lib/ext"/>
      <mkdir dir="${output.dir}/domain/tmp/auth"/>
      <mkdir dir="${output.dir}/standalone/tmp/auth"/>
      <!-- This directory is used by the SASL transparent auth mechanism to validate local user access -->
      <chmod perm="700" dir="${output.dir}/domain/tmp/auth"/>
      <chmod perm="700" dir="${output.dir}/standalone/tmp/auth"/>

      <!-- These files contain digested passwords, which should not be visible to external users -->
      <chmod perm="600" file="${output.dir}/domain/configuration/mgmt-users.properties" />
      <chmod perm="600" file="${output.dir}/standalone/configuration/mgmt-users.properties" />

      <!-- Copy the client libraries -->
      <mkdir dir="${output.dir}/client/hotrod/java" />

      <copy todir="${output.dir}/client/hotrod/java">
         <fileset file="${org.infinispan:infinispan-client-hotrod:jar}" />
         <fileset file="${org.infinispan:infinispan-commons:jar}" />
         <fileset file="${org.infinispan:infinispan-query-dsl:jar}" />
         <fileset file="${org.infinispan:infinispan-remote-query-client:jar}" />
         <fileset file="${org.infinispan.protostream:protostream:jar}" />
         <fileset file="${org.jboss.marshalling:jboss-marshalling-osgi:jar}" />
         <fileset file="${org.jboss.logging:jboss-logging:jar}" />
         <fileset file="${commons-pool:commons-pool:jar}" />
         <mapper type="flatten" />
      </copy>
      
      <!-- Make scripts executable -->
      <chmod perm="755">
         <fileset dir="${output.dir}">
            <include name="**/*.sh" />
            <include name="**/*.py" />
            <include name="**/*.rb" />
         </fileset>
      </chmod>
   </target>

   <!-- These modules are for Infinispan Server-specific functionality -->
   <target name="infinispan-server-modules">
      <module-def name="org.antlr.antlr-runtime">
         <maven-resource group="org.antlr" artifact="antlr-runtime" />
      </module-def>

      <module-def name="com.thoughtworks.xstream">
         <maven-resource group="com.thoughtworks.xstream" artifact="xstream" />
      </module-def>

      <module-def name="xpp3">
         <maven-resource group="xpp3" artifact="xpp3" />
      </module-def>

      <module-def name="org.infinispan.server.endpoint">
         <maven-resource group="org.infinispan.server" artifact="infinispan-server-endpoints" />
      </module-def>

      <module-def name="com.jboss.datagrid" />

      <module-def name="javax.activation.api">
         <maven-resource group="javax.activation" artifact="activation" />
      </module-def>

      <module-def name="org.jboss.as.clustering.infinispan">
         <maven-resource group="org.infinispan.server" artifact="infinispan-server-infinispan" />
      </module-def>

      <module-def name="org.jboss.as.clustering.jgroups">
         <maven-resource group="org.infinispan.server" artifact="infinispan-server-jgroups" />
      </module-def>

      <module-def name="org.jboss.as.cli">
         <maven-resource group="org.wildfly" artifact="wildfly-cli"/>
         <maven-resource group="org.infinispan.server" artifact="infinispan-server-cli"/>
      </module-def>

      <module-def name="org.jboss.as.console" slot="ispn" />

      <module-def name="org.jboss.as.domain-http-error-context" slot="ispn" />

      <module-def name="org.jboss.as.product" slot="ispn" />

      <module-def name="org.jboss.marshalling">
         <maven-resource group="org.jboss.marshalling" artifact="jboss-marshalling-osgi" />
      </module-def>

      <module-def name="io.netty">
         <maven-resource group="io.netty" artifact="netty-all" />
      </module-def>

      <module-def name="org.fusesource.leveldbjni-all">
         <maven-resource group="org.fusesource.leveldbjni" artifact="leveldbjni-all" />
      </module-def>

      <module-def name="org.infinispan.commons">
         <maven-resource group="org.infinispan" artifact="infinispan-commons" />
      </module-def>

      <module-def name="org.infinispan">
         <maven-resource group="org.infinispan" artifact="infinispan-core" />
      </module-def>

      <module-def name="org.infinispan.cli.interpreter">
         <maven-resource group="org.infinispan" artifact="infinispan-cli-interpreter" />
      </module-def>

      <module-def name="org.infinispan.client.hotrod">
         <maven-resource group="org.infinispan" artifact="infinispan-client-hotrod" />
      </module-def>
      
      <module-def name="org.infinispan.cachestore.jdbc" />
      
      <module-def name="org.infinispan.cachestore.remote" />
      
      <module-def name="org.infinispan.persistence.jdbc">
         <maven-resource group="org.infinispan" artifact="infinispan-cachestore-jdbc" />
      </module-def>

      <module-def name="org.infinispan.persistence.remote">
         <maven-resource group="org.infinispan" artifact="infinispan-cachestore-remote" />
      </module-def>

      <module-def name="org.infinispan.persistence.rest">
         <maven-resource group="org.infinispan" artifact="infinispan-cachestore-rest" />
      </module-def>

      <module-def name="org.infinispan.persistence.leveldb">
         <maven-resource group="org.infinispan" artifact="infinispan-cachestore-leveldb" />
      </module-def>

      <module-def name="org.infinispan.lucene-directory">
         <maven-resource group="org.infinispan" artifact="infinispan-lucene-directory" />
      </module-def>

      <module-def name="org.infinispan.protostream">
         <maven-resource group="org.infinispan.protostream" artifact="protostream" />
      </module-def>

      <module-def name="org.infinispan.objectfilter">
         <maven-resource group="org.infinispan" artifact="infinispan-objectfilter" />
      </module-def>

      <module-def name="org.infinispan.query.dsl">
         <maven-resource group="org.infinispan" artifact="infinispan-query-dsl" />
      </module-def>

      <module-def name="org.infinispan.query">
         <maven-resource group="org.infinispan" artifact="infinispan-query" />
      </module-def>

      <module-def name="org.infinispan.remote-query.client">
         <maven-resource group="org.infinispan" artifact="infinispan-remote-query-client" />
      </module-def>

      <module-def name="org.infinispan.remote-query.server">
         <maven-resource group="org.infinispan" artifact="infinispan-remote-query-server" />
      </module-def>

   	  <module-def name="org.infinispan.scripting">
   	     <maven-resource group="org.infinispan" artifact="infinispan-scripting" />
   	  </module-def>
   	
      <module-def name="org.infinispan.server">
         <maven-resource group="org.infinispan" artifact="infinispan-server-core" />
      </module-def>

      <module-def name="org.infinispan.server.hotrod">
         <maven-resource group="org.infinispan" artifact="infinispan-server-hotrod" />
      </module-def>

      <module-def name="org.infinispan.server.memcached">
         <maven-resource group="org.infinispan" artifact="infinispan-server-memcached" />
      </module-def>

      <module-def name="org.infinispan.server.rest">
         <maven-resource-with-classifier group="org.infinispan" artifact="infinispan-server-rest" classifier="classes" />
      </module-def>

      <module-def name="org.infinispan.server.websocket">
         <maven-resource group="org.infinispan" artifact="infinispan-server-websocket" />
      </module-def>

      <module-def name="org.infinispan.hibernate-search.directory-provider">
           <maven-resource group="org.infinispan" artifact="infinispan-directory-provider" />
      </module-def>

      <module-def name="org.iq80.leveldb">
         <maven-resource group="org.iq80.leveldb" artifact="leveldb" />
      </module-def>

      <module-def name="org.jgroups">
         <maven-resource group="org.jgroups" artifact="jgroups" />
      </module-def>

      <module-def name="org.scala-lang.library">
         <maven-resource group="org.scala-lang" artifact="scala-library" />
      </module-def>

      <module-def name="org.scala-lang.modules.xml">
         <maven-resource group="org.scala-lang.modules" artifact="scala-xml_2.11" />
      </module-def>

      <module-def name="org.apache.lucene">
         <maven-resource group="org.apache.lucene" artifact="lucene-core" />
         <maven-resource group="org.apache.lucene" artifact="lucene-queries" />
         <maven-resource group="org.apache.lucene" artifact="lucene-analyzers-common" />
         <maven-resource group="org.apache.lucene" artifact="lucene-queryparser" />
         <maven-resource group="org.apache.lucene" artifact="lucene-highlighter" />
         <maven-resource group="org.apache.lucene" artifact="lucene-grouping" />
         <maven-resource group="org.apache.lucene" artifact="lucene-facet" />
      </module-def>

      <module-def name="org.hibernate.search.engine">
         <maven-resource group="com.thoughtworks.paranamer" artifact="paranamer" />
         <maven-resource group="org.apache.avro" artifact="avro" />
         <maven-resource group="org.hibernate" artifact="hibernate-search-engine" />
         <maven-resource group="org.xerial.snappy" artifact="snappy-java" />
      </module-def>

      <module-def name="org.hibernate.hql.parser">
         <maven-resource group="org.hibernate.hql" artifact="hibernate-hql-parser" />
      </module-def>

      <module-def name="org.hibernate.hql.lucene">
         <maven-resource group="org.hibernate.hql" artifact="hibernate-hql-lucene" />
      </module-def>

      <module-def name="org.jboss.resteasy.resteasy-jaxrs">
         <maven-resource group="org.jboss.resteasy" artifact="resteasy-jaxrs" />
      </module-def>

      <module-def name="org.jboss.resteasy.resteasy-netty4">
         <maven-resource group="org.jboss.resteasy" artifact="resteasy-netty4" />
      </module-def>

   </target>

   <target name="trim-modules">
      <delete includeemptydirs="true" verbose="true">
         <fileset dir="${output.dir}/modules/system/layers/base" includesfile="trim-modules.txt" />
      </delete>
   </target>

   <target name="trim-as">
      <delete>
         <fileset dir="${output.dir}/bin">
            <include name="appclient.*" />
            <include name="run.*" />
            <include name="wsconsume.*" />
            <include name="wsprovide.*" />
         </fileset>
      </delete>
      <delete dir="${output.dir}/docs" />
      <delete dir="${output.dir}/appclient" />
      <delete dir="${output.dir}/welcome-content" />
   </target>

   <target name="all" depends="clean, unpack-server, trim-as, copy-files, infinispan-server-modules, trim-modules" />
</project>
