<?xml version="1.0" encoding="utf-8" ?>
<project name="wrap" default="wrap">

	<target name="wrap" depends="init">

		<!-- create products directory -->
		<mkdir dir="products"/>

		<!-- (windows) -->
		<if><bool><equals arg1="${rk.os}" arg2="windows"/></bool>

			<!--create a directory to build the wrapped installer in -->
			<mkdir dir="wrapped-installer"/>

			<!-- copy wrapper source into place -->
			<copy todir="wrapped-installer" overwrite="true">
				<fileset dir="${rk.home}/shared/windows/wrapper"/>
				<fileset file="${rk.home}/shared/core/icon/icon.ico"/>
				<fileset dir="${rk.home}/shared/core/search4j" includes="libsearch4j.cpp,libsearch4j.h"/>
			</copy>

			<!-- substitute in concrete values -->
			<rsr file="wrapped-installer/wrapper.cpp">
				<job pattern="@java.installer@" replacement="${windows-java.installer}"/>
				<job pattern="@java.min.version@" replacement="${java.min.version}"/>
				<job pattern="@bundled.version.java@" replacement="${bundled.version.windows-java}"/>
				<job pattern="@java.extracted@" replacement="${java.extracted}"/>
			</rsr>

			<!-- get the template resource script -->
			<copy todir="wrapped-installer" file="${rk.home}/shared/windows/wrapper/wrapper.rc" overwrite="true"/>

			<!-- split the resources into chunks, creating a resource script of the chunks -->
			<rsplit
				resource="${rk.home}/shared/windows/wrapper/${windows-java.installer}"
				outputDir="wrapped-installer"					
				resourceScript="wrapped-installer/wrapper.rc"
				resourceName="JAVA"
				resourceType="EXE"
				chunkSize="${resources.chunksize}"/>
			<rsplit
				resource="installer/Greenstone-${version}${version-extra}-${os.suffix}.jar"
				outputDir="wrapped-installer"
				resourceScript="wrapped-installer/wrapper.rc"
				resourceName="JAR"
				resourceType="JAR"
				chunkSize="${resources.chunksize}"
				/>

			<!-- compile the resources -->
			<exec executable="rc" dir="wrapped-installer">
				<arg line="wrapper.rc"/>
			</exec>

			<!-- compile the wrapper -->
			<exec executable="cl" dir="wrapped-installer">
				<arg line="/c -GX /DWINDOWS=yes libsearch4j.cpp /out:libsearch4j.obj"/>
			</exec>
			<exec executable="cl" dir="wrapped-installer">
				<arg line="/c -GX /DWINDOWS=yes wrapper.cpp /out:wrapper.obj"/>
			</exec>

			<!-- link wrapper -->
			<exec executable="link" dir="wrapped-installer">
				<arg line="/OUT:..\products\Greenstone-${version}${version-extra}-${os.suffix}${extension-x64}.exe gdi32.lib advapi32.lib shell32.lib user32.lib libsearch4j.obj wrapper.obj wrapper.res"/>
			</exec>

		<!-- (linux) -->
		<else><if><bool><equals arg1="${rk.os}" arg2="linux"/></bool>
			<!-- create the build directory -->
			<mkdir dir="wrapped-installer"/>

			<!-- copy the source into place --> 
			<copy todir="wrapped-installer" overwrite="true">
				<fileset file="${rk.home}/shared/linux/wrapper.cpp"/>
				<fileset dir="${rk.home}/shared/core/search4j" includes="libsearch4j.cpp,libsearch4j.h"/>
			</copy>

			<!-- copy bundled files into place -->
			<copy todir="wrapped-installer" file="${rk.home}/shared/linux/${linux-java.installer}"/>
			<copy tofile="wrapped-installer/greenstone.jar" file="installer/Greenstone-${version}${version-extra}-${os.suffix}.jar"/>

			<!-- substitute in real values -->
			<rsr file="wrapped-installer/wrapper.cpp">
				<job pattern="@java.installer@" replacement="${linux-java.installer}"/>
				<job pattern="@java.extracted@" replacement="${java.extracted}"/>
				<job pattern="@java.min.version@" replacement="${java.min.version}"/>
			</rsr>

			<!-- create an object file (extension .o) for each file to bundle -->
			<if><bool><equals arg1="${x64}" arg2="true"/></bool>
			<exec executable="/usr/bin/objcopy" dir="wrapped-installer">
			  <arg line="-I binary -O elf64-x86-64 -B i386 --redefine-sym _binary_greenstone_jar_start=greenstonejar --readonly-text greenstone.jar greenstone.jar.o"/>
			</exec>
			<exec executable="/usr/bin/objcopy" dir="wrapped-installer">
			  <arg line="-I binary -O elf64-x86-64 -B i386 --redefine-sym _binary_${linux-java.installer}_start=java --readonly-text ${linux-java.installer} java.o"/>
			</exec>

			<else>
			  <exec executable="/usr/bin/objcopy" dir="wrapped-installer" failonerror="true">
			    <arg line="-I binary -O elf32-i386 -B i386 --redefine-sym _binary_greenstone_jar_start=greenstonejar --readonly-text greenstone.jar greenstone.jar.o"/>
			  </exec>
			  <exec executable="/usr/bin/objcopy" dir="wrapped-installer" failonerror="true">
			    <arg line="-I binary -O elf32-i386 -B i386 --redefine-sym _binary_${linux-java.installer}_start=java --readonly-text ${linux-java.installer} java.o"/>
			  </exec>
			 </else>
			</if>

			<!-- compile libsearch4j -->
			<exec executable="g++" dir="wrapped-installer">
				<arg line="-Wall -static -static-libgcc -c -o libsearch4j.o libsearch4j.cpp"/>
			</exec>

			<!-- figure out jar size (web jar) -->
			<exec executable="ls" dir="wrapped-installer" outputproperty="greenstone.jar.ls"><arg line="-l greenstone.jar"/></exec>
			<exec executable="sed" inputstring="${greenstone.jar.ls}" outputproperty="greenstone.jar.size"><arg line="-e 's/^[^\ ]*\ *[^\ ]*\ *[^\ ]*\ *[^\ ]*\ *\([0-9]*\).*$/\1/g'"/></exec>

			<!-- figure out java size -->
			<exec executable="ls" dir="wrapped-installer" outputproperty="java.ls"><arg line="-l ${linux-java.installer}"/></exec>
			<exec executable="sed" inputstring="${java.ls}" outputproperty="java.size"><arg line="-e 's/^[^\ ]*\ *[^\ ]*\ *[^\ ]*\ *[^\ ]*\ *\([0-9]*\).*$/\1/g'"/></exec>

			<!-- creating wrapper header file -->
			<delete file="wrapper.h"/>
			<!-- ensure no tabs/whitespaces in front of each line in the .h file owing to indentation here -->
			<echo file="wrapped-installer/wrapper.h">extern const char greenstonejar[${greenstone.jar.size}];
extern const char java[${java.size}];
#define java_is_bundled yes
</echo>

			<!-- compile the wrapper program -->
			<exec executable="g++" dir="wrapped-installer" failonerror="true">
				<arg line="-Wall -static -static-libgcc -c -o wrapper.o wrapper.cpp"/>
			</exec>

			<!-- link the compiled code and bundled files -->
			<exec executable="g++" dir="wrapped-installer">
				<arg line="-static -static-libgcc -o ../products/Greenstone-${version}${version-extra}-linux${extension-x64} wrapper.o libsearch4j.o greenstone.jar.o java.o"/>
			</exec>

		<!-- (mac) -->
		<else><if><bool><equals arg1="${rk.os}" arg2="mac"/></bool>

			<!-- create and setup the build directory -->
			<delete dir="wrapped-installer"/>
			<mkdir dir="wrapped-installer"/>

			<!-- create app for main installer -->
			<copy todir="wrapped-installer/Greenstone${version}${version-extra}.app"><fileset dir="${rk.home}/shared/mac/Wrapper.app"/></copy>
			<copy todir="wrapped-installer/Greenstone${version}${version-extra}.app/Contents/Resources" file="${rk.home}/shared/core/icon/icon.icns"/>
			<chmod file="${rk.home}/shared/mac/Install-Greenstone.command" perm="ug+x"/>
			<chmod file="wrapped-installer/Greenstone${version}${version-extra}.app/Contents/MacOS/JavaApplicationStub" perm="ug+x"/>

			<!-- copy bundled files -->
			<!-- Include the self-extracting jdk (for 64 bit) if this Mac is a 64 bit OS
				 http://stackoverflow.com/questions/218989/how-to-determine-build-architecture-32bit-64bit-with-ant -->
			<exec dir="." executable="/bin/sh" outputproperty="bitness.ouput"><arg line="-c &quot;uname -m&quot;"/></exec>
			<if><bool><contains string="${bitness.ouput}" substring="_64"/></bool>
				<copy tofile="wrapped-installer/Greenstone${version}${version-extra}.app/Contents/Resources/Java/java_bin" file="${rk.home}/shared/mac/${mac-java.installer}"/>
			</if>
			<chmod file="wrapped-installer/Greenstone${version}${version-extra}.app/Contents/MacOS/greenstone" perm="ug+x"/>
			<chmod file="wrapped-installer/Greenstone${version}${version-extra}.app/Contents/Resources/Java/java_bin" perm="ug+x"/>
			<copy todir="wrapped-installer/Greenstone${version}${version-extra}.app/Contents/Resources/Java" file="installer/Greenstone-${version}${version-extra}-${os.suffix}.jar"/>

			<!-- substitute in real values -->
			<rsr file="wrapped-installer/Greenstone${version}${version-extra}.app/Contents/Info.plist">
				<job pattern="@jar@" replacement="Greenstone-${version}${version-extra}-${os.suffix}.jar"/>
				<job pattern="@version@" replacement="${version}${version-extra}"/>
			</rsr>

			<!-- turn app into a dmg -->
			<!--Clean out all local testing metadata (quarantine, provenance, finder tags)-->
			<exec executable="xattr" dir="wrapped-installer"><arg line="-cr Greenstone${version}${version-extra}.app"/></exec>
			<!-- get size of app -->
			<exec executable="du" dir="wrapped-installer" outputproperty="app.du"><arg line="-ks Greenstone${version}${version-extra}.app"/></exec>
			<exec executable="sed" inputstring="${app.du}" outputproperty="app.size"><arg line="-e 's/^\([0-9]*\).*$/\1/g'"/></exec>
			<math result="dmg.size" operand1="${app.size}" operation="+" operand2="${dmg.overhead}" datatype="int"/>


			<property name="dmg.name" value="Greenstone-${version}${version-extra}-${os.suffix}"/>

			<!-- New way of creating dmg per Gemini doesn't require first mounting dmg and pre-calculating size of contents -->
			<!-- 1. Purge any stale staging data -->
			<delete dir="products/dmg_staging"/>
			<mkdir dir="products/dmg_staging"/>

			<!-- 2. Copy the .app bundle using standard Ant tasks (Safe & Fast) -->
			<move todir="products/dmg_staging/Greenstone${version}${version-extra}.app">
			  <fileset dir="wrapped-installer/Greenstone${version}${version-extra}.app"/>
			</move>
			
			<!-- 3. Copy the launcher .command file right next to the .app -->
			<!--<copy file="${rk.home}/shared/mac/Install-Greenstone.command" todir="products/dmg_staging"/>-->
			
			<!-- 4. Explicitly stamp permissions BEFORE turning it into a disk image -->
			<!--<chmod file="products/dmg_staging/Install-Greenstone.command" perm="ug+x"/>-->
			<chmod file="products/dmg_staging/Greenstone${version}${version-extra}.app/Contents/MacOS/[launcher]" perm="ug+x"/>

			<!-- 5. Generate the final DMG directly from the local folder layout -->
			<retry retrycount="5" retrydelay="2000">
			  <exec dir="products" executable="hdiutil" failonerror="true">
			    <arg line="create -ov -srcfolder dmg_staging -fs HFS+ -volname ${dmg.name} ${dmg.name}.dmg"/>
			  </exec>
			</retry>

			<!-- 6. Clean up the temporary folder layout -->
			<delete dir="products/dmg_staging"/>

			
			<!-- The old way of creating the dmg is to create an empty one, mount it, copy the app
			     into it, then unmount it. But now and then we may need to recalculate and increase the size
			     Gemini also said we need ensure buffer of items copied into the dmg is flushed, though
			     we never had problems in that area.
			create disk image
			<exec dir="products" executable="hdiutil" failonerror="true"><arg line="create -ov -size ${dmg.size}k -fs HFS+ -volname ${dmg.name} ${dmg.name}.dmg"/></exec>
			mount it
			<exec executable="hdiutil" dir="products" failonerror="true"><arg line="attach ${dmg.name}.dmg"/></exec>
			copy file into it
			<exec executable="cp"><arg line="-r wrapped-installer/Greenstone${version}${version-extra}.app /Volumes/${dmg.name}"/></exec>
			<exec executable="cp" failonerror="true"><arg line="${rk.home}/shared/mac/Install-Greenstone.command /Volumes/${dmg.name}/."/></exec>
			<exec executable="chmod" failonerror="true"><arg line="ug+x /Volumes/${dmg.name}/Install-Greenstone.command"/></exec>
			Force macOS to flush the memory buffer to the disk sector blocks
			<exec executable="sync" failonerror="true"/>
			
			unmount it
			<exec executable="hdiutil" failonerror="false"><arg line="detach /Volumes/${dmg.name}"/></exec>
			-->
		</if></else></if></else></if>


	</target>

</project>

