<?xml version="1.0" encoding="UTF-8"?>

<!-- ====================================================================== -->
<!--  Apache Ant build script for building the Robocode setup file          -->
<!-- ====================================================================== -->

<project name="build" default="archive">
  <description>Apache Ant build script for building the Robocode archive files</description>

  <!-- ====================================== -->
  <!-- Global properties                      -->
  <!-- ====================================== -->

  <!-- Read out the version from the versions.txt -->
  <loadfile property="version" srcfile="../robocode/resources/versions.txt">
    <filterchain>
      <headfilter lines="1"/>
      <replacestring from="Version " to=""/>
      <replacestring from=" " to="-"/>
      <striplinebreaks/>
    </filterchain>
  </loadfile>

  <!-- Disable debug information when compiling -->  
  <property name="debug" value="off"/>  <!-- Debug information is stripped from the setup file -->

  <!-- The build folder -->
  <property name="build" location="build"/>

  <!-- The source folders -->
  <property name="src.robocode" location="../robocode"/>
  <property name="src.extract" location="../robocodeextract"/>
  <property name="src.autoextract" location="../autoextract"/>
  <property name="src.robots" location="../robocodeextract/robots"/>
  <property name="src.roborumble" location="../roborumble"/>
  <property name="src.cachecleaner" location="../robocode/ar"/>
  <property name="src.tests" location="../tests"/>
  <property name="src.testrobots" location="../tests/robots"/>

  <!-- License file -->
  <property name="license.file" location="${src.extract}/license/cpl-v10.html"/>

  <!-- Versions file -->
  <property name="versions.file" location="${src.robocode}/resources/versions.txt"/>

  <!-- The output archive files -->
  <property name="setup.archive" value="robocode-setup-${version}.jar"/>
  <property name="sources.archive" value="robocode-src-${version}.zip"/>

  <!-- ====================================== -->
  <!-- Default Excludes                       -->
  <!-- ====================================== -->

  <defaultexcludes add="build.xml"/>	 <!-- Ant build scripts -->
  <defaultexcludes add="**/.*/**"/>      <!-- Eclipse configuration folders -->
  <defaultexcludes add="**/*.iml"/>      <!-- IDEA -->
  <defaultexcludes add="**/*.ipr"/>      <!-- IDEA -->
  <defaultexcludes add="**/*.iws"/>      <!-- IDEA -->
  <defaultexcludes add="**/*.bak"/>      <!-- Windows backup files -->
  <defaultexcludes add="**/Thumbs.db"/>  <!-- Windows thumbs databases -->
  <defaultexcludes add="**/~*"/>         <!-- Unix backup files -->
  <defaultexcludes add="**/.*"/>         <!-- Hidden unix file types -->
  <defaultexcludes add="**/.svn/**"/>    <!-- Subversion directories -->
  <defaultexcludes add="**/_svn/**"/>    <!-- Subversion directories -->
  <defaultexcludes add="**/launch/**"/>  <!-- Robocode launch directory -->

  <!-- ====================================== -->
  <!-- Target: init                           -->
  <!-- ====================================== -->
  <target
      name="init"
      description="Initializes the build">

  	<echo message="Initializes the build..."/>

  	<!-- Create timestamp -->
  	<tstamp/>

  	<!-- Create folder for containing build files -->
  	<mkdir dir="${build}/libs"/>
  </target>

  <!-- ====================================== -->
  <!-- Target: archive                        -->
  <!-- ====================================== -->
  <target
      name="archive"
      description="Build the Robocode distribution files"
      depends="archive.setup, archive.sources"
  />

  <!-- ====================================== -->
  <!-- Target: archive.setup                  -->
  <!-- ====================================== -->
  <target
      name="archive.setup"
      description="Build the Robocode setup archive"
      depends="clean, extract.jar, compile.autoextract">

    <echo message="Building Robocode setup archive (${setup.archive})..."/>

    <!-- Create the Robocode setup archive -->
	<jar destfile="${build}/${setup.archive}">
      <!-- Specify files to include -->
      <fileset dir="${build}" includes="robocode/AutoExtract.class"/>  <!-- Auto extract class -->
      <fileset file="${build}/extract.jar"/>  <!-- extract.jar file -->
      <!-- Set the main class of the manifest -->
      <manifest>
        <attribute name="Implementation-Title" value="Robocode installer"/>
	    <attribute name="Implementation-Version" value="v${version}, ${TODAY}"/> 
	    <attribute name="Implementation-Vendor" value="Mathew A. Nelson, Flemming N. Larsen"/>
        <attribute name="Main-Class" value="robocode.AutoExtract"/>
      </manifest>
    </jar>
  </target>

  <!-- ====================================== -->
  <!-- Target: archive.sources                -->
  <!-- ====================================== -->
  <target
  	  name="archive.sources"
	  description="Zip all source files">
	
	<echo message="Building Robocode sources archive (${sources.archive})..."/>
	
	<zip
        destfile="${build}/${sources.archive}"
        basedir=".."
        includes="autoextract/**, build/*, robocode/**, robocodeextract/**, roborumble/**, tools/**"
        excludes="build/build, **/bin/**, **/.svn/**, **/_svn/**, **/launch/**"
        defaultexcludes="no"
    />
  </target>

  <!-- ====================================== -->
  <!-- Target: compile.robocode               -->
  <!-- ====================================== -->
  <target
      name="compile.robocode"
      description="Compile Robocode"
  	  depends="init"
      unless="robocode.uptodate"> 

    <echo message="Compiling Robocode..."/>
 
  	<!-- Copy libs into the build folder -->
  	<copy file="${src.extract}/libs/codesize.jar" toDir="${build}/libs"/>
 
    <!-- Compile Robocode -->
    <compile srcdir="${src.robocode}" destdir="${build}" classpath="${build}/libs/codesize.jar"/>

    <!-- Flag that Robocode has been built -->
    <touch file="${build}/.robocode_build"/>
  </target>

  <uptodate property="robocode.uptodate" targetfile="${build}/.robocode_build">
    <srcfiles dir="${src.robocode}/robocode"/>
  </uptodate>

  <!-- ====================================== -->
  <!-- Target: compile.robots                 -->
  <!-- ====================================== -->
  <target
      name="compile.robots"
      description="Compile robots"
      depends="init, compile.robocode"
      unless="robots.uptodate">

    <echo message="Compiling robots..."/>

    <!-- Compile robots -->
    <compile srcdir="${src.extract}/robots" destdir="${build}/robots" classpath="${build}"/>

    <!-- Flag that robots have been built -->
    <touch file="${build}/.robots_build"/>
  </target>

  <uptodate property="robots.uptodate" targetfile="${build}/.robots_build">
    <srcfiles dir="${src.robots}"/>
  </uptodate>


    <!-- ====================================== -->
    <!-- Target: compile.testrobots             -->
    <!-- ====================================== -->
    <target
        name="compile.testrobots"
        description="Compile testing robots"
        depends="init, compile.robocode"
        unless="testrobots.uptodate">

      <echo message="Compiling testrobots..."/>

      <!-- Compile robots -->
      <compile srcdir="${src.testrobots}" destdir="${build}/robots" classpath="${build}"/>

      <copy file="${src.testrobots}/testing/TestTeam.team" toDir="${build}/robots/testing"/>

      <!-- Flag that robots have been built -->
      <touch file="${build}/.testrobots_build"/>
    </target>

    <uptodate property="testrobots.uptodate" targetfile="${build}/.testrobots_build">
      <srcfiles dir="${src.testrobots}"/>
    </uptodate>


    <!-- ====================================== -->
    <!-- Target: compile.tests                  -->
    <!-- ====================================== -->
    <target
        name="compile.tests"
        description="Compile tests"
          depends="init, robocode.jar"
        unless="tests.uptodate">

      <echo message="Compiling tests..."/>

      <!-- Compile Robocode -->
      <compile srcdir="${src.tests}/tests" destdir="${build}/tests" classpath="${build}/libs/codesize.jar;${build}/libs/robocode.jar;${src.tests}/libs/junit-4.4.jar"/>

      <!-- Flag that Robocode has been built -->
      <touch file="${build}/.tests_build"/>
    </target>

    <uptodate property="tests.uptodate" targetfile="${build}/.tests_build">
      <srcfiles dir="${src.tests}/tests"/>
    </uptodate>

  <!-- ====================================== -->
  <!-- Target: compile.autoextract            -->
  <!-- ====================================== -->
  <target
      name="compile.autoextract"
      description="Compile AutoExtract"
  	  depends="init"
      unless="autoextract.uptodate">

    <echo message="Compiling AutoExtract..."/>

    <!-- Compile AutoExtract -->
    <compile srcdir="${src.autoextract}" destdir="${build}"/>
  </target>

  <uptodate property="autoextract.uptodate" targetfile="${build}/robocode/AutoExtract.class">
    <srcfiles dir="${src.autoextract}"/>
  </uptodate>

  <!-- ====================================== -->
  <!-- Target: compile.roborumble             -->
  <!-- ====================================== -->
  <target
      name="compile.roborumble"
      description="Compile RoboRumble"
  	  depends="init"
      unless="roborumble.uptodate"> 

    <echo message="Compiling RoboRumble..."/>
 
  	<!-- Copy libs into the build folder -->
  	<copy file="${src.extract}/libs/codesize.jar" toDir="${build}/libs"/>

  	<!-- Compile RoboRumble -->
    <compile srcdir="${src.roborumble}" destdir="${build}" classpath="${build}/libs/codesize.jar"/>

    <!-- Flag that RoboRumble has been built -->
    <touch file="${build}/.roborumble_build"/>
  </target>

  <uptodate property="roborumble.uptodate" targetfile="${build}/.roborumble_build">
    <srcfiles dir="${src.roborumble}/roborumble"/>
  </uptodate>

  <!-- ====================================== -->
  <!-- Target: compile.cachecleaner           -->
  <!-- ====================================== -->
  <target
      name="compile.cachecleaner"
      description="Compile CacheCleaner"
      depends="init"
      unless="cachecleaner.uptodate">

    <echo message="Compiling CacheCleaner..."/>

    <!-- Compile CacheCleaner -->
    <compile srcdir="${src.cachecleaner}" destdir="${build}"/>

    <!-- Flag that CacheCleaner has been built -->
    <touch file="${build}/.cachecleaner_build"/>
  </target>

  <uptodate property="cachecleaner.uptodate" targetfile="${build}/.cachecleaner_build">
    <srcfiles dir="${src.cachecleaner}"/>
  </uptodate>

  <!-- ====================================== -->
  <!-- Target: robocode.jar                   -->
  <!-- ====================================== -->
  <target
      name="robocode.jar"
      description="Build robocode.jar"
      depends="init, compile.robocode"
      unless="robocode.jar.uptodate">

    <echo message="Building robocode.jar..."/>

    <!-- Create the robocode.jar file -->
    <jar destfile="${build}/libs/robocode.jar">
      <!-- Specify files to include -->
      <fileset dir="${build}" includes="robocode/**">   <!-- Class files -->
        <exclude name="robocode/AutoExtract.class"/>    <!-- Exclude AutoExtract.class -->
      </fileset>
      <fileset dir="${src.robocode}" includes="resources/**"/>  <!-- Resource files -->
      <fileset file="${license.file}"/>                         <!-- License file -->
      <!-- Set the main class of the manifest -->
      <manifest>
        <attribute name="Implementation-Title" value="Robocode"/>
	    <attribute name="Implementation-Version" value="v${version}, ${TODAY}"/> 
	    <attribute name="Implementation-Vendor" value="Mathew A. Nelson, Flemming N. Larsen, and other Robocode contributors"/>
        <attribute name="Main-Class" value="robocode.Robocode"/>
      </manifest>
    </jar>
  </target>

  <uptodate property="robocode.jar.uptodate" targetfile="${build}/libs/robocode.jar">
    <srcfiles dir="${src.robocode}"/>
  </uptodate>

  <!-- ====================================== -->
  <!-- Target: extract.jar                    -->
  <!-- ====================================== -->
  <target
      name="extract.jar"
      description="Build extract.jar"
      depends="init, robocode.jar, roborumble.jar, compile.robots, cachecleaner.jar, javadoc"
      unless="extract.jar.uptodate">

    <echo message="Building extract.jar..."/>

    <!-- Create the extract.jar file -->
    <jar destfile="${build}/extract.jar">
      <!-- Specify files to include -->
      <fileset dir="${src.extract}">      <!-- Files from the extract folder -->
        <exclude name="build/**"/>        <!-- Exclude build files (if any) -->
        <exclude name="bin/**"/>          <!-- Binary files (Eclipse build) -->
        <exclude name="**/.svn/**"/>      <!-- Exclude subversion files (if any) -->
        <exclude name="**/_svn/**"/>      <!-- Exclude subversion files (if any) -->
      </fileset>
      <fileset dir="${src.roborumble}">   <!-- Files from the roborumble folder -->
        <include name="*.bat"/>           <!-- Windows batch files -->
        <include name="*.sh"/>            <!-- Shell files (Mac, Linux, and similar) -->
      	<include name="config/*"/>        <!-- Configuration files -->
        <exclude name="bin/**"/>          <!-- Binary files (Eclipse build) -->
        <exclude name="**/.svn/**"/>      <!-- Exclude subversion files (if any) -->
        <exclude name="**/_svn/**"/>      <!-- Exclude subversion files (if any) -->
      </fileset>
      <fileset dir="${build}">
        <include name="libs/robocode.jar"/>      <!-- Builded robocode.jar -->
        <include name="libs/roborumble.jar"/>    <!-- Builded roborumble.jar -->
        <include name="libs/cachecleaner.jar"/>  <!-- Builded cachecleaner.jar -->
        <include name="javadoc/**"/>             <!-- Builded javadoc files -->
        <include name="robots/**"/>              <!-- Builded robots -->
      </fileset>
      <fileset file="${versions.file}"/>  <!-- Versions file -->
    </jar>
  </target>

  <uptodate property="extract.jar.uptodate" targetfile="${build}/extract.jar">
    <srcfiles dir="${src.extract}" excludes="build/**"/>
  </uptodate>

  <!-- ====================================== -->
  <!-- Target: roborumble.jar                 -->
  <!-- ====================================== -->
  <target
      name="roborumble.jar"
      description="Build roborumble.jar"
      depends="init, compile.robocode, compile.roborumble"
      unless="roborumble.jar.uptodate">

    <echo message="Building roborumble.jar..."/>

    <!-- Create the roborumble.jar file -->
    <jar destfile="${build}/libs/roborumble.jar">
      <!-- Specify files to include -->
      <fileset dir="${build}" includes="roborumble/**"/>  <!-- Class files -->
      <fileset file="${license.file}"/>                   <!-- License file -->
      <!-- Set the main class of the manifest -->
      <manifest>
        <attribute name="Implementation-Title" value="RoboRumble@Home"/>
	    <attribute name="Implementation-Version" value="v${version}, ${TODAY}"/> 
	    <attribute name="Implementation-Vendor" value="Albert Pérez, Flemming N. Larsen, and other Robocode contributors"/>
        <attribute name="Main-Class" value="roborumble.RoboRumbleAtHome"/>
      </manifest>
    </jar>
  </target>

  <uptodate property="roborumble.jar.uptodate" targetfile="${build}/libs/roborumble.jar">
    <srcfiles dir="${src.roborumble}"/>
  </uptodate>

  <!-- ====================================== -->
  <!-- Target: cachecleaner.jar               -->
  <!-- ====================================== -->
  <target
      name="cachecleaner.jar"
      description="Build cachecleaner.jar"
      depends="init, compile.cachecleaner"
      unless="cachecleaner.jar.uptodate">

    <echo message="Building cachecleaner.jar..."/>

    <!-- Create the cachecleaner.jar file -->
    <jar destfile="${build}/libs/cachecleaner.jar">
      <!-- Specify files to include -->
      <fileset dir="${build}" includes="ar/**"/>  <!-- Class files -->
      <!-- Set the main class of the manifest -->
      <manifest>
        <attribute name="Implementation-Title" value="CacheCleaner for Robocode"/>
	    <attribute name="Implementation-Version" value="v${version}, ${TODAY}"/> 
	    <attribute name="Implementation-Vendor" value="AaronR"/>
        <attribute name="Main-Class" value="ar.robocode.cachecleaner.CacheCleaner"/>
      </manifest>
    </jar>
  </target>

  <uptodate property="cachecleaner.jar.uptodate" targetfile="${build}/libs/cachecleaner.jar">
    <srcfiles dir="${src.cachecleaner}"/>
  </uptodate>

    <!-- ====================================== -->
    <!-- Target: run junit tests                -->
    <!-- ====================================== -->
    <target
        name="run.tests"
        description="Run tests"
          depends="init, compile.tests, compile.testrobots, compile.robots"
        unless="tests.green">

      <echo message="Running tests..."/>

    <junit printsummary="yes" haltonfailure="yes" dir="${build}">
      <classpath>
          <pathelement location="${build}/tests"/>
          <pathelement location="${build}/libs/robocode.jar"/>
          <pathelement location="${build}/libs/codesize.jar"/>
          <pathelement location="${src.tests}/libs/junit-4.4.jar"/>
      </classpath>
      <formatter type="plain"/>
        <batchtest fork="yes" todir="${build}/tests">
          <fileset dir="${src.tests}/tests">
            <include name="**/*Test*.java"/>
            <exclude name="**/RobotTestBed.java"/>
          </fileset>
        </batchtest>
    </junit>


      <!-- Flag that Robocode has been built -->
      <touch file="${build}/.tests_run"/>
    </target>

    <uptodate property="tests.green" targetfile="${build}/.tests_green">
      <srcfiles dir="${src.tests}/tests"/>
      <srcfiles dir="${src.robocode}/robocode"/>
    </uptodate>

  <!-- ====================================== -->
  <!-- Target: javadoc                        -->
  <!-- ====================================== -->
  <target
      name="javadoc"
      description="Generate javadoc"
  	  depends="init"
      unless="javadoc.uptodate">

    <echo message="Generating javadoc..."/>

    <!-- Generate the javadoc documentation -->
    <javadoc
        destdir="${build}/javadoc"
        sourcepath="${src.robocode}"
    	excludepackagenames="robocode.*, codesize.*, ar.*"
        windowtitle="Robocode ${version} API"
    	link="http://java.sun.com/j2se/1.5.0/docs/api">
      <fileset dir="${src.robocode}" defaultexcludes="yes">
        <include name="robocode/*.java"/>
        <exclude name="robocode/_*.java"/>
      	<include name="robocode/util/Utils.java"/>
      	<include name="robocode/control/**/*.java"/>
      	<exclude name="robocode/control/RandomFactory.java"/>
        <include name="robocode/robotinterfaces/**/*.java"/>
      </fileset>
      <doctitle><![CDATA[<h1>Robocode ${version} API</h1>]]></doctitle>
    </javadoc>

    <!-- Flag that javadoc has been built -->
    <touch file="${build}/.javadoc_build"/>
  </target>

  <uptodate property="javadoc.uptodate" targetfile="${build}/.javadoc_build">
    <srcfiles dir="${src.robocode}" includes="**/*.java"/>
  </uptodate>

  <!-- ====================================== -->
  <!-- Target: install                        -->
  <!-- ====================================== -->
  <target
      name="install"
      description="Install Robocode"
      depends="archive.setup">

    <echo message="Installing Robocode..."/>

    <!-- Run the Robocode setup file -->
    <java jar="${setup.file}" fork="true"/>
  </target>

  <!-- ====================================== -->
  <!-- Target: quick-install                  -->
  <!-- ====================================== -->
  <target
      name="quick-install"
      description="Quick install Robocode"
      depends="extract.jar">

    <echo message="Quick-installing Robocode..."/>

    <!-- Ask user where to install Robocode -->
    <input
        message="To which folder do you want to install Robocode?"
        addproperty="install.dir"/>

    <!-- Create the installation folder -->
    <mkdir dir="${install.dir}"/>

    <!-- Extract the extract.jar into the installation folder -->
    <unjar src="${build}/extract.jar" dest="${install.dir}"/>
  </target>

  <!-- ====================================== -->
  <!-- Target: clean                          -->
  <!-- ====================================== -->

  <!-- Clean up all build files -->
  <target
      name="clean"
      description="Clean up build files">

    <echo message="Cleaning up build files..."/>

    <!-- Delete the folder containing all build files -->
    <delete dir="${build}"/>
  </target>

  <!-- ====================================== -->
  <!-- Macro: compile                         -->
  <!-- ====================================== -->
  <macrodef name="compile">
    <attribute name="srcdir"/>
    <attribute name="destdir"/>
    <attribute name="classpath" default=""/>
    <sequential>
      <!-- Create folder for class files -->
      <mkdir dir="@{destdir}"/>
      <!-- Compile the sources -->
      <javac
          encoding="8859_1"
      	  srcdir="@{srcdir}"
          destdir="@{destdir}"
          classpath="@{classpath}"
          source="1.5"
          target="1.5"
          debug="${debug}"
          debuglevel="lines,vars,source"
          optimize="true"
          includes="**/*.java">
      	<compilerarg value="-Xlint"/>
	  </javac>
    </sequential>
  </macrodef>

</project>