<?xml version="1.0" encoding="UTF-8"?>

<!-- 

  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 name="dependencies" default="check" basedir="..">
    <description>
    Confirm that dependencies are present and fetch missing dependencies
    where possible.
    </description>

    <property file="make/depends.properties" />
    <import file="${basedir}/make/properties.xml" />

    <target name="check" description="Check whether dependencies are available"
            depends="-check,-check-win,-check-unix,-extract" />

    <target name="-check">
        <check-one-file src="${yoko.url}" dest="${yoko.jar}" />
        <check-one-file src="${yoko-core.url}" dest="${yoko-core.jar}" />
        <check-one-file src="${yoko-rmi-impl.url}" dest="${yoko-rmi-impl.jar}" />
        <check-one-file src="${yoko-rmi-spec.url}" dest="${yoko-rmi-spec.jar}" />
        <check-one-file src="${bcel.url}" dest="${bcel.jar}" />
        <check-one-file src="${xalan.url}" dest="${xalan.jar}" />
        <check-one-file src="${xerces.url}" dest="${xerces.zip}" />
        <check-one-file src="${xmlsec.url}" dest="${xmlsec.zip}" />
        <check-one-file src="${junit.url}" dest="${junit.jar}" />
        <check-one-file src="${bcprov.url}" dest="${bcprov.jar}" />
        <check-one-file src="${icu4j.url}" dest="${icu4j.jar}" />
        <check-one-file src="${mx4j.url}" dest="${mx4j.zip}" />
        <check-one-file src="${ecj.url}" dest="${ecj.jar}" />
        <check-one-file src="${jetty.url}" dest="${jetty.jar}" />
        <check-one-file src="${jetty-util.url}" dest="${jetty-util.jar}" />
        <check-one-file src="${servlet-api.url}" dest="${servlet-api.jar}" />
    </target>

    <target name="-check-win" if="is.windows" depends="-really-check-win" />

    <target name="-select-win-deps-x86" if="is.x86">
        <property name="people.apache.base" value="${people.apache.base.x86}"/>
        <property name="awtdeps.dir" value="${awtdeps.dir.x86}"/>
        <property name="awtdeps.tar" value="${awtdeps.tar.x86}"/>
        <property name="awtdeps.url" value="${awtdeps.url.x86}"/>
        <property name="awtdeps.md5" value="${awtdeps.md5.x86}"/>
        <property name="awtdeps.extract.dir" value="${awtdeps.extract.dir.x86}"/>
        <property name="awtdeps.testfile" value="${awtdeps.testfile.x86}"/>

        <property name="msvcr.dir" value="${msvcr.dir.x86}"/>
        <property name="msvcr.dll" value="${msvcr.dll.x86}"/>
        <property name="msvcr.url" value="${msvcr.url.x86}"/>
        <property name="msvcr.dll.file" value="${msvcr.dll.file.x86}"/>
        <property name="msvcr.md5" value="${msvcr.md5.x86}"/>
    </target>

    <target name="-select-win-deps-x86_64" if="is.x86_64">
        <property name="people.apache.base" value="${people.apache.base.x86_64}"/>
        <property name="awtdeps.dir" value="${awtdeps.dir.x86_64}"/>
        <property name="awtdeps.tar" value="${awtdeps.tar.x86_64}"/>
        <property name="awtdeps.url" value="${awtdeps.url.x86_64}"/>
        <property name="awtdeps.md5" value="${awtdeps.md5.x86_64}"/>
        <property name="awtdeps.extract.dir" value="${awtdeps.extract.dir.x86_64}"/>
        <property name="awtdeps.testfile" value="${awtdeps.testfile.x86_64}"/>

        <property name="msvcr.dir" value="${msvcr.dir.x86_64}"/>
        <property name="msvcr.dll" value="${msvcr.dll.x86_64}"/>
        <property name="msvcr.url" value="${msvcr.url.x86_64}"/>
        <property name="msvcr.dll.file" value="${msvcr.dll.file.x86_64}"/>
        <property name="msvcr.md5" value="${msvcr.md5.x86_64}"/>
    </target>

    <target name="-really-check-win" if="is.windows" depends="-select-win-deps-x86_64,-select-win-deps-x86">
        <property environment="hyenv" />

        <check-one-file src="${msvcr.url}" dest="${msvcr.dll}" />
        <check-one-file src="${awtdeps.url}" dest="${awtdeps.tar}" />
        <uptodate property="awtdeps.uptodate"
                  srcfile="${awtdeps.tar}"
                  targetfile="${awtdeps.testfile}" />
        <antcall target="-awt-tar-extract" />
    </target>

    <target name="-extract" depends="-extract-props,-really-extract-mx4j" />
    <target name="-extract-props">
        <uptodate property="mx4j.uptodate"
                  srcfile="${mx4j.zip}"
                  targetfile="${mx4j.jar}" />
    </target>
    <target name="-really-extract-mx4j" unless="mx4j.uptodate">
        <!-- This doesn't work for me since my ant doesn't support
             mapper in unzip.

        <unzip src="${mx4j.zip}" dest="${mx4j.dir}">
            <patternset>
                <include name="**/mx4j.jar"/>
                <include name="**/mx4j-remote.jar"/>
            </patternset>
            <mapper type="flatten" />
        </unzip>
          -->

        <unzip src="${mx4j.zip}" dest="${mx4j.dir}.tmp" />
        <copy todir="${mx4j.dir}" flatten="yes">
            <fileset dir="${mx4j.dir}.tmp/mx4j-3.0.2/lib">
                <include name="mx4j.jar"/>
                <include name="mx4j-remote.jar"/>
            </fileset>
        </copy>
        <delete dir="${mx4j.dir}.tmp" />
    </target>

    <!-- 
       *  FIXME : the following awful little hack is because we noticed that for whatever
       *  reason, we can't link with libjpg.a et al on at least to kinds of 64-bit linux
      -->

    <target name="-check-unix-common" if="is.unix">
        
        <property name="lcms.msg"
                  value="liblcms development package not installed
${line.separator}See depends/libs/build/README.txt for further details.
${line.separator}For Debian/Ubuntu try: apt-get install liblcms1-dev
${line.separator}For Fedora try: yum install lcms-devel" />
        <mkdir dir="depends/libs/build/lcms" />
        <check-one-link src="${lcms.home}/include/icc34.h"
                        dest="depends/libs/build/lcms/icc34.h"
                        message="${lcms.msg}" />
        <check-one-link src="${lcms.home}/include/lcms.h"
                        dest="depends/libs/build/lcms/lcms.h"
                        message="${lcms.msg}" />

        <property name="png.msg"
                  value="libpng development package not installed
${line.separator}See depends/libs/build/README.txt for further details.
${line.separator}For Debian/Ubuntu try: apt-get install libpng12-dev
${line.separator}For Fedora try: yum install libpng-devel" />
        <mkdir dir="depends/libs/build/png" />
        <check-one-link src="${png.home}/include/pngconf.h"
                        dest="depends/libs/build/png/pngconf.h"
                        message="${png.msg}" />
        <check-one-link src="${png.home}/include/png.h"
                        dest="depends/libs/build/png/png.h"
                        message="${png.msg}" />

        <property name="jpeg.msg"
                  value="libjpeg development package not installed
${line.separator}See depends/libs/build/README.txt for further details.
${line.separator}For Debian/Ubuntu try: apt-get install libjpeg62-dev
${line.separator}For Fedora try: yum install libjpeg-devel" />
        <mkdir dir="depends/libs/build/jpeg" />
        <check-one-link src="${jpeg.home}/include/jconfig.h"
                        dest="depends/libs/build/jpeg/jconfig.lnx"
                        message="${jpeg.msg}" />
        <check-one-link src="${jpeg.home}/include/jpeglib.h"
                        dest="depends/libs/build/jpeg/jpeglib.h"
                        message="${jpeg.msg}" />
        <check-one-link src="${jpeg.home}/include/jmorecfg.h"
                        dest="depends/libs/build/jpeg/jmorecfg.h"
                        message="${jpeg.msg}" />
        <check-one-link src="${jpeg.home}/include/jerror.h"
                        dest="depends/libs/build/jpeg/jerror.h"
                        message="${jpeg.msg}" />
    </target>

    <target name="-check-unix-x86" if="is.x86" unless="is.windows" depends="-check-unix-common">
        <check-one-link src="${lcms.home}/lib/liblcms.a"
                        dest="depends/libs/build/lcms/liblcms.${hy.platform}"
                        message="${lcms.msg}" />

        <check-one-link src="${png.home}/lib/libpng.a"
                        dest="depends/libs/build/png/libpng.${hy.platform}"
                        message="${png.msg}" />

        <check-one-link src="${jpeg.home}/lib/libjpeg.a"
                        dest="depends/libs/build/jpeg/libjpeg.${hy.platform}"
                        message="${jpeg.msg}" />
    </target>

    <target name="-check-unix-x86_64" if="is.x86_64" unless="is.windows" depends="-check-unix-common">
        <check-one-link src="${lcms.home}/lib/liblcms.so"
                        dest="depends/libs/build/lcms/liblcms.${hy.platform}"
                        message="${lcms.msg}" />

        <check-one-link src="${png.home}/lib/libpng.so"
                        dest="depends/libs/build/png/libpng.${hy.platform}"
                        message="${png.msg}" />

        <check-one-link src="${jpeg.home}/lib/libjpeg.so"
                        dest="depends/libs/build/jpeg/libjpeg.${hy.platform}"
                        message="${jpeg.msg}" />
    </target>

    <target name="-check-unix-ia64" if="is.ia64" unless="is.windows" depends="-check-unix-common">
        <check-one-link src="${lcms.home}/lib/liblcms.a"
                        dest="depends/libs/build/lcms/liblcms.${hy.platform}"
                        message="${lcms.msg}" />

        <check-one-link src="${png.home}/lib/libpng.a"
                        dest="depends/libs/build/png/libpng.${hy.platform}"
                        message="${png.msg}" />

        <check-one-link src="${jpeg.home}/lib/libjpeg.a"
                        dest="depends/libs/build/jpeg/libjpeg.${hy.platform}"
                        message="${jpeg.msg}" />
    </target>

    <target name="-check-unix" if="is.unix" depends="-check-unix-x86, -check-unix-x86_64, -check-unix-ia64">
 
<!--        <property name="lcms.msg"
                  value="liblcms development package not installed
${line.separator}See depends/libs/build/README.txt for further details.
${line.separator}For Debian/Ubuntu try: apt-get install liblcms1-dev
${line.separator}For Fedora try: yum install lcms-devel" />
        <mkdir dir="depends/libs/build/lcms" />
        <check-one-link src="${lcms.home}/lib/liblcms.a"
                        dest="depends/libs/build/lcms/liblcms.${hy.platform}"
                        message="${lcms.msg}" />
        <check-one-link src="${lcms.home}/include/icc34.h"
                        dest="depends/libs/build/lcms/icc34.h"
                        message="${lcms.msg}" />
        <check-one-link src="${lcms.home}/include/lcms.h"
                        dest="depends/libs/build/lcms/lcms.h"
                        message="${lcms.msg}" />


      <property name="png.msg"
                  value="libpng development package not installed
${line.separator}See depends/libs/build/README.txt for further details.
${line.separator}For Debian/Ubuntu try: apt-get install libpng12-dev
${line.separator}For Fedora try: yum install libpng-devel" />
        <mkdir dir="depends/libs/build/png" />
        <check-one-link src="${png.home}/lib/libpng.a"
                        dest="depends/libs/build/png/libpng.${hy.platform}"
                        message="${png.msg}" />
        <check-one-link src="${png.home}/include/pngconf.h"
                        dest="depends/libs/build/png/pngconf.h"
                        message="${png.msg}" />
        <check-one-link src="${png.home}/include/png.h"
                        dest="depends/libs/build/png/png.h"
                        message="${png.msg}" />


       <property name="jpeg.msg"
                  value="libjpeg development package not installed
${line.separator}See depends/libs/build/README.txt for further details.
${line.separator}For Debian/Ubuntu try: apt-get install libjpeg62-dev
${line.separator}For Fedora try: yum install libjpeg-devel" />
        <mkdir dir="depends/libs/build/jpeg" />
        <check-one-link src="${jpeg.home}/lib/libjpeg.a"
                        dest="depends/libs/build/jpeg/libjpeg.${hy.platform}"
                        message="${jpeg.msg}" />
        <check-one-link src="${jpeg.home}/include/jconfig.h"
                        dest="depends/libs/build/jpeg/jconfig.lnx"
                        message="${jpeg.msg}" />
        <check-one-link src="${jpeg.home}/include/jpeglib.h"
                        dest="depends/libs/build/jpeg/jpeglib.h"
                        message="${jpeg.msg}" />
        <check-one-link src="${jpeg.home}/include/jmorecfg.h"
                        dest="depends/libs/build/jpeg/jmorecfg.h"
                        message="${jpeg.msg}" />
        <check-one-link src="${jpeg.home}/include/jerror.h"
                        dest="depends/libs/build/jpeg/jerror.h"
                        message="${jpeg.msg}" />
    -->

    </target>

    <target name="download" description="Download required jars"
            depends="-download,-download-win" />

    <target name="-download">

    <mkdir dir="${yoko.dir}" />
    <download-one-file src="${yoko.url}" dest="${yoko.jar}"
                           md5="${yoko.md5}" />

    <mkdir dir="${yoko-core.dir}" />
    <download-one-file src="${yoko-core.url}" dest="${yoko-core.jar}"
                           md5="${yoko-core.md5}" />

    <mkdir dir="${yoko-rmi-impl.dir}" />
    <download-one-file src="${yoko-rmi-impl.url}" dest="${yoko-rmi-impl.jar}"
                           md5="${yoko-rmi-impl.md5}" />

    <mkdir dir="${yoko-rmi-spec.dir}" />
    <download-one-file src="${yoko-rmi-spec.url}" dest="${yoko-rmi-spec.jar}"
                           md5="${yoko-rmi-spec.md5}" />

    <mkdir dir="${bcel.dir}" />
    <download-one-file src="${bcel.url}" dest="${bcel.jar}"
                           md5="${bcel.md5}" />

    <mkdir dir="${xalan.dir}" />
    <download-one-file src="${xalan.url}" dest="${xalan.jar}"
                           md5="${xalan.md5}" />

    <mkdir dir="${xerces.dir}" />
    <download-one-file src="${xerces.url}" dest="${xerces.zip}"
                           md5="${xerces.md5}" />

    <mkdir dir="${xmlsec.dir}" />
    <download-one-file src="${xmlsec.url}" dest="${xmlsec.zip}"
                           md5="${xmlsec.md5}" />

    <mkdir dir="${junit.dir}" />
    <download-one-file src="${junit.url}" dest="${junit.jar}"
                           md5="${junit.md5}" />

    <mkdir dir="${bcprov.dir}" />
    <download-one-file src="${bcprov.url}" dest="${bcprov.jar}"
                           md5="${bcprov.md5}" />

    <mkdir dir="${icu4j.dir}" />
    <download-one-file src="${icu4j.url}" dest="${icu4j.jar}"
                           md5="${icu4j.md5}" />

    <mkdir dir="${mx4j.dir}" />
    <download-one-file src="${mx4j.url}" dest="${mx4j.zip}"
                           md5="${mx4j.md5}" />

    <mkdir dir="${ecj.dir}" />
    <download-one-file src="${ecj.url}" dest="${ecj.jar}"
                           md5="${ecj.md5}" />
        
        <mkdir dir="${jetty.dir}" />
    <download-one-file src="${jetty.url}" dest="${jetty.jar}"
                           md5="${jetty.md5}" />
 
    <download-one-file src="${jetty-util.url}" dest="${jetty-util.jar}"
                           md5="${jetty-util.md5}" />
                           
        <download-one-file src="${servlet-api.url}" dest="${servlet-api.jar}"
                           md5="${servlet-api.md5}" />

    </target>

    <target name="-download-win" if="is.windows" depends="-select-win-deps-x86_64,-select-win-deps-x86">

    <property environment="hyenv" />

    <mkdir dir="${msvcr.dir}" />

    <download-one-file src="${msvcr.url}" dest="${msvcr.dll}"
                           md5="${msvcr.md5}" />

    <mkdir dir="${awtdeps.dir}" />
    <download-one-file src="${awtdeps.url}" dest="${awtdeps.tar}"
                           md5="${awtdeps.md5}" />

    </target>

    <macrodef name="download-one-file">
        <attribute name="src" />
        <attribute name="dest" />
        <attribute name="md5" />
        <sequential>
            <echo>Checking @{dest}</echo>
        <available file="@{dest}" type="file"
                       property="@{dest}.exists" />
            <antcall target="-really-download">
                <param name="src" value="@{src}" />
                <param name="dest" value="@{dest}" />
            </antcall>
            <checksum file="@{dest}" property="@{dest}.md5" />
            <condition property="@{dest}.md5.verified" value="true">
                <equals arg1="${@{dest}.md5}" arg2="@{md5}" />
            </condition>
            <antcall target="-remove-file-if-bad">
                <param name="jar" value="@{dest}" />
            </antcall>
        <fail unless="@{dest}.md5.verified">
...

File @{dest} has incorrect md5 checksum.  Expected:
  @{md5}
found:
  ${@{dest}.md5}

            </fail>
            <echo>Checksum verified (${@{dest}.md5.verified})</echo>
        </sequential>
    </macrodef>

    <target name="-really-download" unless="${dest}.exists">
        <echo>Fetching ${dest}</echo>
    <get src="${src}" dest="${dest}" usetimestamp="true" verbose="true" />
    </target>

    <target name="-remove-file-if-bad" unless="${dest}.md5.verified">
        <delete file="${dest}" />
    </target>

    <macrodef name="check-one-file">
        <attribute name="src" />
        <attribute name="dest" />
        <sequential>
            <echo>Checking for @{dest}</echo>
        <available file="@{dest}" type="file"
                       property="@{dest}.exists" />
        <fail>
        <condition>
            <not>
            <isset property="@{dest}.exists" />
            </not>
        </condition>
...

Missing dependency.  The jar from:

  @{src}

should be downloaded to:

  @{dest}

Run "ant fetch-depends" to automatically fetch dependencies.
Note: Some of Harmony's dependencies are licensed under terms other
than the Apache License v2.
            </fail>
        </sequential>
    </macrodef>

    <target name="-awt-tar-extract" unless="awtdeps.uptodate">
        <echo>Extracting awt dependencies</echo>
        <untar src="${awtdeps.tar}" dest="${awtdeps.extract.dir}"
               compression="gzip" />
        <echo file="${awtdeps.testfile}"
              message="${awtdeps.tar} extracted${line.separator}" />
    </target>

    <macrodef name="check-one-link">
        <attribute name="src" />
        <attribute name="dest" />
        <attribute name="message" />
        <sequential>
            <echo>Checking for @{dest}</echo>
        <available file="@{dest}" type="file"
                       property="@{dest}.exists" />
        <available file="@{src}" type="file"
                       property="@{src}.exists" />
        <fail>
        <condition>
                    <and>
                <not>
                <isset property="@{dest}.exists" />
                        </not>
                        <not>
                <isset property="@{src}.exists" />
                </not>
                    </and>
        </condition>
...

Missing dependency.  The file from:

  @{src}

should be linked to:

  @{dest}

But @{src} doesn't exist.
@{message}
            </fail>
            <antcall target="-really-link">
                <param name="src" value="@{src}" />
                <param name="dest" value="@{dest}" />
            </antcall>
        </sequential>
    </macrodef>

    <target name="-really-link" unless="${dest}.exists">
        <echo>Linking ${dest}</echo>
        <symlink resource="${src}" link="${basedir}/${dest}" />
    </target>

</project>
