<project name="web-client" default="build">

   <path id="path.common" path="${basedir}/../../common"/>
   <property name="dir.common" refid="path.common"/>
   <import file="${dir.common}/common.xml"/>

   <!-- we want to build a WAR file in this project -->
   <target name="package" depends="package-war" />

   <!-- copy the template web.xml and replace the "@facesconfig@" token with list of faces config files -->
   <target name="generate-web-xml" depends="init">
      <copy file="${dir.src.webinf}/${file.name.war.template}" tofile="${dir.src.webinf}/web.xml" overwrite="yes" />
      <replace file="${dir.src.webinf}/web.xml" token="@facesconfig@" value="${files.faces.config}" />
   </target>

   <!-- override the common assemble-war target -->
   <target name="assemble-war" depends="common.assemble-war, generate-web-xml">
      <fail unless="server" message="You must supply a value for the 'server' property to indicate which WAR to build" />
      
      <condition property="isTomcat">
         <equals arg1="${server}" arg2="tomcat" casesensitive="false" trim="yes"/>
      </condition>
      <condition property="isJBoss">
         <equals arg1="${server}" arg2="jboss" casesensitive="false" trim="yes"/>
      </condition>
      
      <echo level="info">isTomcat = ${isTomcat}</echo>
      <echo level="info">isJBoss = ${isJBoss}</echo>
      
      <antcall target="assemble-war-common" />
      <antcall target="assemble-war-tomcat" />
      <antcall target="assemble-war-jboss" />
   </target>
   
   <target name="assemble-war-common">
      <copy todir="${dir.assemble}/WEB-INF">
         <fileset dir="${dir.project.installer}" includes="licenses/**" />
         <fileset dir="${dir.project.remoteapi}/source/web/WEB-INF"/>
      </copy>
      
      <copy todir="${dir.assemble}/WEB-INF/lib">
         <fileset dir="${dir.common.lib}/jibx" includes="*.jar" />
         <fileset dir="${dir.common.lib}/openoffice" includes="*.jar" />
         <fileset dir="${dir.common.lib}/jmagick" includes="*.jar" />
         <fileset dir="${dir.common.lib}/commons" includes="*.jar" />
         <fileset dir="${dir.common.lib}/jgroups" includes="*.jar" />
         <fileset dir="${dir.common.lib}/treecache" includes="*.jar" />
         <fileset dir="${dir.common.lib}/swarmcache" includes="*.jar" />
         <fileset dir="${dir.project.core}/build/dist" includes="${dir.name.core}.jar" />
         <fileset dir="${dir.project.repository}/build/dist" includes="${dir.name.repository}.jar" />
         <fileset dir="${dir.project.remoteapi}/build/dist" includes="${dir.name.remoteapi}.jar" />
      </copy>
      
      <copy todir="${dir.assemble}/wsdl">
         <fileset dir="${dir.project.remoteapi}/source/wsdl" />
      </copy>
   </target>
 
   <target name="assemble-war-tomcat" if="isTomcat">
      <!-- remove the jboss specific stuff from WEB-INF -->
      <delete>
         <fileset dir="${dir.assemble}/WEB-INF" includes="${webinf.delete.tomcat}" />
      </delete>
      
      <!-- add config files to WEB-INF/classes -->
      <copy todir="${dir.assemble}/WEB-INF/classes">
         <fileset dir="${dir.config.repository}"/>
         <fileset dir="${dir.config.webclient}"/>
      </copy>
      <copy todir="${dir.assemble}/WEB-INF/classes" file="${dir.project.core}/source/java/log4j.properties" />
   </target>
   
   <target name="assemble-war-jboss" if="isJBoss">
      <!-- remove the JARs that cause problems in JBoss -->
      <delete>
         <fileset dir="${dir.assemble}/WEB-INF/lib" includes="${webinf.lib.delete.jboss}" />
      </delete>

      <!-- remove existing config files -->
      <delete dir="${dir.deploy.jboss}/../conf/alfresco" quiet="yes"/>
         
      <!-- push externally visible config files into conf directory -->
      <copy todir="${dir.deploy.jboss}/../conf" >
         <fileset dir="${dir.config.repository}"/>
         <fileset dir="${dir.config.webclient}"/>
      </copy>
   </target>
   
   <target name="clean" depends="common.clean" description="Cleans all the normal files plus web.xml">
      <delete file="${dir.src.webinf}/web.xml" />
   </target>
  
</project>
