<?xml version="1.0" encoding="utf-8"?>
<project name="modern" default=".help">
    <!--
    The build-impl.xml file imported here contains the guts of the build process. It is
    a great idea to read that file to understand how the process works, but it is best to
    limit your changes to this file.
    -->
    <script language="javascript">
        <![CDATA[
            var dir = project.getProperty("basedir"),
                cmdDir = project.getProperty("cmd.dir"),
                cmdLoaded = project.getReference("senchaloader");
            
            if (!cmdLoaded) {
                function echo(message, file) {
                    var e = project.createTask("echo");
                    e.setMessage(message);
                    if (file) {
                        e.setFile(file);
                    }
                    e.execute();
                };

                if (!cmdDir) {
                    
                    function exec(args) {
                        var process = java.lang.Runtime.getRuntime().exec(args),
                            input = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream())),
                            headerFound = false,
                            line;

                        while (line = input.readLine()) {
                            line = line + '';
                            java.lang.System.out.println(line);
                            if (line.indexOf("Sencha Cmd") > -1) {
                                headerFound = true;
                            }
                            else if (headerFound && !cmdDir) {
                                cmdDir = line;
                                project.setProperty("cmd.dir", cmdDir);
                            }
                        }
                        process.waitFor();
                        return !!cmdDir;
                    }
                    
                    if (!exec(["sencha", "which"])) {
                        var tmpFile = "tmp.sh";
                        echo("source ~/.bash_profile; sencha " + whichArgs.join(" "), tmpFile);
                        exec(["/bin/sh", tmpFile]);
                        new java.io.File(tmpFile)['delete'](); 
                    }
                }
            }
            
            if (cmdDir && !project.getTargets().containsKey("init-cmd")) {
                var importTask = project.createTask("import");
                importTask.setOwningTarget(self.getOwningTarget());
                importTask.setLocation(self.getLocation());
                importTask.setFile(cmdDir + "/ant/build/package/build-impl.xml");
                importTask.execute();
            }
        ]]>
    </script>
    <import file="${basedir}/.sencha/test/test-impl.xml"/>

    <!--
    The following targets can be provided to inject logic before and/or after key steps
    of the build process:

        The "init-local" target is used to initialize properties that may be personalized
        for the local machine.

            <target name="-before-init-local"/>
            <target name="-after-init-local"/>

        The "clean" target is used to clean build output from the build.dir.

            <target name="-before-clean"/>
            <target name="-after-clean"/>

        The general "init" target is used to initialize all other properties, including
        those provided by Sencha Cmd.

            <target name="-before-init"/>
            <target name="-after-init"/>
        
        The "build" target performs the call to Sencha Cmd to build the application.

            <target name="-before-build"/>
            <target name="-after-build"/>
    -->


    <target name="-js">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            --ignore=${classpath.excludes}
            --include-preprocessor-tags=true
            --options=debug:true,product:ext,minVersion:6,logger:yes
                concatenate
                    --output-file=${build.dir}/ext-modern-all-debug.js
                and
                union
                    --tag=package-core,core
                and
                concatenate
                    --output-file=${build.dir}/ext-modern-debug.js
                and
                include
                    +all
                and
                exclude
                    --tag=pacakge-core,core
                and
                metadata
                    +append
                    +alternates
                    --base-path=${build.dir}
                    --output-file=${build.dir}/ext-modern-debug.js
                and
                metadata
                    +append
                    +alias
                    --base-path=${build.dir}
                    --output-file=${build.dir}/ext-modern-debug.js
                and
            --options=debug:false,logger:no
                union
                    +all
                and
                concatenate
                    +yui
                    --output-file=${build.dir}/ext-modern-all.js
            ]]>
        </x-compile>
    </target>

    <target name="-after-js">

        <x-compress-js
                srcfile="${build.dir}/ext-modern-debug.js"
                outfile="${build.dir}/ext-modern.js"/>

        <x-compress-js
                srcfile="${build.dir}/ext-modern-all-debug.js"
                outfile="${build.dir}/ext-modern-all.js"/>

        <for list="ext-modern,ext-modern-all"
             param="file">
            <sequential>
                <x-strip-js srcfile="${build.dir}/@{file}-debug.js"
                            outfile="${build.dir}/@{file}-debug.js"/>
            </sequential>
        </for>
    </target>

    <target name="calendar" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=calendar
                -path=${framework.dir}/../packages/calendar/modern/src
            and
            classpath
                -name=calendar
                -path=${framework.dir}/../packages/calendar/src
            and
            classpath
                -name=calendar
                -path=${framework.dir}/../packages/calendar/modern/overrides
                -tags=overrides
            and
            classpath
                -name=calendar
                -path=${framework.dir}/../packages/calendar/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <target name="charts" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=charts
                -path=${framework.dir}/packages/charts/modern/src
            and
            classpath
                -name=charts
                -path=${framework.dir}/packages/charts/src
            and
            classpath
                -name=charts
                -path=${framework.dir}/packages/charts/modern/overrides
                -tags=overrides
            and
            classpath
                -name=charts
                -path=${framework.dir}/packages/charts/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <target name="ux" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=ux
                -path=${framework.dir}/packages/ux/modern/src
            and
            classpath
                -name=ux
                -path=${framework.dir}/packages/ux/src
            and
            classpath
                -name=ux
                -path=${framework.dir}/packages/ux/modern/overrides
                -tags=overrides
            and
            classpath
                -name=ux
                -path=${framework.dir}/packages/ux/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <target name="qr-code" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=qr-code
                -path=${framework.dir}/packages/qr-code/modern/src
            and
            classpath
                -name=qr-code
                -path=${framework.dir}/packages/qr-code/src
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <target name="froala-editor" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                -name=froala-editor
                -path=${framework.dir}/../packages/froala-editor/modern/src
            and
            classpath
                -name=froala-editor
                -path=${framework.dir}/../packages/froala-editor/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>
    
    <target name="d3" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=d3
                -path=${framework.dir}/../packages/d3/modern/src
            and
            classpath
                -name=d3
                -path=${framework.dir}/../packages/d3/src
            and
            classpath
                -name=d3
                -path=${framework.dir}/../packages/d3/modern/overrides
                -tags=overrides
            and
            classpath
                -name=d3
                -path=${framework.dir}/../packages/d3/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <target name="exporter" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=exporter
                -path=${framework.dir}/../packages/exporter/modern/src
            and
            classpath
                -name=exporter
                -path=${framework.dir}/../packages/exporter/src
            and
            classpath
                -name=exporter
                -path=${framework.dir}/../packages/exporter/modern/overrides
                -tags=overrides
            and
            classpath
                -name=exporter
                -path=${framework.dir}/../packages/exporter/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <target name="pivot" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=pivot
                -path=${framework.dir}/../packages/pivot/modern/src
            and
            classpath
                -name=pivot
                -path=${framework.dir}/../packages/pivot/src
            and
            classpath
                -name=pivot
                -path=${framework.dir}/../packages/pivot/modern/overrides
                -tags=overrides
            and
            classpath
                -name=pivot
                -path=${framework.dir}/../packages/pivot/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <target name="pivot-d3" depends="init,pivot,d3">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=pivot-d3
                -path=${framework.dir}/../packages/pivot-d3/modern/src
            and
            classpath
                -name=pivot-d3
                -path=${framework.dir}/../packages/pivot-d3/src
            and
            classpath
                -name=pivot-d3
                -path=${framework.dir}/../packages/pivot-d3/modern/overrides
                -tags=overrides
            and
            classpath
                -name=pivot-d3
                -path=${framework.dir}/../packages/pivot-d3/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <target name="signature" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=signature
                -path=${framework.dir}/packages/signature/modern/src
            and
            classpath
                -name=signature
                -path=${framework.dir}/packages/signature/src
            and
            include
                -all
        ]]>
        </x-compile>
    </target>

    <!--
    <target name="google" depends="init">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            classpath
                +new
                -name=google
                -path=${framework.dir}/packages/google/modern/src
            and
            classpath
                -name=google
                -path=${framework.dir}/packages/google/src
            and
            classpath
                -name=google
                -path=${framework.dir}/packages/google/modern/overrides
                -tags=overrides
            and
            classpath
                -name=google
                -path=${framework.dir}/packages/google/overrides
                -tags=overrides
            and
            include
                -all
        ]]>
        </x-compile>
    </target>
    -->

    <target name="core-bootstrap" depends="init">
        <local name="base.path"/>
        <property name="base.path" value="${framework.dir}"/>

        <delete file="${base.path}/bootstrap-modern-manifest.js"/>

        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
                include
                    -all
                and
                exclude
                    +recursive
                    -class=Ext.Loader
                and
                metadata
                    -tpl=var Ext = Ext || '{' '}'; Ext.manifest = {0};
                    -info=AppManifest
                    -basePath=${base.path}
                    +append
                    +bootRelative
                    -output-file=${base.path}/bootstrap-modern-manifest.js
            ]]>
        </x-compile>

        <x-bootstrap file="${base.path}/bootstrap.js"
                     basedir="${base.path}"
                     coreFilesFile="${base.path}/bootstrap-modern-files.js"
                     classMetadataFile="${base.path}/bootstrap-modern-data.js"
                     overridesFile="${base.path}/bootstrap-modern-data.js"
                     includeBoot="true"
                     includeCoreFiles="true"
                     appendCoreFiles="false"
                     appendClassMetadata="false"
                     appendOverrides="true"
                     coreFilesJsonpTpl="Ext.Boot.loadSyncBasePrefix"
                     loaderConfigJsonpTpl="Ext.Loader.addBaseUrlClassPathMappings"
                     overrideTpl="Ext.Loader.loadScriptsSyncBasePrefix"
                     overrideTplType="jsonp"
                     overrideExcludeTags="">
            <![CDATA[
Ext.Boot.loadSyncBasePrefix([
    "bootstrap-modern-manifest.js",
    "bootstrap-modern-files.js"
]);
]]>
        </x-bootstrap>

        <!--
        Duplicate the "ext.js" dev-mode bootstrap file to the other deployed names
        -->
        <for list="ext-modern,ext-modern-all" param="part1">
            <sequential>
                <for list=".,-debug." param="part2">
                    <sequential>
                        <local name="out"/>
                        <property name="out" value="@{part1}@{part2}js"/>
                        <echo>Generating ${out}</echo>
                        <copy file="${base.path}/bootstrap.js" tofile="${base.path}/${out}"
                              overwrite="true"/>
                        <replace file="${base.path}/${out}"
                                 token="bootstrapper"
                                 value="bootstrapper :: GENERATED COPY! Edit &quot;core/bootstrap.js&quot;"/>
                    </sequential>
                </for>
            </sequential>
        </for>
        <delete file="${base.path}/bootstrap.js"/>
    </target>

    <target name="bootstrap"
            description="*** Regenerate files needed by standalone examples and unit tests ***"
            depends="calendar,charts,ux,froala-editor,qr-code, exporter,d3,pivot,pivot-d3,signature,core-bootstrap,test-bootstrap"/>

</project>
