<?xml version="1.0"?>

<!-- This file includes targets for building FreeCol,
     the metaserver, distribution packages, running tests
     and creating documentation. -->
<project name="FreeCol" default="package">
    <description>
        This file includes targets for building FreeCol,
        the metaserver, distribution packages, running tests
        and creating documentation.
    </description>
    <property name="java.target.version" value="1.5"/>
    <property name="freecol.name" value="freecol"/>
    <property environment="env"/>
    <property name="freecol.data.dir" value="${basedir}/data"/>
    <property name="freecol.rules.dir" value="${basedir}/data/rules"/>
    <property name="freecol.src.dir" value="${basedir}/src"/>
    <property name="freecol.build.dir" value="${basedir}/build"/>
    <property name="freecol.packaging.dir" value="${basedir}/packaging"/>
    <property name="freecol.release.dir" value="${basedir}/dist"/>
    <property name="freecol.doc.dir" value="${basedir}/doc"/>
    <property name="freecol.javadoc.dir" value="${freecol.doc.dir}/javadoc"/>
    <property name="freecol.jar.file" value="${basedir}/FreeCol.jar"/>
    <property name="freecol.metaserver.jar.file" value="${basedir}/FreeCol-metaserver.jar"/>
    <property name="freecol.overview.file" value="${freecol.src.dir}/overview.html"/>
    <property name="test" value="AllTests"/>
    <property name="miglayout.jar" value="jars/miglayout-4.0-swing.jar"/>
    <property name="cli.jar" value="jars/commons-cli-1.1.jar"/>
    <property name="cortado.jar" value="jars/cortado-0.6.0.jar"/>
<!--
    <property name="stax.jar" value="jars/stax2-api-3.0.3.jar"/>
    <property name="woodstox.jar" value="jars/woodstox-core-lgpl-4.0.9.jar"/>
-->
    <property name="stax.jar" value="jars/jsr173_1.0_api.jar"/>
    <property name="woodstox.jar" value="jars/wstx-lgpl-4.0pr1.jar"/>


    <path id="libraries.classpath">
        <!-- StAX xml parser -->
        <pathelement location="${basedir}/${stax.jar}" />
        <!-- Woodstox XML processor -->
        <pathelement location="${basedir}/${woodstox.jar}"/>
        <!-- MigLayout manager -->
        <pathelement location="${basedir}/${miglayout.jar}"/>
        <!-- Command line processor -->
        <pathelement location="${basedir}/${cli.jar}"/>
        <!-- Cortado Video Applet -->
        <pathelement location="${basedir}/${cortado.jar}"/>
        <!-- JOgg/JOrbis libraries -->
        <pathelement location="${basedir}/jars/jogg-0.0.7.jar"/>
        <pathelement location="${basedir}/jars/jorbis-0.0.15.jar"/>
    </path>
    <path id="junit.classpath">
        <pathelement location="${basedir}/test/lib/junit.jar" />
    </path>
    <path id="freecol.build.classpath">
        <path refid="libraries.classpath" />
    </path>
    <path id="test.build.classpath">
        <path refid="junit.classpath"/>
        <path refid="libraries.classpath" />
     </path>
    <path id="test.run.classpath">
        <pathelement location="${freecol.build.dir}" />
        <path refid="junit.classpath"/>
        <path refid="libraries.classpath" />
    </path>

    <condition property="isOsUnixLike">
        <os family="unix" />
    </condition>

    <condition property="isOsWindows">
            <os family="windows" />
    </condition>
    <condition property="isOsMac">
        <os family="mac" />
    </condition>

    <condition property="isNotMacOrWindows">
        <not>
            <or>
                <isset property="isOsWindows" />
                <isset property="isOsMac" />
            </or>
        </not>
    </condition>


    <target name="define-jsmoothgen">
        <taskdef name="jsmoothgen"
                classname="net.charabia.jsmoothgen.ant.JSmoothGen"
                classpath="${freecol.build.dir}/jars/jsmoothgen-ant.jar"
        />
    </target>

    <target name="init" unless="TSTAMP">
        <tstamp/>
    </target>

    <!-- Compiles the java source files. -->
    <target name="compile" depends="init">
        <mkdir dir="${freecol.build.dir}"/>
        <javac srcdir="${freecol.src.dir}"
            destdir="${freecol.build.dir}"
            classpathref="freecol.build.classpath"
            debug="on"
            optimize="on"
            deprecation="off"
            source="${java.target.version}"
            target="${java.target.version}"
            includeantruntime="false">
          <compilerarg value="-Xlint:unchecked"/>
          <compilerarg value="-Xlint:deprecation"/>
          <compilerarg value="-g"/>
        </javac>
    </target>

    <!-- Compile the JUnit tests -->
    <target name="compile-test" depends="init">
        <mkdir dir="${freecol.build.dir}"/>
        <javac srcdir="test/src"
            destdir="${freecol.build.dir}"
            classpathref="test.build.classpath"
            debug="on"
            optimize="on"
            deprecation="off"
            source="${java.target.version}"
            target="${java.target.version}"
            includeantruntime="false">
        </javac>
    </target>

    <target name="build" depends="init,compile" description="Compiles the source files.">
      <copy todir="${freecol.build.dir}">
        <fileset dir="${freecol.src.dir}" includes="**/*.xml" />
      </copy>
    </target>

    <target name="svnproperties" unless="svn.Revision"
            description="Creates the svn properties file using svn">
      <exec executable="svn" failifexecutionfails="false"
            output="${basedir}/build/svn.properties">
        <arg value="info"/>
        <arg value="."/>
      </exec>
      <property prefix="svn" file="build/svn.properties"/>
    </target>

    <target name="gitproperties" unless="svn.Revision"
            description="Creates the svn properties file using git">
      <exec executable="git" failifexecutionfails="false"
            output="${basedir}/build/svn.properties">
        <arg value="svn"/>
        <arg value="info"/>
        <arg value="."/>
      </exec>
      <property prefix="svn" file="build/svn.properties"/>
    </target>
            
    <target name="manifest" depends="svnproperties,gitproperties"
            description="Creates the Manifest file.">
      <manifest file="${basedir}/src/MANIFEST.MF">
        <attribute name="Created-By" value="FreeCol Team"/>
        <attribute name="Main-Class" value="net.sf.freecol.FreeCol"/>
        <attribute name="Class-Path" value="${stax.jar} ${woodstox.jar} jars/jogg-0.0.7.jar jars/jorbis-0.0.15.jar jars/tritonus_share.jar jars/vorbisspi1.0.3.jar ${miglayout.jar} ${cli.jar} ${cortado.jar}"/>
        <attribute name="Product-Name" value="FreeCol"/>
        <attribute name="Package-Title" value="FreeCol"/>
        <attribute name="Package-Version" value="${svn.Revision}"/>
        <attribute name="Package-Vendor" value="FreeCol Team"/>
      </manifest>
    </target>

    <!-- Compiles the source files and creates a JAR-file. -->
    <target name="package" depends="init,build,manifest" description="Compiles the source files and creates a JAR-file.">
        <jar jarfile="${freecol.jar.file}"
            basedir="${freecol.build.dir}"
            manifest="${basedir}/src/MANIFEST.MF"
            includes="net/**, org/**"
            excludes="**/.svn/**, **/metaserver/**"
        />
    </target>


    <!-- Compiles the metaserver and creates a JAR-file. -->
    <target name="metaserver" depends="init,build,manifest" description="Compiles the metaserver and creates a JAR-file.">
        <jar jarfile="${freecol.metaserver.jar.file}"
            basedir="${freecol.build.dir}"
            manifest="${basedir}/src/MANIFEST.MF"
            includes="net/**, org/**"
            excludes="**/.svn/**"
        />
    </target>

    <target name="mydist" depends="mydistWindows,mydistMac,mydistJava"
            description="Creates a distribution package for the current system." />
    <target name="mydistWindows" if="isOsWindows">
        <antcall target="winInstallerBundle"/>
    </target>
    <target name="mydistMac" if="isOsMac">
        <antcall target="tarMacJavaBundle"/>
    </target>
    <target name="mydistJava" if="isNotMacOrWindows">
        <antcall target="javaInstallerBundle"/>
    </target>

    <!--
        Creates archives and installers for distribution and places these in the
        "dist" directory.
        Various platforms can be omitted for prerelease testing by setting properties on the command line:
        skip.dist.mac="true" suppresses mac packaging
        skip.dist.installer="true" suppresses creation of installers
        skip.dist.java="true" suppresses zip and tar creation of the jarfiles
        skip.dist.source="true" suppresses packaging the source files

        For a formal release, none of these properties should be set.
    -->
    <target name="dist" description="Creates archives and installers for distribution."
            depends="init,initDist,zipJavaBundle,zipSourceBundle,javaInstallerBundle,winInstallerBundle,tarMacJavaBundle">

        <echo message="" />
        <echo message="-------------------------------------------------"/>
        <echo message="Distribution files created:"/>
        <echo message="${freecol.release.dir}"/>
        <echo message="-------------------------------------------------"/>
    </target>

    <target name="initDist" depends="init,setReleaseVersion">
        <mkdir dir="${freecol.release.dir}"/>
    </target>

    <target name="setReleaseVersion" depends="askReleaseVersion">
        <property name="freecol.release.name" value="${freecol.name}-${freecol.version}"/>
        <echo message="Creating Distribution of ${freecol.release.name}"/>
        <replaceregexp file="${freecol.src.dir}/net/sf/freecol/FreeCol.java"
                       match="FREECOL_VERSION\s*=\s*&quot;(.*?)&quot;\;"
                       replace="FREECOL_VERSION = &quot;${freecol.version}&quot;\;"
                       />
        <echo message="Game version updated in FreeCol.java" />
    </target>

    <target name="askReleaseVersion" unless="freecol.version">
        <input message="Please specify the release version (default: svn-${DSTAMP}): "
               addproperty="freecol.version" defaultvalue="svn-${DSTAMP}" />
    </target>

    <target name="prepareManual" depends="initDist,print-manual">
        <mkdir dir="${freecol.release.dir}/manual/${freecol.name}"/>
        <copy todir="${freecol.release.dir}/manual/${freecol.name}">
            <fileset dir="${freecol.doc.dir}" includes="FreeCol.pdf"/>
        </copy>
    </target>

    <target name="prepareBaseFiles" depends="initDist">
        <antcall target="clean"/>
        <antcall target="package"/>
        <mkdir dir="${freecol.release.dir}/base/${freecol.name}"/>
        <copy todir="${freecol.release.dir}/base/${freecol.name}">
            <fileset dir="${basedir}" includes="FreeCol.jar"/>
            <fileset dir="${freecol.packaging.dir}/common" includes="COPYING"/>
            <fileset dir="${freecol.packaging.dir}/common" includes="README"/>
            <fileset dir="${freecol.packaging.dir}/common" includes="freecol.cmd"/>
            <fileset dir="${freecol.packaging.dir}/common" includes="freecol.xpm"/>
            <fileset dir="${basedir}" includes="data/**" excludes="**/.svn/**"/>
            <fileset dir="${basedir}" includes="jars/**" excludes="**/.svn/**"/>
        </copy>
        <copy file="${freecol.packaging.dir}/common/freecol.sh"
              tofile="${freecol.release.dir}/base/${freecol.name}/freecol"/>
    </target>

    <target name="prepareSourceFiles" depends="initDist">
        <antcall target="clean"/>
        <mkdir dir="${freecol.release.dir}/source/${freecol.name}"/>
        <copy todir="${freecol.release.dir}/source/${freecol.name}">
            <fileset dir="${basedir}" excludes="**/.svn/**,dist/**,data/**,jars/**"/>
        </copy>
    </target>

    <target name="prepareWinFiles" depends="initDist,define-jsmoothgen">
        <jsmoothgen project="${freecol.build.dir}/startup.jsmooth" skeletonroot="${freecol.build.dir}/skeletons"/>
        <move todir="${freecol.release.dir}/win/${freecol.name}">
            <fileset dir="${freecol.build.dir}" includes="freecol.exe"/>
        </move>
        <mkdir dir="${freecol.release.dir}/win/${freecol.name}/Uninstaller"/>
        <jsmoothgen project="${freecol.build.dir}/uninstaller.jsmooth" skeletonroot="${freecol.build.dir}/skeletons"/>
        <move todir="${freecol.release.dir}/win/${freecol.name}/Uninstaller">
            <fileset dir="${freecol.build.dir}" includes="uninstaller.exe"/>
        </move>
    </target>

    <target name="zipJavaBundle" unless="skip.dist.java" depends="initDist,prepareManual,prepareBaseFiles">
        <zip destfile="${freecol.release.dir}/${freecol.release.name}.zip">
            <fileset dir="${freecol.release.dir}/base" includes="${freecol.name}/**"/>
            <fileset dir="${freecol.release.dir}/manual" includes="${freecol.name}/**"/>
        </zip>
    </target>

    <!-- A target that builds a 'source' package in the Debian 
	 of the term, stripped of all potential legal/building 
	 problems, such as jars without sources and already built jars,
	 as those cause problems and are unnecessary for a distribution.
    -->
    <target name="tarDistBundle" unless="skip.dist.source" depends="initDist,prepareSourceFiles">
        <tar destfile="${freecol.release.dir}/${freecol.release.name}-dist-src.tar.bz2" compression="bzip2">
            <tarfileset dir="${freecol.release.dir}/source/${freecol.name}" 
			prefix="${freecol.release.name}"
			includes="**"
			excludes="build/**, test/lib/**" />
	    <tarfileset dir="${basedir}" includes="data/**" 
			prefix="${freecol.release.name}"
			excludes="**/.svn/**, data/fonts/Plakat-Fraktur.ttf" 
			/>

	    <!-- We remove test/lib because there is junit.jar there -->
        </tar>
    </target>

    <target name="zipSourceBundle" unless="skip.dist.source" depends="initDist,prepareSourceFiles">
        <zip destfile="${freecol.release.dir}/${freecol.release.name}-src.zip">
            <fileset dir="${freecol.release.dir}/base" includes="${freecol.name}/**"/>
            <fileset dir="${freecol.release.dir}/source" includes="${freecol.name}/**"/>
        </zip>
    </target>

    <target name="executeIzpack">
        <izpack output="${freecol.release.dir}/${freecol.release.name}-installer.jar"
                installerType="standard"
                basedir="${freecol.release.dir}/"
                izpackdir="${freecol.build.dir}">
            <config><![CDATA[
                <installation version="1.0">
                    <info>
                        <appname>FreeCol</appname>
                        <appversion>@{freecol.version}</appversion>
                        <appsubpath>freecol</appsubpath>
                        <url>http://www.freecol.org</url>
                        <authors>
                            <author name="The FreeCol Team" email="developers@freecol.org" />
                        </authors>
                        <javaversion>1.5</javaversion>
                    </info>
                    <guiprefs width="640" height="480" resizable="no">
                        <laf name="looks">
                            <os family="unix" />
                        </laf>
                        <modifier key="langDisplayType" value="native"/>
                    </guiprefs>
                    <locale>
                        <langpack iso3="cat"/>
                        <langpack iso3="chn"/>
                        <langpack iso3="cze"/>
                        <langpack iso3="dan"/>
                        <langpack iso3="deu"/>
                        <langpack iso3="eng"/>
                        <!-- <langpack iso3="eus"/> -->
                        <langpack iso3="fin"/>
                        <langpack iso3="fra"/>
                        <!-- <langpack iso3="glg"/> -->
                        <langpack iso3="hun"/>
                        <langpack iso3="ita"/>
                        <langpack iso3="jpn"/>
                        <!-- <langpack iso3="mys"/> -->
                        <langpack iso3="ned"/>
                        <langpack iso3="nor"/>
                        <langpack iso3="pol"/>
                        <langpack iso3="por"/>
                        <!-- <langpack iso3="prt"/> -->
                        <!-- <langpack iso3="rom"/> -->
                        <langpack iso3="rus"/>
                        <!-- <langpack iso3="scg"/> -->
                        <langpack iso3="spa"/>
                        <!-- <langpack iso3="svk"/> -->
                        <langpack iso3="swe"/>
                        <langpack iso3="ukr"/>
                    </locale>
                    <resources>
                        <res id="LicencePanel.licence" src="base/@{freecol.name}/COPYING"/>
                        <res id="shortcutSpec.xml" src="@{freecol.build.dir}/winShortcutSpec.xml"/>
                        <res id="Unix_shortcutSpec.xml" src="@{freecol.build.dir}/unixShortcutSpec.xml"/>
                        <res id="installer.langsel.img" src="@{freecol.build.dir}/install.png"/>
                        <res id="packsLang.xml_cat" src="@{freecol.build.dir}/installer/lang.xml_cat"/>
                        <res id="packsLang.xml_chn" src="@{freecol.build.dir}/installer/lang.xml_chn"/>
                        <res id="packsLang.xml_cze" src="@{freecol.build.dir}/installer/lang.xml_cze"/>
                        <res id="packsLang.xml_dan" src="@{freecol.build.dir}/installer/lang.xml_dan"/>
                        <res id="packsLang.xml_deu" src="@{freecol.build.dir}/installer/lang.xml_deu"/>
                        <res id="packsLang.xml_eng" src="@{freecol.build.dir}/installer/lang.xml_eng"/>
                        <res id="packsLang.xml_eus" src="@{freecol.build.dir}/installer/lang.xml_eus"/>
                        <res id="packsLang.xml_fin" src="@{freecol.build.dir}/installer/lang.xml_fin"/>
                        <res id="packsLang.xml_fra" src="@{freecol.build.dir}/installer/lang.xml_fra"/>
                        <res id="packsLang.xml_glg" src="@{freecol.build.dir}/installer/lang.xml_glg"/>
                        <res id="packsLang.xml_hun" src="@{freecol.build.dir}/installer/lang.xml_hun"/>
                        <res id="packsLang.xml_ita" src="@{freecol.build.dir}/installer/lang.xml_ita"/>
                        <res id="packsLang.xml_jpn" src="@{freecol.build.dir}/installer/lang.xml_jpn"/>
                        <!-- <res id="packsLang.xml_mys" src="@{freecol.build.dir}/installer/lang.xml_mys"/> -->
                        <res id="packsLang.xml_ned" src="@{freecol.build.dir}/installer/lang.xml_ned"/>
                        <res id="packsLang.xml_nor" src="@{freecol.build.dir}/installer/lang.xml_nor"/>
                        <res id="packsLang.xml_pol" src="@{freecol.build.dir}/installer/lang.xml_pol"/>
                        <res id="packsLang.xml_por" src="@{freecol.build.dir}/installer/lang.xml_por"/>
                        <res id="packsLang.xml_prt" src="@{freecol.build.dir}/installer/lang.xml_prt"/>
                        <!-- <res id="packsLang.xml_rom" src="@{freecol.build.dir}/installer/lang.xml_rom"/> -->
                        <res id="packsLang.xml_rus" src="@{freecol.build.dir}/installer/lang.xml_rus"/>
                        <!-- <res id="packsLang.xml_scg" src="@{freecol.build.dir}/installer/lang.xml_scg"/> -->
                        <res id="packsLang.xml_spa" src="@{freecol.build.dir}/installer/lang.xml_spa"/>
                        <!-- <res id="packsLang.xml_svk" src="@{freecol.build.dir}/installer/lang.xml_svk"/> -->
                        <res id="packsLang.xml_swe" src="@{freecol.build.dir}/installer/lang.xml_swe"/>
                        <res id="packsLang.xml_ukr" src="@{freecol.build.dir}/installer/lang.xml_ukr"/>
                    </resources>
                    <native type="izpack" name="ShellLink.dll"/>
                    <panels>
                        <panel classname="HelloPanel"/>
                        <panel classname="LicencePanel"/>
                        <panel classname="TargetPanel"/>
                        <panel classname="PacksPanel"/>
                        <panel classname="InstallPanel"/>
                        <panel classname="ShortcutPanel"/>
                        <panel classname="SimpleFinishPanel"/>
                    </panels>
                    <packs>
                        <pack id="FreeCol" name="FreeCol" required="yes">
                            <description>The FreeCol application and data files. This pack includes all files required to play the game.</description>
                            <fileset dir="base/@{freecol.name}" targetdir="$INSTALL_PATH"/>
                            <fileset dir="@{freecol.packaging.dir}/common/" includes="Uninstaller/**" targetdir="$INSTALL_PATH"/>
                            <fileset dir="win/@{freecol.name}" targetdir="$INSTALL_PATH" os="windows"/>
                            <executable targetfile="$INSTALL_PATH/freecol" stage="never" keep="true" />
                            <executable targetfile="$INSTALL_PATH/Uninstaller/uninstaller" stage="never" keep="true" />
                        </pack>
                        <pack id="GameManual" name="Game Manual" required="no" preselected="yes">
                            <description>This pack includes the game manual.</description>
                            <fileset dir="manual/@{freecol.name}" targetdir="$INSTALL_PATH"/>
                        </pack>
                    </packs>
                </installation>
            ]]></config>
        </izpack>
    </target>

    <target name="javaInstallerBundle" unless="skip.dist.installer" description="Builds a platform independent installer"
            depends="initDist,prepareWinFiles,prepareManual,prepareBaseFiles,prepareSourceFiles,installer-translations">
        <taskdef name="izpack"
                classname="com.izforge.izpack.ant.IzPackTask"
                classpath="${freecol.build.dir}/jars/standalone-compiler.jar"
        />
        <antcall target="executeIzpack" />
    </target>

    <target name="winInstallerBundle" unless="skip.dist.installer" description="Builds a windows installer"
            depends="initDist,javaInstallerBundle,define-jsmoothgen">

        <copy todir="${freecol.release.dir}">
            <fileset dir="${freecol.build.dir}" includes="installer.jsmooth"/>
        </copy>
        <replace file="${freecol.release.dir}/installer.jsmooth"
                token="%ant:freecol.release.name%"
                value="${freecol.release.name}"
        />
        <replace file="${freecol.release.dir}/installer.jsmooth"
                token="%ant:freecol.installerjar%"
                value="${freecol.release.dir}/${freecol.release.name}-installer.jar"
        />
        <jsmoothgen project="${freecol.release.dir}/installer.jsmooth" skeletonroot="${freecol.build.dir}/skeletons"/>
    </target>

    <!-- Builds a doubleclickable application bundle running on Mac OS X -->
	<target name="macJavaBundle" unless="skip.dist.mac" depends="prepareManual,prepareBaseFiles">
        <!-- JarBundler is a feature-rich Ant task which will create a Mac OS X
             application bundle from a list of Jar files and a main class name.
             (GPL-Software) -> http://informagen.com/JarBundler/  -->
        <taskdef name="jarbundler"
                classname="net.sourceforge.jarbundler.JarBundler"
                classpath="${freecol.build.dir}/jars/jarbundler.jar"
        />

        <jarbundler dir="${freecol.release.dir}"
                            name="FreeCol"
                            mainclass="net.sf.freecol.FreeCol"
                            version="${freecol.version}"
                            vmoptions="-Xmx256M -Dapple.awt.fakefullscreen=true"
                            jvmversion="1.6+"
                            arguments="--windowed --freecol-data FreeCol.app/Contents/Resources/"
                            stubfile="${freecol.build.dir}/skeletons/FreeCol"
                            icon="${freecol.packaging.dir}/icons/FreeCol.icns">
            <jarfileset dir="${freecol.release.dir}/base/freecol/">
                <include name="**/*" />
                <exclude name="data/**" />
	    </jarfileset>
            <resourcefileset dir="${freecol.release.dir}/base/freecol/data" />
        </jarbundler>
    </target>

    <target name="tarMacJavaBundle" unless="skip.dist.mac" depends="tarMacJavaBundleOnUnixLike,tarMacJavaBundleOnWindows" description="Creates a Mac OS X application bundle inside a tar file.">
    </target>

    <target name="tarMacJavaBundleOnUnixLike" unless="skip.dist.mac" if="isOsUnixLike" depends="macJavaBundle">
        <delete file="${freecol.release.dir}/FreeCol.app/Contents/MacOS/FreeCol"/>
        <symlink link="${freecol.release.dir}/FreeCol.app/Contents/MacOS/FreeCol" 	resource="/System/Library/Frameworks/JavaVM.framework/Resources/MacOS/JavaApplicationStub"/>
        <exec executable="tar">
            <arg value="cfvj"/>
            <arg value="${freecol.release.dir}/${freecol.release.name}-mac.tar.bz2"/>
            <arg value="-C"/>
            <arg value="${freecol.release.dir}"/>
            <arg value="FreeCol.app/"/>
        </exec>
    </target>

    <target name="tarMacJavaBundleOnWindows" depends="macJavaBundle" unless="skip.dist.mac" if="isOsWindows">
        <echo message="Warning: You are building a Mac Os X Java Application Bundle containing a Copy Java Application Stub. Creating a bundle with the current copy of Java Launching Stub could lead to a future situation where the JavaApplicationStub file could become incompatible with a future OS X update. To avoid this, use a unix like system ... "/>
        <tar destfile="${freecol.release.dir}/${freecol.release.name}-mac.tar.bz2" compression="bzip2">
            <tarfileset dir="${freecol.release.dir}" mode="755"
                    includes="FreeCol.app/Contents/MacOS/FreeCol"
                    excludes="**/freecol*"
                    followsymlinks="false"
            />
            <tarfileset followsymlinks="false" dir="${freecol.release.dir}">
                <include name="FreeCol.app/**"/>
                <exclude name="**/freecol*"/>
                <exclude name="FreeCol.app/Contents/MacOS/FreeCol"/>
            </tarfileset>
        </tar>
    </target>

    <!-- Cleans the directories by removing auto-generated files. -->
    <target name="clean" depends="init" description="Cleans the directories by removing auto-generated files.">
        <delete quiet="true" includeEmptyDirs="true">
            <fileset dir="${freecol.build.dir}" includes="net/**" />
            <fileset dir="${freecol.build.dir}" includes="timestamp.inf" />
        	<fileset dir="${freecol.build.dir}" includes="report/**" />
        	<fileset dir="${freecol.build.dir}" includes="TEST-net.sf.freecol.*" />
            <fileset dir="${freecol.build.dir}" includes="dependencies.txt" />
        </delete>
    	<delete quiet="true" file="${freecol.jar.file}"/>
    	<delete quiet="true" file="TESTS-TestSuites.xml"/>
        <delete quiet="true" dir="${freecol.javadoc.dir}"/>
    	<delete quiet="true" file="Freecol.log"/>
    </target>

    <target name="distclean" depends="init,clean" description="Cleans the directories and removes the distribution directory.">
        <delete quiet="true" includeEmptyDirs="true">
            <fileset dir="${freecol.release.dir}" />
        </delete>
    </target>

    <target name="fix" depends="init">
        <fixcrlf srcdir="${freecol.src.dir}"
                includes="**/*.java"
                eol="asis"
                tab="remove"
                javafiles="yes"
                tablength="4"
                eof="asis"
        />
    </target>

    <!-- Generate JavaDoc documentation. -->
    <target name="javadoc" depends="init" description="Generate JavaDoc documentation.">
        <mkdir dir="${freecol.javadoc.dir}"/>
        <javadoc sourcepath="${freecol.src.dir}"
                overview="${freecol.overview.file}"
                destdir="${freecol.javadoc.dir}"
                packagenames="net.sf.freecol.*"
                private="true"
                classpathref="freecol.build.classpath"
                Author="true"
                Windowtitle="FreeCol Code Documentation"
        />
        <echo message="" />
        <echo message="-------------------------------------------------"/>
        <echo message="JavaDoc-generated documentation created:"/>
        <echo message="${freecol.javadoc.dir}"/>
        <echo message="-------------------------------------------------"/>
    </target>


    <!-- example:
        ant -lib test/lib/junit.jar -Dtest=common.model.BuildingTest test
        -->
    <target name="test" depends="build-unit-tests"
            description="Runs a particular unit test"
    >
        <echo message="if the next task fails then please read the build script" />
        <!-- if the next fails then here is the long answer:
            http://ant.apache.org/faq.html#delegating-classloader
            the short answer depends.
            if you have Ant 1.7, the junit.jar can exist on the classpath.
            upgrading to 1.7 may be the best solution.

            if you have Ant 1.6:
            ant -lib test/lib/junit.jar testall
            if you have a version of Ant prior to 1.6 on Windows:
            set CLASSPATH=%CLASSPATH%;test\lib\junit.jar
            ant testall
            if you have a version of Ant prior to 1.6 on UNIX:
            export CLASSPATH=${CLASSPATH}:test/lib/junit.jar; ant testall
            -->
        <junit printsummary="yes">
          <classpath refid="test.run.classpath"/>
          <formatter type="brief" usefile="false" />
          <formatter type="plain" />
          <test todir="${freecol.build.dir}" name="net.sf.freecol.${test}" />
          <sysproperty key="java.awt.headless" value="true" />
        </junit>
    	<echo message="For more detailed information about this test run see:"/>
    	<echo message="${freecol.build.dir}/TEST-net.sf.freecol.${test}.txt"/>
    </target>

    <target name="testall" depends="build-unit-tests" unless="skip.dist.test"
            description="Runs all unit tests">

    	<delete dir="${freecol.build.dir}/report" />
    	<mkdir dir="${freecol.build.dir}/report"/>
    	<mkdir dir="${freecol.build.dir}/report/xml"/>

        <echo message="if the next task fails then please read the build script" />
        <!-- if the next fails then here is the long answer:
            http://ant.apache.org/faq.html#delegating-classloader
            the short answer depends.
            if you have Ant 1.7, the junit.jar can exist on the classpath.
            upgrading to 1.7 may be the best solution.

            if you have Ant 1.6:
            ant -lib test/lib/junit.jar testall
            if you have a version of Ant prior to 1.6 on Windows:
            set CLASSPATH=%CLASSPATH%;test\lib\junit.jar
            ant testall
            if you have a version of Ant prior to 1.6 on UNIX:
            export CLASSPATH=${CLASSPATH}:test/lib/junit.jar; ant testall
            -->
        <junit printsummary="on" errorProperty="error.junit"
               failureProperty="error.junit" fork="yes" maxmemory="48m">
            <classpath refid="test.run.classpath"/>
            <formatter type="xml" />

           	<test todir="${freecol.build.dir}/report/xml" name="net.sf.freecol.AllTests" fork="yes"/>

        	<!--
        	<batchtest fork="false" todir="${freecol.build.dir}/report/xml">
                <fileset dir="test/src" includes="**/*Test.java" />
            </batchtest>
            -->
                <sysproperty key="java.awt.headless" value="true" />
        </junit>

        <junitreport>
            <fileset dir="${freecol.build.dir}/report/xml">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${freecol.build.dir}/report" />
        </junitreport>

    	<echo message="For detailed results of the test run see: ${freecol.build.dir}/report/index.html"/>

    	<fail if="error.junit" message="Automated Unit Tests failed."/>
    </target>

    <target name="build-unit-tests" depends="build">
        <depend srcDir="test/src" destDir="${freecol.build.dir}"
                cache="${freecol.build.dir}" />
        <javac srcdir="test/src" destdir="${freecol.build.dir}"
            classpathref="test.build.classpath" debug="on" deprecation="off"
            source="${java.target.version}" target="${java.target.version}"
            includeantruntime="false"/>
    </target>

    <target name="validate" description="Validates the specification.">
      <echo message="Validating freecol/specification.xml."/>
      <schemavalidate
         noNamespaceFile="schema/specification-schema.xsd"
         file="${freecol.rules.dir}/freecol/specification.xml">
      </schemavalidate>
      <echo message="Validating classic/specification.xml."/>
      <schemavalidate
         noNamespaceFile="schema/specification-schema.xsd"
         file="${freecol.rules.dir}/classic/specification.xml">
      </schemavalidate>
      <echo message="Validating testing/specification.xml."/>
      <schemavalidate
         noNamespaceFile="schema/specification-schema.xsd"
         file="${freecol.rules.dir}/testing/specification.xml">
      </schemavalidate>
    </target>

    <target name="print-manual"
            description="Creates the printable user guide.">
        <delete file="doc/FreeCol.ind" />
        <exec executable="pdflatex" dir="doc" >
            <arg file="doc/FreeCol.tex"/>
        </exec>
        <exec executable="makeindex" dir="doc" >
            <arg file="doc/FreeCol.idx"/>
        </exec>
        <exec executable="pdflatex" dir="doc" >
            <arg file="doc/FreeCol.tex"/>
        </exec>
    </target>

    <target name="online-manual"
            description="Creates the online user guide.">
        <delete file="doc/FreeCol.ind" />
        <exec executable="htlatex" dir="doc" >
            <arg file="doc/FreeCol.tex"/>
        </exec>
        <exec executable="tex" dir="doc">
            <arg line="\def\filename{{FreeCol}{idx}{4dx}{ind}} \input  idxmake.4ht" />
        </exec>
        <exec executable="makeindex" dir="doc" >
            <arg file="doc/FreeCol.4dx"/>
        </exec>
        <exec executable="htlatex" dir="doc" >
            <arg file="doc/FreeCol.tex"/>
        </exec>
    </target>

    <target name="manual" depends="print-manual,online-manual"
            description="Creates the printable and online user guides.">
    </target>

    <target name="fixTabsWindows">
        <fixcrlf srcdir="${freecol.src.dir}" tab="remove" tablength="4"
            includes="**/*.java" eol="crlf"
        />
    </target>

    <target name="fixTabsUnix">
        <fixcrlf srcdir="${freecol.src.dir}" tab="remove" tablength="4"
            includes="**/*.java" eol="lf"
        />
    </target>

    <!-- Prints a report on certain translations, which you can specify
        by setting the localeKey property ("-DlocaleKey=fi", for example),
        or all translations. Use "-emacs" in order to suppress extensive
        logging.  -->
    <target name="translation-report" depends="build"
            description="Creates report on translations.">
        <condition property="localeKey" else="">
            <isset property="localeKey" />
        </condition>
        <java classname="net.sf.freecol.tools.TranslationReport" classpath="build/" >
            <arg value="src/net/sf/freecol/client/gui/i18n/" />
            <arg value="${localeKey}" />
        </java>
    </target>

    <!-- Merges new translations from trunk. You can specify a
        particular language by setting the localeKey property
        ("-DlocaleKey=fi", for example), or all translations. -->
    <target name="merge-translations" depends="build"
            description="Merges translations from trunk.">
        <condition property="localeKey" else="">
            <isset property="localeKey" />
        </condition>
        <java classname="net.sf.freecol.tools.MergeTranslations" classpath="build/" >
            <arg value="../../trunk/src/net/sf/freecol/client/gui/i18n/" />
            <arg value="src/net/sf/freecol/client/gui/i18n/" />
            <arg value="${localeKey}" />
        </java>
    </target>

    <!-- Validates a savegame files specified as property file
        ("-Dsavegame=whatever.fsg", for example).  If file is a directory,
        all savegame files in that directory will be validated. -->
    <target name="validate-savegame" depends="build"
            description="Validates a savegame, or directory of savegames.">
        <condition property="savegame" else="">
            <isset property="savegame" />
        </condition>
        <java classname="net.sf.freecol.tools.SaveGameValidator">
            <classpath refid="test.run.classpath"/>
			<arg value="${savegame}" />
        </java>
    </target>

    <!-- Creates installer strings. -->
    <target name="installer-translations" depends="build"
            description="Creates properties files for installer translations.">
        <java classname="net.sf.freecol.tools.InstallerTranslations"
              classpath="build/" />
    </target>

    <!-- Creates '.desktop' file. -->
    <target name="desktop-entry" depends="build"
            description="Creates '.desktop' file.">
        <java classname="net.sf.freecol.tools.DesktopEntry"
              classpath="build/" />
    </target>

    <target name="pmd">
      <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"
               classpath="./pmd-4.1/lib/pmd-4.1.jar"/>
      <pmd shortFilenames="true">
        <ruleset>basic</ruleset>
        <ruleset>imports</ruleset>
        <!-- <ruleset>optimizations</ruleset> -->
        <ruleset>unusedcode</ruleset>
        <formatter type="html" toFile="pmd_report.html" linkPrefix="http://pmd.sourceforge.net/xref/"/>
        <fileset dir="src">
          <include name="**/*.java"/>
        </fileset>
      </pmd>
    </target>

    <target name="prepare-commit"
            description="Rebuild from scratch and run all tests."
            depends="clean,package,validate,testall"/>

    <!-- This is a shortcut for NetBeans, and possibly other IDEs. -->
    <target name="run" depends="package" description="Runs the game">
      <java jar="FreeCol.jar" fork="true">
        <jvmarg value="-Xmx128m" />
      </java>
    </target>

    <target name="cobertura" depends="build">
      <property name="cobertura.dir" value="cobertura" />

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

      <taskdef classpathref="cobertura.classpath" resource="tasks.properties" />

      <cobertura-instrument todir="build/instrumented-classes">
        <fileset dir="build/net">
          <include name="**/*.class"/>
        </fileset>
      </cobertura-instrument>

      <delete dir="${freecol.build.dir}/report" />
      <mkdir dir="${freecol.build.dir}/report"/>
      <mkdir dir="${freecol.build.dir}/report/xml"/>

      <echo message="if the next task fails then please read the build script" />
      <junit printsummary="on" errorproperty="error.junit" fork="yes" maxmemory="48m">
        <classpath location="build/instrumented-classes"/>
        <classpath location="build/net"/>
        <classpath refid="cobertura.classpath"/>
        <classpath refid="test.run.classpath"/>
        <formatter type="xml" />

        <test todir="${freecol.build.dir}/report/xml" name="net.sf.freecol.AllTests" fork="yes"/>
        <sysproperty key="java.awt.headless" value="true" />
      </junit>

      <cobertura-report srcdir="${freecol.src.dir}" destdir="cobertura/report"/>
    </target>


</project>
