<?xml version="1.0" encoding="utf-8"?>
<project name="JOSM i18n" default="build" basedir=".">
    <!-- compilation properties -->
    <property name="josm.build.dir" value="../core"/>
    <property name="josm.resources.dir" value="${josm.build.dir}/resources"/>
    <property name="josm.presets" value="${josm.resources.dir}/data/defaultpresets.xml"/>
    <property name="plugin.dir" value="../plugins"/>
    <property name="maps.srcfile" value="https://josm.openstreetmap.de/maps"/>
    <property name="wiki.srcfile" value="https://josm.openstreetmap.de/josmfile?mode=en"/>
    <property name="surveyor.srcfile" value="${plugin.dir}/surveyor/resources/surveyor.xml"/>
    <property name="i18n.build.dir" value="build"/>
    <property name="i18n.install.dir" value="${josm.resources.dir}/data"/>
    <property name="pluginlist.install.dir" value="${josm.build.dir}/nodist/trans"/>
    <property name="po.build.dir" value="${i18n.build.dir}"/>
    <property name="ant.build.javac.target" value="1.5"/>
    <property name="gettext.keywords" value="-k -ktrc:1c,2 -kmarktrc:1c,2 -ktr -kmarktr -ktrn:1,2 -ktrnc:1c,2,3"/>
    <property name="gettexttasks.jar" value="lib/gettext-ant-tasks-0.9.7.jar"/>
    <property name="antcontrib.jar" value="lib/ant-contrib-1.0b3.jar"/>
    <property name="grand.jar" value="lib/grand-1.9.5.jar"/>
    <target name="init" description="Loads the Ant gettext and contrib tasks">
        <taskdef name="gettext-extract" classname="org.xnap.commons.ant.gettext.GettextExtractKeysTask" classpath="${gettexttasks.jar}"/>
        <!-- for the <foreach> possibility -->
        <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${antcontrib.jar}"/>
    </target>
    <target name="builddir">
        <mkdir dir="${i18n.build.dir}"/>
    </target>
    <target name="trans_.java" depends="builddir">
        <exec executable="perl" failonerror="true" logError="true" output="${i18n.build.dir}/trans_presets.java">
            <arg line="convpreset.pl ${josm.presets}"/>
        </exec>
        <exec executable="perl" failonerror="true" logError="true" output="${i18n.build.dir}/trans_maps.java">
            <arg line="convmaps.pl ${maps.srcfile}"/>
        </exec>
        <exec executable="perl" failonerror="true" logError="true" output="${i18n.build.dir}/trans_surveyor.java">
            <arg line="convsurveyor.pl ${surveyor.srcfile}"/>
        </exec>
        <exec executable="perl" failonerror="true" logError="true" output="${i18n.build.dir}/trans_plugins.java">
            <arg line="convplugins.pl ${plugin.dir}/*/build.xml"/>
        </exec>
        <exec executable="perl" failonerror="true">
            <env key="TAGGING_PRESET_XSD" value="${josm.resources.dir}/data/tagging-preset.xsd"/>
            <arg line="convwiki.pl ${wiki.srcfile} ${i18n.build.dir}/josmfiles ${i18n.build.dir}/josmfiles.zip"/>
        </exec>
        <copy file="specialmessages.java" todir="${i18n.build.dir}"/>
    </target>
    <!-- Create the file build/pluginSrc.txt, which contains a list of include patterns for all *.java files that should be translated relative to "${plugin.dir}" -->
    <target name="filterpluginsources" depends="init">
        <delete file="${po.build.dir}/pluginSrc.txt"/>
        <foreach param="path" target="filterpluginsource">
            <path>
                <dirset dir="${plugin.dir}" includes="*" excludes="00_*" />
            </path>
        </foreach>
    </target>
    <!-- Helper task for `filterpluginsources`: Appends an include pattern to the file `build/pluginSrc.txt` if there is no file `.tx/config` for a plugin -->
    <target name="filterpluginsource">
      <if>
          <not>
              <available file="${path}/.tx/config" type="file" />
          </not>
          <then>
              <basename file="${path}" property="dir"/>
              <echo file="${po.build.dir}/pluginSrc.txt" append="true" message="${dir}/**/*.java&#xa;"></echo>
          </then>
      </if>
    </target>
    <target name="po/josm.pot" description="Extracts message keys from the source code" depends="trans_.java,init,filterpluginsources">
        <gettext-extract keysFile="josm.pot" poDirectory="po" keywords="${gettext.keywords} --exclude-file=excludes.pot">
            <!-- core.pot -->
            <fileset dir="${josm.build.dir}/src" includes="**/*.java"/>
            <fileset dir="${i18n.build.dir}" includes="specialmessages.java"/>
            <fileset dir="${i18n.build.dir}" includes="trans_presets.java"/>
            <fileset dir="${josm.resources.dir}/data/validator" includes="*.mapcss"/>
            <fileset dir="${josm.resources.dir}/styles/standard" includes="*.mapcss"/>
            <fileset dir="${i18n.build.dir}" includes="trans_maps.java"/>
            <!-- data.pot -->
            <fileset dir="${i18n.build.dir}" includes="trans_plugins.java"/>
            <!-- <fileset dir="${i18n.build.dir}/josmfiles" includes="trans_*.*"/> disable for now -->
            <!-- plugins.pot -->
            <fileset dir="${plugin.dir}" includesfile="${po.build.dir}/pluginSrc.txt"/>
            <fileset dir="${i18n.build.dir}" includes="trans_surveyor.java"/>
        </gettext-extract>
    </target>
    <target name="po/core+maplist+presets.pot" depends="po/core.pot,po/maplist.pot,po/presets.pot">
        <exec executable="msgcat" failonerror="true" output="po/core+maplist+presets.pot">
            <arg line="po/core.pot po/maplist.pot po/presets.pot"/>
        </exec>
    </target>
    <target name="po/core.pot" description="Extracts message keys from the JOSM core source code" depends="init">
        <gettext-extract keysFile="core.pot" poDirectory="po" keywords="${gettext.keywords} --exclude-file=excludes.pot">
            <fileset dir="${josm.build.dir}/src" includes="**/*.java"/>
            <fileset dir="${i18n.build.dir}" includes="specialmessages.java"/>
            <fileset dir="${josm.resources.dir}/data/validator" includes="*.mapcss"/>
            <fileset dir="${josm.resources.dir}/styles/standard" includes="*.mapcss"/>
        </gettext-extract>
    </target>
    <target name="po/maplist.pot" description="Extracts message keys from plugin descriptions" depends="trans_.java,init">
        <gettext-extract keysFile="maplist.pot" poDirectory="po" keywords="${gettext.keywords} --exclude-file=excludes.pot">
            <!-- maps should be translated server-side, but that requires language file loading support in trac python code -->
            <fileset dir="${i18n.build.dir}" includes="trans_maps.java"/>
        </gettext-extract>
    </target>
    <target name="po/pluginlist.pot" description="Extracts message keys from plugin descriptions" depends="trans_.java,init">
        <gettext-extract keysFile="pluginlist.pot" poDirectory="po" keywords="${gettext.keywords} --exclude-file=excludes.pot">
            <fileset dir="${i18n.build.dir}" includes="trans_plugins.java"/>
        </gettext-extract>
    </target>
    <target name="po/presets.pot" description="Extracts message keys from presets (minus core)" depends="trans_.java,po/core.pot,init">
        <gettext-extract keysFile="presets.pot" poDirectory="po" keywords="${gettext.keywords} --exclude-file=excludes.pot --exclude-file=po/core.pot">
            <fileset dir="${i18n.build.dir}" includes="trans_presets.java"/>
        </gettext-extract>
    </target>
    <target name="po/plugins.pot" description="Extracts message keys from the plugins source code (minus core)" depends="trans_.java,po/core.pot,po/presets.pot,init,filterpluginsources">
        <gettext-extract keysFile="plugins.pot" poDirectory="po" keywords="${gettext.keywords} --exclude-file=excludes.pot --exclude-file=po/core.pot --exclude-file=po/presets.pot">
            <fileset dir="${plugin.dir}" includesfile="${po.build.dir}/pluginSrc.txt"/>
            <fileset dir="${i18n.build.dir}" includes="trans_surveyor.java"/>
        </gettext-extract>
    </target>
    <target name="build">
        <antcall target="buildcore"/>
        <antcall target="buildplugins"/>
    </target>
    <target name="buildplugins" depends="init">
        <foreach param="path" target="plugintrans" parallel="true" maxThreads="8">
            <path>
                <dirset dir="${plugin.dir}" includes="*" excludes="00_*" />
            </path>
        </foreach>
    </target>
    <target name="singlepluginpot" depends="init">
        <gettext-extract keysFile="josm.pot" poDirectory="po" keywords="${gettext.keywords}">
            <fileset dir="${plugin.dir}/${plugin}"/>
        </gettext-extract>
    </target>
    <target name="singleplugintrans" depends="init">
        <antcall target="plugintrans">
            <param name="path" value="${plugin}"/>
        </antcall>
    </target>
    <target name="buildcore" depends="pot">
        <antcall target="coretrans"/>
        <antcall target="pluginlisttrans"/>
    </target>
    <target name="coretrans" depends="po/core+maplist+presets.pot">
        <exec executable="perl" failonerror="true">
            <arg line="i18n.pl --potfile=po/core+maplist+presets.pot --basedir=${i18n.install.dir}/ po/*.po"/>
        </exec>
    </target>
    <target name="pluginlisttrans" depends="po/pluginlist.pot">
        <exec executable="perl" failonerror="true">
            <arg line="i18n.pl --potfile=po/pluginlist.pot --basedir=${pluginlist.install.dir}/ po/*.po"/>
        </exec>
    </target>
    <!-- If the file `josm.pot` is present and the plugin has a `data/` directory, update its *.po files based on the ones in the `po/` directory. -->
    <!-- If the plugin has a Transifex config file `.tx/config`, skip this target altogether -->
    <target name="plugintrans">
        <basename file="${path}" property="dir"/>
        <echo message="Run plugintrans target for plugin ${dir}" level="info"/>
        <mkdir dir="${po.build.dir}/plugin_${dir}"/>
        <exec executable="perl" failonerror="true" output="${po.build.dir}/plugin_${dir}/trans_plugin.java">
            <arg line="convplugins.pl ${plugin.dir}/${dir}/build.xml"/>
        </exec>
        <gettext-extract keysFile="josm.pot" poDirectory="${po.build.dir}/plugin_${dir}" keywords="${gettext.keywords}">
            <fileset dir="${plugin.dir}/${dir}" includes="**/*.java"/>
            <fileset dir="${po.build.dir}/plugin_${dir}" includes="trans_*.java"/>
            <fileset dir="${i18n.build.dir}" includes="trans_${dir}.java"/> <!-- include plugin specific translation -->
        </gettext-extract>
        <if>
            <available file="${po.build.dir}/plugin_${dir}/josm.pot" type="file"/>
            <then>
                <if>
                    <available file="${plugin.dir}/${dir}/data/" type="dir"/>
                    <else>
                        <mkdir dir="${plugin.dir}/${dir}/data/"/>
                    </else>
                </if>
                <exec executable="perl" failonerror="true">
                    <arg line="i18n.pl --potfile=${po.build.dir}/plugin_${dir}/josm.pot --basedir=${plugin.dir}/${dir}/data/ po/*.po"/>
                </exec>
            </then>
        </if>
        <delete dir="${po.build.dir}/plugin_${dir}"/>
    </target>
    <target name="clean">
        <delete dir="${i18n.build.dir}"/>
        <delete file="build.dot"/>
        <delete file="build.png"/>
        <delete>
            <fileset dir="po" includes="*.*~" defaultexcludes="false"/>
            <fileset dir="po" includes="*.po" defaultexcludes="false"/>
            <fileset dir="po" includes="*.pot" defaultexcludes="false"/>
        </delete>
    </target>
    <target name="test" depends="pot">
        <mkdir dir="${i18n.build.dir}/test"/>
        <exec executable="perl" failonerror="true">
            <arg line="i18n.pl --potfile=po/josm.pot --basedir=${i18n.build.dir}/test/ po/*.po"/>
        </exec>
    </target>
    <target name="pot" depends="po/josm.pot" />
    <target name="update">
        <exec executable="perl" failonerror="true">
            <arg line="launchpad.pl bzronly"/>
        </exec>
        <antcall target="build"/>
    </target>
    <target name="updatetest">
        <exec executable="perl" failonerror="true">
            <arg line="launchpad.pl bzronly"/>
        </exec>
        <antcall target="test"/>
    </target>
    <target name="updatecore">
        <exec executable="perl" failonerror="true">
            <arg line="launchpad.pl bzronly"/>
        </exec>
        <antcall target="buildcore"/>
    </target>
    <target name="ant-dependencies">
        <typedef resource="net/ggtools/grand/antlib.xml" classpath="${grand.jar}"/>
        <grand output="build.dot" outputconfigprefix="grand"/>
        <exec executable="dot">
            <arg line="-Tpng -o build.png build.dot"/>
        </exec>
    </target>
</project>
