<?xml version="1.0" ?>

<project name="libphonenumber" default="compile">
  <property name="libphonenumber.src.dir" value="libphonenumber/src"/>
  <property name="libphonenumber.test.dir" value="libphonenumber/test"/>
  <property name="carrier.src.dir" value="carrier/src"/>
  <property name="carrier.test.dir" value="carrier/test"/>
  <property name="geocoder.src.dir" value="geocoder/src"/>
  <property name="geocoder.test.dir" value="geocoder/test"/>
  <property name="prefixmapper.src.dir" value="internal/prefixmapper/src"/>
  <property name="prefixmapper.test.dir" value="internal/prefixmapper/test"/>
  <property name="build.dir" value="build"/>
  <property name="classes.dir" value="${build.dir}/classes"/>
  <property name="jar.dir" value="${build.dir}/jar"/>
  <property name="lib.dir" value="lib"/>
  <property name="report.dir" value="${build.dir}/junitreport"/>
  <property name="resources.dir" value="../resources"/>
  <property name="javascript.dir" value="../javascript"/>
  <property name="build.tools.jar"
      value="../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar"/>

  <path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
  </path>
  <path id="test.classpath">
    <pathelement location="${classes.dir}"/>
    <pathelement location="${lib.dir}/junit/junit-4.8.1.jar"/>
    <pathelement location="${jar.dir}/${ant.project.name}-test.jar"/>
    <fileset dir="${lib.dir}">
      <include name="**/*.jar"/>
    </fileset>
  </path>

  <target name="build-phone-metadata">
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="BuildMetadataProtoFromXml"/>
      <arg value="--input-file=${resources.dir}/PhoneNumberMetadata.xml"/>
      <arg value="--output-dir=${libphonenumber.src.dir}/com/google/i18n/phonenumbers"/>
      <arg value="--data-prefix=data/PhoneNumberMetadataProto"/>
      <arg value="--mapping-class=CountryCodeToRegionCodeMap"/>
      <arg value="--copyright=2010"/>
      <arg value="--lite-build=false"/>
    </exec>
  </target>

  <target name="build-short-metadata">
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="BuildMetadataProtoFromXml"/>
      <arg value="--input-file=${resources.dir}/ShortNumberMetadata.xml"/>
      <arg value="--output-dir=${libphonenumber.src.dir}/com/google/i18n/phonenumbers"/>
      <arg value="--data-prefix=data/ShortNumberMetadataProto"/>
      <arg value="--mapping-class=ShortNumbersRegionCodeSet"/>
      <arg value="--copyright=2013"/>
      <arg value="--lite-build=false"/>
    </exec>
  </target>

  <target name="build-test-metadata">
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="BuildMetadataProtoFromXml"/>
      <arg value="--input-file=${resources.dir}/PhoneNumberMetadataForTesting.xml"/>
      <arg value="--output-dir=${libphonenumber.test.dir}/com/google/i18n/phonenumbers"/>
      <arg value="--data-prefix=data/PhoneNumberMetadataProtoForTesting"/>
      <arg value="--mapping-class=CountryCodeToRegionCodeMapForTesting"/>
      <arg value="--copyright=2010"/>
      <arg value="--lite-build=false"/>
    </exec>
  </target>

  <target name="build-alternate-metadata">
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="BuildMetadataProtoFromXml"/>
      <arg value="--input-file=${resources.dir}/PhoneNumberAlternateFormats.xml"/>
      <arg value="--output-dir=${libphonenumber.src.dir}/com/google/i18n/phonenumbers"/>
      <arg value="--data-prefix=data/PhoneNumberAlternateFormatsProto"/>
      <arg value="--mapping-class=AlternateFormatsCountryCodeSet"/>
      <arg value="--copyright=2012"/>
      <arg value="--lite-build=false"/>
    </exec>
  </target>

  <target name="build-carrier-data">
    <delete dir="carrier/src/com/google/i18n/phonenumbers/carrier/data"/>
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="GeneratePhonePrefixData"/>
      <arg value="${resources.dir}/carrier/"/>
      <arg value="carrier/src/com/google/i18n/phonenumbers/carrier/data"/>
    </exec>
  </target>

  <target name="build-carrier-test-data">
    <delete dir="carrier/test/com/google/i18n/phonenumbers/carrier/testing_data"/>
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="GeneratePhonePrefixData"/>
      <arg value="${resources.dir}/test/carrier/"/>
      <arg value="carrier/test/com/google/i18n/phonenumbers/carrier/testing_data"/>
    </exec>
  </target>

  <target name="build-geo-data">
    <delete dir="geocoder/src/com/google/i18n/phonenumbers/geocoding/data"/>
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="GeneratePhonePrefixData"/>
      <arg value="${resources.dir}/geocoding/"/>
      <arg value="geocoder/src/com/google/i18n/phonenumbers/geocoding/data"/>
    </exec>
  </target>

  <target name="build-geo-test-data">
    <delete dir="geocoder/test/com/google/i18n/phonenumbers/geocoding/testing_data"/>
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="GeneratePhonePrefixData"/>
      <arg value="${resources.dir}/test/geocoding/"/>
      <arg value="geocoder/test/com/google/i18n/phonenumbers/geocoding/testing_data"/>
    </exec>
  </target>

  <target name="build-timezones-data">
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="GenerateTimeZonesMapData"/>
      <arg value="${resources.dir}/timezones/map_data.txt"/>
      <arg value="geocoder/src/com/google/i18n/phonenumbers/timezones/data"/>
    </exec>
  </target>

  <target name="build-timezones-test-data">
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="GenerateTimeZonesMapData"/>
      <arg value="${resources.dir}/test/timezones/map_data.txt"/>
      <arg value="geocoder/test/com/google/i18n/phonenumbers/timezones/testing_data"/>
    </exec>
  </target>

  <target name="build-js-metadata">
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="BuildMetadataJsonFromXml"/>
      <arg value="${resources.dir}/PhoneNumberMetadata.xml"/>
      <arg value="${javascript.dir}/i18n/phonenumbers/metadata.js"/>
    </exec>
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="BuildMetadataJsonFromXml"/>
      <arg value="${resources.dir}/PhoneNumberMetadata.xml"/>
      <arg value="${javascript.dir}/i18n/phonenumbers/metadatalite.js"/>
      <arg value="true"/> <!-- Lite metadata. -->
    </exec>
    <exec executable="java">
      <arg value="-jar" />
      <arg value="${build.tools.jar}"/>
      <arg value="BuildMetadataJsonFromXml"/>
      <arg value="${resources.dir}/PhoneNumberMetadataForTesting.xml"/>
      <arg value="${javascript.dir}/i18n/phonenumbers/metadatafortesting.js"/>
    </exec>
  </target>

  <target name="compile" description="Compile Java source."
          depends="build-phone-metadata,build-short-metadata,build-alternate-metadata,build-carrier-data,build-geo-data,build-timezones-data">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${libphonenumber.src.dir};${carrier.src.dir};${geocoder.src.dir};${prefixmapper.src.dir}"
           destdir="${classes.dir}" classpathref="classpath" includeAntRuntime="false" encoding="UTF-8"/>
    <javac srcdir="${libphonenumber.test.dir};${carrier.test.dir};${geocoder.test.dir};${prefixmapper.test.dir}"
           destdir="${classes.dir}" classpathref="classpath" debug="on"
           includeAntRuntime="false"/>
  </target>

  <target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar">
      <fileset dir="${classes.dir}">
        <include name="**/*.class"/>
        <exclude name="**/*Test*"/>
        <exclude name="**/BuildMetadata*"/>
        <exclude name="**/PhoneNumberTo*"/>
        <exclude name="**/geocoding/*"/>
        <exclude name="**/prefixmapper/*"/>
      </fileset>
      <fileset dir="${libphonenumber.src.dir}">
        <include name="**/PhoneNumberMetadataProto*"/>
        <include name="**/PhoneNumberAlternateFormatsProto*"/>
        <include name="**/ShortNumberMetadataProto*"/>
      </fileset>
    </jar>
    <jar destfile="${jar.dir}/carrier-mapper.jar">
      <fileset dir="${classes.dir}">
        <include name="**/PhoneNumberToCarrierMapper.class"/>
        <include name="**/prefixmapper/*.class"/>
        <exclude name="**/*Test*"/>
      </fileset>
      <fileset dir="${carrier.src.dir}">
        <include name="**/carrier/data/*"/>
      </fileset>
    </jar>
    <jar destfile="${jar.dir}/offline-geocoder.jar">
      <fileset dir="${classes.dir}">
        <include name="**/geocoding/*.class"/>
        <include name="**/PhoneNumberToTimeZonesMapper*"/>
        <include name="**/prefixmapper/*.class"/>
        <exclude name="**/*Test*"/>
      </fileset>
      <fileset dir="${geocoder.src.dir}">
        <include name="**/geocoding/data/*"/>
        <include name="**/timezones/data/*"/>
      </fileset>
    </jar>
  </target>

  <target name="test-jar"
          depends="compile,build-test-metadata,build-carrier-test-data,build-geo-test-data,build-timezones-test-data">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}-test.jar">
      <fileset dir="${classes.dir}">
        <include name="**/*.class"/>
        <exclude name="**/*Test*"/>
      </fileset>
      <fileset dir="${libphonenumber.src.dir}">
        <include name="**/PhoneNumberMetadataProto*"/>
        <include name="**/PhoneNumberAlternateFormatsProto*"/>
        <include name="**/ShortNumberMetadataProto*"/>
      </fileset>
      <fileset dir="${libphonenumber.test.dir}">
        <include name="**/PhoneNumberMetadataProtoForTesting*"/>
      </fileset>
      <fileset dir="${carrier.test.dir}">
        <include name="**/carrier/testing_data/*"/>
      </fileset>
      <fileset dir="${geocoder.test.dir}">
        <include name="**/geocoding/testing_data/*"/>
        <include name="**/timezones/testing_data/*"/>
      </fileset>
    </jar>
  </target>

  <target name="junit" depends="test-jar">
    <mkdir dir="${report.dir}"/>
    <junit printsummary="yes"
        failureProperty="test.failed" errorProperty="test.failed">
      <classpath refid="test.classpath"/>
      <formatter type="xml"/>
      <batchtest fork="no" todir="${report.dir}">
        <fileset dir="${libphonenumber.test.dir}" includes="**/*Test.java"/>
      </batchtest>
      <batchtest fork="no" todir="${report.dir}">
        <fileset dir="${carrier.test.dir}" includes="**/*Test.java"/>
      </batchtest>
      <batchtest fork="no" todir="${report.dir}">
        <fileset dir="${geocoder.test.dir}" includes="**/*Test.java"/>
      </batchtest>
      <batchtest fork="no" todir="${report.dir}">
        <fileset dir="${prefixmapper.test.dir}" includes="**/*Test.java"/>
      </batchtest>
    </junit>
    <fail message="Tests failed. Run 'ant junitreport' for more info."
        if="test.failed"/>
  </target>

  <target name="junitreport">
    <junitreport todir="${report.dir}">
      <fileset dir="${report.dir}" includes="TEST-*.xml"/>
        <report todir="${report.dir}"/>
    </junitreport>
  </target>

  <target name="testname" unless="test">
    <fail message="You must run this target with -Dtest=TestName"/>
  </target>

  <target name="test" depends="test-jar, testname">
    <junit printsummary="withOutAndErr">
      <classpath refid="test.classpath"/>
      <formatter type="plain" usefile="false"/>
      <batchtest fork="no">
        <fileset dir="${libphonenumber.test.dir}" includes="**/*${test}.java"/>
        <fileset dir="${carrier.test.dir}" includes="**/*${test}.java"/>
        <fileset dir="${geocoder.test.dir}" includes="**/*${test}.java"/>
      </batchtest>
    </junit>
  </target>

  <target name="clean" description="Remove generated files.">
    <delete dir="${build.dir}"/>
  </target>

  <target name="clean-build" depends="clean,jar"/>
</project>
