<?xml version="1.0" encoding="UTF-8"?>

<project name="LAMS-Build" default="usage" basedir=".">

        <!-- import properties from the shared and o/s specific files -->
        <property file="common.properties" />
        <property file="shared.properties" />
        <property file="${osPropertiesName}.properties" />

        <target name="init">
                <mkdir dir="${assembly}" />
        </target>

        <target name="usage" description="print usage message">
                <echo>------------ Utility Methods ------------------------</echo>
                <echo>usage: this message</echo>
                <echo>clean: clean the ear assembly directories</echo>
                <echo>clean-subprojects: clean sub project build directories</echo>
                <echo>build-subprojects: build sub projects</echo>
                <echo>build-tool-deploy-utility: build the tool_deploy utility and update copy in lams_build</echo>
                <echo>------------ Build/Deploy LAMS  ------------------------</echo>
                <echo>assemble-ear: assemble ear structure</echo>
                <echo>deploy-ear: copy ear to local jboss server</echo>
                <echo>copyfiles: copy configuration files to local jboss server</echo>
                <echo>----------------------------------------------------------</echo>
                <echo>To deploy LAMS:</echo>
                <echo>   1. copyfiles (this only needs to be done once, or when basic configuration the files change)</echo>
                <echo>   2. assemble-ear</echo>
                <echo>   3. manually refresh the project</echo>
                <echo>   4. deploy-ear</echo>
                <echo>   5. deploy-tools</echo>
                <echo>Do not try to assemble and deploy in the same run - if you do,</echo>
                <echo>Eclipse may not deploy all of the newly created files.</echo>
        </target>

        <!-- clean dirs for this script only -->
        <target name="clean" description="cleans assembly">
                <delete includeemptydirs="true">
                        <fileset dir="${assembly}">
                                <include name="**/*" />
                        </fileset>
                </delete>
                <delete includeemptydirs="true" failonerror="false">
                        <fileset dir="${jboss.server.instance}/tmp">
                                <include name="**/*" />
                        </fileset>
                </delete>
                <delete includeemptydirs="true" failonerror="false">
                        <fileset dir="${jboss.server.instance}/work/jboss.web/localhost">
                                <include name="lams*/**" />
                        </fileset>
                </delete>
        </target>

        <!-- clean all projects -->
        <target name="clean-subprojects" description="cleans sub projects" />


        <!-- build sub-projects -->
        <target name="build-subprojects"
                description="builds sub porjects">
                <copy overwrite="yes" todir="${shareddtd}">
                        <fileset dir="${conf.auth}">
                                <include name="**/*.dtd"/>
                        </fileset>
                </copy>
                <ant antfile="../${lams_common}/build.xml" target="build-jar"
                        inheritAll="false" />
                <ant antfile="../${lams_contentrepository}/build.xml"
                        target="build-jar" inheritAll="false" />
                <ant antfile="../${lams_central}/build.xml" target="build-war"
                        inheritAll="false" />
                <ant antfile="../${lams_learning}/build.xml" target="build-war" inheritAll="false"/>
                <ant antfile="../${lams_monitoring}/build.xml"  target="build-war" inheritAll="false"/>
                <ant antfile="../${lams_admin}/build.xml" target="build-war" inheritAll="false"/>
                <ant antfile="../${lams_www}/build.xml" target="build-war" inheritAll="false"/>
        </target>

        <!-- build tool deployment utility -->
        <target name="build-tool-deploy-utility"
                description="builds the tool deploy utility and copy to lams_build">

                <ant antfile="../${lams_tool_deploy}/build.xml" target="build"
                        inheritAll="false" />

                <mkdir dir="${deploy.tool}" />
                <mkdir dir="${deploy.tool}/lib" />

                <copy todir="${deploy.tool}" overwrite="yes" flatten="true">
                        <fileset dir="../${lams_tool_deploy}">
                                <include name="deploy.bat" />
                                <include name="deploy.sh" />
                        </fileset>
                </copy>

                <copy todir="${deploy.tool}/lib" overwrite="yes" flatten="true">
                        <fileset dir="../${lams_tool_deploy}/lib">
                                <include name="jakarta-commmons/*.jar" />
                                <include name="mysql/*.jar" />
                                <include name="xstream/*.jar" />
                        </fileset>
                </copy>

                <copy todir="${deploy.tool}/lib" overwrite="yes" flatten="true">
                        <fileset dir="../${lams_tool_deploy}/build/lib">
                                <include name="lams-tool-deploy.jar" />
                        </fileset>
                </copy>

        </target>

        <!-- cruise control task : build lams -->
        <target name="lams-cruise" depends="assemble-ear, rebuild-db, deploy-ear, deploy-tools, copyfiles" description="builds entire LAMS (used for CruiseControl instance)">
                <echo>Building LAMS using CruiseControl</echo>
        </target>

        <!-- assemble ear -->
        <target name="assemble-ear" depends="init, clean, build-subprojects"
                description="Assemble lams ear in exploded form">
                <!-- have to copy the libs etc. to a temp location otherwise we end up with all the directories under lib as well-->
                <mkdir dir="${assembly}/temp" />
                <copy todir="${assembly}/temp" failonerror="true"
                        flatten="true">
                        <fileset dir="${lib}">
                                <patternset id="lib.jars">
                                        <!--  the desired jars are nominated explicitly so -->
                                        <!--  we don't release a build only one by accident -->
                                        <include name="axis/*.jar" />
                                        <include name="cglib/*.jar" />
                                        <include name="dom4j/*.jar" />
                                        <include name="ehcache/*.jar" />
                                        <include name="fckeditor/*.jar" />
                                        <include name="hibernate/*.jar" />
                                        <include name="j2ee/*.jar" />
                                        <include name="jakarta-commons/*.jar" />
                                        <include name="jakarta-poi/*.jar" />
                                        <include name="jakarta-taglibs/*.jar" />
                                        <include name="jboss/*.jar" />
                                        <include name="jdom/*.jar" />
                                        <include name="jfreechart/*.jar" />
                                        <include name="log4j/*.jar" />
                                        <include name="mysql/*.jar" />
                                        <include name="odmg/*.jar" />
                                        <include name="quartz/*.jar" />
                                        <include name="smack/smack.jar"/>
                                        <include name="smack/smackx.jar"/>
                                        <include name="spring/*.jar" />
                                        <include name="struts/*.jar" />
                                        <include name="wddx/*.jar" />
                                        <include name="concurrent/*.jar" />
                                        <include name="xstream/*.jar" />
                                </patternset>
                        </fileset>
                        <fileset dir="..">
                                <include name="${lams_common}/${sub.build.lib}/*.jar" />
                                <include name="${lams_contentrepository}/${sub.build.lib}/*.jar" />
                                <include name="${lams_central}/${sub.build.lib}/*.war" />
                                <include name="${lams_central}/${sub.build.lib}/*.jar" />
                                <include name="${lams_monitoring}/${sub.build.lib}/*.jar"/>
                                <include name="${lams_monitoring}/${sub.build.lib}/*.war"/>
                                <include name="${lams_learning}/${sub.build.lib}/*.jar"/>
                                <include name="${lams_learning}/${sub.build.lib}/*.war"/>
                                <include name="${lams_admin}/${sub.build.lib}/*.jar"/>
                                <include name="${lams_admin}/${sub.build.lib}/*.war"/>
                                <include name="${lams_www}/${sub.build.lib}/*.war"/>
                        </fileset>
                </copy>
                <!-- Pull out all the language files -->
                <mkdir dir="${assembly}/temp/lams-dictionary.jar" />
                <copy todir="${assembly}/temp/lams-dictionary.jar" failonerror="true">
                        <fileset dir="../${lams_common}/${sub.build.language}"><include name="**" /></fileset>
                        <fileset dir="../${lams_contentrepository}/${sub.build.language}"><include name="**" /></fileset>
                        <fileset dir="../${lams_central}/${sub.build.language}"><include name="**" /></fileset>
                        <fileset dir="../${lams_monitoring}/${sub.build.language}"><include name="**" /></fileset>
                        <fileset dir="../${lams_learning}/${sub.build.language}"><include name="**" /></fileset>
                        <fileset dir="../${lams_admin}/${sub.build.language}"><include name="**" /></fileset>
                </copy>
                <!-- create a temporary ear -->
                <ear destfile="${assembly.temp.ear}" compress="false"
                        appxml="${app.xml}">
                        <fileset dir="${assembly}/temp" includes="**" />
                </ear>
                <!-- remove temp lib -->
<!--            <delete dir="${assembly}/temp" includeEmptyDirs="true" /> -->
                <!-- explode the ear (Why doesn't the EAR task support exploded format?) -->
                <unjar src="${assembly.temp.ear}" dest="${assembly.lams.ear}" />
                <move file="${assembly.lams.ear}/lams-central.war" tofile="${assembly.lams.ear}/temp.war"/>
                <unjar src="${assembly.lams.ear}/temp.war" dest="${assembly.lams.ear}/lams-central.war"/>
                <move file="${assembly.lams.ear}/lams-www.war" tofile="${assembly.lams.ear}/temp.war"/>
                <unjar src="${assembly.lams.ear}/temp.war" dest="${assembly.lams.ear}/lams-www.war"/>
                <delete file="${assembly.lams.ear}/temp.war"/>
                <!-- delete temp.ear -->
                <delete file="${assembly.temp.ear}" />
        </target>

        <!-- ============================================================================== -->
        <!-- Deploy the ear file to the server                                              -->
        <!-- ============================================================================== -->

        <target name="deploy-ear"
                description="Deploys previously assembled ear to jboss">
                <copy todir="${jboss.ear.deploy}" overwrite="true"
                        failonerror="true">
                        <fileset dir="${assembly}">
                                <include name="${lams.ear}/**/*.*" />
                        </fileset>

                </copy>
        </target>

        <!-- ============================================================================== -->
        <!-- Deploy IMS Content Package tool to the depoloyed ear                                                       -->
        <!--  Relies on the tools using the parameter jboss.deploy                                                      -->
        <!-- ============================================================================== -->
        <target name="deploy-tools" description="Deploy the tools, including any special parallel activities">
                <ant antfile="../${tool_forum_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_lamc_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_lanb_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_laqa_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_sbmt_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_chat_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_larsrc_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_vote_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_notebook_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_survey_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant antfile="../${tool_scribe_dir}/build.xml" target="deploy-tool"
                                inheritAll="false" >
                        <property name="jboss.deploy" location="${jboss.deploy}"/>
                </ant>
                <ant target="deploy-learning-libraries"/>
        </target>

        <!-- ============================================================================== -->
        <!--  Rebuild the LAMS Database                                                                                                         -->                             
        <!-- ============================================================================== -->
        <target name="rebuild-db" description="Rebuild the LAMS Database.">
                <ant antfile="../${lams_common}/build.xml" target="rebuild-db" inheritAll="false" />
        </target>

        <!-- ============================================================================== -->
        <!-- Copy the third party jar files and cofiguration files for deployment           -->
        <!-- ============================================================================== -->

        <target name="copyfiles"
                description="copy lib and config files">
                <copy overwrite="yes" todir="${jboss.server.instance}/conf/">
                        <fileset dir="${conf.jboss}/">
                                <include name="login-config.xml" />
                                <include name="log4j.xml" />
                                <include name="jboss-service.xml" />
                        </fileset>
                <filterset>
                        <filter token="confdir" value="${jboss.server.instance}/conf"/>
                </filterset>
                </copy>

                <copy overwrite="yes" todir="${jboss.server.instance}/conf/">
                        <fileset dir="${conf.auth}/">
                                <include name="*.xml" />
                        </fileset>
                <filterset>
                        <filter token="confdir" value="${jboss.server.instance}/conf"/>
                </filterset>
                </copy>

                <copy overwrite="yes" todir="${jboss.ear.deploy}">
                        <fileset dir="${conf.jboss}/service">
                                <include name="mysql*.xml" />
                                <include name="local-service.xml" />
                        </fileset>
                </copy>
                <copy overwrite="yes" todir="${jboss.ear.deploy}/jbossweb-tomcat55.sar/META-INF">
                        <fileset dir="${conf.jboss}/tomcat">
                                <include name="jboss-service.xml" />
                        </fileset>
                </copy>
                <copy overwrite="yes" todir="${jboss.ear.deploy}/jbossweb-tomcat55.sar/">
                        <fileset dir="${conf.jboss}/tomcat">
                                <include name="server.xml" />
                                <include name="context.xml" />
                        </fileset>
                </copy>
                <copy overwrite="yes" todir="${jboss.ear.deploy}/jbossweb-tomcat55.sar/conf">
                        <fileset dir="${conf.jboss}/tomcat">
                                <include name="web.xml" />
                        </fileset>
                </copy>

                <ant antfile="../${lams_common}/build.xml" target="copy-shared-session-jar" inheritAll="false"/>
                <ant antfile="../${lams_common}/build.xml" target="copy-lams-valve-jar" inheritAll="false"/>
                <echo message="Please copy jboss-cache.jar and jgroups.jar from server/all/lib to server/default/lib"
                        level="warning"/>
        </target>

        <target name="test-report" description="Run all the test cases across the system. Takes a long time.">
                <!-- Core projects first -->
                <ant antfile="../${lams_common}/build.xml" target="insert-test-data" inheritAll="false"/>
                <ant antfile="../${lams_common}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${lams_contentrepository}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${lams_common}/build.xml" target="insert-test-data" inheritAll="false"/>
                <ant antfile="../${lams_central}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${lams_common}/build.xml" target="insert-test-data" inheritAll="false"/>
                <ant antfile="../${tool_survey_dir}/build.xml" target="build-db" inheritAll="false"/>
<!--            <ant antfile="../${tool_survey_dir}/build.xml" target="build-jar" inheritAll="false"/>  -->
                <ant antfile="../${lams_monitoring}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${lams_learning}/build.xml" target="test-report" inheritAll="false"/>

                <!--  Now the tools -->
<!--            <ant antfile="../${lams_common}/build.xml" target="insert-test-data" inheritAll="false"/>
                <ant antfile="../${tool_forum_dir}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${tool_imscp_dir}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${tool_laqa_dir}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${tool_lanb_dir}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${tool_sbmt_dir}/build.xml" target="test-report" inheritAll="false"/>
                <ant antfile="../${tool_survey_dir}/build.xml" target="test-report" inheritAll="false"/>
        -->
        </target>


        <!-- Build and deploy the parallel activities and other complex activities. Must be done after -->
        <!-- the tools are deployed -->
        <target name="deploy-learning-libraries">

                <echo>Deploying Library Activities</echo>

                <!-- Generate the deployment package for share resources / forum parallel activity. -->
                <ant antfile="buildcombinedtasks.xml" target="deploy-package-library">
                        <property name="assembly.activity" location="${basedir}/assembly/shareresourcesforum"/>
                        <property name="package.cfg" location="librarypackages/shareresourcesforum"/>
                </ant>

                <!-- Generate the deployment package for chat / scribe parallel activity. -->
                <ant antfile="buildcombinedtasks.xml" target="deploy-package-library">
                        <property name="assembly.activity" value="${basedir}/assembly/chatscribe"/>
                        <property name="package.cfg" value="librarypackages/chatscribe"/>
                </ant>

                <!-- Generate the deployment package for forum / scribe parallel activity. -->
                <ant antfile="buildcombinedtasks.xml" target="deploy-package-library">
                        <property name="assembly.activity" value="${basedir}/assembly/forumscribe"/>
                        <property name="package.cfg" value="librarypackages/forumscribe"/>
                </ant>
</target>

        <!-- Copy lams.tld and the lams tags from lams-central to the other projects -->
        <target name="copy-tags" description="Copy lams.tld and the lams tags from lams-central to the other projects">
                <ant antfile="../${lams_admin}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${lams_learning}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${lams_monitoring}/build.xml"  target="copy-tags" inheritAll="false"/>
                <ant antfile="../${lams_www}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_chat_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_forum_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_lamc_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_laqa_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_larsrc_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_lanb_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_notebook_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_sbmt_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_scribe_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_survey_dir}/build.xml" target="copy-tags" inheritAll="false"/>
                <ant antfile="../${tool_vote_dir}/build.xml" target="copy-tags" inheritAll="false"/>
        </target>


</project>
