<?xml version="1.0" encoding="utf-8" ?>
<project name="rk3-compile">

	<target name="compile">

		<!-- checkout greenstone3 -->
		<exec executable="svn">
			<arg value="checkout"/>
			<arg value="${svn.root}/main/${branch.path}/greenstone3"/>
			<arg value="compiled"/>
			<arg value="-r"/><arg value="${branch.revision}"/>
		</exec>

		<if><bool><equals arg1="${with.jni}" arg2="false"/></bool>
			<rsr file="${basedir}/compiled/build.properties.svn" pattern="with.jni=true" replacement="#with.jni=true"/>
		</if>

		<if><bool><equals arg1="${with.gli.and.gems}" arg2="false"/></bool>
			<rsr file="${basedir}/compiled/build.properties.svn" pattern="with.gli.and.gems=true" replacement="#with.gli.and.gems=true"/>
		</if>

		<!-- make sure all c++ compilation is done statically on linux -->
		<if><bool><equals arg1="${rk.os}" arg2="linux"/></bool>
			<property name="forward.compile.static" value="true"/>
		</if>

		<!-- make sure the c++ compilation can see the support library in the extension folder -->
		<if><bool><or><equals arg1="${rk.os}" arg2="mac"/><equals arg1="${rk.os}" arg2="linux"/></or></bool>
			<property name="forward.use.gnomelib.ext" value="true"/>
		</if>

		<antcall target="persist-forwarded-properties">
			<param name="greenstone3basedir" value="compiled"/>
		</antcall>

		<!-- run greenstone3's ant prepare -->
		<ant dir="compiled" inheritAll="false">
			<target name="prepare"/>
			<propertyset refid="forward.properties"/>
			<property name="properties.accepted" value="true"/>
		</ant>

		<!-- set all version numbers -->
		<antcall target="greenstone3-set-version-numbers"><param name="greenstone3basedir" value="compiled"/></antcall>
		<antcall target="gli-set-version-numbers"><param name="glibasedir" value="compiled/gli"/></antcall>
		<antcall target="gsdl-set-version-numbers"><param name="gsdl.basedir" value="compiled/gs2build"/></antcall>

		<!-- mac or linux: compile the support library -->
		<if><bool><or><equals arg1="${rk.os}" arg2="mac"/><equals arg1="${rk.os}" arg2="linux"/></or></bool>
			<!-- checkout the gnome support library extension-->

			<!-- For Lions, need a specific gnome-lib-minimal tar file. The file name contains darwin12 for Lion, it contains the os.shell name (darwin/linux) for other cases.
			Mac OS X v10.7.4 onwards is Lion/Mountain Lion, different from Leopard/Snow Leopard. See http://en.wikipedia.org/wiki/Darwin_(operating_system) -->
			<condition property="specific.os.type" value="${os.shell}-Lion" else="${os.shell}-ElCapitan">
				<and>
					<contains string="${os.shell}" substring="darwin" casesensitive="false"/>
					<matches pattern="^(10\.7\.[4-9]|10\.[8-9]|1[1-9]|[2-9])" string="${os.version}"/>
				</and>
			</condition>

			<if><bool><or>
				<equals arg1="${post.2.83.version}" arg2="true"/>
			</or></bool>
				<echo>EXPORTING GNOME LIB EXTENSION</echo>
				<exec executable="svn">
					<arg value="export"/>
					<arg value="${svn.gs2.extensions}/gnome-lib/gnome-lib-minimal-${specific.os.type}${arch}${extension-x64}.tar.gz"/>
					<arg value="${basedir}/compiled/ext/gnome-lib-${specific.os.type}.tar.gz"/>
				</exec>

				<exec executable="tar" dir="${basedir}/compiled/ext">
					<arg value="xvzf"/>
					<arg value="gnome-lib-${specific.os.type}.tar.gz"/>
				</exec>

				<exec executable="/bin/bash" dir="${basedir}/compiled/ext/gnome-lib-minimal">
					<arg value="-c"/>
					<arg value="source setup.bash_old &amp;&amp; source devel.bash"/>
				</exec>
				<echo>DONE COMPILING GNOME LIB EXTENSION</echo>
			</if>
		</if>

		<!-- <rsr file="${basedir}/compiled/build.xml" pattern="CFLAGS=&amp;quot;" replacement="CFLAGS=&amp;quot;-fPIC "/> -->

		<!-- run greenstone3's ant install and ant dist-tidy -->
		<ant dir="compiled" inheritAll="false">
			<target name="install"/>
			<target name="dist-tidy"/>
			<propertyset refid="forward.properties"/>
			<property name="properties.accepted" value="true"/>
			<property name="properties.keep.src" value="${keep.src}"/>
		</ant>

		<!-- Delete all collections apart from the Lucene demo collection -->
		<delete dir="${basedir}/compiled/web/sites/localsite/collect/gberg"/>
		<delete dir="${basedir}/compiled/web/sites/localsite/collect/gs2mgdemo"/>
		<delete dir="${basedir}/compiled/web/sites/localsite/collect/gs2mgppdemo"/>
		<delete dir="${basedir}/compiled/web/sites/localsite/collect/infomine"/>		

		<!-- Delete the entire gnome support library on Linux -->
		<if><bool><equals arg1="${rk.os}" arg2="linux"/></bool>
			<delete dir="${basedir}/compiled/ext/gnome-lib-minimal"/>
			<delete file="${basedir}/compiled/ext/gnome-lib-linux.tar.gz"/>
		</if>

		<!-- Delete all but the lib folder of the gnome support library on Mac, 
	         then move this smaller gnome-lib-minimal dir from the top-level ext folder 
	         to gs2build/ext, since wvware on mac needs gnome-lib-minimal -->
		<if><bool><equals arg1="${rk.os}" arg2="mac"/></bool>
			<delete dir="${basedir}/compiled/ext/gnome-lib-minimal/cascade-make"/>
			<delete dir="${basedir}/compiled/ext/gnome-lib-minimal/packages"/>
			<delete dir="${basedir}/compiled/ext/gnome-lib-minimal/darwin/bin"/>
			<delete dir="${basedir}/compiled/ext/gnome-lib-minimal/darwin/etc"/>
			<delete dir="${basedir}/compiled/ext/gnome-lib-minimal/darwin/include"/>
			<delete dir="${basedir}/compiled/ext/gnome-lib-minimal/darwin/share"/>
			<delete file="${basedir}/compiled/ext/gnome-lib-minimal/setup.bat"/>
			<delete file="${basedir}/compiled/ext/gnome-lib-minimal/CASCADE-MAKE.sh"/>
			<delete file="${basedir}/compiled/ext/gnome-lib-darwin.tar.gz"/>
	        <move file="${basedir}/compiled/ext/gnome-lib-minimal" todir="compiled/gs2build/ext"/>
		</if>
		
		<!-- Delete Lucene 3.3 specific ext/solr folders, since we've switched over to Lucene 4.7.2 -->
		<delete dir="${basedir}/compiled/ext/solr/lib3" />
		<delete dir="${basedir}/compiled/ext/solr/conf3" />
		<delete dir="${basedir}/compiled/ext/solr/etc3" />

		  
		<if><bool><and>
		      <equals arg1="${rk.os}" arg2="linux"/>
		      <equals arg1="${extension-x64}" arg2="-x64"/>
		  </and></bool>
		  <property name="bitness" value="64"/>
		  <else>
		    <property name="bitness" value="32"/>
		  </else>
		</if>

		<!-- insert an xml parser (matched to the version of Perl being used by the release kit). 
		     For Linux 64 bit, use the 64 bit versions. 
		     For other *nix cases, including Macs, we stick to the 32 bit 
		     versions since there have been no problems yet, probably
		     because our Mac has 5.12 which is not included in 
		     XML-Parser so that our mac then tries to find the xml 
		     parser on the system.-->
		<if><bool><not><equals arg1="${rk.os}" arg2="windows"/></not></bool>	 
			<copy todir="compiled/gs2build/perllib/cpan">
				<fileset dir="${rk.home}/shared/linux/XML-Parser" includes="${bitness}-bit/**/*"/>
			</copy>
			<move file="compiled/gs2build/perllib/cpan/${bitness}-bit" tofile="compiled/gs2build/perllib/cpan/XML-Parser"/>
		</if>
			
		<!-- insert ant -->
		<property name="insert.ant.version" value="1.8.2"/>
		<mkdir dir="compiled/packages"/>
		<!--<get src="http://www.greenstone.org/gs3files/apache-ant-${insert.ant.version}-bin.tar.gz" dest="compiled/packages/apache-ant-${insert.ant.version}-bin.tar.gz"/>-->
		<exec executable="wget" failonerror="true">
		  <arg value="-P"/>
		  <arg value="compiled/packages"/>
		  <arg value="--no-check-certificate"/>
		  <arg value="http://www.greenstone.org/gs3files/apache-ant-${insert.ant.version}-bin.tar.gz"/>
		</exec>
		<untar src="compiled/packages/apache-ant-${insert.ant.version}-bin.tar.gz" dest="compiled/packages/" compression="gzip"/>
		<move file="compiled/packages/apache-ant-${insert.ant.version}" tofile="compiled/packages/ant"/>
		<delete file="compiled/packages/apache-ant-${insert.ant.version}-bin.tar.gz"/>

		<!-- put the uninstaller in place -->
		<antcall target="insert-and-compile-uninstaller"/>

		<!-- (windows only) -->
		<if><bool><equals arg1="${rk.os}" arg2="windows"/></bool>
			<!-- insert perl -->
			<unzip src="${rk.home}/shared/windows/perl.zip" dest="compiled/gs2build/bin/windows"/>
		</if>

		<!-- minify tomcat -->
		<delete dir="compiled/packages/tomcat/webapps/docs"/>
		<delete dir="compiled/packages/tomcat/webapps/examples"/>

		<!-- tidy up gli to make it smaller -->
		<antcall target="gli-tidy-for-dist"><param name="glibasedir" value="compiled/gli"/></antcall>

		<!-- strip out .svn dirs -->
		<antcall target="strip-svn-dirs"><param name="dir" value="compiled"/></antcall>

		<!-- get rid of bin/formatconverter on windows and get rid of bin/formatconverter.exe on linux/mac. Now we have specific OS dirs, so get rid of the irrelevant OS dirs instead -->
		<if>
			<bool><equals arg1="${rk.os}" arg2="windows"/></bool>
				<delete dir="compiled/bin/darwin"/>
				<delete dir="compiled/bin/linux"/>
			<else><if><bool><equals arg1="${rk.os}" arg2="mac"/></bool>
				<delete dir="compiled/bin/windows"/>
				<delete dir="compiled/bin/linux"/>
			<else><if><bool><equals arg1="${rk.os}" arg2="linux"/></bool>
				<delete dir="compiled/bin/windows"/>
				<delete dir="compiled/bin/darwin"/>
			</if></else>
			</if></else>
		</if>		
		
		<!-- (linux only) -->
		<if><bool><equals arg1="${rk.os}" arg2="linux"/></bool>
			<!-- grab imagemagick binaries -->
			<delete dir="compiled/gs2build/bin/linux/imagemagick"/>

            <exec executable="svn">
               <arg value="export"/>
               <arg value="${svn.gs2.extensions}/imagemagick/imagemagick-linux${extension-x64}.tar.gz"/>
               <arg value="compiled/ext/imagemagick-linux${extension-x64}.tar.gz"/>
            </exec>

            <exec executable="tar" dir="compiled/ext">
               <arg value="xvzf"/>
               <arg value="imagemagick-linux${extension-x64}.tar.gz"/>
            </exec>
            <delete file="compiled/ext/imagemagick-linux${extension-x64}.tar.gz"/>
            <move todir="compiled/gs2build/bin/linux/imagemagick">
               <fileset dir="compiled/ext/imagemagick/linux"/>
            </move>
            <delete dir="compiled/ext/imagemagick"/>

			<!--<delete dir="compiled/gs2build/bin/linux/imagemagick"/>
			<exec executable="svn">
				<arg value="export"/>
				<arg value="${svn.root}/main/${branch.path}/binaries/linux/imagemagick"/>
				<arg value="compiled/gs2build/bin/linux/imagemagick"/>
			</exec>-->

			<!-- grab wv binaries -->
			<delete dir="compiled/gs2build/bin/linux/wv"/>
			<exec executable="svn">
				<arg value="export"/>
				<arg value="${svn.root}/main/${branch.path}/binaries/linux/wv"/>
				<arg value="compiled/gs2build/bin/linux/wv"/>
			</exec>
		<!-- (mac only) -->
		<else><if><bool><equals arg1="${rk.os}" arg2="mac"/></bool>
			<!-- grab imagemagick binaries -->

		    <delete dir="compiled/gs2build/bin/darwin/imagemagick"/>
	            <exec executable="svn">
		      <arg value="export"/>
		      <arg value="${svn.gs2.extensions}/imagemagick/imagemagick-darwin-10.11.tar.gz"/>
		      <arg value="compiled/ext/imagemagick-darwin-10.11.tar.gz"/>
		    </exec>		    
		    <exec executable="tar" dir="compiled/ext">
		      <arg value="xvzf"/>
		      <arg value="imagemagick-darwin-10.11.tar.gz"/>
		    </exec>
		    <delete file="compiled/ext/imagemagick-darwin-10.11.tar.gz"/>
		    <move todir="compiled/gs2build/bin/darwin/imagemagick">
		      <fileset dir="compiled/ext/imagemagick/darwin"/>
		    </move>
		    <delete dir="compiled/ext/imagemagick"/>

		    <!--
			<delete dir="compiled/gs2build/bin/darwin/imagemagick"/>
			<exec executable="svn">
				<arg value="export"/>
				<arg value="${svn.root}/main/${branch.path}/binaries/mac/intel/imagemagick"/>
				<arg value="compiled/gs2build/bin/darwin/imagemagick"/>
			</exec>-->

			<if><bool><not><equals arg1="${forward.install.flax}" arg2="true"/></not></bool>
				<!-- grab ghostscript binaries -->
				<delete dir="compiled/gs2build/bin/darwin/ghostscript"/>
				<exec executable="svn">
					<arg value="export"/>
					<arg value="${svn.root}/main/${branch.path}/binaries/mac/intel/ghostscript"/>
					<arg value="compiled/gs2build/bin/darwin/ghostscript"/>
				</exec>
			</if>
		</if></else></if>

		<if><bool><equals arg1="${rk.os}" arg2="windows"/></bool>
			<exec dir="${basedir}/compiled" executable="${basedir}/compiled/gs2build/bin/windows/perl/bin/perl">
				<arg line="-S"/>
				<arg line="gs2build/bin/script/import.pl -removeold -site localsite lucene-jdbm-demo"/>
				<env key="GSDLHOME" path="${basedir}/compiled/gs2build"/>
				<env key="GSDL3HOME" path="${basedir}/compiled/web"/>
				<env key="GSDL3SRCHOME" path="${basedir}/compiled"/>
				<env key="GSDLCOLLECTDIR" path="${basedir}/compiled/web/sites/localsite/collect"/>
				<env key="GSDLOS" value="windows"/>
				<env key="PATH" path="${basedir}/compiled/gs2build/bin/windows/perl/bin;${basedir}/compiled/gs2build/bin/windows;${basedir}/compiled/gs2build/bin/script;${env.PATH}"/>
			</exec>
			<exec dir="${basedir}/compiled" executable="${basedir}/compiled/gs2build/bin/windows/perl/bin/perl">
				<arg line="-S"/>
				<arg line="gs2build/bin/script/buildcol.pl -removeold -site localsite lucene-jdbm-demo"/>
				<env key="GSDLHOME" path="${basedir}/compiled/gs2build"/>
				<env key="GSDL3HOME" path="${basedir}/compiled/web"/>
				<env key="GSDL3SRCHOME" path="${basedir}/compiled"/>
				<env key="GSDLCOLLECTDIR" path="${basedir}/compiled/web/sites/localsite/collect"/>
				<env key="GSDLOS" value="windows"/>
				<env key="PATH" path="${basedir}/compiled/gs2build/bin/windows/perl/bin;${basedir}/compiled/gs2build/bin/windows;${basedir}/compiled/gs2build/bin/script;${env.PATH}"/>
			</exec>
			<delete dir="${basedir}/compiled/web/sites/localsite/collect/lucene-jdbm-demo/index"/>
			<move file="${basedir}/compiled/web/sites/localsite/collect/lucene-jdbm-demo/building" tofile="${basedir}/compiled/web/sites/localsite/collect/lucene-jdbm-demo/index"/>
		<else>
			<echo file="${basedir}/compiled/buildscript.sh">
				#!/bin/bash
				source gs3-setup.sh
				/usr/bin/perl -S ${basedir}/compiled/gs2build/bin/script/import.pl -site localsite lucene-jdbm-demo
				/usr/bin/perl -S ${basedir}/compiled/gs2build/bin/script/buildcol.pl -site localsite lucene-jdbm-demo 
				rm -rf ${basedir}/compiled/web/sites/localsite/collect/lucene-jdbm-demo/index
				mv ${basedir}/compiled/web/sites/localsite/collect/lucene-jdbm-demo/building ${basedir}/compiled/web/sites/localsite/collect/lucene-jdbm-demo/index
			</echo>
			<chmod perm="775" file="${basedir}/compiled/buildscript.sh"/>
			<exec dir="${basedir}/compiled" executable="/bin/bash">
				<arg line="buildscript.sh"/>
			</exec>
			<delete file="${basedir}/compiled/buildscript.sh"/> 
		</else>
		</if>
		
	</target>

</project>
