<!-- Build file for Apache Ant (think Makefile) -->
<project name="AddCI" default="run" basedir=".">
	<description>CTL4j example application.</description>

	<!-- Whether or not to put debug symbols into .class files -->
	<property name="debug" value="true"/>

	<!-- This code looks for JARs in ./lib/ and adds them to the CLASSPATH -->
	<exec executable="find" outputproperty="tmp-jars">
		<arg value="lib/"/>
		<arg value="-name"/>
		<arg value="*.jar"/>
		<arg value="-print0"/>
	</exec>
	<exec executable="sed" outputproperty="jars" inputstring="${tmp-jars}">
		<arg value="s/jar/jar${path.separator}/g"/>
	</exec>

	<!-- Setting up the CLASSPATH -->
	<property environment="env"/>
	<property name="classpath" value="${env.CLASSPATH}${path.separator}${env.HOME}/projects/wire/ctl4j/build${path.separator}common/build${path.separator}${jars}${path.separator}client/build${path.separator}service/build"/>

	<!-- This task runs the example client -->
	<target name="run" depends="compile" description="Run example.">
		<java classname="Client" classpath="${classpath}" fork="true">
			<jvmarg value="-Dfile.encoding=ISO-8859-1"/>
		</java>
		<!-- <java classname="ReflWrap.ByteCode" classpath="${classpath}" fork="true"/> -->
	</target>

	<!-- This is the task which compiles everything -->
	<target name="compile" depends="init" description="Compile.">
		<!-- Common interface, standard Ant build rule -->
		<depend srcdir="common/src" destdir="common/build"
			cache="common/depcache" closure="yes"/>
		<javac srcdir="common/src" destdir="common/build" nowarn="true"
			debug="${debug}" classpath="${classpath}"/>


		<!-- Compiling the service -->

		<depend srcdir="service/src" destdir="service/build"
			cache="service/depcache" closure="yes"/>
		<javac srcdir="service/src" destdir="service/build" nowarn="true"
			debug="${debug}" classpath="${classpath}"/>
		<!-- Generate code to export the implementation -->
		<java classname="CodeGen.Main" classpath="${classpath}" fork="true">
			<arg value="-oservice"/>
			<arg value="Impl.AddImpl"/>
		</java>
		<!-- Compile again for the generated code -->
		<javac srcdir="service/src" destdir="service/build" nowarn="true"
			debug="${debug}" classpath="${classpath}"/>


		<!-- Compiling the client -->

		<!-- Generate code for using the interface 

			The -o option specifies an output directory for the generated code. 
			The actual classes will be put into $DIR/javaSys or $DIR/javaSys/src
			if there exists a src/ subdirectory. If not given, the output 
			directory defaults to the CWD.

			The -i option specifies an optional implementation of the Java
			interface, which will be used if the 'lib' transport was selected
			by the user. This implementation class needs to be available in 
			the CLASSPATH at compile time and runtime. The result is undefined
			if you specify a class which does not implement the given
			interface.
		-->
		<java classname="CodeGen.Main" classpath="${classpath}" fork="true">
			<arg value="-oclient"/>
			<arg value="-iImpl.AddImpl"/>
			<arg value="common.Add" />
		</java>
		<!-- Compile the code -->
		<depend srcdir="client/src" destdir="client/bild"
			cache="client/depcache" closure="yes"/>
		<javac srcdir="client/src" destdir="client/build" nowarn="true"
			debug="${debug}" classpath="${classpath}"/>
	</target>

	<!-- This creates some directories for compiled code and dependencies -->
	<target name="init" description="Initialize.">
		<tstamp/>
		<mkdir dir="common/build"/>
		<mkdir dir="common/depcache"/>
		<mkdir dir="service/build"/>
		<mkdir dir="service/depcache"/>
		<mkdir dir="client/build"/>
		<mkdir dir="client/depcache"/>
	</target>

	<!-- This deletes the directories and the generated code -->
	<target name="clean" description="Cleanup.">
		<delete dir="common/build" quiet="true"/>
		<delete dir="common/depcache" quiet="true"/>
		<delete dir="service/build" quiet="true"/>
		<delete dir="service/depcache" quiet="true"/>
		<delete dir="client/build" quiet="true"/>
		<delete dir="client/depcache" quiet="true"/>

		<delete file="client/src/javaSys/AddRI.java"/>
		<delete file="client/src/javaSys/AddDebug.java"/>
		<delete file="client/src/javaSys/AddCI.java"/>
		<delete file="client/src/javaSys/AddLocal.java"/>

		<delete file="service/src/javaSys/AddImplRI.java"/>
		<delete file="service/src/javaSys/AddImplDebug.java"/>
		<delete file="service/src/javaSys/AddImplCI.java"/>
		<delete file="service/src/javaSys/AddImplLocal.java"/>
	</target>
</project>
