<project name="CIVL" basedir="." default="compile" xmlns:jacoco="antlib:org.jacoco.ant">

	<import file="common.xml" />

	<!-- Create VERSION file from data in Version.java -->

	<property name="version.java" location="${mc.src.dir}/dev/civl/mc/config/IF/Version.java" />
	<property name="version.file" location="${root.dir}/VERSION" />

	<target name="version-check">
		<uptodate property="version.is.current" targetfile="${version.file}" srcfile="${version.java}" />
	</target>

	<target name="version" depends="version-check" unless="version.is.current">
		<echo message="Generating VERSION." />
		<loadfile property="extracted.version" srcFile="${version.java}">
			<filterchain>
				<linecontainsregexp>
					<regexp pattern="version\s*=" />
				</linecontainsregexp>
				<tokenfilter>
					<replaceregex pattern=".*version\s*=\s*&quot;([^&quot;]+)&quot;.*" replace="\1" />
				</tokenfilter>
				<striplinebreaks />
			</filterchain>
		</loadfile>
		<loadfile property="extracted.date" srcFile="${version.java}">
			<filterchain>
				<linecontainsregexp>
					<regexp pattern="date\s*=" />
				</linecontainsregexp>
				<tokenfilter>
					<replaceregex pattern=".*date\s*=\s*&quot;([^&quot;]+)&quot;.*" replace="\1" />
				</tokenfilter>
				<striplinebreaks />
			</filterchain>
		</loadfile>
		<echo file="${root.dir}/VERSION" message="${extracted.version} ${extracted.date}&#10;" />
	</target>

	<!-- Compile -->
	<target name="compile" depends="version">
		<ant dir="${mc.dir}" target="compile" inheritAll="false" />
	</target>

	<!-- Test -->
	<target name="test">
		<ant dir="${sarl.dir}" target="test" inheritAll="false" />
		<ant dir="${abc.dir}" target="test" inheritAll="false" />
		<ant dir="${gmc.dir}" target="test" inheritAll="false" />
		<ant dir="${mc.dir}" target="test" inheritAll="false" />
	</target>

	<!-- Javadocs -->
	<target name="javadoc">
		<ant dir="${sarl.dir}" target="javadoc" inheritAll="false" />
		<ant dir="${abc.dir}" target="javadoc" inheritAll="false" />
		<ant dir="${gmc.dir}" target="javadoc" inheritAll="false" />
		<ant dir="${mc.dir}" target="javadoc" inheritAll="false" />
	</target>

	<!-- Clean -->
	<target name="clean" depends="clean-default">
		<ant dir="${sarl.dir}" target="clean" inheritAll="false" />
		<ant dir="${abc.dir}" target="clean" inheritAll="false" />
		<ant dir="${gmc.dir}" target="clean" inheritAll="false" />
		<ant dir="${mc.dir}" target="clean" inheritAll="false" />
		<delete dir="${lib.dir}" />
		<delete dir="${app.dir}" />
	</target>


	<!-- Single non-modular JAR creation -->

	<!-- everything in one JAR, including all dependencies -->

	<target name="check.uptodate.complete.jar">
		<uptodate property="uptodate.complete.jar" targetfile="${civl.complete.jar}">
			<srcfiles dir="${sarl.bin.dir}" />
			<srcfiles dir="${gmc.bin.dir}" />
			<srcfiles dir="${abc.bin.dir}" />
			<srcfiles dir="${mc.bin.dir}" />
			<srcfiles file="${antlr3.runtime.jar}" />
			<srcfiles file="${antlr4.runtime.jar}" />
		</uptodate>
	</target>

	<target name="civl.complete.jar" depends="compile,check.uptodate.complete.jar" unless="uptodate.complete.jar" description="Create single non-modular JAR">
		<jar destfile="${civl.complete.jar}">
			<fileset dir="${root.dir}" includes="include/" />
			<fileset dir="${sarl.bin.dir}" excludes="module-info.class" />
			<fileset dir="${gmc.bin.dir}" excludes="module-info.class" />
			<fileset dir="${abc.bin.dir}" excludes="module-info.class" />
			<fileset dir="${mc.bin.dir}" excludes="module-info.class" />
			<zipfileset includes="**/*.class" excludes="module-info.class" src="${antlr3.runtime.jar}" />
			<zipfileset includes="**/*.class" excludes="module-info.class" src="${antlr4.runtime.jar}" />
			<manifest>
				<attribute name="Build-By" value="${user.name}" />
				<attribute name="Main-Class" value="${main.class}" />
			</manifest>
		</jar>
	</target>


	<!-- Modular JAR creation -->

	<!-- These modular jars contain only the code in the module, not
       code used by that code. So, e.g., ANTLR runtime code is not
       included. -->

	<target name="jar.sarl" depends="compile">
		<mkdir dir="${lib.dir}" />
		<jar destfile="${lib.dir}/${mod.sarl}.jar" basedir="${sarl.bin.dir}" />
	</target>

	<target name="jar.gmc" depends="compile">
		<mkdir dir="${lib.dir}" />
		<jar destfile="${lib.dir}/${mod.gmc}.jar" basedir="${gmc.bin.dir}" />
	</target>

	<target name="jar.abc" depends="compile">
		<mkdir dir="${lib.dir}" />
		<jar destfile="${lib.dir}/${mod.abc}.jar" basedir="${abc.bin.dir}" />
	</target>

	<target name="jar.mc" depends="compile">
		<mkdir dir="${lib.dir}" />
		<jar destfile="${lib.dir}/${mod.mc}.jar" basedir="${mc.bin.dir}" />
	</target>

	<target name="lib" depends="jar.sarl,jar.gmc,jar.abc,jar.mc">
		<copy file="${antlr3.runtime.mod.jar.path}" todir="${lib.dir}" />
		<copy file="${antlr4.runtime.mod.jar.path}" todir="${lib.dir}" />
	</target>

	<!-- Stand-alone app with custom JVM -->

	<!-- for reasons I don't understand, the app doesn't get updated
        even after it and the lib are deleted and ant app is re-run.
        Where is it getting the old code from? Is it cached somewhere?
   -->

	<target name="app" depends="lib">
		<link destdir="${app.dir}" modulepath="${lib.dir}" launchers="civl=${mod.mc}/${main.class}">
			<module name="${mod.sarl}" />
			<module name="${mod.gmc}" />
			<module name="${mod.abc}" />
			<module name="${mod.mc}" />
			<module name="${mod.antlr3}" />
			<module name="${mod.antlr4}" />
			<module name="java.base" />
		</link>
	</target>

	<target name="run" depends="app" description="Run the civl app">
		<exec executable="${app.dir}/bin/civl" />
	</target>

	<target name="all" depends="civl.complete.jar,app,test,javadoc" />

</project>
