<project name="x-sub-builds-impl">

    <macrodef name="x-process-sub-packages">
        <attribute name="all" default="false"/>
        <element name="tasks" implicit="true"/>
        <sequential>
            <if>
                <and>
                    <isset property="package.subpkgs.packages"/>
                    <not>
                        <isset property="package.subpkgs.dir"/>
                    </not>
                </and>
                <then>
                    <property name="package.subpkgs.dir" value="${package.dir}"/>
                </then>
            </if>
            <if>
                <and>
                    <isset property="package.subpkgs.dir"/>
                    <available file="${package.subpkgs.dir}" type="dir"/>
                </and>
                <then>
                    <local name="sub.packages.list"/>
                    <condition property="sub.packages.list" value="${package.subpkgs.packages}">
                        <and>
                            <isset property="package.subpkgs.packages"/>
                            <equals arg1="@{all}" arg2="false"/>
                        </and>
                    </condition>
                    <property name="sub.packages.list" value="*"/>
                    <for param="pkg-dir">
                        <dirset dir="${package.subpkgs.dir}" includes="${sub.packages.list}"/>
                        <sequential>
                            <if>
                                <available file="@{pkg-dir}/package.json"/>
                                <then>
                                    <tasks/>
                                </then>
                            </if>
                        </sequential>
                    </for>
                </then>
            </if>
        </sequential>
    </macrodef>

    <macrodef name="x-process-examples">
        <attribute name="all" default="false"/>
        <element name="tasks" implicit="true"/>
        <sequential>
            <local name="example.apps"/>
            <local name="example.path"/>
            <local name="example.dir"/>

            <condition property="example.apps" value="*">
                <equals arg1="@{all}" arg2="true"/>
            </condition>
            <condition property="example.apps" value="${package.example.apps}">
                <isset property="package.example.apps"/>
            </condition>
            <condition property="example.apps" value="${package.examples}">
                <isset property="package.examples"/> <!-- legacy value -->
            </condition>

            <condition property="example.path" value="${package.example.path}">
                <isset property="package.example.path"/>
            </condition>
            <condition property="example.path" value="${package.examples.dir}">
                <isset property="package.examples.dir"/> <!-- legacy value -->
            </condition>

            <if>
                <isset property="example.path"/>
                <then>
                    <for list="${example.path}" delimiter="," param="dir">
                        <sequential>
                            <x-canonical-path property="example.dir" overwrite="true"
                                    path="@{dir}"
                                    basedir="${package.dir}"/>

                            <x-echo>Processing examples in "@{dir}" (${example.dir})</x-echo>

                            <if>
                                <isset property="example.apps"/>
                                <then>
                                    <for list="${example.apps}" delimiter="," param="app">
                                        <sequential>
                                            <if>
                                                <available file="${example.dir}/@{app}/app.json"/>
                                                <then>
                                                    <!--
                                                    Use for loop so <tasks> can use the
                                                    pieces @{app} and @{dir} of the full
                                                    path @{example-dir}
                                                    -->
                                                    <for param="example-dir">
                                                        <dirset dir="${example.dir}" includes="@{app}"/>
                                                        <sequential>
                                                            <tasks/>
                                                        </sequential>
                                                    </for>
                                                </then>
                                                <else>
                                                    <if>
                                                        <available file="${example.dir}/@{app}" type="dir"/>
                                                        <then>
                                                            <x-echo>No app at ${example.dir}/@{app}</x-echo>
                                                        </then>
                                                    </if>
                                                </else>
                                            </if>
                                        </sequential>
                                    </for>
                                </then>
                                <elseif>
                                    <available file="@{dir}" type="dir"/>
                                    <then>
                                        <for param="example-dir">
                                            <dirset dir="@{dir}" includes="*"/>
                                            <sequential>
                                                <if>
                                                    <available file="@{example-dir}/app.json"/>
                                                    <then>
                                                        <tasks/>
                                                    </then>
                                                </if>
                                            </sequential>
                                        </for>
                                    </then>
                                </elseif>
                            </if>
                        </sequential>
                    </for>
                </then>
            </if>
        </sequential>
    </macrodef>

    <macrodef name="x-run-sub-build">
        <attribute name="dir"/>
        <attribute name="target"/>
        <element name="properties" implicit="true"/>
        <sequential>
            <if>
                <available file="@{dir}/build.xml"/>
                <then>
                    <local name="sub.name"/>
                    <basename file="@{dir}" 
                             property="sub.name"/>
                    <ant dir="@{dir}" 
                         inheritall="false" 
                         inheritrefs="true"
                         target="@{target}">
                        <property name="compiler.ref.id" value="compiler-${sub.name}"/>
                        <properties/>
                    </ant>
                </then>
            </if>
        </sequential>
    </macrodef>

    <macrodef name="x-sub-build">
        <attribute name="dir"/>
        <attribute name="target" default="build"/>
        <attribute name="inherit-version" default="0"/>
        <sequential>
            <if>
                <x-is-true value="@{inherit-version}"/>
                <then>
                    <x-run-sub-build dir="@{dir}" target="@{target}">
                        <property name="cmd.dir" value="${cmd.dir}"/>
                        <property name="build.version" value="${build.version}"/>
                    </x-run-sub-build>
                </then>
                <else>
                    <x-run-sub-build dir="@{dir}" target="@{target}">
                        <property name="cmd.dir" value="${cmd.dir}"/>
                    </x-run-sub-build>
                </else>
            </if>
        </sequential>
    </macrodef>

    <target name="-before-subpkgs"/>
    <target name="-after-subpkgs"/>
    <target name="-subpkgs">
        <x-process-sub-packages>
            <local name="sub.name"/>
            <basename file="@{pkg-dir}"
                      property="sub.name"/>
            <x-echo>Building sub package ${sub.name}</x-echo>
            <if>
                <x-is-true value="${build.subpkgs.inherit.version}"/>
                <then>
                    <x-sencha-command dir="@{pkg-dir}" inheritall="false">
                        <property name="extra.packages.dir" value="${extra.packages.dir}"/>
                        <property name="compiler.ref.id" value="compiler-${sub.name}"/>
                        <property name="build.version" value="${build.version}"/>
                        <property name="package.sass.rhino" value="${package.sass.rhino}"/>
                        package
                        build
                    </x-sencha-command>
                </then>
                <else>
                    <x-sencha-command dir="@{pkg-dir}" inheritall="false">
                        <property name="extra.packages.dir" value="${extra.packages.dir}"/>
                        <property name="compiler.ref.id" value="compiler-${sub.name}"/>
                        <property name="package.sass.rhino" value="${package.sass.rhino}"/>
                        package
                        build
                    </x-sencha-command>
                </else>
            </if>
        </x-process-sub-packages>
    </target>

    <target name="-before-clean-subpkgs"/>
    <target name="-after-clean-subpkgs"/>
    <target name="-clean-subpkgs">
        <x-process-sub-packages>
            <x-echo>Cleaning sub package in @{pkg-dir}</x-echo>
            <x-sub-build dir="@{pkg-dir}"
                         target="clean"
                         inherit-version="${build.subpkgs.inherit.version}"/>
        </x-process-sub-packages>
    </target>

    <target name="-before-upgrade-subpkgs"/>
    <target name="-after-upgrade-subpkgs"/>
    <target name="-upgrade-subpkgs">
        <x-process-sub-packages>
            <x-echo>Upgrading sub package in @{pkg-dir}</x-echo>
            <x-sencha-command dir="@{pkg-dir}" inheritall="false">
                <property name="args.force" value="true"/>
                package
                upgrade
            </x-sencha-command>
            <delete dir="@{example-dir}/.sencha_backup"/>
        </x-process-sub-packages>
    </target>

    <target name="-before-examples"/>
    <target name="-after-examples"/>
    <target name="-examples">
        <x-process-examples>
            <x-echo>Building example in @{example-dir}</x-echo>
            <x-sencha-command dir="@{example-dir}" inheritall="false">
                <property name="extra.packages.dir" value="${extra.packages.dir}"/>
                <property name="app.sass.rhino" value="${package.sass.rhino}"/>
                app
                build
                -uses
            </x-sencha-command>
        </x-process-examples>
    </target>
    
    <target name="-before-upgrade-examples"/>
    <target name="-after-upgrade-examples"/>
    <target name="-upgrade-examples">
        <x-process-examples>
            <x-echo>Upgrading example in @{example-dir}</x-echo>
            <x-sencha-command dir="@{example-dir}" inheritall="false">
                <property name="args.force" value="true"/>
                app
                upgrade
            </x-sencha-command>
            <delete dir="@{example-dir}/.sencha_backup"/>
        </x-process-examples>
    </target>
        
    <target name="-before-clean-examples"/>
    <target name="-after-clean-examples"/>
    <target name="-clean-examples">
        <x-process-examples>
            <x-echo>Cleaning example in @{example-dir}</x-echo>
            <x-sub-build dir="@{example-dir}"
                         target="clean"/>
        </x-process-examples>
    </target>

    <target name="list-examples" depends="init"
            description="List all example apps for this package">
        <x-process-examples>
            <x-echo>   @{example-dir}</x-echo>
        </x-process-examples>
    </target>

</project>
