<?xml version="1.0"?>
<project name="FaceMovie build script" default="compile" basedir=".">

	<taskdef resource="pyAntTasks.properties"/>
	
	<!-- INIT : Initialize properties and classpath  -->
	<target name="init">
		
		<!-- Properties -->
		<property name="old.version" value="0.8"/>		
		<property name="version" value="0.8.2"/>		
		
		<property name="base.dir" value="."/>		
		<property name="src.dir" value="${base.dir}/facemovie"/>
		<property name="haar.dir" value="${base.dir}/haarcascades"/>
		<property name="test.dir" value="${base.dir}/test"/>
		<property name="doc.dir" location="${base.dir}/doc"/>
		<property name="pkg.dir" value="${base.dir}/packaging"/>
		<property name="samples.dir" value="data/inputs/samples"/>
		<!-- Specialized packaging folders -->
		<property name="old.pkg.dir" value="${pkg.dir}/old"/>
		<property name="curr.pkg.dir" value="${pkg.dir}/current"/>
		<property name="new.pkg.dir" value="${curr.pkg.dir}/${version}"/>
		
		<property name="pipy.pkg.dir" value="${base.dir}/dist/fm_pipy"/>
		<!-- Archive files -->
		<property name="haar.zip" value="${new.pkg.dir}/haarcascades.zip"/>
		<property name="samples.zip" value="${new.pkg.dir}/samples.zip"/>
		<property name="exe.zip" value="${new.pkg.dir}/Facemovie-${version}-exe.zip"/>
		
		<!-- Time -->
		<tstamp>
			<format property="TODAY" pattern="yyyy-MM-dd" />
		</tstamp>	
	</target>

	<!-- CLEAN : Removes all compiled stuff   -->
	<target name="clean" depends="init" description="Removes all compiled stuff">
		<echo message="Removes build folder"/>
		<delete dir="${base.dir}/build"/>
		<echo message="Removes dist folder"/>
		<delete dir="${base.dir}/dist"/>
		<echo message="Removes all compiled Python files"/>
		<delete>
		    <fileset dir="${base.dir}" includes="**/*.pyc"/>
		</delete>
		<echo message="Removes old doc"/>
		<delete includeEmptyDirs="true">
		    <fileset dir="${doc.dir}/build" includes="**/*"/>
		</delete>
		<delete file="${base.dir}/README.rst"/>
	</target>	
	
	<!-- COMPILE : Checks that all Python files compile before packaging -->
	<target name="compile" depends="init" >
		<py-compile dir="${src.dir}" pythonpath="${src.dir}" optimize="0"/>
		<py-compile dir="${test.dir}" pythonpath="${test.dir}" optimize="0"/>
	</target>
	
	<!-- DOC : Generates documentation for the project -->
	<target name="doc" depends="init,compile">
		<exec dir="${base.dir}" executable="sphinx-build">
			<arg line="${doc.dir}/source ${doc.dir}/build"/>
		</exec>
	</target>	
	
	<!-- EXE : Generates single Windows executable -->
	<target name="exe" depends="init">
		<exec dir="${base.dir}" executable="pandoc.exe">
					<arg line="${base.dir}/README.markdown -o ${base.dir}/README.rst"/>
		</exec>
		<py-run script="setup.py" pythonpath="${base.dir}" optimize="0">
			<arg value="build"/>
		</py-run>
	</target>
	
	<!-- PACKAGING  -->
	<!-- package.clean : Moves old packages into proper folder  -->
	<target name="package.clean" depends="init">
		<move todir="${old.pkg.dir}">
			<fileset dir="${curr.pkg.dir}">
		    	<include name="*/*"/>
		    </fileset>
		</move>
		<delete includeEmptyDirs="true">
			<fileset dir="${curr.pkg.dir}" includes="**/*"/>
		</delete>
	</target>	

	<!-- package.create : Creates the new output folder and saves static data in it  -->
	<target name="package.create" depends="init,clean,compile">
		<echo message="Creating output folder"/>
		<mkdir dir="${new.pkg.dir}"/>
		
		<echo message="Compressing HaarCascades"/>
		<delete file="${haar.zip}"/>
		<zip destfile="${haar.zip}" update="true">
			<zipfileset dir="${haar.dir}" prefix="haarcascades"  excludes="*.py"/>
		</zip>

		<echo message="Compressing Samples"/>
		<delete file="${samples.zip}"/>
		<zip destfile="${samples.zip}" update="true">
			<zipfileset dir="${samples.dir}" prefix="samples"  excludes="*.py"/>
		</zip>
		
		<echo message="Compressing Samples"/>
		<delete file="${samples.zip}"/>
		<zip destfile="${samples.zip}" update="true">
			<zipfileset dir="${samples.dir}" prefix="samples"  excludes="*.py"/>
		</zip>		
	</target>
	
	<!-- package.win : Creates the application package for Windows  -->
	<target name="package.win" depends="init,package.create,exe">
		<echo message="Creating Windows Executable archive"/>		
		<delete file="${exe.zip}"/>
		<zip destfile="${exe.zip}">
			<zipfileset dir="${base.dir}/dist" includes="Facemoviefier.exe"/>
			<zipfileset dir="${base.dir}" includes="README.markdown"/>
			<zipfileset dir="${haar.dir}" excludes="*.py" prefix="haarcascades"/>
		</zip>		
	</target>

	<!-- package.pipy : Creates the application package for Windows  -->
	<target name="package.pipy" depends="init,package.create">
		<echo message="Creating Windows Pipy archive"/>		
		<py-run script="setup.py" pythonpath="${base.dir}" optimize="0">
			<arg value="sdist"/>
		</py-run>
		<copy file="${base.dir}/dist/Facemovie-${version}.zip" todir="${new.pkg.dir}"/>
	</target>

	<!-- package.doc : Creates the source documentation package -->
	<target name="package.doc" depends="init,package.create,doc">
		<echo message="Creating Documentation archive"/>
		<zip destfile="${new.pkg.dir}/doc.zip"
		    basedir="${doc.dir}/build"
			update="true"
		/>
	</target>

	<!-- Whole packaging   -->
	<target name="package" depends="init,package.clean,package.create,package.win,package.pipy,package.doc" description="Performs all packaging">
		<echo message="Packaging all information"/>
	</target>

	<!-- Whole packaging   -->
	<target name="package_lin" depends="init,package.clean,package.create,package.doc" description="Performs all packaging">
		<echo message="Packaging all information"/>
	</target>

</project>
