<?xml version="1.0" encoding="UTF-8"?>
<project name="FBReaderJ" default="help">
	<loadproperties srcFile="local.properties" />
	<property file="ant.properties" />
	<loadproperties srcFile="project.properties" />

	<!-- quick check on sdk.dir -->
	<fail
		message="sdk.dir is missing. Make sure to generate local.properties using 'android update project'"
		unless="sdk.dir"
	/>

	<target name="init" description="Initialization...">
		<fail message="Ant 1.7.0 or higher is required. Please upgrade your ant.">
			<condition>
				<not>
					<antversion property="ant.version" atleast="1.7.0"/>
				</not>
			</condition>
		</fail>
	</target>

	<target name="native" unless="native.libraries.are.uptodate">
		<fail message="Native libraries are not up-to-date. Please run ndk-build manually.">
			<condition>
				<os family="windows" />
			</condition>
		</fail>
		<echo message="Building native libraries..."/>
		<exec executable="${ndk.dir}/ndk-build" />
		<echo message="DONE (Building native libraries)"/>
	</target>

	<target name="check.native.libraries" depends="init">
		<echo message="Checking if native libraries are up-to-date..."/>
		<condition property="native.libraries.are.uptodate" value="true">
			<and>
				<!-- Check if libDeflatingDecompressor exists and is up to date -->
				<available file="./libs/armeabi/libDeflatingDecompressor-v3.so" />
				<uptodate targetfile="libs/armeabi/libDeflatingDecompressor-v3.so">
					<srcfiles dir="jni/DeflatingDecompressor" includes="**/*.cpp"/>
				</uptodate>
				<!-- Check if libLineBreak exists and is up to date -->
				<available file="./libs/armeabi/libLineBreak-v2.so" />
				<uptodate property="libLineBreak-build-is-not-required" targetfile="libs/armeabi/libLineBreak-v2.so">
					<srcfiles dir="jni/LineBreak" includes="**/*.cpp,**/*.c,**/*.h"/>
				</uptodate>
				<!-- Check if libNativeFormats exists and is up to date -->
				<available file="./libs/armeabi/libNativeFormats-v4.so" />
				<uptodate property="libNativeFormats-build-is-not-required" targetfile="libs/armeabi/libNativeFormats-v4.so">
					<srcfiles dir="jni/NativeFormats" includes="**/*.cpp,**/*.c,**/*.h"/>
				</uptodate>
			</and>
		</condition>
		<echo message="DONE (Checking if native libraries are up-to-date)"/>
	</target>

	<target name="genhelp">
		<exec executable="./help/generate.py">
			<arg value="help/proto"/>
			<arg value="help/html"/>
			<arg value="assets/data/intro"/>
		</exec>
	</target>

	<target name="-pre-build" depends="init, native, genhelp">
	</target>
<!--
	<target name="-pre-compile">
	</target>

	<target name="-post-compile">
	</target>
-->
	<target name="clean">
		<echo message="Deleting temporary files..." />
		<delete dir="gen" />
		<delete dir="bin" />
		<delete dir="out" />
		<delete dir="assets/data/intro" />
		<echo message="DONE (Deleting temporary files)" />
	</target>

	<target name="distclean" depends="clean">
		<echo message="Deleting native libraries..." />
		<delete dir="obj" />
		<delete dir="libs/armeabi" />
		<delete dir="libs/armeabi-v7a" />
		<delete dir="libs/arm64-v8a" />
		<delete dir="libs/x86_64" />
		<delete dir="libs/x86" />
		<delete dir="libs/mips" />
		<delete dir="libs/mips64" />
		<delete dir="libs/mips-r2" />
		<delete dir="libs/mips-r2-sf" />
		<echo message="DONE (Deleting native libraries)" />
	</target>

	<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
