<project default="build_jar" name="ZeroTierOneSDK" basedir=".">
    <property environment="env"/>

    <condition property="isWindows">
      <os family="windows"/>
    </condition>

    <condition property="isMac">
      <os family="mac"/>
    </condition>

    <target name="clean_ant">
        <delete dir="bin" failonerror="false"/>
        <delete dir="classes" failonerror="false"/>
        <delete dir="build_win32" failonerror="false"/>
        <delete dir="build_win64" failonerror="false"/>
        <delete dir="mac32_64" failonerror="false"/>
        <delete dir="libs" failonerror="false"/>
        <delete dir="obj" failonerror="false"/>
    </target>

    <target name="build_java">
      <echo message="os.name          = ${os.name}"/>
        <echo message="os.arch          = ${os.arch}"/>
        <echo message="ant.java.version = ${ant.java.version}"/>
        <echo message="java.version     = ${java.version}"/>
        <echo message="ndk.loc          = ${env.NDK_BUILD_LOC}"/>
        <echo message="sdk.loc          = ${env.ANDROID_PLATFORM}"/>
        <echo message="user.dir         = ${user.dir}"/>
        <echo message="zt1.dir          = ${env.ZT}"/>
        <mkdir dir="bin"/>
        <mkdir dir="classes"/>
        <javac srcdir="src"
               destdir="classes"
               source="1.7"
               target="1.7"
               classpath="${env.ANDROID_PLATFORM}/android.jar"
               includeantruntime="false"/>
    </target>

    <target name="build_android">
        <exec dir="jni" executable="${env.NDK_BUILD_LOC}" failonerror="true">
            <arg value="ZT1=${env.ZT}"/>
            <arg value="V=1"/>
            <!-- <arg value="NDK_DEBUG=1"/> -->
        </exec>
        <copy file="libs/arm64-v8a/libZeroTierOneJNI.so"
              tofile="classes/lib/arm64-v8a/libZeroTierOneJNI.so"
              overwrite="true"/>
        <copy file="libs/armeabi/libZeroTierOneJNI.so" 
              tofile="classes/lib/armeabi/libZeroTierOneJNI.so"
              overwrite="true"/>
        <copy file="libs/armeabi-v7a/libZeroTierOneJNI.so"
              tofile="classes/lib/armeabi-v7a/libZeroTierOneJNI.so"
              overwrite="true"/>
        <copy file="libs/mips/libZeroTierOneJNI.so"
              tofile="classes/lib/mips/libZeroTierOneJNI.so"
              overwrite="true"/>
        <copy file="libs/mips64/libZeroTierOneJNI.so"
              tofile="classes/lib/mips64/libZeroTierOne.so"
              overwrite="true"/>
        <copy file="libs/x86/libZeroTierOneJNI.so"
              tofile="classes/lib/x86/libZeroTierOneJNI.so"
              overwrite="true"/>
        <copy file="libs/x86_64/libZeroTierOneJNI.so"
              tofile="classes/lib/x86_64/libZeroTierOneJNI.so"
              overwrite="true"/>
    </target>


    <target name="windows" if="isWindows">
      <mkdir dir="build_win32"/>
      <exec dir="build_win32/" executable="cmake" failonerror="true">
        <arg line=".. -G&quot;Visual Studio 11 2012&quot; -DCMAKE_BUILD_TYPE=Release"/>
      </exec>
      <exec dir="build_win32/" executable="cmake" failonerror="true">
        <arg line="--build . --config Release"/>
      </exec>
      <copy file="build_win32/Release/ZeroTierOneJNI.dll"
            tofile="classes/lib/ZeroTierOneJNI_win32.dll"
            overwrite="true"/>

      <mkdir dir="build_win64"/>
      <exec dir="build_win64/" executable="cmake" failonerror="true">
        <arg line=".. -G&quot;Visual Studio 11 2012 Win64&quot; -DCMAKE_BUILD_TYPE=Release"/>
      </exec>
      <exec dir="build_win64/" executable="cmake" failonerror="true">
        <arg line="--build . --config Release"/>
      </exec>
      <copy file="build_win64/Release/ZeroTierOneJNI.dll"
            tofile="classes/lib/ZeroTierOneJNI_win64.dll"
            overwrite="true"/>
    </target>

    <target name="mac" if="isMac">
      <mkdir dir="mac32_64"/>
      <exec dir="mac32_64/" executable="cmake" failonerror="true">
        <arg line=".. -DCMAKE_BUILD_TYPE=Release"/>
      </exec>
      <exec dir="mac32_64/" executable="cmake" failonerror="true">
        <arg line="--build . --config Release"/>
      </exec>
      <copy file="mac32_64/libZeroTierOneJNI.jnilib"
            tofile="classes/lib/libZeroTierOneJNI.jnilib"
            overwrite="true"/>
    </target>

    <target name="build_jar" depends="build_java,build_android,windows,mac">
        <jar destfile="bin/ZeroTierOneSDK.jar" basedir="classes"/>
    </target>

    <target name="docs">
      <echo message="Generating Javadocs"/>
      <mkdir dir="doc/"/>
      <javadoc sourcepath="src/"
               destdir="doc/"/>
    </target>
</project>