<?xml version="1.0" encoding="UTF-8"?>
<project name="keystoreNative" default="install" basedir=".">
	<property name="src" location="src"/> <!-- java source folder -->
	<property name="bin" location="bin"/> <!-- intermediate build products -->
	<property name="jars" location="jars"/> <!-- jar files -->
	<property name="lib" location="lib"/> <!-- local libraries linked against -->
	<property name="dist" location="dist"/> <!-- build product location -->
	<property name="resources" location="resources"/> <!-- location of general java resources -->
    <property name="compile.debug" value="true"/>
    <property name="native.target" value="JNILib"/>
    <property name="native.project" value="keystoreNative.xcodeproj"/>
    <property name="native.library" value="libkeystoreNative.jnilib"/>
    <property name="env.CONFIGURATION" value="Release"/>

    <fileset id="lib.jars" dir="${lib}">
        <include name="**/*.jar"/>
    </fileset>
	
    <path id="lib.path">
        <fileset refid="lib.jars"/>
    </path>

    <target name="init" description="Initialize environment">
		<!-- Get properties from environment -->
		<property environment="env"/>	
		<property name="curr_build_dir" location="build/${env.CONFIGURATION}"/>
		<property name="curr_header_dir" location="build/${env.CONFIGURATION}/Headers"/>
		
		<!-- if you tweak these names, you'll have to change the JNILib target and the Xcode launcher arguments -->
		<property name="jarfilename" value="keystoreNative"/>
		<property name="jarfile" location="${jars}/${jarfilename}.jar"/>

        <mkdir dir="${curr_build_dir}"/>
        <mkdir dir="${curr_header_dir}"/>
        <mkdir dir="${lib}"/>
        <mkdir dir="${bin}"/>
        <mkdir dir="${jars}"/>
        <mkdir dir="${dist}"/>		
    </target>

    <target name="compile" depends="init" description="Compile JNI Wrapper code">
        <javac srcdir="${src}" destdir="${bin}"
               source="1.8" target="1.8"
               includeAntRuntime="no"
               classpathref="lib.path" debug="${compile.debug}">
        </javac>			
    </target>

    <target name="jar" depends="compile" description="Build jar">
        <jar jarfile="${jarfile}" basedir="${bin}" manifest="${resources}/Manifest">
			<!-- Merge library jars into final jar file -->
			<zipgroupfileset refid="lib.jars"/>
		</jar>
		
		<!-- generate the header files -->
		<exec executable="/usr/bin/javah">
			<arg line="-classpath '${jarfile}' -force -d '${curr_header_dir}' keystoreNative"/>
		</exec>
    </target>

	<!-- Note: this target requires that Xcode Tools be installed -->
	<target name="nativelib" depends="jar">
		<exec executable="/usr/libexec/java_home" outputproperty="JAVA_HOME"/>
		<echo>JAVA_HOME is ${JAVA_HOME}"</echo>
		<exec executable="/usr/bin/xcodebuild">
			<arg line="-project ${native.project}"/>
			<arg line="-target ${native.target}"/>
			<arg line="JAVA_HOME=${JAVA_HOME}"/>
		</exec>		
        <copy file="${curr_build_dir}/${native.library}" toDir="${bin}" failonerror="true" verbose="true"/>
	</target>
	
    <!-- install target. Copy the built objects to the 'dist' directory. -->
    <target name="install" description="Install jar" depends="nativelib">
        <copy file="${jarfile}" toDir="${dist}" failonerror="true" verbose="true"/>
        <copy toDir="${dist}" failonerror="true" verbose="true">
			<fileset dir="${bin}">
				<include name="**/*.jnilib"/>
			</fileset>
		</copy>
    </target>

	<target name="run" depends="install">
		<java classpath="${jarfile}" classname="${ant.project.name}" fork="true">
			<sysproperty key="java.library.path" value="${dist}"/>
		</java>
	</target>
	
    <target name="clean" depends="init" description="Remove build and dist directories">
        <delete dir="${bin}"/>
        <delete dir="${dist}"/>
        <delete dir="${jars}"/>
		<delete dir="${curr_build_dir}"/>
    </target>
</project>
