<!--
   Build script for the project "Ciform".
-->
<project name="Ciform" default="javascript.package" xmlns:ivy="antlib:org.apache.ivy.ant">

    <!-- local properties for this project -->
    <property file="local.properties"/>
    <!-- default properties for this project -->
    <property file="build.properties"/>

    <!-- common properties for all projects -->
    <property file="${common.properties}"/>

    <!-- template for javascript projects -->
    <import file="${ant.jsproject.path}" />

    <property name="src.php.dir" value="${src.dir}/php" />

    <target name="phpdoc" description="Generates PHP API docs">
        <mkdir dir="${target.dir}/doc/api/php" />
        <exec command="${phpdoc.exe}" failonerror="true">
            <arg value="--directory" />
                <arg file="${src.php.dir}" />
                <!--<arg file="${src.php.dir},/opt/PEAR/Crypt" />-->
            <arg value="--pear"/>
            <arg value="--target"/>
                <arg file="${target.dir}/doc/api/php/"/>
            <arg line="--output HTML:Smarty:PHP"/>
            <arg line="--sourcecode on"/>
            <arg value="--title"/>
                <arg value="${ant.project.name} PHP API documentation"/>
            <arg line="--parseprivate on"/>
        </exec>
    </target>

    <target name="build-biglib" depends="validate" description="Builds the bigsource file to work with for the 'lib' artifact" >
        <tempfile property="build.bigsource.js" destDir="${build.dir}" prefix="lib${ant.project.name}" suffix=".js" />
        <!-- TODO handle encoding (and eol?) -->
        <concat destfile="${build.bigsource.js}">
            <!-- Order is important because of function dependencies -->
            <!-- TODO no direct reference to the directory where dependencies are stored : use Ivy to order them accordingly to dependency priorities -->
            <fileset dir="${build.dir}/ivy/default">
                <include name="rsa*.js" />
                <include name="hex*.js" />
                <include name="base64*.js" />
                <include name="sha1*.js" />
                <include name="minilib*.js" />
            </fileset>
            <fileset dir="${src.dir}" includes="*.js" />
        </concat>
        <echo message="Created : ${build.bigsource.js}" level="verbose" />
    </target>

    <target name="build-libdefault" extensionOf="package" depends="build-biglib" description="Builds the default .js.">
        <make-default bigsource="${build.bigsource.js}" module="libciform" />
    </target>

    <target name="build-libmin" extensionOf="package" depends="build-biglib" description="Builds a minified version of the .js.">
        <make-min bigsource="${build.bigsource.js}" module="libciform" />
    </target>

    <target name="resolve-dependencies" description="(internal) Builds an ordered list of dependent modules." >
        <ivy:buildlist root="ciform" excluderoot="true" reference="build.lib.ants">
            <fileset dir="." includes="build.xml" />
            <fileset dir="../crypto" includes="**/build.xml"/>
            <fileset dir="../minilib" includes="**/build.xml"/>
        </ivy:buildlist>
        <echo level="verbose" message="build.lib.ants=${toString:build.lib.ants}" />
    </target>

    <target name="install-all" depends="resolve-dependencies" description="Builds all dependent projects and installs their artifacts into the local Ivy repo.">
        <!-- executes the build script on each dependent module -->
        <subant buildpathref="build.lib.ants">
            <target name="package" />
            <target name="install" />
        </subant>
    </target>

    <target name="deploy-all" depends="resolve-dependencies, clean-site, install-all" description="Deploys all dependent projects.">
        <!-- executes the build script on each dependent module -->
        <subant buildpathref="build.lib.ants">
            <target name="deploy" />
        </subant>
    </target>

    <target name="clean-all" depends="resolve-dependencies" description="Builds all dependent projects and installs their artifacts into the local Ivy repo.">
        <!-- executes the build script on each dependent module -->
        <subant buildpathref="build.lib.ants">
            <target name="clean" />
        </subant>
    </target>

</project>
