<project name="javapam" default="build" xmlns:artifact="urn:maven-artifact-ant">

    <!-- Load Maven antlib and make pom.xml available as maven.project -->
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
        <classpath>
            <pathelement location="tools/maven-artifact-ant-2.0.4-dep.jar"/>
        </classpath>
    </typedef>

    <property name="os" value="${os.name}_${os.arch}"/>

    <!-- Load local environment overrides -->
    <property environment="env"/>
    <!-- OS and Architecture specific properties -->
    <property file="${os}.properties"/>
    <!-- Default properties -->
    <property file="default.properties"/>


    <artifact:pom file="pom.xml" id="maven.project"/>


    <!-- Load local environment overrides -->
    <property environment="env"/>

    <property name="name" value="${maven.project.name}"/>
    <property name="version" value="${maven.project.version}"/>

    <artifact:dependencies filesetId="dependency.fileset" pathId="dependency.classpath"
                           verbose="false">
        <pom refid="maven.project"/>
    </artifact:dependencies>

    <artifact:dependencies filesetId="compile.dependency.fileset"
                           pathId="compile.dependency.classpath" useScope="compile">
        <pom refid="maven.project"/>
    </artifact:dependencies>

    <!-- Set up project.class.path -->
    <path id="project.class.path">
        <path refid="dependency.classpath"/>
    </path>

    <target name="os" description="OS and Architecture label">
        <echo message="${os}"/>
    </target>


    <target name="clean" description="Cleans the build directory">
        <delete dir="${build.dir}"/>
    </target>

    <!-- Top level target -->
    <target name="build" depends="clean, test, dist-tgz"
            description="Builds a clean distribution"/>

    <!-- compiles -->
    <target name="compile" depends="checkstyle, compile-src, compile-tests"
            description="Performs a compile"/>

    <target name="shared-object" depends="compile, generate-c-headers"
            description="Creates the shared library">

        <copy todir="${generated.src.c.dir}">
            <fileset dir="${src.c.dir}" includes="*"/>
        </copy>
        <exec dir="${generated.src.c.dir}" executable="make" failonerror="true">
            <arg value="${make.target}"/>
        </exec>
    </target>

    <target name="generate-checkstyle-config">
        <mkdir dir="${build.log.dir}"/>
        <!-- Make hardcoded version available to IDE plugin -->
        <copy file="${checkstyle.config}" todir="${build.log.dir}">
            <filterchain>
                <expandproperties/>
            </filterchain>
        </copy>
    </target>

    <!-- Runs checkstyle over the java source -->
    <target name="checkstyle-check">
        <uptodate property="checkstyle.up-to-date" value="true"
                  targetfile="${build.dir}/checkstyle">
            <srcfiles dir="${src.java.dir}" includes="**/*.java"/>
            <srcfiles dir="${test.java.dir}" includes="**/*.java"/>
        </uptodate>
    </target>

    <!-- Runs checkstyle over the java source -->
    <target name="checkstyle" depends="generate-checkstyle-config, checkstyle-check"
            unless="checkstyle.up-to-date">
        <mkdir dir="${build.log.dir}"/>
        <taskdef name="checkstyle" classname="com.puppycrawl.tools.checkstyle.CheckStyleTask">
            <classpath refid="project.class.path"/>
        </taskdef>
        <checkstyle config="${checkstyle.config}" failOnViolation="true">
            <fileset dir="${src.java.dir}/" includes="**/*.java"/>
            <fileset dir="${test.java.dir}" includes="**/*.java"/>
            <formatter type="plain"/>
            <formatter type="xml" tofile="${build.log.dir}/checkstyle-report.xml"/>
        </checkstyle>
    </target>

    <!-- Compiles the source classes -->
    <target name="compile-src" description="compile src files">
        <mkdir dir="${build.classes.dir}"/>
        <javac destdir="${build.classes.dir}" debug="true" deprecation="on">
            <src location="${src.java.dir}"/>
            <classpath refid="project.class.path"/>
        </javac>
    </target>


    <target name="generate-c-headers">
        <mkdir dir="${generated.src.c.dir}"/>
        <javah destdir="${generated.src.c.dir}" class="net.sf.jpam.Pam">
            <classpath>
                <pathelement location="${build.classes.dir}"/>
            </classpath>
        </javah>
    </target>

    <target name="compile-tests" depends="compile-src">
        <mkdir dir="${test.classes.dir}"/>
        <javac destdir="${test.classes.dir}" debug="true" deprecation="on">
            <src location="${test.java.dir}"/>
            <classpath>
                <path path="${build.classes.dir}"/>
                <path refid="project.class.path"/>
            </classpath>
        </javac>
    </target>


    <target name="test" depends="run-tests-no-check" if="tests.failed"
            description="Runs Tests">
        <fail message="One or more tests failed. See the log file for details."/>
    </target>


    <!-- Make sure you have copied junit-3.8.1.jar to $ANT_HOME/lib -->
    <target name="run-tests-no-check"
            depends="compile-tests, install-shared-object, install-jaas-config">
        <mkdir dir="${test.results.dir}"/>

        <condition property="test.pattern" value="*">
            <not>
                <isset property="test.case"/>
            </not>
        </condition>
        <condition property="test.pattern" value="${test.case}">
            <isset property="test.case"/>
        </condition>


        <junit printSummary="true" failureproperty="tests.failed" fork="true" dir="${basedir}">
            <sysproperty key="java.util.logging.config.file"
                         file="${test.logging.properties.file}"/>
            <classpath>
                <path location="${test.classes.dir}"/>
                <path location="${build.classes.dir}"/>
                <path refid="project.class.path"/>
            </classpath>
            <formatter type="xml"/>
            <formatter type="plain" usefile="false"/>
            <batchtest todir="${test.results.dir}">
                <fileset dir="${test.classes.dir}">
                    <include name="**/${test.pattern}Test.class"/>
                    <exclude name="**/Abstract*"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <!-- assumes we have permission -->
    <target name="install-shared-object"
            depends="shared-object"
            description="Installs the shared object. Does not install config">
        <echo message="Installing shared object named: ${shared.library.name} to ${native.java.library.path}"/>
        <copy todir="${native.java.library.path}">
            <fileset dir="${generated.src.c.dir}" includes="${shared.library.name}"/>
        </copy>
    </target>

    <target name="install-jaas-config">
        <copy file="${src.config.dir}/${os}/.java.login.config" todir="${env.HOME}"/>
    </target>

    <!-- Builds a jar file containing the application  -->
    <target name="dist-jar" depends="compile" description="Builds the application jar">
        <mkdir dir="${dist.dir}"/>
        <jar destfile="${build.dir}/${name}-${version}.jar">
            <fileset dir="${build.classes.dir}"/>
            <fileset dir="${basedir}" includes="LICENSE.txt"/>
        </jar>
    </target>


    <!-- Builds a src release tar-ball -->
    <target name="dist-src" depends="dist-jar, zip-javadoc"
            description="Builds a source distribution">
        <zip destfile="${build.dir}/${name}-${version}-src.zip">
            <zipfileset dir="${src.dir}" prefix="${name}-${version}">
                <exclude name="build/**"/>
                <exclude name="jpam.iws"/>
                <exclude name="src/site/**"/>
            </zipfileset>
        </zip>
    </target>


    <!-- Builds a release tar-ball for the os this target is executed on -->
    <target name="dist-tgz" depends="test, dist-jar, zip-javadoc, dist-src"
            description="Builds a distribution for this os">
        <tar destfile="${dist.dir}/${name}-${os}-${version}.tgz" compression="gzip">
            <tarfileset dir="${build.dir}" prefix="${name}-${version}">
                <include name="${name}-${version}.jar"/>
                <include name="${name}-${version}-src.zip"/>
                <include name="${name}-${version}-${javadoc.zip}"/>
            </tarfileset>
            <tarfileset dir="${src.dist.dir}" prefix="${name}-${version}">
                <include name="*.txt"/>
            </tarfileset>
            <tarfileset dir="${generated.src.c.dir}" prefix="${name}-${version}">
                <include name="${shared.library.name}"/>
            </tarfileset>
            <tarfileset dir="${src.config.dir}/${os}" prefix="${name}-${version}"/>
        </tar>
    </target>

    <!-- Create zip file for javadocs -->
    <target name="zip-javadoc" depends="javadoc" description="Creates a zip file for javadocs">
        <zip destfile="${build.dir}/${name}-${version}-${javadoc.zip}">
            <zipfileset dir="${build.javadoc.dir}" prefix="docs/"/>
        </zip>
    </target>

    <!-- Produce the javadocs -->
    <target
            name="javadoc"
            description="Creates the javadocs">

        <mkdir dir="${build.javadoc.dir}"/>
        <javadoc
                sourcepath="${src.java.dir}"
                destdir="${build.javadoc.dir}"
                packagenames="net.sf.*"
                Package="true"
                overview="${src.java.dir}/net/sf/jpam/overview.html"
                author="true"
                version="true"
                use="true"
                windowtitle="${name}">
            <classpath refid="project.class.path"/>
            <doctitle><![CDATA[<h1>${name}</h1>]]></doctitle>

        </javadoc>
    </target>

    <condition property="isLinux">
        <os name="Linux"/>
    </condition>

    <target name="rpm" description="Build a JPackage compatible rpm">
        <echo message="${isLinux}"/>
        <mkdir dir="${build.rpm.dir}"/>
        <mkdir dir="${build.rpm.dir}/SPECS"/>
        <mkdir dir="${build.rpm.dir}/SOURCES"/>
        <mkdir dir="${build.rpm.dir}/BUILD"/>
        <mkdir dir="${build.rpm.dir}/SRPMS"/>
        <copy file="${src.rpm.dir}/jpam.spec" todir="${build.rpm.dir}/SPECS"/>
        <copy file="${build.dir}/${name}-${version}-src.zip" todir="${build.rpm.dir}/SOURCES"/>
        <rpm specfile="jpam.spec"
             topdir="${build.rpm.dir}"
             output="${build.rpm.dir}/out"
             error="${build.rpm.dir}/err"/>


    </target>

    <!-- Requires quite a few utils to be present. See building in the doco -->
    <target name="book">
        <copy file="src/site/apt/.aptconvert" todir="${env.HOME}"/>
        <mkdir dir="${build.dir}/book"/>
        <copy todir="${build.dir}/book/images">
            <fileset dir="src/site/resources/images"/>
        </copy>
        <copy todir="${build.dir}/book">
            <fileset dir="src/site/apt/documentation">
                <include name="*.apt"/>
            </fileset>
            <filterset>
                <filter token=".gif" value=""/>
                <filter token=".png" value=""/>
                <filter token=".jpg" value=""/>
            </filterset>
            <fileset dir="src/site/apt">
                <include name="features.apt"/>
                <include name="samples.apt"/>
                <include name="nameandlogo.apt"/>
            </fileset>
            <fileset dir="src/site/resources/book"/>
        </copy>

        <replace dir="${build.dir}/book" token=".jpg" includes="*.apt"/>
        <replace dir="${build.dir}/book" token=".gif" includes="*.apt"/>
        <replace dir="${build.dir}/book" token=".png" includes="*.apt"/>

        <property name="extension" value="pdf"/>
        <antcall target="create_book" inheritall="true"/>
        <exec executable="pdftk" newenvironment="false" dir="${build.dir}/book" spawn="false">
            <arg value="title_page.pdf"/>
            <arg value="JPamUserGuide.pdf"/>
            <arg value="cat"/>
            <arg value="output"/>
            <arg value="TempJPamUserGuide.pdf"/>
        </exec>
        <move file="${build.dir}/book/TempJPamUserGuide.pdf" tofile="${build.dir}/book/JPamUserGuide.pdf"/>
        <copy file="${build.dir}/book/JPamUserGuide.pdf" todir="${build.dir}/site"/>
    </target>

    <target name="singlepageguide">
        <mkdir dir="${build.dir}/book"/>
        <copy todir="${build.dir}/book/images">
            <fileset dir="src/site/resources/images"/>
        </copy>
        <copy todir="${build.dir}/book">
            <fileset dir="src/site/resources/css"/>
        </copy>
        <copy todir="${build.dir}/book">
            <fileset dir="src/site/apt/documentation">
                <include name="*.apt"/>
            </fileset>
            <fileset dir="src/site/apt">
                <include name="features.apt"/>
                <include name="samples.apt"/>
                <include name="nameandlogo.apt"/>
            </fileset>
        </copy>

        <replace dir="${build.dir}/book" token=".jpg" includes="*.apt"/>
        <replace dir="${build.dir}/book" token=".gif" includes="*.apt"/>
        <replace dir="${build.dir}/book" token=".png" includes="*.apt"/>

        <antcall target="create_singlepageguide" inheritall="true"/>

        <copy file="${build.dir}/book/JPamUserGuide.html" todir="${build.dir}/site"/>
        <copy file="src/site/resources/images/apt_toc.gif" todir="${build.dir}/site"/>
    </target>

    <macrodef name="create_artifact">
        <attribute name="extension" default="NOT SET"/>
        <element name="some-tasks" optional="yes"/>
        <sequential>
            <exec executable="aptconvert" newenvironment="false" dir="${build.dir}/book"
                  spawn="false">
                <arg value="JPamUserGuide.@{extension}"/>
                <arg value="preface.apt"/>
                <arg value="introduction.apt"/>
                <arg value="dependencies.apt"/>
                <arg value="getting_started.apt"/>
                <arg value="configuration.apt"/>
                <arg value="features.apt"/>
                <arg value="samples.apt"/>
                <arg value="logging.apt"/>
                <arg value="faq.apt"/>
                <arg value="limitations.apt"/>
                <arg value="building.apt"/>
                <arg value="nameandlogo.apt"/>
            </exec>
        </sequential>
    </macrodef>

    <target name="prepare-book-resources" depends="checkstyle-check">
        <antcall target="compile"/>
        <antcall target="ydoc"/>
    </target>

    <!-- Produce the ydocs images for the book and singlepage guide.  -->
    <target name="ydoc" description="Creates the javadocs">

        <javadoc
                sourcepath="${src.java.dir}"
                destdir="${build.dir}/book/apidocs"
                packagenames="net.sf.*"
                Package="true"
                overview="${src.java.dir}/net/sf/jpam/overview.html"
                author="true"
                version="true"
                use="true"
                windowtitle="${name}"
                doclet="ydoc.doclets.YStandard"
                docletpath="${basedir}/tools/ydoc-2.2_02-jdk1.5/lib/ydoc.jar:${basedir}/tools/ydoc-2.2_02-jdk1.5/lib/class2svg.jar:${basedir}/tools/ydoc-2.2_02-jdk1.5/resources:${build.classes.dir}"
                additionalparam="-umlautogen">
            <tag name="noinspection" description="IntelliJ Inspection Ignore tag"
                 enabled="false"/>
            <tag name="revised"
                 description="Indicates the version a public APU method was revised"
                 enabled="true"/>
            <classpath refid="project.class.path"/>
            <header><![CDATA[<a href="/" target="_top">${name}</a>]]></header>
        </javadoc>
    </target>


    <target name="create_singlepageguide" depends="prepare-book-resources">
        <create_artifact extension="html"/>
    </target>


    <target name="create_book" depends="prepare-book-resources">
        <create_artifact extension="pdf"/>
    </target>


</project>
