<?xml version="1.0"?>

<project name="on-assembly" default="prepare-jboss" basedir="." xmlns:artifact="urn:maven-artifact-ant">

  <target name="prepare-jboss"
          description="prepare JBossAS 4.0 for hosting the JBoss ON Enterprise EAR">

    <!-- Validate the 'jboss.home' param... -->
    <available file="${jboss.home}" type="dir" property="jboss.home.exists" />
    <fail unless="jboss.home.exists" message="Directory specified via 'jboss.home' param (${jboss.home}) does not exist - aborting..." />
    <available file="${jboss.home}/lib/jboss-system.jar" type="file" property="jboss.home.is.valid" />
    <fail unless="jboss.home.is.valid" message="Directory specified via 'jboss.home' param (${jboss.home}) does not contain a valid JBoss 4.0 installation - aborting..." />
      
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
      <classpath>
        <pathelement location="${settings.localRepository}/org/apache/maven/maven-artifact-ant/2.0.4/maven-artifact-ant-2.0.4.jar" />
      </classpath>
    </typedef>
    
    <property name="jboss.main.lib.dir" location="${jboss.home}/lib" />
    <echo>Updating ${jboss.main.lib.dir} ...</echo>
    <!-- JDOM is needed by the WebDAV repo impl, though not sure why it needs to be in the main JBoss lib dir... -->
    <copy file="${settings.localRepository}/jdom/jdom/b9/jdom-b9.jar" 
          toDir="${jboss.main.lib.dir}" verbose="true" />      
          
    <property name="jboss.lib.dir" location="${jboss.home}/server/default/lib" />
    <echo>Updating ${jboss.lib.dir} ...</echo>
    <delete file="${jboss.lib.dir}/commons-logging.jar" />  <!-- commons-loggong v1.0.3 -->
    <!-- NOTE: We don't use the Maven artifact:dependencies task because it also pulls in transitive deps. -->
    <copy file="${settings.localRepository}/commons-logging/commons-logging/1.0.4/commons-logging-1.0.4.jar" 
          toDir="${jboss.lib.dir}" verbose="true" />
    <copy file="${settings.localRepository}/commons-modeler/commons-modeler/1.1/commons-modeler-1.1.jar" 
          toDir="${jboss.lib.dir}" verbose="true" />
    <copy file="${settings.localRepository}/fast-md5/fast-md5/2.5/fast-md5-2.5.jar" 
          toDir="${jboss.lib.dir}" verbose="true" />
    <copy file="${settings.localRepository}/jgroups/jgroups-all/2.2.9.1/jgroups-all-2.2.9.1.jar" 
          toDir="${jboss.lib.dir}" verbose="true" />
    <copy file="${settings.localRepository}/ojdbc14/ojdbc14/10.2.0.1.0/ojdbc14-10.2.0.1.0.jar" 
          toDir="${jboss.lib.dir}" verbose="true" />
    <copy file="${settings.localRepository}/postgresql/postgresql/8.0-312.jdbc3/postgresql-8.0-312.jdbc3.jar" 
          toDir="${jboss.lib.dir}" verbose="true" />

    <property name="tmp.dir" location="${java.io.tmpdir}/on-assembly-m2-plugin.tmp" />
    <delete dir="${tmp.dir}" quiet="true" />
    <mkdir dir="${tmp.dir}" />
    <unjar src="${settings.localRepository}/org/jboss/on/on-assembly-m2-plugin/1.0-SNAPSHOT/on-assembly-m2-plugin-1.0-SNAPSHOT.jar"
           dest="${tmp.dir}" />    
      
    <!-- Replace any config files we need to modify... -->
    <property name="jboss.conf.dir" location="${jboss.home}/server/default/conf" />
    <echo>Updating ${jboss.conf.dir} ...</echo>          

    <move todir="${jboss.conf.dir}">
      <fileset dir="${tmp.dir}/jboss-mods/conf" />
    </move>      

    <replace file="${jboss.conf.dir}/jboss-service.xml" >
      <replacefilter token="@@@hq-engine.server.port@@@" value="${jboss.server.port}" />
      <replacefilter token="@@@hq-engine.jnp.port@@@" value="${jboss.jnp.port}" />
    </replace>

    <available property="available.keystore" file="${jboss.conf.dir}/jon.keystore" />
    <!-- NOTE: We have to use ant instead of antcall, because antcall doesn't work from within Maven 2. -->
    <ant antfile="src/main/scripts/on-assembly.build.xml" target="prepare-ssl" />

    <!-- Update deploy dir - add extra stuff we need and remove anything we don't use... -->
    <property name="jboss.deploy.dir" value="${jboss.home}/server/default/deploy" />
    <echo>Updating ${jboss.deploy.dir} ...</echo>      
    
    <move todir="${jboss.deploy.dir}">
      <fileset dir="${tmp.dir}/jboss-mods/deploy" />
    </move>      

    <replace file="${jboss.deploy.dir}/hq-ds.xml">
      <replacefilter token="@@@server.database-url@@@" value="${on.jdbc.url}"/>
      <replacefilter token="@@@server.database-driver@@@" value="${on.jdbc.driver}"/>
      <replacefilter token="@@@server.database-user@@@" value="${on.jdbc.user}"/>
      <replacefilter token="@@@server.database-password@@@" value="${on.jdbc.password}"/>
    </replace>
    
    <delete dir="${jboss.deploy.dir}/http-invoker.sar"/>
    <delete file="${jboss.deploy.dir}/jms/jbossmq-destinations-service.xml"/>   
    <delete dir="${jboss.deploy.dir}/jms/jbossmq-httpil.sar"/>
    <delete>  
      <fileset dir="${jboss.deploy.dir}/jms">
        <include name="oil-service.xml" />
        <include name="oil2-service.xml" />
        <include name="rmi-il-service.xml" />
        <include name="uil2-service.xml" />
      </fileset>
    </delete>

    <!-- Remove the JMS Cache Invalidation service -->
    <delete file="${jboss.deploy.dir}/cache-invalidation-service.xml"/>
    
    <!-- Remove the UUID generator service -->
    <delete dir="${jboss.deploy.dir}/uuid-key-generator.sar"/>

    <!-- Remove the SNMP adaptor - we speak JMX here -->
    <delete dir="${jboss.deploy.dir}/snmp-adaptor.sar"/>

    <!-- Remove the jboss-xa-jdbc service - we manage our own transactions -->
    <delete file="${jboss.deploy.dir}/jboss-xa-jdbc.rar"/>

    <!-- Remove the jboss-net.sar - we just don't like it -->
    <delete dir="${jboss.deploy.dir}/jboss-net.sar" />
    
    <!-- TODO: Create an M2 project for the Lather SAR, so we can pull it down from the repo here. -->
    <!--
    <delete dir="${jboss.deploy.dir}/lather-jboss.sar" />
    <unzip src="${lather.lib}/lather-jboss.sar"
           dest="${jboss.deploy.dir}/lather-jboss.sar"
           overwrite="false"/>
    <mkdir dir="${jboss.deploy.dir}/lather-jboss.sar/net/hyperic/util/notReady"/>
    <copy todir="${jboss.deploy.dir}/lather-jboss.sar/net/hyperic/util/notReady">
        <fileset dir="${build.dir}/classes/net/hyperic/util/notReady">
            <include name="*.class"/>
        </fileset>
    </copy>
    -->
    
    <property name="tomcat.home" location="${jboss.deploy.dir}/jbossweb-tomcat55.sar" />
    <move todir="${tomcat.home}">
      <fileset dir="${tmp.dir}/tomcat-mods" />
    </move> 

    <replace file="${tomcat.home}/server.xml">
      <replacefilter token="@@@server.webapp.port@@@" value="${on.server.http.port}"/>
      <replacefilter token="@@@server.webapp.secure.port@@@" value="${on.server.https.port}"/>
    </replace>
    
    <!-- Copy our startup webapp... -->
    <echo>Updating Tomcat ROOT.war ...</echo>
    <unwar src="${settings.localRepository}/org/jboss/on/on-startup/2.0-SNAPSHOT/on-startup-2.0-SNAPSHOT.war"
           dest="${tomcat.home}/ROOT.war" />   

    <!-- NOTE: We have to use ant instead of antcall, because antcall doesn't work from within Maven 2. -->
    <!-- <ant antfile="src/main/scripts/on-assembly.build.xml" target="instrument-jboss" /> -->          
    
    <!-- Delete 'minimal' and 'all' config dirs... -->
    <delete dir="${jboss.home}/server/minimal" />
    <delete dir="${jboss.home}/server/all" />
    
    <!-- Delete docs dir and README files... -->    
    <delete dir="${jboss.home}/docs" />
    <delete>
      <fileset dir="${jboss.home}" includes="*.*" />
    </delete>
     
  </target>

  <!-- TODO: Figure out if this target should be exposed as an M2 goal. -->
  <target name="prepare-release-jboss">
    <!-- Copy our log4j config file, overwrite the existing one -->
    <delete file="${jboss.home}/${jboss.server}/conf/log4j.xml"/>
    <copy file="${hq.home}/installer/data/hq-engine/log4j.xml"
          tofile="${jboss.home}/${jboss.server}/conf/log4j.xml"/>
  </target>
  
  <target name="prepare-ssl" unless="available.keystore">
    <!-- Generate SSL server key - 128-bit key that expires in 20 years... -->        
    <genkey alias="JON" 
            keystore="${jboss.conf.dir}/jon.keystore"
            keyalg="rsa"
            storepass="JB0ssNetw0rkEnterpr1seM8n8gement" 
            validity="7300"
            dname="CN=JBoss Operations Network, OU=JON, O=jboss.com, C=US"/>
  </target>
  
</project>
