<?xml version="1.0"?>

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

	@author: Robin Vobruba <hoijui.quaero@gmail.com>
-->
<project name="JavaSkirmishAI" basedir="../" default="pack">
	<!--
		Targets:
		* compile:       compile source files
		* doc:           generate javadoc
		* pack,archive:  archive classes and sources into jar files
		* dist,install:  install these two jar files and the data dir
		* bump-version:  changes the version string in VERSION
		                 and data/AIInfo.lua
		* create-independent:
			Copies this project to a specified location and gives it a new name.
			It is assured that the new project dir then contains everything
			needed to compile the AI.
	-->

	<!-- These can not be changed from within "bin/ant.properties". -->
	<property name="src" location="src"/>
	<property name="bin.dir" location="bin"/>
	<property name="jar.file.name"      value="SkirmishAI"/>
	<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="independent.interface.libs.dir"  value="Java-AIInterface"/>
	<property name="independent.wrapper.oo.libs.dir" value="JavaOO-AIWrapper"/>

	<!-- If it is true, we are in an independent project. -->
	<condition property="independent">
		<and>
			<available file="${independent.interface.libs.dir}"/>
			<available file="${independent.wrapper.oo.libs.dir}"/>
		</and>
	</condition>
	<condition property="proj.dep.type"
			value="independent"
			else="dependent">
		<istrue value="${independent}"/>
	</condition>


	<path id="source.path.id">
		<dirset dir="${src}">
			<include name="**"/>
		</dirset>
	</path>
	<path id="class.path.base">
		<fileset dir="data">
			<include name="jlib/**/*.jar"/>
		</fileset>
		<dirset dir="data">
			<include name="jlib"/>
		</dirset>
	</path>


	<target name="init-dependent" unless="independent">

		<property file="bin/ant.${proj.dep.type}.properties"/>
		<!--
			The following values may be overriden in either
			"bin/ant.dependent.properties" or any other properties file
			which may be specified to ant on the command line like this:
			ant -propertyfile someFile.properties
		-->
		<!-- read content of file VERSION into the property -->
		<loadfile property="skirmish.ai.version"  srcFile="VERSION"/>
		<!-- use the projects root dir name as project name -->
		<basename property="skirmish.ai.name"     file="."/>
		<property name="spring.home"              location="../../.."/>
		<property name="build.home"               location="${spring.home}"/>
		<property name="build.dir"                location="${build.home}/AI/Skirmish/${skirmish.ai.name}"/>
		<property name="build.classes.dir"        location="${build.dir}/classes"/>
		<property name="dist.home"                location="${spring.home}/dist"/>
		<property name="dist.dir"                 location="${dist.home}/AI/Skirmish/${skirmish.ai.name}/${skirmish.ai.version}"/>
		<property name="doc.dir"                  location="${dist.home}/AI/Skirmish/${skirmish.ai.name}/${skirmish.ai.version}/doc/jdoc"/>
		<property name="ai.interface.src.home"    location="${spring.home}/AI/Interfaces/Java"/>
		<property name="ai.interface.build.home"  location="${build.home}/AI/Interfaces/Java"/>
		<property name="ai.wrapper.oo.src.home"   location="${spring.home}/AI/Wrappers/JavaOO"/>
		<property name="ai.wrapper.oo.build.home" location="${build.home}/AI/Wrappers/JavaOO"/>

		<available file="${ai.interface.build.home}/AIInterface.jar" property="ai.interface.jar.exists"/>
		<fail message="File not found: ${ai.interface.build.home}/AIInterface.jar" unless="ai.interface.jar.exists">.
File not found: ${ai.interface.build.home}/AIInterface.jar
Please make sure to compile the Java AI Interface
before trying to compile this AI, or request
an independent version of this project from
wherever you got this AI.
		</fail>

		<available file="${ai.wrapper.oo.build.home}/JavaOO-AIWrapper.jar" property="ai.wrapper.oo.jar.exists"/>
		<fail message="File not found: ${ai.wrapper.oo.build.home}/JavaOO-AIWrapper.jar" unless="ai.wrapper.oo.jar.exists">.
File not found: ${ai.wrapper.oo.build.home}/JavaOO-AIWrapper.jar
Please make sure to compile the Java OO AI Wrapper
before trying to compile this AI, or request
an independent version of this project from
wherever you got this AI from.
		</fail>

		<path id="class.path.id">
			<!-- AI classpath -->
			<path refid="class.path.base"/>

			<!-- OO AI Wrapper classpath -->
			<fileset dir="${ai.wrapper.oo.src.home}">
				<include name="jlib/**/*.jar"/>
			</fileset>
			<fileset dir="${ai.wrapper.oo.build.home}">
				<include name="JavaOO-AIWrapper.jar"/>
			</fileset>

			<!-- AI Interface classpath -->
			<fileset dir="${ai.interface.src.home}/data">
				<include name="jlib/**/*.jar"/>
			</fileset>
			<fileset dir="${ai.interface.build.home}">
				<include name="AIInterface.jar"/>
			</fileset>
		</path>
	</target>


	<target name="init-independent" if="independent">

		<property file="bin/ant.${proj.dep.type}.properties"/>
		<!--
			The following values may be overriden in either
			"bin/ant.independent.properties" or any other properties file
			which may be specified to ant on the command line like this:
			ant -propertyfile someFile.properties
		-->
		<!-- read content of file VERSION into the property -->
		<loadfile property="skirmish.ai.version"  srcFile="VERSION"/>
		<!-- use the projects root dir name as project name -->
		<basename property="skirmish.ai.name"     file="."/>
		<property name="build.home"               location="build"/>
		<property name="build.dir"                location="${build.home}"/>
		<property name="build.classes.dir"        location="${build.dir}/classes"/>
		<property name="dist.home"                location="dist"/>
		<property name="dist.dir"                 location="${dist.home}/AI/Skirmish/${skirmish.ai.name}/${skirmish.ai.version}"/>
		<property name="ai.wrapper.oo.src.home"   location="${independent.wrapper.oo.libs.dir}"/>
		<property name="ai.wrapper.oo.build.home" location="${independent.wrapper.oo.libs.dir}"/>

		<path id="class.path.id">
			<!-- AI classpath -->
			<path refid="class.path.base"/>

			<!-- OO AI Wrapper classpath -->
			<fileset dir="${independent.wrapper.oo.libs.dir}">
				<include name="**/*.jar"/>
			</fileset>

			<!-- AI Interface classpath -->
			<fileset dir="${independent.interface.libs.dir}">
				<include name="**/*.jar"/>
			</fileset>
		</path>
	</target>


	<target name="init" depends="init-independent,init-dependent">
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${build.classes.dir}"/>
	</target>


	<target name="bump-version" depends="init">

		<fail message="Please specify the version to bump to, for example: ant bump-version -Dversion.new=0.5">
			<condition>
				<not><isset property="version.new"/></not>
			</condition>
		</fail>

		<property name="skirmish.ai.version.new" value="${version.new}"/>

		<!--
			Set the new version in the files:
			* VERSION
			* data/AIInfo.lua
		-->
		<echo file="VERSION" message="${skirmish.ai.version.new}"/>
		<replace file="data/AIInfo.lua"
				token="'${skirmish.ai.version}', -- AI version"
				value="'${skirmish.ai.version.new}', -- AI version"/>

		<echo level="info">Successfully bumped ${skirmish.ai.name} version from ${skirmish.ai.version} to ${skirmish.ai.version.new}.</echo>
	</target>


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


	<target name="doc" depends="init">
		<mkdir dir="${doc.dir}"/>
		<javadoc
				destdir="${doc.dir}"
				sourcepathref="source.path.id"
				classpathref="class.path.id"
				Windowtitle="${skirmish.ai.name} ${skirmish.ai.version} javadoc"/>
	</target>


	<target name="pack-bin" depends="compile">
		<jar destfile="${build.dir}/${jar.file.name.bin}">
			<fileset dir="${build.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}">
				<include name="**/*.java"/>
			</fileset>
		</jar>
	</target>

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


	<target name="dist" depends="pack"
				description="install the built jars and the data files" >
		<mkdir dir="${dist.dir}/jlib"/>

		<!-- install the AI -->
		<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>

		<!-- install OO AI Wrapper -->
		<copy todir="${dist.dir}/jlib" overwrite="true">
			<fileset dir="${ai.wrapper.oo.src.home}/jlib">
				<include name="**/*.jar"/>
			</fileset>
			<fileset dir="${ai.wrapper.oo.build.home}">
				<include name="JavaOO-AIWrapper.jar"/>
			</fileset>
		</copy>
	</target>
	<target name="install" depends="dist"/>


	<target name="clean" depends="init">
		<delete dir="${build.dir}"/>
	</target>


	<target name="create-independent" depends="init">

		<fail>.
Please specify either home.new or name.new, for example:
	ant create-independent -Dhome.new=~/projects/SpringSkirmishAIs/
or
	ant create-independent -Dname.new=MyJavaAI
			<condition>
				<and>
					<not><isset property="home.new"/></not>
					<not><isset property="name.new"/></not>
				</and>
			</condition>
		</fail>

		<!-- use the current project name, if no new one was specified -->
		<condition property="skirmish.ai.name.new"
				value="${name.new}"
				else="${skirmish.ai.name}">
			<isset property="name.new"/>
		</condition>

		<!-- use the current project home, if no new one was specified -->
		<dirname property="home.current" file="."/>
		<condition property="skirmish.ai.home.new"
				value="${home.new}"
				else="${home.current}">
			<isset property="home.new"/>
		</condition>

		<property name="project.dir.new"
				location="${skirmish.ai.home.new}/${skirmish.ai.name.new}"/>

		<!-- copy the whole project as it is -->
		<copy todir="${project.dir.new}" overwrite="false">
			<fileset dir="."/>
		</copy>

		<antcall target="create-independent-addons-independent"/>

		<!-- set the new project name in AIInfo.lua -->
		<replace file="${project.dir.new}/data/AIInfo.lua"
				token="'${skirmish.ai.name}'"
				value="'${skirmish.ai.name.new}'"/>
		
		<echo level="info">
Spring source independant Java Skirmish AI project copied to:
	${project.dir.new}

You may want to change your java source package structure
and the data/AIInto.lua file for the new project.
		</echo>

	</target>

	<target name="create-independent-addons-independent" unless="independent">

		<!-- copy the interface jars -->
		<copy todir="${project.dir.new}/${independent.interface.libs.dir}" overwrite="false">
			<fileset dir="${ai.interface.src.home}/data">
				<include name="jlib/**/*.jar"/>
			</fileset>
			<fileset dir="${ai.interface.build.home}"
					includes="AIInterface*.jar"/>
		</copy>

		<!-- copy the OO wrapper jars -->
		<copy todir="${project.dir.new}/${independent.wrapper.oo.libs.dir}" overwrite="false">
			<fileset dir="${ai.wrapper.oo.src.home}">
				<include name="jlib/**/*.jar"/>
			</fileset>
			<fileset dir="${ai.wrapper.oo.build.home}"
					includes="JavaOO-AIWrapper*.jar"/>
		</copy>

	</target>

</project>
