<?xml version="1.0"?>
<project name="eUI-build" basedir=".">

    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <fileset dir="lib" includes="ant-contrib-*.jar"/>
        </classpath>
    </taskdef>

    <scriptdef language="javascript" name="lower">
        <attribute name="string" />
        <attribute name="to" />

        project.setProperty( attributes.get( "to" ),
                             attributes.get( "string" ).toLowerCase() );
    </scriptdef>

    <scriptdef language="javascript" name="ucfirst">
        <attribute name="string" />
        <attribute name="to" />

        var the_string = attributes.get( "string" );
        project.setProperty( attributes.get( "to" ),
                    the_string.substr(0,1).toUpperCase() + the_string.substr(1) );
    </scriptdef>

    <scriptdef name="camelCaseString" language="javascript">
        <attribute name="text" />
        <attribute name="separator" />
        <attribute name="property" />
        <attribute name="first" />
        <![CDATA[
            var string = attributes.get("text");
            var stringArray = string.split(attributes.get("separator"));
            var result = "";
            var s;

            for (var i=0; i<stringArray.length;i++) {
                s = stringArray[i];
                if (i == 0) {
                    if (attributes.get("first") == 'true') {
                        result += s.substring(0,1).toUpperCase() + s.substr(1);
                    } else {
                        result += s;
                    }
                } else {
                    result += s.substring(0,1).toUpperCase() + s.substr(1);
                }
            }

            project.setProperty(attributes.get("property"), result);
        ]]>
    </scriptdef>


    <macrodef name="replaceAll" taskname="@{taskname}">
        <attribute name="src" />
        <attribute name="dest" default="" />
        <attribute name="replace" default="" />
        <attribute name="with" default="" />
        <sequential>
            <loadresource property="@{dest}">
                <propertyresource name="@{src}" />
                <filterchain>
                    <tokenfilter>
                        <filetokenizer/>
                        <replacestring from="@{replace}" to="@{with}"/>
                    </tokenfilter>
                </filterchain>
            </loadresource>
        </sequential>
    </macrodef>



    <!--======================================================================-->
    <!--                     TOKEN FILTERS                                    -->
    <!--======================================================================-->

    <!--application files replacement-->
    <filter token="app.name" value="${app.artifact.id}"/>
    <filter token="app.name.display" value="${app.artifact.id}"/>
    <filter token="app.group.id" value="${app.group.id}"/>

    <target name="generate.web-spring-boot">
        <echo></echo>
        <echo>TARGET : ${target.path}</echo>
        <echo></echo>
        <echo>app.name: ${app.name}</echo>
        <echo>app.artifact.id: ${app.artifact.id}</echo>
        <echo>app.group.id: ${app.group.id}</echo>
        <echo></echo>

         <copy todir="${target.path}" filtering="true" overwrite="true">
            <fileset dir="${src.path}"/>
         </copy>

         <!--REPLACE MODULE NAMES DIRECTORIES-->
         <move toFile="${target.path}/${app.artifact.id}-web-rest"
               file="${target.path}/myapp-web-rest"/>
         <move toFile="${target.path}/${app.artifact.id}-web"
               file="${target.path}/myapp-web"/>

        <!--RENAMING JAVA SOURCES TARGET by custom app group id provided-->

        <replaceAll src="app.group.id" dest="app.group.id.dir" replace="." with="/"/>

        <move tofile="${target.path}/${app.name}-web-rest/src/main/java/${app.group.id.dir}/${app.name}"
              file="${target.path}/${app.name}-web-rest/src/main/java/myapp"/>
        <mkdir dir="${target.path}/${app.name}-web-rest/src/test/java/${app.group.id.dir}/${app.name}"/>

    </target>

</project>

