<?xml version="1.0"?>
<project name="muse_build" default="build">
    
    <!-- set the property to indicate that JAVA_HOME is set -->
    <target name="cond_check">
        <property environment="env"/>
        <condition property="JAVA_HOME_SET">
            <isset property="env.JAVA_HOME"/>
        </condition>
    </target>

    <!-- fail if JAVA_HOME isn't set, otherwise we don't execute the body -->
    <target name="check_environment" unless="JAVA_HOME_SET" depends="cond_check">
        <fail message="The environment variable JAVA_HOME is not set. Set this variable to the directory where the JDK is installed."/>
    </target>
    
    <!-- check out all of the sources we need -->
    <target name="checkout" depends="check_environment">
        <!-- start logging things to a log file -->
        <property name="LOG_FILE" value="${basedir}/log.txt"/>
        <delete file="${LOG_FILE}"/>
        <record name="${LOG_FILE}" append="false" action="start"/>
        <echo message="A copy of this output is in ${LOG_FILE}"/>

		<!-- A server 400 error during SVN checkout may be a symptom of your SVN client requiring an https connection -->
		<!-- If this occurs, change the http://svn.apache.org/* references in this script to https://svn.apache.org/* -->
		
        <!-- the URL of the trunk -->
        <property name="MUSE_SVN_TRUNK_REPOSITORY" value="http://svn.apache.org/repos/asf/webservices/muse/trunk"/>
        
        <!-- the URL of the modules repository -->
        <property name="MUSE_SVN_MODULES_REPOSITORY" value="${MUSE_SVN_TRUNK_REPOSITORY}/modules"/>

        <!-- the name of the directory to which we will checkout the modules -->
        <property name="MUSE_SVN_MODULES" value="modules"/>

        <!-- the URL of the modules repository -->
        <property name="MUSE_SVN_SAMPLES_REPOSITORY" value="${MUSE_SVN_TRUNK_REPOSITORY}/samples"/>

        <!-- the name of the directory to which we will checkout the samples -->
        <property name="MUSE_SVN_SAMPLES" value="samples"/>

        <!-- the URL of the site (ie docs) repository -->
        <property name="MUSE_SVN_SITE_REPOSITORY" value="http://svn.apache.org/repos/asf/webservices/muse/site"/>

        <!-- the name of the directory to which we will checkout the site -->
        <property name="MUSE_SVN_SITE" value="docs"/>

        <!-- the name of the directory where the release zip files will be put -->
        <property name="MUSE_DIST" value="dist"/>

        <!-- a temporary directory to which we download the dependency zip files -->
        <property name="TEMP_DOWNLOAD_DIR" value="tempDownload"/>

        <!-- the locations of all of the dependency zip files -->
        <property name="AXIS2_OSGI_LIB_URL" value="http://ws.apache.org/muse/build/lib/latest/axis2-osgi.zip"/>
        <property name="AXIS2_LIB_URL" value="http://ws.apache.org/muse/build/lib/latest/axis2.zip"/>
        <property name="COMMON_LIB_URL" value="http://ws.apache.org/muse/build/lib/latest/common.zip"/>
        <property name="ECLIPSE_OSGI_URL" value="http://ws.apache.org/muse/build/lib/latest/eclipse-osgi.zip"/>
        <property name="MUSE_LOGGING_URL" value="http://ws.apache.org/muse/build/lib/latest/muse-logging-2.2.0.jar"/>

        <!-- the names of the downloaded dependencies (they all get put into the TEMP_DOWNLOAD_DIR dir -->
        <property name="AXIS2_OSGI_LIB_FILE" value="${TEMP_DOWNLOAD_DIR}/axis2-osgi.zip"/>
        <property name="AXIS2_LIB_FILE" value="${TEMP_DOWNLOAD_DIR}/axis2.zip"/>
        <property name="COMMON_LIB_FILE" value="${TEMP_DOWNLOAD_DIR}/common.zip"/>
        <property name="ECLIPSE_OSGI_FILE" value="${TEMP_DOWNLOAD_DIR}/eclipse-osgi.zip"/>
        <property name="MUSE_LOGGING_FILE" value="${TEMP_DOWNLOAD_DIR}/muse-logging-2.2.0.jar"/>

        <!-- the svn ant task didn't work so call it directly 
             an executable svn must be on the system PATH -->
        <exec executable="svn" searchpath="true" dir="." failonerror="true">
            <arg line="co ${MUSE_SVN_MODULES_REPOSITORY} ${MUSE_SVN_MODULES} --non-interactive"/>
        </exec>

        <exec executable="svn" searchpath="true" dir="." failonerror="true">
            <arg line="co ${MUSE_SVN_SAMPLES_REPOSITORY} ${MUSE_SVN_SAMPLES} --non-interactive"/>
        </exec>

        <exec executable="svn" searchpath="true" dir="." failonerror="true">
            <arg line="co ${MUSE_SVN_SITE_REPOSITORY} ${MUSE_SVN_SITE} --non-interactive"/>
        </exec>

        <property name="APACHE_LICENSE_FILE" value="${basedir}/LICENSE.txt"/>
        <property name="APACHE_LICENSE_URL" value="http://svn.apache.org/viewvc/webservices/muse/trunk/LICENSE.txt?view=co"/>
        <property name="APACHE_NOTICE_FILE" value="${basedir}/NOTICE.txt"/>
        <property name="APACHE_NOTICE_URL" value="http://svn.apache.org/viewvc/webservices/muse/trunk/NOTICE.txt?view=co"/>

		<!-- Uncomment and set appropriate parameters if running behind a firewall -->
		<!--<setproxy proxyhost="proxy.host.name" proxyport="80" proxyuser="myuserid" proxypassword="mypassword"/> -->
        <!-- get the license -->
        <get src="${APACHE_LICENSE_URL}" dest="${APACHE_LICENSE_FILE}"/>
        <get src="${APACHE_NOTICE_URL}" dest="${APACHE_NOTICE_FILE}"/>
    </target>

    <!-- Add a build tag to the version in the main pom.xml. If there was no build
         tag specified then skip this step (but still do the dependencies) -->
    <target name="update_version" depends="checkout" if="BUILD_TAG">
        <replaceregexp
             match="([0-9]+.[0-9]+.[0-9]+)-[^&lt;]*"
             replace="\1-${BUILD_TAG}"
             byline="true">
            <fileset dir="modules">
                <include name="**/pom.xml"/>
            </fileset>
         </replaceregexp>
    </target>
    
    <!-- initialize properties -->
    <target name="init" depends="update_version">
        <!-- the executable for maven, mvn.bat on windows, mvn otherwise -->
        <condition property="MAVEN_EXECUTABLE" value="mvn.bat" else="mvn">
            <os family="windows"/>
        </condition>

        <!-- pull out the version we're building from the pom.xml -->
        <xmlproperty file="${MUSE_SVN_MODULES}/pom.xml"/>
        <property name="VERSION" value="${project.version}"/>

        <!-- temporary directory -->
        <property name="TEMP_DIR" value="temp"/>
        <property name="TEMP_BIN_DIST_DIR" value="${TEMP_DIR}/muse-${VERSION}-bin"/>
        <property name="TEMP_SRC_DIST_DIR" value="${TEMP_DIR}/muse-${VERSION}-src"/>
        
        <!-- set up properties for all of the directories we're creating -->
        <property name="MUSE_TOOLS_BIN" value="${TEMP_DIR}/muse-${VERSION}-bin/bin"/>

        <property name="MUSE_CORE_DIR" value="${TEMP_BIN_DIST_DIR}/modules/core"/>
        <property name="MUSE_TOOLS_DIR" value="${TEMP_BIN_DIST_DIR}/modules/tools"/>
        <property name="MUSE_AXIS2_DIR" value="${TEMP_BIN_DIST_DIR}/modules/axis2"/>
        <property name="MUSE_MINI_DIR" value="${TEMP_BIN_DIST_DIR}/modules/mini"/>
        <property name="MUSE_WS_API_DIR" value="${TEMP_BIN_DIST_DIR}/modules/ws-fx-api"/>
        <property name="MUSE_WS_IMPL_DIR" value="${TEMP_BIN_DIST_DIR}/modules/ws-fx-impl"/>

        <property name="MUSE_OSGI_CORE_DIR" value="${TEMP_BIN_DIST_DIR}/modules/osgi/core"/>
        <property name="MUSE_OSGI_MINI_DIR" value="${TEMP_BIN_DIST_DIR}/modules/osgi/mini"/>
        <property name="MUSE_OSGI_AXIS2_DIR" value="${TEMP_BIN_DIST_DIR}/modules/osgi/axis2"/>
        <property name="MUSE_OSGI_COMPLETE_DIR" value="${TEMP_BIN_DIST_DIR}/modules/osgi/complete"/>
        <property name="MUSE_OSGI_WS_API_DIR" value="${TEMP_BIN_DIST_DIR}/modules/osgi/ws-fx-api"/>
        <property name="MUSE_OSGI_WS_IMPL_DIR" value="${TEMP_BIN_DIST_DIR}/modules/osgi/ws-fx-impl"/>

        <property name="MUSE_SAMPLE_DIR" value="${TEMP_BIN_DIST_DIR}/samples"/>
        <property name="J2EE_SAMPLE_DIR" value="${MUSE_SAMPLE_DIR}/j2ee"/>
        <property name="OSGI_SAMPLE_DIR" value="${MUSE_SAMPLE_DIR}/eclipse-osgi"/>

        <property name="MUSE_JAVADOC_DIR" value="${TEMP_BIN_DIST_DIR}/docs/docs/${VERSION}/javadoc"/>
        <property name="MUSE_LIB_DIR" value="${TEMP_BIN_DIST_DIR}/lib"/>
    </target>

    <target name="directory_structure" depends="init">
        <!-- make sure the directories are wiped before we continue -->
        <delete dir="${TEMP_DIR}"/>
        <delete dir="${MUSE_DIST}"/>

        <!-- remake the directories -->
        <mkdir dir="${MUSE_DIST}"/>

        <mkdir dir="${MUSE_JAVADOC_DIR}"/>

        <mkdir dir="${MUSE_CORE_DIR}"/>    	
        <mkdir dir="${MUSE_AXIS2_DIR}"/>       	
        <mkdir dir="${MUSE_MINI_DIR}"/> 	
        <mkdir dir="${MUSE_TOOLS_DIR}"/>    	
        <mkdir dir="${MUSE_WS_API_DIR}"/>    	
        <mkdir dir="${MUSE_WS_IMPL_DIR}"/>    	

        <mkdir dir="${MUSE_OSGI_CORE_DIR}"/> 
        <mkdir dir="${MUSE_OSGI_MINI_DIR}"/>
        <mkdir dir="${MUSE_OSGI_AXIS2_DIR}"/>
        <mkdir dir="${MUSE_OSGI_COMPLETE_DIR}"/>    	
        <mkdir dir="${MUSE_OSGI_WS_API_DIR}"/>    	
        <mkdir dir="${MUSE_OSGI_WS_IMPL_DIR}"/>    	
        <mkdir dir="${MUSE_LIB_DIR}"/> 

        <mkdir dir="${MUSE_SAMPLE_DIR}"/>    	

        <mkdir dir="${TEMP_SRC_DIST_DIR}"/>    	
    </target>

    <target name="src_dist" depends="directory_structure">

        <!-- copy the source from the svn checkout folder to our temporary 
        source distribution folder -->
        <copy todir="${TEMP_SRC_DIST_DIR}">
            <fileset dir="${MUSE_SVN_MODULES}">
                <exclude name="**/build/**"/>
                <exclude name="**/.svn/**"/>
                <exclude name="preview/**"/>
                <exclude name="*.bat"/>
            </fileset>
        </copy>

        <!-- copy in the notice -->
        <copy todir="${TEMP_SRC_DIST_DIR}" file="${APACHE_NOTICE_FILE}"/>

        <!-- copy in the license -->
        <copy todir="${TEMP_SRC_DIST_DIR}" file="${APACHE_LICENSE_FILE}"/>

        <!-- zip up and put into the distribution folder -->
        <zip destfile="${MUSE_DIST}/muse-${VERSION}-src.zip">
            <fileset dir="${TEMP_DIR}">
                <include name="muse-${VERSION}-src/**"/>
            </fileset>
        </zip>
    	
    	<!-- zip up everything again - as a .tar.gz file -->
        <tar destfile="${MUSE_DIST}/muse-${VERSION}-src.tar.gz" basedir="${TEMP_DIR}" includes="muse-${VERSION}-src/**" compression="gzip"/>
    	
    </target>

    <target name="bin_dist" depends="directory_structure"> 
        <!-- get maven to clean up any existing generated files -->
        <exec executable="${MAVEN_EXECUTABLE}" searchpath="true" dir="${MUSE_SVN_MODULES}" failonerror="true">
             <arg line="clean"/>
             <env key="MAVEN_TERMINATE_CMD" value="on" />
        </exec>

        <!-- call maven directly -->
        <exec executable="${MAVEN_EXECUTABLE}" searchpath="true" dir="${MUSE_SVN_MODULES}" failonerror="true">
             <arg line="-ff -e"/>
             <env key="MAVEN_TERMINATE_CMD" value="on" />
        </exec>

        <!-- copy in the notice -->
        <copy todir="${TEMP_BIN_DIST_DIR}" file="${APACHE_NOTICE_FILE}"/>

        <!-- copy in the license -->
        <copy todir="${TEMP_BIN_DIST_DIR}" file="${APACHE_LICENSE_FILE}"/>

        <!-- create the javadoc -->
        <javadoc destdir="${MUSE_JAVADOC_DIR}" useexternalfile="yes" failonerror="true">
            <fileset dir="${TEMP_SRC_DIST_DIR}">
                <include name="muse-util-xml/**/*.java"/>
                <include name="muse-util-qname/**/*.java"/>
                <include name="muse-util/src/org/apache/muse/util/uuid/**/*.java"/>
                <include name="muse-util/src/org/apache/muse/util/messages/**/*.java"/>
                <include name="muse-util/src/org/apache/muse/util/LoggingUtils.java"/>
                <include name="muse-ws*/**/*.java"/>
                <include name="muse-core/src/org/apache/muse/ws/**/*.java"/>
                <include name="muse-core/src/org/apache/muse/core/**/*.java"/>
                <exclude name="muse-core/src/org/apache/muse/core/serializer/*.java"/>
                <exclude name="muse-core/src/org/apache/muse/core/platform/**/*.java"/>
                <exclude name="muse-core/src/org/apache/muse/core/descriptor/**/*.java"/>
            </fileset>
            <fileset dir="${TEMP_SRC_DIST_DIR}">
                <include name="muse-core/src/org/apache/muse/core/serializer/Serializer.java"/>
                <include name="muse-core/src/org/apache/muse/core/serializer/SerializerRegistry.java"/>
            </fileset>
        </javadoc>

        <!-- copy in docs -->
        <copy todir="${TEMP_BIN_DIST_DIR}">
            <fileset dir="${basedir}">
                <include name="docs/*"/>
                <include name="docs/images/**/*"/>
                <include name="docs/skin/**/*"/>
                <include name="docs/docs/${VERSION}/**/*"/>
                <include name="docs/docs/index.html"/>
                <exclude name="**/.svn/**"/>
            </fileset>
        </copy>

        <!-- copy in core modules -->
        <copy todir="${MUSE_CORE_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse*/**/muse-core-${VERSION}.jar"/>
                <include name="muse*/**/muse-wsa-soap-${VERSION}.jar"/>
                <include name="muse*/**/muse-util*-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in AXIS2 modules -->
        <copy todir="${MUSE_AXIS2_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse*/**/muse-platform-axis2-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in Mini SOAP Engine modules -->
        <copy todir="${MUSE_MINI_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse*/**/muse-platform-mini-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in tools modules -->
        <copy todir="${MUSE_TOOLS_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse*/**/muse-tools-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in WS-* api modules -->
        <copy todir="${MUSE_WS_API_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse*/**/muse-*api-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in WS-* implementation modules -->
        <copy todir="${MUSE_WS_IMPL_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse*/**/muse-*impl-${VERSION}.jar"/>
                <include name="muse*/**/muse-wsrf-rmd-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in osgi core modules -->
        <copy todir="${MUSE_OSGI_CORE_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse-osgi*/**/muse-osgi-core-${VERSION}.jar"/>
                <include name="muse-osgi*/**/muse-osgi-soa-core-${VERSION}.jar"/>
            </fileset>
            <fileset dir="${basedir}/modules/osgi-bundles">
                <include name="*/target/muse-core-${VERSION}.jar"/>
                <include name="*/target/muse-wsa-soap-${VERSION}.jar"/>
                <include name="*/target/muse-util-all-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in the osgi mini module -->
        <copy todir="${MUSE_OSGI_MINI_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse-osgi*/**/muse-osgi-soa-mini-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in the osgi axis2 module -->
        <copy todir="${MUSE_OSGI_AXIS2_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}">
                <include name="muse-osgi*/**/muse-osgi-soa-axis2-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in osgi complete module -->
        <copy todir="${MUSE_OSGI_COMPLETE_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}/osgi-bundles">
                <include name="*/target/muse-complete-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in osgi WS-* api modules -->
        <copy todir="${MUSE_OSGI_WS_API_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}/osgi-bundles">
                <include name="*/target/muse-*api-${VERSION}.jar"/>
            </fileset>
        </copy>

        <!-- copy in osgi WS-* implementation modules -->
        <copy todir="${MUSE_OSGI_WS_IMPL_DIR}" flatten="true">
            <fileset dir="${MUSE_SVN_MODULES}/osgi-bundles">
                <include name="*/target/muse-*impl-${VERSION}.jar"/>
                <include name="*/target/muse-wsrf-rmd-${VERSION}.jar"/>
            </fileset>
        </copy>


        <!-- copy in samples -->
        <copy todir="${MUSE_SAMPLE_DIR}">
            <fileset dir="${MUSE_SVN_SAMPLES}">
                <exclude name="**/.svn/**"/>
            </fileset>
        </copy>
    	
    	<!-- copy build.xml file into each J2EE sample dir -->
    	<copy file="${J2EE_SAMPLE_DIR}/build.xml" todir="${J2EE_SAMPLE_DIR}/apache-httpd"/>
    	<copy file="${J2EE_SAMPLE_DIR}/build.xml" todir="${J2EE_SAMPLE_DIR}/simple"/>
        <copy file="${J2EE_SAMPLE_DIR}/build.xml" todir="${J2EE_SAMPLE_DIR}/wsn-consumer"/>
        <copy file="${J2EE_SAMPLE_DIR}/build.xml" todir="${J2EE_SAMPLE_DIR}/wsn-producer"/>
        <copy file="${J2EE_SAMPLE_DIR}/build.xml" todir="${J2EE_SAMPLE_DIR}/wsrf"/>
    	<delete file="${J2EE_SAMPLE_DIR}/build.xml"/>
    	
    	<!-- copy web.xml file into each J2EE sample's config dir -->
    	<copy file="${J2EE_SAMPLE_DIR}/web.xml" todir="${J2EE_SAMPLE_DIR}/apache-httpd/config"/>
    	<copy file="${J2EE_SAMPLE_DIR}/web.xml" todir="${J2EE_SAMPLE_DIR}/simple/config"/>
        <copy file="${J2EE_SAMPLE_DIR}/web.xml" todir="${J2EE_SAMPLE_DIR}/wsn-consumer/config"/>
        <copy file="${J2EE_SAMPLE_DIR}/web.xml" todir="${J2EE_SAMPLE_DIR}/wsn-producer/config"/>
        <copy file="${J2EE_SAMPLE_DIR}/web.xml" todir="${J2EE_SAMPLE_DIR}/wsrf/config"/>
    	<delete file="${J2EE_SAMPLE_DIR}/web.xml"/>
    	
    	<!-- make Unix scripts executable by owner -->
    	<chmod dir="${MUSE_SVN_MODULES}/muse-tools/scripts" includes="*.sh" perm="700"/>
    		
        <!-- copy in scripts -->
        <copy todir="${MUSE_TOOLS_BIN}">
            <fileset dir="${MUSE_SVN_MODULES}/muse-tools/scripts">
                <include name="*.sh"/>
                <include name="*.bat"/>
                <include name="*.xml"/>
            </fileset>
        </copy>

        <!-- fix line endings for unix scripts -->
        <fixcrlf srcdir="${MUSE_TOOLS_BIN}"
            eol="unix" 
            includes="**/*.sh"
            />

        <!-- get *all* of the dependencies -->
        <mkdir dir="${TEMP_DOWNLOAD_DIR}"/>
        <get src="${AXIS2_OSGI_LIB_URL}" dest="${AXIS2_OSGI_LIB_FILE}"/>
        <get src="${AXIS2_LIB_URL}" dest="${AXIS2_LIB_FILE}"/>
        <get src="${COMMON_LIB_URL}" dest="${COMMON_LIB_FILE}"/>
        <get src="${ECLIPSE_OSGI_URL}" dest="${ECLIPSE_OSGI_FILE}"/>
        <get src="${MUSE_LOGGING_URL}" dest="${MUSE_LOGGING_FILE}"/>

        <unzip dest="${MUSE_LIB_DIR}">
            <fileset dir="${TEMP_DOWNLOAD_DIR}">
                <include name="*.zip"/>
            </fileset>
        </unzip>

        <copy file="${MUSE_LOGGING_FILE}" todir="${MUSE_LIB_DIR}"/>

        <!-- zip up everything and put it into the distribution -->
        <zip destfile="${MUSE_DIST}/muse-${VERSION}-bin.zip">
            <fileset dir="${TEMP_DIR}">
                <include name="muse-${VERSION}-bin/**"/>
            </fileset>
        </zip>
    	
    	<!-- zip up everything again - as a .tar.gz file -->
        <tar destfile="${MUSE_DIST}/muse-${VERSION}-bin.tar.gz" basedir="${TEMP_DIR}" includes="muse-${VERSION}-bin/**" compression="gzip"/>

        <!-- get maven to clean up the generated files -->
        <exec executable="${MAVEN_EXECUTABLE}" searchpath="true" dir="${MUSE_SVN_MODULES}" failonerror="true">
            <arg line="clean"/>
            <env key="MAVEN_TERMINATE_CMD" value="on" />
        </exec>
    	
    </target>

    <target name="cleanup">
        <delete dir="${TEMP_DIR}"/>
        <delete file="${APACHE_LICENSE_FILE}"/>
        <delete file="${APACHE_NOTICE_FILE}"/>
    </target>

    <target name="build" depends="src_dist,bin_dist,cleanup">
        <record name="${LOG_FILE}" append="yes" action="stop"/>
    </target>    
</project>

