<?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="svn-deps">
    
    <target name="-svn-info" 
        description="Sets global SVN properties of the workspace: 
        is.svn, svn.revision and svn.info">
        <svn-info dir="${basedir}" prefix="global"/>
        <condition property="is.svn">
            <isset property="global.entry.revision"/>
        </condition>
        <condition property="svn.revision" value="${global.entry.revision}" else="unknown">
            <isset property="is.svn"/>
        </condition>
        <condition property="svn.info" 
            value="${global.entry.repository.root} ${svn.revision}">
            <isset property="is.svn"/>
        </condition>
        <condition property="svn.root" 
                   value="${global.entry.repository.root}"
                   else="http://svn.apache.org/repos/asf">
            <isset property="is.svn"/>
        </condition>
        
        <tstamp>
            <format property="svn.info" pattern="'Unknown revision at' dd-MMM-yyyy hh:mm aa"/>
        </tstamp>
    </target>
    
    <target name="fetch-svn">
        <setup-svn dep="${dependency}"/>
        <antcall target="svn-co" inheritAll="false">
            <propertyset>
                <propertyref prefix="${dependency}."/>
                <mapper type="glob" from="${dependency}.*" to="dep.*"/>
            </propertyset>
        </antcall>
    </target>

    <target name="svn-co" unless="dep.svn.dir.uptodate">
        <exec executable="svn" failonerror="true">
            <arg line="co -r ${dep.svn.revision} ${dep.svn.url} ${dep.svn.dir}" />
        </exec>
    </target>

    <target name="check-svn">
        <check-svn dep="${dependency}"/>
    </target>
    
    <macrodef name="check-svn">
        <attribute name="dep" />
        <sequential>
            <setup-svn dep="@{dep}"/>
            <echo message="Checking @{dep} SVN workspace at ${@{dep}.svn.dir}"/>
            <fail unless="@{dep}.svn.dir.uptodate">
                ...
Dependency is not available or out of date: ${dependency}.
It should be checked out
    from SVN repository: ${@{dep}.svn.url} 
    revision: ${@{dep}.svn.revision}
    to: ${@{dep}.svn.dir}
    
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>

    <macrodef name="setup-svn">
        <attribute name="dep" />
        <sequential>
            <property name="@{dep}.svn.dir" value="${depends.oss}/@{dep}"/>
            <property name="@{dep}.svn.revision" value="HEAD"/>
            <svn-info dir="${@{dep}.svn.dir}" prefix="@{dep}"/>

            <condition property="@{dep}.svn.dir.uptodate">
                <and>
                    <equals arg1="${@{dep}.svn.revision}" arg2="${@{dep}.entry.revision}"/>
                    <equals arg1="${@{dep}.svn.url}" arg2="${@{dep}.entry.url}"/>
                </and>
            </condition>
        </sequential>
    </macrodef>
    
    <macrodef name="svn-info">
        <attribute name="dir" />
        <attribute name="prefix" default=""/>
        <sequential>
            <tempfile suffix=".xml" property="@{dir}.tmp"/>
            <exec executable="svn" output="${@{dir}.tmp}" resultproperty="@{dir}.svn.status">
                <arg line="info @{dir} --non-interactive --xml" />
            </exec>
            <condition property="@{dir}.info.file" value="${@{dir}.tmp}">
                <equals arg1="0" arg2="${@{dir}.svn.status}"/>
            </condition>
            <xmlproperty file="${@{dir}.info.file}" prefix="@{prefix}" 
                keeproot="no" collapseAttributes="yes"/>
            <delete file="${@{dir}.tmp}" quiet="true"/>
        </sequential>
    </macrodef>

</project>
