<?xml version="1.0"?>
<!--suppress JSUnresolvedVariable, JSUnresolvedFunction -->
<project name="get-dependency">

    <!-- # Runtime library dependency -->
    <property name="repos.dir" location="${user.home}/.javalib/repos" />


    <!-- http get from @{url} to @{dest} -->
    <macrodef name="get-from-http">
        <attribute name="url" />
        <attribute name="destdir" />
        <sequential>
            <mkdir dir="@{destdir}.tmp" />
            <get src="@{url}" dest="@{destdir}.tmp/" />
            <move file="@{destdir}.tmp" tofile="@{destdir}" />
        </sequential>
    </macrodef>

    <!-- http get from @{url} and then uncompress to @{dest} -->
    <macrodef name="uncompress-from-http">
        <attribute name="url" />
        <attribute name="destdir" />
        <sequential>
            <mkdir dir="@{destdir}.tmp" />
            <get src="@{url}" dest="@{destdir}.tmp/tmp" />
            <!-- Use command line tar - xzf to keep file permissions -->
            <exec executable="tar" spawn="true" osfamily="unix">
                <arg value="-xzf" />
                <arg value="@{destdir}.tmp/tmp" />
                <arg value="-C" />
                <arg value="@{destdir}.tmp" />
            </exec>
            <untar src="@{destdir}.tmp/tmp" dest="@{destdir}.tmp" compression="gzip" />
            <move tofile="@{destdir}">
                <dirset dir="@{destdir}.tmp" includes="*" />
            </move>
            <delete dir="@{destdir}.tmp" quiet="yes" />
        </sequential>
    </macrodef>

    <!-- http get from @{url} and then uncompress to @{dest} -->
    <macrodef name="unzip-from-http">
        <attribute name="url" />
        <attribute name="destdir" />
        <sequential>
            <mkdir dir="@{destdir}.tmp" />
            <get src="@{url}" dest="@{destdir}.tmp/tmp" />
            <unzip src="@{destdir}.tmp/tmp" dest="@{destdir}.tmp" />
            <move tofile="@{destdir}">
                <dirset dir="@{destdir}.tmp" includes="*" />
            </move>
            <delete dir="@{destdir}.tmp" quiet="yes" />
        </sequential>
    </macrodef>

    <!-- svn export from @{url} to @{dest} -->
    <macrodef name="export-from-svn">
        <attribute name="url" />
        <attribute name="destdir" />
        <sequential>
            <exec executable="svn" failonerror="true">
                <arg value="export" />
                <arg value="-q" />
                <arg value="@{url}" />
                <arg value="@{destdir}" />
            </exec>
        </sequential>
    </macrodef>

    <macrodef name="get-dependency">
        <attribute name="url" />
        <attribute name="destdir" />
        <sequential>
            <script language="javascript" taskname="get">
                // url:     @{url}
                // destdir: @{destdir}

                (function install() {
                    // 为兼容 Windows 的路径分隔符，暂时只能使用这么特殊的方式读取宏变量
                    var text = self.runtimeConfigurableWrapper.text.toString();
                    var url = text.match(/\/\/\s*url: *(.*)/)[1];
                    var destdir = text.match(/\/\/\s*destdir: *(.*)/)[1];

                    // 加入使得对旧版 rhino 引擎有更好的兼容性
                    String.prototype.endsWith = function (suffix) {
                        return (this.substring(this.length - suffix.length) === suffix);
                    };

                    if (project.resolveFile(destdir).exists()) {
                        self.log(url + " -> " + destdir, org.apache.tools.ant.Project.MSG_VERBOSE);
                        return;
                    } else {
                        self.log(url + " -> " + destdir);
                    }

                    function runTask(taskName) {
                        // noinspection ES6ConvertVarToLetConst
                        var task = project.createTask(taskName);
                        task.setDynamicAttribute("url", url);
                        task.setDynamicAttribute("destdir", destdir);
                        task.perform();
                    }

                    if (url.endsWith(".tgz") || url.endsWith(".tar.gz")) {
                        return runTask("uncompress-from-http");
                    } else if (url.endsWith(".zip")) {
                        return runTask("unzip-from-http");
                    } else if (url.endsWith(".jar")) {
                        return runTask("get-from-http");
                    } else if (url.endsWith("/")) {
                        return runTask("export-from-svn");
                    }

                    // noinspection ES6ConvertVarToLetConst
                    var task = project.createTask("fail");
                    task.message = "Unknown url: " + url;
                    task.perform();
                }());
            </script>
        </sequential>
    </macrodef>
</project>
