<?xml version="1.0"?>

<project name="stackimpact" basedir="." default="bin">
    <property name="version" value="1.0.0"/>
    <property name="src.dir" value="src"/>
    <property name="lib.dir" value="lib"/>
    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="classes"/>
    <property name="dist.dir" value="dist"/>

    <target name="build-jar">
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${dist.dir}"/>
        
        <exec executable="make" failonerror="true">
        </exec>

        <delete includeEmptyDirs="true">
            <fileset dir="${dist.dir}" />
        </delete>
    
        <copy todir="${classes.dir}">
          <fileset dir="${build.dir}"/>
        </copy>

        <javac destdir="${classes.dir}" source="1.7" target="1.7" debug="true">
            <src path="${src.dir}" />

            <include name="com/eclipsesource/json/**"/>
            <include name="com/stackimpact/agent/**"/>
        </javac>

        <jar manifest="${src.dir}/META-INF/MANIFEST.MF" destfile="${dist.dir}/stackimpact.jar">
            <fileset dir="${classes.dir}" />
        </jar>
    </target>


    <target name="build-tests">
        <mkdir dir="${classes.dir}"/>

        <javac destdir="${classes.dir}" source="1.7" target="1.7" debug="true">
            <src path="${src.dir}" />

            <include name="test/stackimpact/agent/**"/>

            <classpath>
                <pathelement location="${lib.dir}/junit-4.12.jar"/>
            </classpath>
        </javac>

        <jar destfile="${dist.dir}/stackimpact-test.jar">
            <fileset dir="${classes.dir}" />
        </jar>
    </target>


    <target name="run-tests">
        <delete>
            <fileset dir="/tmp">
                <include name="libstackimpact-*"/>
            </fileset>
        </delete>

        <junit printsummary="yes" haltonfailure="yes">
            <classpath>
                <pathelement location="${lib.dir}/junit-4.12.jar"/>
                <pathelement location="${lib.dir}/hamcrest-core-1.3.jar"/>
                <pathelement location="${dist.dir}/stackimpact.jar"/>
                <pathelement location="${dist.dir}/stackimpact-test.jar"/>
            </classpath>

            <formatter type="plain" usefile="false"/>
            <test name="test.stackimpact.agent.StackImpactTest"/>
            <test name="test.stackimpact.agent.APIRequestTest"/>
            <test name="test.stackimpact.agent.ConfigLoaderTest"/>
            <test name="test.stackimpact.agent.MessageQueueTest"/>
            <test name="test.stackimpact.agent.model.MetricTest"/>
            <test name="test.stackimpact.agent.model.BreakdownTest"/>
            <test name="test.stackimpact.agent.reporters.ProcessReporterTest"/>
            <test name="test.stackimpact.agent.profilers.CPUProfilerTest"/>
            <test name="test.stackimpact.agent.profilers.LockProfilerTest"/>
        </junit>
    </target>


    <target name="cleanup">
        <delete includeEmptyDirs="true">
            <fileset erroronmissingdir="false" dir="${classes.dir}" />
        </delete>
    </target>


    <target name="bin" depends="build-jar, cleanup" />
    <target name="test" depends="build-jar, build-tests, run-tests, cleanup" />
    <target name="test-nobuild" depends="run-tests, cleanup" />

</project>
