<?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="11"/>
    <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.main.class" value="net.sf.freecol.FreeCol"/>
    <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-swing.jar"
              value="jars/miglayout-swing-5.0.jar"/>
    <property name="miglayout-core.jar"
              value="jars/miglayout-core-5.0.jar"/>
    <property name="cli.jar" value="jars/commons-cli-1.4.jar"/>
    <property name="cortado.jar" value="jars/cortado-0.6.0.jar"/>


    <path id="libraries.classpath">
        <!-- MigLayout manager -->
        <pathelement location="${basedir}/${miglayout-swing.jar}"/>
        <pathelement location="${basedir}/${miglayout-core.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.17.jar"/>
        <pathelement location="${basedir}/jars/jorbis-0.0.17.jar"/>
        <!-- findbugs annotations -->
        <pathelement location="${basedir}/jars/findbugs-annotations.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">
            <compilerarg value="-Xlint:unchecked"/>
            <compilerarg value="-Xlint:deprecation"/>
            <compilerarg value="-g"/>
        </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>

    <condition property="have-revision">
        <isset property="FreeCol.Revision" />
    </condition>

    <target name="git-raw-revision" unless="have-revision"
            description="Set gitRawRevision property using git">
        <exec executable="git" failifexecutionfails="false"
              outputproperty="gitRawRevision" logError="true">
            <arg value="rev-parse"/>
            <arg value="HEAD"/>
        </exec>
    </target>

    <target name="git-revision" unless="have-revision"
            depends="git-raw-revision" if="gitRawRevision"
            description="Set FreeCol.Revision from gitRawRevision using git">
        <exec executable="git" failifexecutionfails="false"
              outputproperty="FreeCol.Revision" logError="true">
            <arg value="describe"/>
            <arg value="--always"/>
        </exec>
        <echo message="${FreeCol.Revision}" />
    </target>

    <target name="manifest" depends="git-revision"
            description="Creates the Manifest file.">
        <manifestclasspath property="jar.classpath"
                           jarfile="Freecol.jar">
            <classpath refid="libraries.classpath" />
        </manifestclasspath>
        <manifest file="${basedir}/src/MANIFEST.MF">
            <attribute name="Created-By" value="FreeCol Team"/>
            <attribute name="Main-Class" value="${freecol.main.class}"/>
            <attribute name="Class-Path" value="${jar.classpath}"/>
            <attribute name="Product-Name" value="FreeCol"/>
            <attribute name="Package-Title" value="FreeCol"/>
            <attribute name="Package-Version" value="${FreeCol.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/**, splash.jpg"
             excludes="net/sf/freecol/metaserver/**"/>
    </target>

    <target name="metamanifest" depends="git-revision"
            description="Creates the Manifest file for the metaserver.">
        <manifestclasspath property="jar.classpath"
                           jarfile="${freecol.metaserver.jar.file}">
            <classpath refid="libraries.classpath" />
        </manifestclasspath>
        <manifest file="${basedir}/src/METAMANIFEST.MF">
            <attribute name="Created-By" value="FreeCol Team"/>
            <attribute name="Main-Class" value="net.sf.freecol.metaserver.MetaServer"/>
            <attribute name="Class-Path" value="${jar.classpath}"/>
            <attribute name="Product-Name" value="FreeCol"/>
            <attribute name="Package-Title" value="FreeCol"/>
            <attribute name="Package-Version" value="${FreeCol.Revision}"/>
            <attribute name="Package-Vendor" value="FreeCol Team"/>
        </manifest>
    </target>

    <!-- Compiles the metaserver and creates a JAR-file.
         FIXME: including everything in common is excessive. -->
    <target name="metaserver" depends="init,build,metamanifest"
            description="Compiles the metaserver and creates a JAR-file.">
        <jar jarfile="${freecol.metaserver.jar.file}"
             basedir="${freecol.build.dir}"
             manifest="${basedir}/src/METAMANIFEST.MF"
             includes="net/sf/freecol/metaserver/**,
                       net/sf/freecol/client/ClientOptions.class,
                       net/sf/freecol/common/**,
                       org/**"/>
    </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>

    <target name="nightly-release" description="Creates nightly build distribution.">
      <property name="print.manual.is.up.to.date" value="true"/>
      <property name="print.devmanual.is.up.to.date" value="true"/>
      <property name="nightly.version" value="true" />
      <property name="skip.dist.source" value="true" />

      <antcall target="distclean" />
      <antcall target="dist" />

      <mkdir dir="${freecol.release.dir}/releases"/>
      <copy todir="${freecol.release.dir}/releases">
          <fileset dir="${freecol.release.dir}" includes="freecol-git-*"/>
      </copy>
    </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;\;"/>
        <replaceregexp file="${freecol.doc.dir}/version.sty"
                       match="fcversion\}\{(.*?)\}"
                       replace="fcversion\}\{${freecol.version}\}"/>
        <echo message="Game version updated in FreeCol.java" />
    </target>

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

    <!-- Whenever calling an ant command that prompts for version
         passing in -Dnightly.version=true will set the
         version to the datestamp automatically -->
    <target name="setNightlyVersion" unless="freecol.version">
        <condition value="git-${DSTAMP}" property="freecol.version">
            <isset property="nightly.version"/>
        </condition>
    </target>

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

    <target name="prepareBaseFiles" depends="initDist,desktop-entry">
        <antcall target="cleanMain"/>
        <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="${basedir}" includes="splash.jpg"/>
            <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/**"/>
            <fileset dir="${basedir}" includes="jars/**"/>
        </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="cleanMain"/>
        <mkdir dir="${freecol.release.dir}/source/${freecol.name}"/>
        <copy todir="${freecol.release.dir}/source/${freecol.name}">
            <fileset dir="${basedir}"
                     excludes="splash.jpg,dist/**,data/**,jars/**,www.freecol.org/**,nbproject/**,srcdata/**,unused/**"/>
        </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>

    <target name="zipSourceBundle" unless="skip.dist.source"
            depends="initDist,prepareBaseFiles,prepareSourceFiles">
        <zip destfile="${freecol.release.dir}/${freecol.release.name}-src.zip">
            <fileset dir="${freecol.release.dir}/base"
                     includes="${freecol.name}/**"
                     excludes="${freecol.name}/FreeCol.jar,${freecol.name}/freecol.*,${freecol.name}/freecol"/>
            <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>11</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"/>
                    <native type="izpack" name="ShellLink_x64.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>
    
    <target name="win-complete-installer"
            description="Builds an installer for Windows with an included JVM."
            depends="initDist,prepareManual,prepareBaseFiles">
        
        <exec executable="jpackage" dir="." >
            <arg value="--input"/>
            <arg file="${freecol.release.dir}/base/freecol/"/>
            <arg value="--type"/>
            <arg value="exe"/>
            <arg value="--name"/>
            <arg value="FreeCol"/>
            <arg value="--dest"/>
            <arg file="${freecol.release.dir}"/>
            <arg value="--add-modules"/>
            <arg value="java.base,java.datatransfer,java.desktop,java.logging,java.xml"/>
            <arg value="--main-jar"/>
            <arg value="FreeCol.jar"/>
            <arg value="--win-dir-chooser"/>
            <arg value="--win-menu"/>
            <arg value="--win-shortcut"/>
            <arg value="--arguments"/>
            <arg value="--freecol-data app/data"/>
            <arg value="--app-version"/>
            <arg value="${freecol.version}"/>
            <arg value="--icon"/>
            <arg file="packaging/icons/freecol.ico"/>
        </exec>
    	
        <mkdir dir="${freecol.release.dir}/releases"/>
        <copy todir="${freecol.release.dir}/releases">
            <fileset dir="${freecol.release.dir}" includes="FreeCol-*"/>
        </copy>
    </target>
	
    <target name="mac-complete-installer"
            description="Builds an installer for macOs with an included JVM."
            depends="initDist,prepareManual,prepareBaseFiles">
        
        <exec executable="jpackage" dir="." >
            <arg value="--input"/>
            <arg file="${freecol.release.dir}/base/freecol/"/>
            <arg value="--name"/>
            <arg value="FreeCol"/>
            <arg value="--dest"/>
            <arg file="${freecol.release.dir}"/>
            <arg value="--add-modules"/>
            <arg value="java.base,java.datatransfer,java.desktop,java.logging,java.xml"/>
            <arg value="--main-jar"/>
            <arg value="FreeCol.jar"/>
            <arg value="--arguments"/>
            <arg value="--freecol-data $$APPDIR/data"/>
            <arg value="--app-version"/>
            <arg value="${freecol.version}"/>
            <arg value="--mac-package-identifier"/>
            <arg value="org.freecol.FreeCol"/>
            <arg value="--icon"/>
            <arg file="packaging/icons/FreeCol.icns"/>
        </exec>
        <mkdir dir="${freecol.release.dir}/releases"/>
        <copy todir="${freecol.release.dir}/releases">
            <fileset dir="${freecol.release.dir}" includes="FreeCol-*"/>
        </copy>
    </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/  -->
        <!-- Applications created with JarBundler attempt to prompt the user to
             download Java SE 6, a universalJavaApplicationStub is used for
             Java 1.8+.
             https://github.com/tofi86/universalJavaApplicationStub
             -->
        <taskdef name="jarbundler"
                 classname="com.ultramixer.jarbundler.JarBundler"
                 classpath="${freecol.build.dir}/jars/jarbundler-core-3.3.0.jar"/>
        <jarbundler dir="${freecol.release.dir}"
                    name="FreeCol"
                    mainclass="${freecol.main.class}"
                    version="${freecol.version}"
                    vmoptions="-Xmx2G -Dapple.awt.fakefullscreen=true"
                    jvmversion="1.11"
                    arguments="--windowed --freecol-data FreeCol.app/Contents/Resources/"
                    stubfile="${freecol.build.dir}/skeletons/universalJavaApplicationStub"
                    useJavaXKey="true"
                    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="tarMacJavaBundleOnWindows"
            description="Creates a Mac OS X application bundle inside a tar file.">
        <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>

    <target name="cleanBuild" 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" />
            <fileset dir="${freecol.doc.dir}"
                     excludes="*.tex *.sty images/* specification.*" />
        </delete>
    </target>

    <target name="cleanMain" depends="init"
            description="Clean the main (non-build) tree by removing auto-generated files.">
        <delete quiet="true" file="${freecol.jar.file}"/>
        <delete quiet="true" file="${freecol.metaserver.jar.file}"/>
        <delete quiet="true" file="TESTS-TestSuites.xml"/>
        <delete quiet="true" dir="${freecol.javadoc.dir}"/>
        <delete quiet="true" file="Freecol.log"/>
        <delete quiet="true" file="${basedir}/test/data/specification.xml"/>
    </target>

    <target name="clean" depends="cleanBuild,cleanMain"
            description="General cleanup.">
    </target>

    <target name="distclean" depends="init,clean"
            description="Cleans the directories and removes the distribution directory.">
        <delete quiet="true" includeEmptyDirs="true" followsymlinks="false"
                removeNotFollowedSymlinks="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">
        <!-- 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.  Later ant
             should be fine.

             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"/>
        <!-- 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="96M">
            <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>

    <!-- Run full unit test suite and fail on errors, use brief format for CI -->
    <target name="testci" depends="build-unit-tests" unless="skip.dist.test"
            description="Runs all unit tests for CI">
        <delete dir="${freecol.build.dir}/report" />
        <mkdir dir="${freecol.build.dir}/report"/>

        <junit printsummary="on" errorProperty="error.junit"
               failureProperty="error.junit" fork="yes" maxmemory="128M">
            <classpath refid="test.run.classpath"/>
            <formatter type="brief" usefile="false" />
            <formatter type="plain" />
            <sysproperty key="java.awt.headless" value="true" />
            <test todir="${freecol.build.dir}/report"
                  name="net.sf.freecol.AllTests" fork="yes"/>
        </junit>

        <echo message="For more detailed information about this test run see:"/>
        <echo message="${freecol.build.dir}/report/TEST-net.sf.freecol.AllTests.txt"/>
        <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"
               optimize="on"
               deprecation="off"
               source="${java.target.version}"
               target="${java.target.version}"
               includeantruntime="false">
            <compilerarg value="-Xlint:unchecked"/>
            <compilerarg value="-Xlint:deprecation"/>
        </javac>
    </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" unless="print.manual.is.up.to.date"
            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>
        <property name="print.manual.is.up.to.date" value="true" />
    </target>

    <target name="print-devmanual" unless="print.devmanual.is.up.to.date"
            description="Creates the printable user guide.">
        <delete file="doc/developer.ind" />
        <exec executable="pdflatex" dir="doc" >
            <arg file="doc/developer.tex"/>
        </exec>
        <exec executable="makeindex" dir="doc" >
            <arg file="doc/developer.idx"/>
        </exec>
        <exec executable="pdflatex" dir="doc" >
            <arg file="doc/developer.tex"/>
        </exec>
        <property name="print.devmanual.is.up.to.date" value="true" />
    </target>

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

    <target name="online-manual" unless="online.manual.is.up.to.date"
            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>
        <property name="online.manual.is.up.to.date" value="true" />
    </target>

    <target name="online-devmanual" unless="online.devmanual.is.up.to.date"
            description="Creates the online user guide.">
        <delete file="doc/developer.ind" />
        <exec executable="htlatex" dir="doc" >
            <arg file="doc/developer.tex"/>
        </exec>
        <exec executable="tex" dir="doc">
            <arg line="\def\filename{{developer}{idx}{4dx}{ind}} \input  idxmake.4ht" />
        </exec>
        <exec executable="makeindex" dir="doc" >
            <arg file="doc/developer.4dx"/>
        </exec>
        <exec executable="htlatex" dir="doc" >
            <arg file="doc/developer.tex"/>
        </exec>
        <property name="online.devmanual.is.up.to.date" value="true" />
    </target>

    <target name="online-manuals" depends="online-manual,online-devmanual"
            description="Creates the online user and developer guides.">
    </target>

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

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

    <target name="manuals" depends="manual,devmanual"
            description="Creates the printable and online user and developer 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="data/strings" />
            <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. -->
    <!-- Now that translations are hosted at translatewiki, we no longer need this.
    <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/data/strings" />
            <arg value="data/strings" />
            <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"
              failonerror="true">
            <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 HTML documentation from specification. -->
    <target name="generate-documentation" depends="build"
            description="Creates HTML documentation from specification.">
        <java classname="net.sf.freecol.tools.GenerateDocumentation"
              classpath="build/" fork="true"/>
    </target>

    <!-- Creates 'freecol.desktop' file. -->
    <target name="desktop-entry" depends="build"
            description="Creates 'freecol.desktop' file.">
        <mkdir dir="${freecol.release.dir}/base/freecol"/>
        <java classname="net.sf.freecol.tools.DesktopEntry"
              classpath="build/">
            <arg value="${freecol.release.dir}/base/freecol/freecol.desktop" />
        </java>
    </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="-Xmx2G" />
        </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"/>

        <junit printsummary="on" errorproperty="error.junit"
               fork="yes" maxmemory="64M">
            <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>
