<?xml version="1.0"?>

<!--
	Do not edit this file!
	But edit ant.properties instead.

	@author: Robin Vobruba <hoijui.quaero@gmail.com>
-->
<project name="Java" basedir="../" default="pack">
	<!--
		This file does:
			* generate-src:  generate java sources
			* compile:       compile java sources
			* doc:           generate javadoc
			* pack:          pack java classes and sources into jar files
			* dist:          install these two jar files
			* sdk:           create the Java AI SDK in a dir specified through a parameter
	-->

	<loadfile property="interface.version" srcFile="VERSION"/>
	<property name="interface.name" value="${ant.project.name}"/>


	<target name="init">

		<property file="bin/ant.properties"/>
		<!--
			The following values may be overriden in either
			"bin/ant.properties" or any other properties file
			which may be specified to ant on the command line like this:
			ant -propertyfile someFile.properties
		-->
		<property name="spring.home"            location="../../.."/>
		<property name="awk.common.scripts.dir" location="${spring.home}/AI/Wrappers/CUtils/bin"/>
		<property name="build.home"             location="${spring.home}/build"/>
		<property name="build.dir"              location="${build.home}/AI/Interfaces/${interface.name}"/>
		<property name="classes.dir"            location="${build.dir}/classes"/>
		<property name="dist.home"              location="${spring.home}/dist"/>
		<property name="dist.dir"               location="${dist.home}/AI/Interfaces/${interface.name}/${interface.version}"/>
		<property name="doc.dir"                location="${dist.home}/AI/Interfaces/${interface.name}/${interface.version}/doc/jdoc"/>
		<property name="src.generated"          location="${build.dir}/src-generated/main"/>
		<property name="src.generated.java"     location="${src.generated}/java"/>
		<property name="src.generated.native"   location="${src.generated}/native"/>

		<property name="jar.file.name"     value="AIInterface"/>
		<property name="jar.file.name.bin" value="${jar.file.name}.jar"/>
		<property name="jar.file.name.src" value="${jar.file.name}-src.jar"/>
		<property name="java.pkg.dir"      value="com/springrts/ai"/>

		<property name="spring.ai.interface.home" location="${spring.home}/rts/ExternalAI/Interface"/>
		<property name="src.java"                 location="src/main/java"/>
		<property name="bin.dir"                  location="bin"/>
		<path id="source.path.id">
			<pathelement path="${src.java}"/>
			<pathelement path="${src.generated.java}"/>
		</path>
		<path id="class.path.id">
			<fileset dir="data">
				<include name="jlib/**/*.jar"/>
			</fileset>
		</path>

		<mkdir dir="${build.dir}"/>
		<mkdir dir="${classes.dir}"/>

		<condition property="awk.bin" value="awk.exe" else="awk">
			<os family="windows" />
		</condition>
	</target>


	<target name="generate-init" depends="init">
		<mkdir dir="${src.generated}"/>
		<mkdir dir="${src.generated.native}"/>
		<mkdir dir="${src.generated.java}/${java.pkg.dir}"/>
	</target>

	<target name="generate-src-native" depends="generate-init">
		<exec dir="${bin.dir}" executable="${awk.bin}"
				failonerror="true" resolveexecutable="true" searchpath="true">
			<arg value="-v"/><arg value="SPRING_SOURCE_DIR=${spring.home}"/>
			<arg value="-v"/><arg value="INTERFACE_SOURCE_DIR=${src.java}"/>
			<arg value="-v"/><arg value="GENERATED_SOURCE_DIR=${src.generated}"/>
			<arg value="-v"/><arg value="NATIVE_GENERATED_SOURCE_DIR=${src.generated.native}"/>
			<arg value="-v"/><arg value="JAVA_GENERATED_SOURCE_DIR=${src.generated.java}"/>

			<arg value="-f"/><arg value="${awk.common.scripts.dir}/common.awk"/>
			<arg value="-f"/><arg value="${awk.common.scripts.dir}/commonDoc.awk"/>
			<arg value="-f"/><arg value="native_wrappCallback.awk"/>

			<arg value="${spring.ai.interface.home}/SSkirmishAICallback.h"/>
		</exec>

		<exec dir="${bin.dir}" executable="${awk.bin}"
				failonerror="true" resolveexecutable="true" searchpath="true">
			<arg value="-v"/><arg value="SPRING_SOURCE_DIR=${spring.home}"/>
			<arg value="-v"/><arg value="INTERFACE_SOURCE_DIR=${src.java}"/>
			<arg value="-v"/><arg value="GENERATED_SOURCE_DIR=${src.generated}"/>
			<arg value="-v"/><arg value="NATIVE_GENERATED_SOURCE_DIR=${src.generated.native}"/>
			<arg value="-v"/><arg value="JAVA_GENERATED_SOURCE_DIR=${src.generated.java}"/>

			<arg value="-f"/><arg value="${awk.common.scripts.dir}/common.awk"/>
			<arg value="-f"/><arg value="${awk.common.scripts.dir}/commonDoc.awk"/>

			<arg value="-f"/><arg value="native_wrappCommands.awk"/>

			<arg value="${spring.ai.interface.home}/AISCommands.h"/>
		</exec>
	</target>




	<target name="generate-src-callback" depends="generate-src-native">
		<exec dir="${bin.dir}" executable="${awk.bin}"
				failonerror="true" resolveexecutable="true" searchpath="true">
			<arg value="-v"/><arg value="SPRING_SOURCE_DIR=${spring.home}"/>
			<arg value="-v"/><arg value="INTERFACE_SOURCE_DIR=${src.java}"/>
			<arg value="-v"/><arg value="GENERATED_SOURCE_DIR=${src.generated}"/>
			<arg value="-v"/><arg value="NATIVE_GENERATED_SOURCE_DIR=${src.generated.native}"/>
			<arg value="-v"/><arg value="JAVA_GENERATED_SOURCE_DIR=${src.generated.java}"/>

			<arg value="-f"/><arg value="${awk.common.scripts.dir}/common.awk"/>
			<arg value="-f"/><arg value="${awk.common.scripts.dir}/commonDoc.awk"/>

			<arg value="-f"/><arg value="jni_wrappCallback.awk"/>

			<arg value="${src.generated.native}/CallbackFunctionPointerBridge.h"/>
		</exec>
	</target>




	<target name="generate-src-events" depends="generate-init">
		<exec dir="${bin.dir}" executable="${awk.bin}"
				failonerror="true" resolveexecutable="true" searchpath="true">
			<arg value="-v"/><arg value="SPRING_SOURCE_DIR=${spring.home}"/>
			<arg value="-v"/><arg value="INTERFACE_SOURCE_DIR=${src.java}"/>
			<arg value="-v"/><arg value="GENERATED_SOURCE_DIR=${src.generated}"/>
			<arg value="-v"/><arg value="NATIVE_GENERATED_SOURCE_DIR=${src.generated.native}"/>
			<arg value="-v"/><arg value="JAVA_GENERATED_SOURCE_DIR=${src.generated.java}"/>

			<arg value="-f"/><arg value="${awk.common.scripts.dir}/common.awk"/>
			<arg value="-f"/><arg value="${awk.common.scripts.dir}/commonDoc.awk"/>

			<arg value="-f"/><arg value="jni_wrappEvents.awk"/>

			<arg value="${spring.ai.interface.home}/AISEvents.h"/>
		</exec>
    </target>


	<target name="generate-src-commands" depends="generate-init">
		<exec dir="${bin.dir}" executable="${awk.bin}"
				failonerror="true" resolveexecutable="true" searchpath="true">
			<arg value="-v"/><arg value="SPRING_SOURCE_DIR=${spring.home}"/>
			<arg value="-v"/><arg value="INTERFACE_SOURCE_DIR=${src.java}"/>
			<arg value="-v"/><arg value="GENERATED_SOURCE_DIR=${src.generated}"/>
			<arg value="-v"/><arg value="NATIVE_GENERATED_SOURCE_DIR=${src.generated.native}"/>
			<arg value="-v"/><arg value="JAVA_GENERATED_SOURCE_DIR=${src.generated.java}"/>

			<arg value="-f"/><arg value="${awk.common.scripts.dir}/common.awk"/>
			<arg value="-f"/><arg value="${awk.common.scripts.dir}/commonDoc.awk"/>

			<arg value="-f"/><arg value="jni_wrappCommands.awk"/>

			<arg value="${spring.ai.interface.home}/AISCommands.h"/>
		</exec>
	</target>

	<target name="generate-src"
			depends="generate-src-events, generate-src-callback, generate-src-commands"/>


	<target name="compile" depends="generate-src">
		<javac destdir="${classes.dir}">
			<src refid="source.path.id"/>
			<classpath refid="class.path.id"/>
		</javac>
	</target>


	<target name="doc" depends="generate-src">
		<mkdir dir="${doc.dir}"/>
		<javadoc
				destdir="${doc.dir}"
				sourcepathref="source.path.id"
				classpathref="class.path.id"
				Windowtitle="Sprint RTS Java AI Interface javadoc"/>
	</target>


	<target name="pack-bin" depends="compile">
		<jar destfile="${build.dir}/${jar.file.name.bin}">
			<fileset dir="${classes.dir}">
				<include name="**/*.class"/>
			</fileset>
		</jar>
	</target>

	<target name="pack-src" depends="init">
		<jar destfile="${build.dir}/${jar.file.name.src}">
			<fileset dir="${src.java}">
				<include name="**/*.java"/>
			</fileset>
			<fileset dir="${src.generated.java}">
				<include name="**/*.java"/>
			</fileset>
		</jar>
	</target>

	<target name="pack" depends="pack-bin, pack-src"/>


	<target name="dist" depends="pack"
				description="generate the distribution (Java part only)" >
		<mkdir dir="${dist.dir}/jlib"/>
		<copy file="${build.dir}/${jar.file.name.bin}" todir="${dist.dir}" overwrite="true"/>
		<copy file="${build.dir}/${jar.file.name.src}" todir="${dist.dir}/jlib" overwrite="true"/>
		<copy todir="${dist.dir}" overwrite="true">
			<fileset dir="data"/>
		</copy>
	</target>

	<target name="clean" depends="init">
		<delete dir="${src.generated.java}"/>
		<delete dir="${src.generated.native}"/>
		<delete dir="${src.generated}"/>
		<delete dir="${classes.dir}"/>
		<delete dir="${build.dir}"/>
	</target>


	<target name="sdk-init">
		<fail message="No valid SDK root set. Please specify like this: -Dsdk.home=/tmp/mySpringJavaAISDK">
			<condition>
				<not>
					<isset property="sdk.home"/>
				</not>
			</condition>
		</fail>

		<property name="build.home" location="${sdk.home}/build"/>
		<property name="build.dir"  location="${build.home}"/>
		<property name="dist.home"  location="${sdk.home}"/>
		<property name="dist.dir"   location="${dist.home}"/>
		<property name="doc.dir"    location="${dist.home}/doc/jdoc"/>
		<property name="src.generated"        location="${build.dir}/src-generated/main"/>
		<property name="src.generated.java"   location="${src.generated}/java"/>
		<property name="src.generated.native" location="${src.generated}/native"/>
	</target>

	<target name="sdk" depends="sdk-init, init, dist, doc">
		<delete dir="${build.dir}" quiet="true"/>
	</target>

</project>
