<?xml version="1.0"?>
<!-- build script -->
<project name="Simple-database" default="hudson.all" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">
    <property file="build.properties"/>
    <property file="default.build.properties"/>
    <property file="default.properties"/>
    <property file="version.properties"/>

    <property name="build.number" value="${build.major.number}.${build.minor.number}"/>

    <path id="project.class.path">
        <pathelement location="${java.home}/../lib/tools.jar"/>
    </path>

    <path id="build.class.path">
        <pathelement location="${build.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement location="${java.home}/../lib/tools.jar"/>
    </path>

    <path id="test.class.path">
        <pathelement location="${build.classes.dir}"/>
        <pathelement location="${test.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement location="${java.home}/../lib/tools.jar"/>
    </path>

    <path id="jdepend.class.path">
        <pathelement location="${build.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="jdepend*/**/*.jar"/>
        </fileset>
    </path>

    <path id="junit.class.path">
        <pathelement location="${build.classes.dir}"/>
        <pathelement location="${test.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <path id="run.class.path">
        <pathelement location="${build.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <path id="compile.class.path">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement location="${java.home}/../lib/tools.jar"/>
    </path>

    <path id="jar.class.path">
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="current-number">
        <echo>Current build number:${build.number}</echo>
    </target>

    <target name="revision">
        <propertyfile  file="version.properties">
            <entry key="build.revision.number" type="int" operation="+" value="1" pattern="00"/>
        </propertyfile>
    </target>

    <target name="minor">
        <propertyfile  file="version.properties">
            <entry key="build.minor.number" type="int" operation="+" value="1" pattern="00"/>
            <entry key="build.revision.number" type="int" value="0" pattern="00"/>
        </propertyfile>
    </target>

    <target name="major">
        <propertyfile  file="version.properties">
            <entry key="build.major.number" type="int" operation="+" value="1" pattern="00"/>
            <entry key="build.minor.number" type="int" value="0" pattern="00"/>
            <entry key="build.revision.number" type="int" value="0" pattern="00"/>
        </propertyfile>
    </target>

    <!-- =================================================================== -->
    <!-- Clean                                                               -->
    <!-- =================================================================== -->
    <target name="clean">
        <delete dir="${build.dir}"/>
        <delete dir="${build.dir}/javadoc"/>
        <delete dir="${generated.dir}"/>
    </target>

    <!-- =================================================================== -->
    <!-- Initialise                                                          -->
    <!-- =================================================================== -->
    <target name="init">

        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="build.class.path"
                 loaderRef="default"/>

        <taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask" classpathref="build.class.path"
                 loaderRef="default"/>

        <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpathref="build.class.path"
                 loaderRef="default"/>

        <taskdef name="javancss" classname="javancss.JavancssAntTask" classpathref="build.class.path"
                 loaderRef="default"/>

    </target>

    <!-- =================================================================== -->
    <!-- Prepares the directory structure                                    -->
    <!-- =================================================================== -->
    <target name="prepare" depends="init">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${dist.dir}"/>
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${instrumented.classes.dir}"/>
        <mkdir dir="${test.classes.dir}"/>
        <mkdir dir="${build.dir}/testreports"/>
        <mkdir dir="${build.dir}/javadoc"/>
        <mkdir dir="${generated.dir}"/>
        <mkdir dir="${jar.dir}"/>
    </target>

    <!-- =================================================================== -->
    <!-- Generate javadoc                                                    -->
    <!-- =================================================================== -->
    <target name="javadoc">
        <javadoc destdir="${build.dir}/javadoc"
                 author="true"
                 version="true"
                 use="true"
                 windowtitle="Simple-Database Api"
                 classpathref="build.class.path"
                 useexternalfile="false"
                 private="true">
            <!-- stylesheetfile="javadoc.css" -->

            <fileset dir="${src.dir}" defaultexcludes="yes">
                <include name="**/*.java"/>
            </fileset>

            <doctitle>
                Simple Database 0.1
            </doctitle>

            <bottom>
                All rights whatever.
            </bottom>

        </javadoc>
    </target>

    <!-- =================================================================== -->
    <!-- PMD                                                                 -->
    <!-- =================================================================== -->
    <target name="pmd" depends="prepare">
        <pmd shortFilenames="true">
            <ruleset>rulesets/java/braces.xml</ruleset>
            <ruleset>java-basic</ruleset>
            <formatter type="html" toFile="${build.dir}/pmd_report.html"/>
            <formatter type="xml" toFile="${build.dir}/pmd.xml" />
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </pmd>
    </target>

    <!-- =================================================================== -->
    <!-- PMD                                                                 -->
    <!-- =================================================================== -->
    <target name="cpd" depends="prepare">
        <cpd minimumTokenCount="100" format="xml" outputFile="${build.dir}/cpd.xml" ignoreidentifiers="true"
             ignoreliterals="true">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </cpd>
    </target>

    <!-- =================================================================== -->
    <!-- FindBugs                                                            -->
    <!-- =================================================================== -->
    <target name="findbugs" depends="compile">
        <!-- home="${findbugs.home}" -->
        <findbugs home="${build.lib.dir}/findbugs-1.3.9" output="xml" sourcepath="${src.dir}"
                  outputFile="${build.dir}/findbugs.xml"
                  reportLevel="medium"
                  excludeFilter="findbug-exclude.xml"
                  jvmargs="-Xmx512m"
                  timeout="1200000">

            <classpath refid="build.class.path"/>
            <class location="${build.classes.dir}"/>
            <!-- sourcePath path="${src.dir}" /-->
        </findbugs>

    </target>

    <!-- =================================================================== -->
    <!-- NCSS                                                                -->
    <!-- =================================================================== -->
    <target name="ncss" depends="prepare">
        <javancss srcdir="${src.dir}"
                  includes="**/*.java"
                  generateReport="true"
                  outputfile="${build.dir}/javancss_metrics.xml"
                  format="xml">
            <classpath refid="project.class.path"/>
        </javancss>
    </target>

    <!-- =================================================================== -->
    <!-- Compile web sources                                                 -->
    <!-- =================================================================== -->
    <target name="compile" depends="prepare">
        <!-- generate.jsp, webdoclet,  -->
        <javac encoding="utf-8" destdir="${build.classes.dir}" includes="**"
               classpathref="compile.class.path" debug="true"
               debuglevel="lines,vars,source" deprecation="on"
               optimize="on" verbose="false" target="1.7" source="1.7" includeantruntime="false">
            <src path="${src.dir}"/>
        </javac>
    </target>

    <!-- =================================================================== -->
    <!-- Copy non java files                                                 -->
    <!-- =================================================================== -->
    <target name="copynonjavafiles" depends="prepare">
        <copy todir="${build.classes.dir}">
            <fileset dir="${src.dir}">
                <exclude name="**/*.java"/>
                <exclude name="**/*.class"/>
            </fileset>
            <fileset dir="${test.dir}">
                <exclude name="**/*.java"/>
                <exclude name="**/*.class"/>
            </fileset>
        </copy>
    </target>

    <target name="run" depends="compile, copynonjavafiles">
        <java fork="true" classname="nl.astraeus.forum.web.HttpServer" classpathref="run.class.path"
              dir=".."
                >
        </java>
    </target>
    <!-- =================================================================== -->
    <!-- Compile test sources                                                 -->
    <!-- =================================================================== -->
    <target name="compiletest" depends="prepare, compile, copynonjavafiles">
        <!-- generate.jsp, webdoclet,  -->
        <javac encoding="utf-8" destdir="${test.classes.dir}" includes="**" classpathref="test.class.path" debug="on"
               debuglevel="lines,vars,source" deprecation="on" optimize="on" verbose="false" target="1.7" source="1.7">
            <src path="${test.dir}"/>
        </javac>
    </target>

    <target name="jacoco" depends="compiletest">
        <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
            <classpath path="${build.lib.dir}/jacoco/jacocoant.jar"/>
        </taskdef>

        <jacoco:coverage destfile="${build.dir}/jacoco.exec">
            <junit fork="true" forkmode="once">
                <jvmarg value="-XX:-UseSplitVerifier"/>
                <jvmarg value="-noverify"/>
                <classpath refid="test.class.path" />

                <formatter type="xml" usefile="true"/>

                <batchtest todir="${build.dir}/testreports">
                    <fileset dir="${test.dir}">
                    </fileset>
                </batchtest>
            </junit>
        </jacoco:coverage>

        <jacoco:report>
            <executiondata>
                <file file="${build.dir}/jacoco.exec"/>
            </executiondata>

            <structure name="Simple Database">
                <classfiles>
                    <fileset dir="${build.classes.dir}"/>
                </classfiles>
                <sourcefiles encoding="UTF-8">
                    <fileset dir="${src.dir}"/>
                </sourcefiles>
            </structure>

            <html destdir="${build.dir}/jacoco"/>
        </jacoco:report>
    </target>

    <target name="jar" depends="compile, copynonjavafiles" description="Create jar distribution dir">

        <copy todir="${jar.dir}/lib">
            <fileset dir="${lib.dir}" includes="*.jar"/>
        </copy>

        <manifestclasspath property="jar.class.path"
                           jarfile="${jar.dir}/${jar.name}.jar">
            <classpath refid="jar.class.path"/>
        </manifestclasspath>

        <pathconvert property="manifest.classpath" pathsep=" ">
            <path refid="jar.class.path"/>
            <mapper>
                <chainedmapper>
                    <flattenmapper/>
                    <globmapper from="*.jar" to="lib/*.jar"/>
                </chainedmapper>
            </mapper>
        </pathconvert>

        <jar destfile="${jar.dir}/${jar.name}.jar" basedir="${build.classes.dir}">
            <manifest>
                <attribute name="Class-Path" value="${manifest.classpath}"/>
            </manifest>
        </jar>

    </target>

    <!-- =================================================================== -->
    <!-- Archive                                                            -->
    <!-- =================================================================== -->
    <target name="dist" depends="jar">
        <antcall target="minor" />

        <!-- create WAR file -->
        <zip destfile="${dist.dir}/${jar.name}.zip">
            <zipfileset dir="${jar.dir}" />
        </zip>

    </target>

    <!-- =================================================================== -->
    <!-- Hudson                                                              -->
    <!-- =================================================================== -->
    <target name="hudson" depends="clean, compile, jar">
        <echo>Done.</echo>
    </target>

    <!-- =================================================================== -->
    <!-- Hudson                              jdepend                                -->
    <!-- =================================================================== -->
    <target name="all"
            depends="init, prepare, javadoc, ncss, findbugs, pmd, cpd, jacoco, jar, dist">
        <echo>Done.</echo>
    </target>

    <!-- =================================================================== -->
    <!-- Archive                                                            -->
    <!-- =================================================================== -->
    <target name="archive" depends="clean, init">

        <mkdir dir="${build.dir}"/>

        <!-- create WAR file -->
        <zip destfile="${build.dir}/PrevaylerReferenceArchive.zip">
            <zipfileset dir="${src.dir}" prefix="src/"/>
            <zipfileset dir="${project.dir}/script" prefix="script/"/>
            <zipfileset dir="${project.dir}/config" prefix="config/"/>
        </zip>

    </target>

</project>
