<project name="x-sass-impl">

    <target name="-init-sass-compiler" depends="-init-compiler">
        <x-normalize-path path="${build.out.resources.dir}"
                          property="image.search.path"/>
        <condition property="is.theme.package" value="true">
            <equals arg1="${package.type}" arg2="theme"/>
        </condition>
    </target>

    <target name="-compile-sass-rtl-theme" depends="-init-sass-compiler" if="is.theme.package">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            include
                -all
            and
            sass
                -etc=true
                -vars=true
                -rules=true
                -class-name-vars=true
                -variable=$image-search-path:'${image.search.path}' !default
                -variable=$theme-name: '${package.name}' !default
                -output=${build.all.rtl.scss}
                -forward=${package.sass.dynamic}
            and
            sass
                -ruby=true
                -output=${build.all.ruby}
            ]]>
        </x-compile>
    </target>

    <target name="-compile-sass-ltr-theme" depends="-init-sass-compiler" if="is.theme.package">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            exclude
                -all
            and
            include
                -not
                -namespace=Ext.rtl
            and
            sass
                -etc=true
                -vars=true
                -rules=true
                -class-name-vars=true
                -variable=$image-search-path:'${image.search.path}' !default
                -variable=$theme-name: '${theme.name}' !default
                -output=${build.all.scss}
                -forward=${package.sass.dynamic}
            and
            sass
                -ruby=true
                -output=${build.all.ruby}
            ]]>
        </x-compile>
    </target>

    <target name="-compile-sass-rtl" depends="-init-sass-compiler" unless="is.theme.package">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            exclude
                -all
            and
            include
                -tag=package-${package.name}
            and
            save
                pkg
            and
            sass
                -class-name-vars=true
                -variable=$image-search-path:'${image.search.path}' !default
                -variable=$theme-name: '${theme.name}' !default
                -output=${build.all.rtl.scss}
                -forward=${package.sass.dynamic}
            and
            include
                -all
            and
            save
                all
            and
            sass
                -etc=true
                -vars=true
                +append
                -output=${build.all.rtl.scss}
                -forward=${package.sass.dynamic}
            and
            restore
                pkg
            and
            sass
                -rules=true
                +append
                -output=${build.all.rtl.scss}
            and
            sass
                -ruby=true
                -output=${build.all.ruby}
            ]]>
        </x-compile>
    </target>

    <target name="-compile-sass-ltr" depends="-init-sass-compiler" unless="is.theme.package">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            exclude
                -all
            and
            include
                -tag=package-${package.name}
            and
            save
                pkg
            and
            sass
                -class-name-vars=true
                -variable=$image-search-path:'${image.search.path}' !default
                -variable=$theme-name: '${theme.name}' !default
                -output=${build.all.scss}
                -forward=${package.sass.dynamic}
            and
            exclude
                -all
            and
            include
                -not
                -namespace=Ext.rtl
            and
            save
                all-rtl
            and
            sass
                -etc=true
                -vars=true
                +append
                -output=${build.all.scss}
                -forward=${package.sass.dynamic}
            and
            restore
                pkg
            and
            sass
                -rules=true
                +append
                -output=${build.all.scss}
            and
            sass
                -ruby=true
                -output=${build.all.ruby}
            ]]>
        </x-compile>
    </target>

    <target name="-compile-sass"
            depends="-compile-sass-rtl-theme,-compile-sass-ltr-theme,-compile-sass-rtl,-compile-sass-ltr">
        <echo file="${package.example.compass.config}">
            require '${build.all.ruby}'
            cache_path = '${compass.cache.dir}'
        </echo>
    </target>

    <macrodef name="x-compress-css-files">
        <attribute name="dir"/>
        <attribute name="prefix"/>
        <attribute name="outprefix"/>
        <sequential>
            <x-split-css file="@{dir}/@{prefix}.css"
                         outdir="${build.resources.dir}"
                         limit="${build.css.selector.limit}"/>

            <for param="cssfile">
                <fileset dir="@{dir}" includes="@{prefix}*.css"/>
                <sequential>
                    <local name="css.output.name"/>
                    <local name="pattern"/>
                    <property name="pattern" value="(.*?)(@{prefix})(_\d{1,2})*\.css"/>
                    <propertyregex property="css.output.name"
                                   input="@{cssfile}"
                                   regexp="${pattern}"
                                   select="\1@{outprefix}\3.css"
                                   override="true"/>
                    <x-echo>Compressing @{cssfile} to ${css.output.name}</x-echo>
                    <x-compress-css srcfile="@{cssfile}"
                                    outfile="${css.output.name}"/>
                </sequential>
            </for>

            <replaceregexp file="@{dir}/@{outprefix}.css"
                           match="@import '@{prefix}(_\d\d).css';"
                           replace="@import '@{outprefix}\1.css';"
                           flags="g"/>
        </sequential>
    </macrodef>

    <target name="-compass-compile" depends="-load-sass-page,-compile-sass">
        <if>
            <x-is-true value="${package.sass.fashion}"/>
            <then>
                <if>
                    <x-is-false value="${package.sass.rhino}"/>
                    <then>
                        <x-sass-build input="${compass.sass.dir}"
                                      output="${compass.css.dir}"
                                      refId="package.web.server"/>
                    </then>
                    <else>
                        <x-fashion-compile
                                file="${compass.sass.dir}"
                                toFile="${compass.css.dir}"/>
                    </else>
                </if>
                
            </then>
            <else>
                <x-compass-compile
                        rubyPath="${build.ruby.path}"
                        trace="${compass.compile.trace}"
                        boring="${compass.compile.boring}"
                        force="${compass.compile.force}"
                        dir="${compass.working.dir}"
                        sassdir="${compass.sass.dir}"
                        cssdir="${compass.css.dir}"
                        config="${package.example.compass.config}"/>
            </else>
        </if>
    </target>

    <target name="-compile-css" depends="-compass-compile">
        <x-compress-css-files
                dir="${build.resources.dir}"
                prefix="${build.all.css.debug.prefix}"
                outprefix="${build.all.css.prefix}"/>

        <x-compress-css-files
                dir="${build.resources.dir}"
                prefix="${build.all.rtl.css.debug.prefix}"
                outprefix="${build.all.rtl.css.prefix}"/>
    </target>

    <target name="-before-sass"/>
    <target name="-sass" depends="-compile-css"/>
    <target name="-after-sass"/>

</project>