<?xml version="1.0" encoding="UTF-8"?>
<project name="build openal-soft natives" default="all" basedir="../">
    <!-- load cpp compiler ant task -->
    <taskdef resource="cpptasks.tasks" classpath="lib/antlibs/cpptasks.jar"/>
    <!-- load properties -->
    <property file="nbproject/openal-soft-native.properties"/>

    <condition property="ndk-build-name" value="ndk-build.cmd" else="ndk-build">
        <os family="windows" />
    </condition>

    <fileset id="lib.jars" dir="${openal-soft.lib.dir}">
        <include name="**/*.jar"/>
    </fileset>
    <fileset id="lib.jme.jars" dir="${openal-soft.jme.dir}">
        <include name="**/*.jar"/>
    </fileset>

    <pathconvert property="lib.importpath">
        <fileset refid="lib.jars"/>
        <fileset refid="lib.jme.jars"/>
    </pathconvert>

    <target name="build-openal-soft-natives" description="builds the native openal-soft library for android" depends="create-native-headers, -create-folders, compile-android">
        <echo message="Updating native jME3-openal-soft-natives.jar"/>
        <zip basedir="${openal-soft.output.dir}" file="${openal-soft.output.base}/jME3-openal-soft-natives-android.jar" compress="true"/>
        <copy file="${openal-soft.output.base}/jME3-openal-soft-natives-android.jar" todir="dist/opt/native-openal-soft/"/>
    </target>

    <target name="create-native-headers" description="creates the native C++ headers for the java native methods">
        <javah destdir="${openal-soft.source.dir}" classpath="${openal-soft.build.dir}${path.separator}${lib.importpath}">
            <class name="com.jme3.audio.android.AndroidOpenALSoftAudioRenderer"/>
        </javah>
    </target>

    <target name="-create-folders" description="creates the needed folders">
        <mkdir dir="${openal-soft.build.dir}"/>
        <mkdir dir="${openal-soft.build.dir}/jni"/>
        <mkdir dir="${openal-soft.output.base}"/>
        <mkdir dir="${openal-soft.output.dir}"/>
    </target>

    <target name="compile-android" description="compiles libopenalsoftjme using the Android NDK" depends="-check-android-ndk" if="haveAndoidNdk">
        <!-- copy OpenAL Soft files into jni directory -->
        <echo>Copying OpenAL Soft source files to build directory from ${openal-soft.include}</echo>
        <copy todir="${openal-soft.build.dir}/jni" verbose="true" flatten="false" overwrite="true">
            <fileset dir="${openal-soft.include}">
                <include name="**/*.*"/>
<!--
                <include name="**/*.cpp"/>
                <include name="**/*.h"/>
                <include name="**/*.cl"/>
                <include name="**/*.c"/>
                <include name="**/*.inc"/>
                <exclude name="/android/*.*"/>
                <include name="**/*.*" />
-->
            </fileset>
        </copy>

        <!-- copy jME3 Native OpenAL Soft files into jni directory -->
<!--
        <echo>Copying jME3 JNI files for OpenAL Soft to build directory from ${openal-soft.source.dir}</echo>
        <copy todir="build/openal-soft-android/jni" verbose="true" flatten="false" overwrite="true">
            <fileset dir="${openal-soft.source.dir}">
                <include name="*.cpp" />
                <include name="*.h" />
                <include name="**/*.*" />
            </fileset>
        </copy>
-->

        <!-- copy Android.mk and Application.mk files into jni directory -->
        <echo>Copying Android Make and JNI files for OpenAL Soft to build directory from ${openal-soft.source.dir}</echo>
        <copy todir="${openal-soft.build.dir}/jni" verbose="true" flatten="false" overwrite="true">
            <fileset dir="${openal-soft.source.dir}">
                <include name="*.*" />
                <!--<include name="**/*.*"/>-->
            </fileset>
        </copy>
<!--
        <copy file="${openal-soft.source.dir}/Android.mk" todir="build/openal-soft-android/jni" verbose="true"/>
        <copy file="${openal-soft.source.dir}/Application.mk" todir="build/openal-soft-android/jni" verbose="true"/>
-->

        <exec executable="${ndk.dir}/${ndk-build-name}" failonerror="true" >
            <!--<arg line="NDK_DEBUG=1"/>-->
            <arg line="TARGET_PLATFORM=android-9"/>
            <arg line="-C ${openal-soft.build.dir}/"/>
        </exec>

        <!-- copy resulting library directories to jarcontent directory -->
        <copy todir="${openal-soft.output.dir}" verbose="true" flatten="false">
            <fileset dir="${openal-soft.build.dir}/libs">
                <include name="**/*.*"/>
                <!--exclude name="**/x86/*.*"/-->
            </fileset>
        </copy>

    </target>

    <target name="-check-android-ndk">
        <available file="${ndk.dir}/${ndk-build-name}" property="haveAndoidNdk"/>
    </target>

</project>