<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<project default="locales" basedir="../../../../../..">

<!-- Set Properties -->
  <!-- User settings -->
  <property file="${user.home}/ant.properties"/>
  <!-- Set property dir -->
  <property name="properties.dir" value="tools/ant/properties"/>
  <!-- Significant dirs -->
  <property file="${properties.dir}/dirs.properties"/>
  <!-- Product names and release info -->
  <property file="${properties.dir}/release.properties"/>

<!-- Define the task which creates the engine's messages_en.properties -->
  <taskdef
    name="buildMessages"
    classname="org.apache.derbyBuild.MessageBuilder"
    classpath="${out.dir}"
  />
  <property name="messageDescriptors" value="${derby.locales.dir}/messages.xml"/>
  <property name="englishMessages" value="${generated.engine.locale.dir}/messages_en.properties"/>

<!-- Targets -->
  <target name="locales" depends="init,buildMessages,split"/>

  <!-- Initialization-->
  <target name="init">

    <uptodate
      property="messagesUpToDate"
      targetfile="${englishMessages}"
    >
      <srcfiles file="${messageDescriptors}"/>
      <srcfiles dir="${derby.build.dir}" includes="*.java"/>
    </uptodate>

  </target>
  <!-- Generate english messages from xml descriptors-->
  <target name="buildMessages" unless="messagesUpToDate">

    <mkdir dir="${gen.doc.dir}" />

    <buildMessages
      xmlSourceFile="${messageDescriptors}"
      propertiesTargetFile="${englishMessages}"
      ditaTargetFile="${sqlstateTables}"
    />

  </target>

  <target name="split" depends="verifysplit">

    <antcall target="doSplit">
      <param name="msgdir" value="${generated.engine.locale.dir}"/>
      <param name="msgfile" value="messages_en"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_cs"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_de_DE"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_es"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_hu"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_ja_JP"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_ko_KR"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_qq_PP_testOnly"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_pl"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_pt_BR"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_ru"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_zh_CN"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_zh_TW"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_it"/>
    </antcall>
    <antcall target="doSplit">
      <param name="msgdir" value="${derby.locales.dir}"/>
      <param name="msgfile" value="messages_fr"/>
    </antcall>
  </target>

  <target name="doSplit" depends="splitprops,verifysplit,noSplit,chksplit" unless="split.done">
    <mkdir dir="${loc.dir}"/>
    <java classname="org.apache.derbyBuild.splitmessages"
          classpath="${out.dir}"
          failonerror="true">
      <arg value="${loc.dir}"/> 
      <arg value="${split.properties}"/>
    </java>
    <touch file="${split.donefile}"/>
  </target>

  <target name="splitprops">
    <property name="split.properties" value="${msgdir}/${msgfile}.properties"/>
    <property name="split.donefile" value="${loc.dir}/${msgfile}.done"/>
  </target>

  <target name="chksplit">
    <uptodate property="split.done"
              targetfile="${split.donefile}" >
      <srcfiles dir="${derby.locales.dir}" includes="${msgfile}.properties" />
    </uptodate>
  </target>

  <target name="verifysplit">
    <available classname="org.apache.derbyBuild.splitmessages"
               property="splitmsgs.available">
      <classpath>
        <pathelement path="${out.dir}"/>
      </classpath>
    </available>
  </target>

  <target name="noSplit" unless="splitmsgs.available">
    <echo level="error" message=""/>
    <echo level="error" message="***** SplitMessages not available *****"/>
    <echo level="error" message=" ***** Run &quot;all&quot; target first *****"/>
    <echo level="error" message=""/>
    <fail message="  Exiting ant build..."/>
  </target>
</project>

