<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Mibble MIB Parser (www.mibble.org)

  See LICENSE.txt for licensing information.

  Copyright (c) 2004-2017 Per Cederberg. All rights reserved.
-->

<project name="mibble" default="all">

<!--

  BUILD INSTRUCTIONS

  This is an Ant build file for Mibble. You can use it to simplify
  compiling and packaging of your own modifications to the original
  Mibble source code. Note that this build file will overwrite content
  in the current directory as a result of the build process.

  To run Ant, choose one (or more) of the targets under the TOP-LEVEL
  TARGETS heading below. If you run Ant without any target specified
  it will build the whole Mibble project, while also performing tests
  and creating the documentation.

  The following build properties are available (specify on the
  command-line with -Dname=value):

    build.name
        The base name of the build. Defaults to "mibble".

    build.version
        The build version number. Defaults to todays date.

    build.optimized
        The optimized build flag. Set to any value to compile without
        debug information. By default this flag is off.

-->


<!-- INITIALIZATION -->
  <property name="build.name" value="${ant.project.name}" />
  <property name="build.title" value="Mibble" />
  <property name="build.java.package" value="net.percederberg.mibble" />
  <tstamp>
    <format property="build.year" pattern="yyyy" />
    <format property="build.printdate" pattern="yyyy-MM-dd" />
  </tstamp>
  <property name="build.version" value="${DSTAMP}" />
  <property name="build.date" value="${DSTAMP}" />
  <property name="build.sysclasspath" value="ignore" />
  <patternset id="pattern.srcfiles">
    <include name="**/*.css" />
    <include name="**/*.grammar" />
    <include name="**/*.java" />
    <include name="**/*.sh" />
    <include name="**/*.txt" />
    <include name="**/*.xml" />
    <include name="**/*.xsl" />
  </patternset>
  <path id="project.class.path">
    <fileset dir="lib" includes="*.jar" />
  </path>


<!-- TOP-LEVEL TARGETS -->
  <target name="all" description="Performs a full rebuild, including tests"
          depends="compile,test,doc" />

  <target name="minimal" description="Performs a minimal rebuild, excluding tests"
          depends="compile,doc" />

  <target name="compile" description="Compiles the source code"
          depends="compile-clean,compile-codegen,compile-java,compile-scripts" />

  <target name="test" description="Runs the validation tests"
          depends="compile,test-validator" />

  <target name="doc" description="Generates the documentation"
          depends="doc-clean,doc-text,doc-html,doc-java" />


<!-- COMPILATION TARGETS -->
  <target name="compile-clean">
    <delete quiet="true">
      <fileset dir="bin" />
      <fileset dir="classes" />
      <fileset dir="lib" includes="mibble*.jar" />
    </delete>
    <mkdir dir="bin" />
    <mkdir dir="classes" />
    <mkdir dir="lib" />
    <replaceregexp match="\s+$" replace="" flags="g" byline="true">
      <fileset dir=".">
        <patternset refid="pattern.srcfiles" />
        <include name="**/*.bat" />
        <include name="src/mibs/**/*" />
      </fileset>
    </replaceregexp>
    <fixcrlf srcdir="." tab="remove">
      <patternset refid="pattern.srcfiles" />
      <include name="src/mibs/**/*" />
    </fixcrlf>
    <fixcrlf srcdir="." eol="dos" eof="add" tab="remove">
      <include name="**/*.bat" />
    </fixcrlf>
  </target>

  <target name="compile-codegen">
    <taskdef resource="ant-grammatica.properties" classpathref="project.class.path" />
    <grammatica grammar="src/grammar/asn1.grammar">
      <java dir="src/java"
            package="${build.java.package}.asn1"
            public="true" />
    </grammatica>
  </target>

  <target name="compile-java"
          depends="compile-java-optimized,compile-java-debug">
    <echo file="classes/net/percederberg/mibble/build.properties"># Automatically Generated, DO NOT EDIT
build.title = ${build.title}
build.version = ${build.version}
build.date = ${build.printdate}
</echo>
    <unjar src="lib/grammatica-1.6.jar" dest="classes">
      <patternset>
        <include name="net/percederberg/grammatica/parser/**/*.class" />
      </patternset>
    </unjar>
    <jar jarfile="lib/${build.name}-parser-${build.version}.jar">
      <manifest>
        <attribute name="Main-Class" value="${build.java.package}.MibbleBrowser" />
        <attribute name="Class-Path" value="${build.name}-mibs-${build.version}.jar" />
      </manifest>
      <fileset dir="." includes="README.txt" />
      <fileset dir="." includes="LICENSE.txt" />
      <fileset dir="classes" />
    </jar>
    <jar jarfile="lib/${build.name}-mibs-${build.version}.jar">
      <zipfileset dir="src/mibs" prefix="mibs" />
    </jar>
    <unjar src="lib/snmp6_1.jar" dest="classes" />
    <jar jarfile="${build.title}Browser.jar">
      <manifest>
        <attribute name="Main-Class" value="${build.java.package}.MibbleBrowser" />
      </manifest>
      <fileset dir="." includes="README.txt" />
      <fileset dir="." includes="LICENSE.txt" />
      <fileset dir="classes" />
      <zipfileset dir="src/mibs" prefix="mibs" />
    </jar>
  </target>

  <target name="compile-java-optimized" if="build.optimized">
    <javac srcdir="src/java"
           destdir="classes"
           classpathref="project.class.path"
           source="1.7"
           target="1.7"
           debug="off"
           optimize="on"
           deprecation="on" />
  </target>

  <target name="compile-java-debug" unless="build.optimized">
    <javac srcdir="src/java"
           destdir="classes"
           classpathref="project.class.path"
           source="1.7"
           target="1.7"
           debug="on"
           deprecation="on" />
  </target>

  <target name="compile-scripts">
    <copy todir="bin" overwrite="true">
      <fileset dir="src/bin" />
      <filterset>
        <filter token="NAME" value="${build.name}" />
        <filter token="VERSION" value="${build.version}" />
      </filterset>
    </copy>
    <chmod dir="bin" includes="*.sh" perm="ugo+x" />
  </target>


<!-- TEST TARGETS -->
  <target name="test-validator">
    <java classname="${build.java.package}.MibbleValidator"
          classpath="lib/${build.name}-parser-${build.version}.jar"
          failonerror="true">
      <arg value="src/mibs" />
    </java>
  </target>


<!-- DOCUMENTATION TARGETS -->
  <target name="doc-clean">
    <delete dir="doc" quiet="true" />
    <mkdir dir="doc" />
    <mkdir dir="doc/api" />
  </target>

  <target name="doc-text">
    <xslt style="src/doc/txt.xsl"
          basedir="src/doc/release"
          destdir="doc"
          extension=".txt"
          includes="*.xml">
      <param name="name" expression="${build.name}" />
      <param name="version" expression="${build.version}" />
      <param name="year" expression="${build.year}" />
      <param name="date" expression="${build.printdate}" />
    </xslt>
  </target>

  <target name="doc-html">
    <copy file="src/doc/style.css"
          todir="doc" />
    <xslt style="src/doc/html.xsl"
          basedir="src/doc/release"
          destdir="doc"
          extension=".html"
          includes="*.xml">
      <param name="name" expression="${build.name}" />
      <param name="version" expression="${build.version}" />
      <param name="year" expression="${build.year}" />
      <param name="date" expression="${build.printdate}" />
      <param name="style" expression="style.css" />
    </xslt>
  </target>

  <target name="doc-java">
    <javadoc packagenames="${build.java.package}.*"
             excludepackagenames="${build.java.package}.asn1.*,${build.java.package}.browser.*"
             sourcepath="src/java"
             destdir="doc/api"
             classpath="classes"
             classpathref="project.class.path"
             version="false"
             use="true"
             author="false"
             windowtitle="${build.title} ${build.version} Documentation"
             failonerror="true" />
  </target>

</project>
