<project name="MAGE Loader" basedir="." default="build">

    <!-- Determine version number -->
    <property name="common.dir" location="${basedir}/../utils/src/java"/>
    <property file="${common.dir}/src/version.prop"/>

    <!-- Running properties -->
    <property name="input.properties" location="developer.properties"/>
    <property file="${input.properties}"/>

    <!-- Program meta data -->
    <property name="module.name" value="mage"/>
    <property name="module.jar" value="${module.name}-${version.number}.jar"/>
    <property name="unit.test.suffix" value="_Test"/>
    <property name="functional.test.suffix" value="_FuncTest"/>
    <property name="test.prefix" value="Load"/>

    <!-- Build properties -->
    <property name="src.dir" location="${basedir}/src"/>
    <property name="build.dir" location="${basedir}/build"/>
    <property name="build.classes.dir" location="${build.dir}/classes"/>
    <property name="build.src.dir" location="${build.dir}/src"/>
    <property name="xbean.src.dir" location="${build.dir}/xsrc"/>
    <property name="lib.dir" location="${basedir}/lib"/>
    <property name="dist.dir" location="${basedir}/dist"/>
    <property name="module.jar.path" location="${dist.dir}/${module.jar}"/>
    <property name="docs.dir" location="${basedir}/doc"/>
    <property name="api.dir" location="${docs.dir}/api"/>
    <property name="xmlbeans.src.dir" location="${basedir}/xmlbeans/src"/>
    <property name="common.lib.dir" location="${common.dir}/lib"/>
    <property name="warehouse.jar.name" value="biowarehouse.jar"/>
    <property name="warehouse.jar" location="${common.dir}/dist/${warehouse.jar.name}"/>
    <property name="etc.dir" location="${basedir}/etc"/>
    <property name="properties.template" location="${etc.dir}/mage.properties"/>
    <property name="run.script1.0" value="runMAGE-1.0-Loader.sh"/>
    <property name="run.script1.0.location" location="${etc.dir}/${run.script1.0}"/>
    <property name="run.script1.1" value="runMAGE-1.1-Loader.sh"/>
    <property name="run.script1.1.location" location="${etc.dir}/${run.script1.1}"/>
    <property name="schema.definition.file" location="${src.dir}/com/sri/bw/mage/mage-schema.xml"/>

    <!-- Testing properties -->
    <property name="test.dir" location="${basedir}/test"/>
    <property name="test.data.dir" location="${test.dir}/data"/>
    <property name="test.src.dir" location="${test.dir}/src"/>
    <property name="common.test.src.dir" location="${common.dir}/test/src"/>
    <property name="test.build.dir" location="${test.dir}/build"/>
    <property name="test.results.dir" location="${test.dir}/results"/>
    <property name="test.xmlResults.dir" location="${test.results.dir}/xmlResults"/>
    <property name="test.reports.dir" location="${test.results.dir}/htmlReports"/>


    <!-- Third party libraries -->
    <property name="mage-sdk.jar" location="${lib.dir}/MAGEstk-2004-11-29-v1.1-compatible.jar"/>
    <property name="xmlbeans.jar" location="${common.lib.dir}/xbean.jar"/>
    <property name="xmlbeans2.jar" location="${common.lib.dir}/jsr173_1.0_api.jar"/>
    <property name="log4j.jar" location="${common.lib.dir}/log4j-1.2.8.jar"/>
    <property name="mysql.driver.jar" location="${common.lib.dir}/mysql-connector-java-3.0.9-stable-bin.jar"/>
    <property name="oracle.driver.jar" location="${common.lib.dir}/ojdbc14.jar"/>
    <property name="cli.jar" location="${common.lib.dir}/commons-cli-1.0.jar"/>
    <property name="xerces1.jar" location="${common.lib.dir}/xercesImpl.jar"/>
    <property name="xerces2.jar" location="${common.lib.dir}/xml-apis.jar"/>
    <property name="db-schema.jar" location="${common.lib.dir}/db-schema.jar"/>
    <property name="element-map-beans.jar" location="${common.lib.dir}/element-map-beans.jar"/>
    <property name="mage-generation.jar" location="${lib.dir}/mage-generation.jar"/>

    <!-- Set the properties needed to connect to the warehouse -->
    <property file="${input.properties}"/>

    <!-- Determine DBMS type -->
    <condition property="is.oracle.dbms" value="true">
        <contains string="${dbms}" substring="oracle"/>
    </condition>

    <condition property="is.mysql.dbms" value="true">
        <contains string="${dbms}" substring="mysql"/>
    </condition>

    <!-- CLASSPATHS -->

    <path id="classpath.compile" description="The classpath used for compiling the program">
        <pathelement path="${mage-sdk.jar}"/>
        <pathelement path="${xmlbeans.jar}"/>
        <pathelement path="${log4j.jar}"/>
        <pathelement path="${warehouse.jar}"/>
        <pathelement path="${cli.jar}"/>
        <pathelement path="${xerces1.jar}"/>
        <pathelement path="${xerces2.jar}"/>
        <pathelement path="${db-schema.jar}"/>
        <pathelement path="${element-map-beans.jar}"/>
        <pathelement path="${mage-generation.jar}"/>
    </path>

    <path id="classpath.run" description="The classpath used for running the program">
        <path refid="classpath.compile"/>
        <pathelement path="${module.jar.path}"/>
        <pathelement path="${mysql.driver.jar}"/>
        <pathelement path="${oracle.driver.jar}"/>
        <!--<pathelement path="${c3p0.jar}"/>-->
    </path>

    <path id="classpath.test" description="The classpath used for compiling and running unit tests">
        <path refid="classpath.run"/>
        <pathelement path="${build.classes.dir}"/>
        <pathelement path="${test.build.dir}"/>
        <pathelement path="${common.dir}/test/build"/>
        <pathelement path="${common.lib.dir}/junit-3.8.1.jar"/>
    </path>

    <path id="classpath.schemadoc" description="The classpath used by the XSLT processor for generating schema doc">
        <pathelement path="${lib.dir}/saxon8.jar"/>
    </path>

    <!-- =========================================================== -->
    <!--  INITIALIZATION                                             -->
    <!-- =========================================================== -->
    <target name="init">
        <tstamp>
            <format pattern="yyyyMMdd-HHmm-z" property="build.number"/>
        </tstamp>
        <echo level="info" message="## ${ant.project.name}: Build number = ${build.number}"/>
        <ant dir="${common.dir}" inheritall="false"/>
    </target>

    <!-- =========================================================== -->
    <!--  CLEAN                                                      -->
    <!-- =========================================================== -->
    <target name="clean" description="deletes files/dirs made by this build">
        <echo level="info" message="## ${ant.project.name}: Cleaning build..."/>
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
        <delete dir="${test.build.dir}"/>
        <delete dir="${test.results.dir}"/>
    </target>



    <!-- =========================================================== -->
    <!--  COMPILE                                                    -->
    <!-- =========================================================== -->
    <target name="compile" depends="init" description="Compile source code">
        <echo level="info" message="## ${ant.project.name}: Compiling source code..."/>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${build.src.dir}"/>

        <!-- Copy src to build.src.dir so that filtering can be performed -->
        <filter token="VERSION_NUMBER" value="${version.number}"/>
        <filter token="BUILD_NUMBER" value="${build.number}"/>
        <copy todir="${build.src.dir}" filtering="true">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </copy>

        <!-- Compile -->
        <javac srcdir="${build.src.dir}" destdir="${build.classes.dir}" debug="true" failonerror="true">
            <classpath refid="classpath.compile"/>
            <exclude name="**/*${unit.test.suffix}.java"/>
            <exclude name="**/org/biomage/**/*.java"/>
        </javac>
    </target>

    <!-- =========================================================== -->
    <!--  BUILD                                                      -->
    <!-- =========================================================== -->
    <target name="build" depends="compile" description="Build module jar file">
        <echo level="info" message="## ${ant.project.name}: Creating jar file..."/>
        <mkdir dir="${dist.dir}"/>
        <jar destfile="${module.jar.path}">
            <fileset dir="${build.classes.dir}">
                <include name="**/*.class"/>
            </fileset>
            <fileset dir="${src.dir}">
                <include name="**/*.dtd"/>
                <include name="**/*.txt"/>
                <include name="**/*.xml"/>
            </fileset>
        </jar>

        <!-- Copy run script to dist dir and make sure it has executable permission -->
        <!-- Note: as of Ant 1.6, chmod only works on unix/linux machines -->
        <filter token="MODULE_JAR" value="${module.jar}"/>
        <filter token="WAREHOUSE_JAR" value="${warehouse.jar.name}"/>
        <copy file="${run.script1.0.location}" todir="${dist.dir}" filtering="true"/>
        <chmod file="${dist.dir}/${run.script1.0}" perm="+x"/>
        <copy file="${run.script1.1.location}" todir="${dist.dir}" filtering="true"/>
        <chmod file="${dist.dir}/${run.script1.1}" perm="+x"/>
    </target>

    <!-- Target used to build mage-generation.jar from MAGE UML to DTD application -->
    <!--<target name="build-mage-gen">-->
    <!--<property name="gen.src.dir" location="../gen2"/>-->
    <!--<mkdir dir="${build.classes.dir}"/>-->
    <!--<javac srcdir="${gen.src.dir}" destdir="${build.classes.dir}" debug="true" failonerror="true">-->
    <!--<classpath refid="classpath.compile"/>-->
    <!--</javac>-->
    <!--<jar destfile="${mage-generation.jar}">-->
    <!--<fileset dir="${build.classes.dir}">-->
    <!--<include name="org/biomage/**/*.class"/>-->
    <!--</fileset>-->
    <!--</jar>-->
    <!--</target>-->

    <!-- =========================================================== -->
    <!--  RUN                                                        -->
    <!-- =========================================================== -->
    <target name="run-mage-loader" depends="build" description="Run the loader">
        <echo level="info" message="## ${ant.project.name}: Running the MAGE Loader..."/>
        <java fork="true" classname="com.sri.bw.mage.MAGELoader2" maxmemory="1024M" failonerror="true">
            <classpath refid="classpath.run"/>
            <arg line="-p ${input.properties}"/>
        </java>
    </target>

    <target name="run-translation" depends="build">
        <java classname="com.sri.bw.mage.translation.MAGETranslator" fork="true" failonerror="true">
            <classpath>
                <pathelement path="${mage-generation.jar}"/>
                <path refid="classpath.run"/>
            </classpath>
            <arg line="-cfg ${src.dir}/com/sri/bw/mage/params.xml"/>
        </java>
    </target>

    <!-- =========================================================== -->
    <!--  COMPILE-TEST                                               -->
    <!-- =========================================================== -->
    <target name="compile-test" description="Compile test source code">
        <echo level="info" message="## ${ant.project.name}: Compiling test code..."/>
        <mkdir dir="${test.dir}"/>
        <mkdir dir="${test.build.dir}"/>

        <ant dir="${common.dir}" target="compile-test" inheritall="false" inheritrefs="false"/>

        <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" debug="true">
            <classpath refid="classpath.test"/>
        </javac>
    </target>

    <!-- =========================================================== -->
    <!--  TEST                                                       -->
    <!-- =========================================================== -->
    <target name="unit-test" depends="build, compile-test" description="Run unit tests">
        <bwtest test.suffix="${unit.test.suffix}" test.type="Unit"/>
    </target>

    <target name="func-test" depends="build, compile-test" description="Run functional tests">
        <bwtest test.suffix="${functional.test.suffix}" test.type="Functional"/>
    </target>

    <macrodef name="bwtest">
        <attribute name="test.suffix"/>
        <attribute name="test.type"/>
        <sequential>
            <echo level="info" message="## ${ant.project.name}: Performing @{test.type} tests..."/>

            <!-- Make dirs to receive reports -->
            <mkdir dir="${test.reports.dir}"/>
            <mkdir dir="${test.xmlResults.dir}"/>

            <!-- Run functional tests -->
            <junit fork="true" showoutput="yes" printsummary="on" dir="${basedir}" maxmemory="512M">
                <classpath refid="classpath.test"/>
                <formatter type="xml"/>
                <batchtest todir="${test.xmlResults.dir}">
                    <fileset dir="${test.build.dir}">
                        <include name="**/${test.prefix}*@{test.suffix}*"/>
                    </fileset>
                </batchtest>
                <sysproperty key="warehouse.test.database.host" value="${host}"/>
                <sysproperty key="warehouse.test.database.port" value="${port}"/>
                <sysproperty key="warehouse.test.database.name" value="${name}"/>
                <sysproperty key="warehouse.test.database.username" value="${username}"/>
                <sysproperty key="warehouse.test.database.password" value="${password}"/>
                <sysproperty key="warehouse.test.database.dbms.type" value="${dbms}"/>

            </junit>

            <!-- Transform xml results into human-readable html results -->
            <junitreport todir="${test.reports.dir}">
                <fileset dir="${test.xmlResults.dir}">
                    <include name="TEST-*.xml"/>
                </fileset>
                <report format="frames" todir="${test.reports.dir}"/>
            </junitreport>

            <echo level="info"
                  message="## ${ant.project.name}:  @{test.type} tests are completed.  See the results at ${test.reports.dir}/index.html..."/>
        </sequential>
    </macrodef>

    <!-- =========================================================== -->
    <!--  DOCUMENTATION                                              -->
    <!-- =========================================================== -->
    <target name="mage-doc" description="Generate MAGE DB Schema documentation">
        <xslt basedir="${src.dir}/com/sri/bw/mage" destdir="${docs.dir}" style="${docs.dir}/mage-doc.xsl"
              classpathref="classpath.schemadoc" force="true">
            <include name="mage-elements.xml"/>
        </xslt>
        <delete file="${docs.dir}/mage-elements.html"/>
    </target>

</project>