<?xml version="1.0" encoding="utf-8" ?>
<project name="create-installer">

	<target name="create-installer" depends="init">

		<!-- create installer directory -->
		<mkdir dir="installer/classes"/>

		<!-- insert the installer descriptors -->
		<copy todir="installer" overwrite="true">
			<fileset dir="${rk.home}/kits/${rk.name}/installer" includes="antinstall-config.xml,build.xml"/>
		</copy>

		<!-- insert the language bundle -->
		<!-- See comment in shared.xml -->
		<!--
		<native2ascii
			implementation="default"
			encoding="UTF-8"
			src="${rk.home}/shared/core/language-strings"
			dest="${basedir}/installer/classes/resources"/>
		-->
		<copy todir="${basedir}/installer/classes/resources" overwrite="true">
			<fileset dir="${rk.home}/shared/core/language-strings" erroronmissingdir="false"/>
		</copy>
		
		<!-- insert core, rk-specific and os-specific installer classes -->
		<copy todir="${basedir}/installer/classes" overwrite="true">
			<fileset dir="${rk.home}/shared/core/installer-classes" erroronmissingdir="false"/>
			<fileset dir="${rk.home}/kits/${rk.name}/installer-classes" erroronmissingdir="false"/>
			<fileset dir="${rk.home}/shared/${rk.os}/installer-classes" erroronmissingdir="false"/>
		</copy>

		<!-- insert the licence -->
		<copy file="compiled/LICENSE.txt" todir="${basedir}/installer/classes" overwrite="true"/>

		<!-- insert greenstone's ant tasks -->
		<unjar src="${rk.home}/shared/core/ant-tasks/greenstone/anttasks.jar" dest="installer/classes"/>
		
		<!-- insert antelope tasks -->
		<unjar src="${rk.home}/shared/core/ant-tasks/antelope/AntelopeTasks_3.4.2.jar" dest="installer/classes"/>

		<!-- Include lib/crypt.class for both releasekit2 and 3 (rk2 and rk3) 
		since we need it to encrypt the admin password -->		
			<unjar src="${rk.home}/shared/core/lib/crypt.jar" dest="${basedir}/installer/classes">
				<patternset><include name="**/*.class"/></patternset>
			</unjar>

		<!-- (windows only) -->
		<if><bool><equals arg1="${rk.os}" arg2="windows"/></bool>
			<!-- insert 7za.exe tool -->
			<copy todir="installer/classes" overwrite="true" file="${rk.home}/shared/windows/7za/7za.exe"/>
			<copy todir="installer/classes" overwrite="true" file="${rk.home}/shared/windows/create-shortcut.vbs"/>
			<!-- insert roxes and orangevolt for windows tricks in ant -->
			<!--<unjar src="${rk.home}/shared/core/ant-tasks/orangevolt/roxes-win32forjava-1.1.1.jar" dest="installer/classes"/>
			<unjar src="${rk.home}/shared/core/ant-tasks/orangevolt/orangevolt-ant-tasks-1.3.8.jar" dest="installer/classes"/>-->

		<!-- (linux and mac only) -->
		<else><if><bool><or><equals arg1="${rk.os}" arg2="linux"/><equals arg1="${rk.os}" arg2="mac"/></or></bool>
			<!-- insert 7zip ant task -->
			<unjar src="${rk.home}/shared/core/ant-tasks/7z-ant/7z.jar" dest="installer/classes"/>

		</if></else></if>

		<!-- clean up meta directory -->
		<delete dir="installer/classes/META-INF"/>

		<!-- calculate the size of: core -->
		<length property="component.bytesize.core-without-java" mode="all"><fileset file="${basedir}/components/core.comp"/></length>
		<if><bool><equals arg1="${rk.os}" arg2="windows"/></bool>
			<math result="component.bytesize.core" operand1="${component.bytesize.core-without-java}" operation="+" operand2="${component.bytesize.windows-java}" datatype="int"/>
		<else><if><bool><equals arg1="${rk.os}" arg2="linux"/></bool>
			<math result="component.bytesize.core" operand1="${component.bytesize.core-without-java}" operation="+" operand2="${component.bytesize.linux-java}" datatype="int"/>
		<else>
			<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>
				<math result="component.bytesize.core" operand1="${component.bytesize.core-without-java}" operation="+" operand2="${component.bytesize.mac-java}" datatype="int"/>
				<else><property name="component.bytesize.core" value="${component.bytesize.core-without-java}"/></else>
			</if>
		</else></if></else></if>
		<math result="component.megabytesize.core" operand1="${component.bytesize.core}" operation="/" operand2="1048576" datatype="int"/>
		<property name="component.size.core" value="${component.megabytesize.core} MB"/>

		<!-- calculate the size of: imagemagick -->
		<length property="component.bytesize.imagemagick" mode="all"><fileset file="${basedir}/components/imagemagick.comp"/></length>
		<math result="component.megabytesize.imagemagick" operand1="${component.bytesize.imagemagick}" operation="/" operand2="1048576" datatype="int"/>
		<property name="component.size.imagemagick" value="${component.megabytesize.imagemagick} MB"/>

		<!-- calculate the size of: ghostscript -->
		<length property="component.bytesize.ghostscript" mode="all"><fileset file="${basedir}/components/ghostscript.comp"/></length>
		<math result="component.megabytesize.ghostscript" operand1="${component.bytesize.ghostscript}" operation="/" operand2="1048576" datatype="int"/>
		<property name="component.size.ghostscript" value="${component.megabytesize.ghostscript} MB"/>
		
		<!-- calculate the size of: apache httpd -->
		<length property="component.bytesize.httpd" mode="all"><fileset file="${basedir}/components/apachehttpd.comp"/></length>
		<math result="component.megabytesize.httpd" operand1="${component.bytesize.httpd}" operation="/" operand2="1048576" datatype="int"/>
		<property name="component.size.httpd" value="${component.megabytesize.httpd} MB"/>
		
		<!-- calculate the size of: tomcat -->
		<length property="component.bytesize.tomcat" mode="all"><fileset file="${basedir}/components/tomcat.comp"/></length>
		<math result="component.megabytesize.tomcat" operand1="${component.bytesize.tomcat}" operation="/" operand2="1048576" datatype="int"/>
		<property name="component.size.tomcat" value="${component.megabytesize.tomcat} MB"/>

		<!-- EXT -->
		<!-- calculate the size of: amp (and put it into the components directory) -->
		<if><bool><and><equals arg1="${amp.enabled}" arg2="true"/><not><available file="${rk.home}/ext/amp.lzma"/></not></and></bool>
		  <fail>amp.lzma file not found in the release kit ext directory</fail>
		</if>

		<if><bool><and><equals arg1="${amp.enabled}" arg2="true"/></and></bool>
		  <echo>Copying amp.lzma to compontents directory</echo>
		  <copy file="${rk.home}/ext/amp.lzma" todir="${basedir}/components"/>
		  <property name="component.size.amp" value="257 MB"/>
		</if>
		<!-- /EXT -->

		<!-- make the installer descriptors relevant to the current os -->
		<dcff file="installer/antinstall-config.xml" startTag="&lt;!--\s*if\s*(?!.*${rk.os})[^ ]+\s*--&gt;" endTag="&lt;!--\s*/if\s*--&gt;" />
		<dcff file="installer/build.xml" startTag="&lt;!--\s*if\s*(?!.*${rk.os})[^ ]+\s*--&gt;" endTag="&lt;!--\s*/if\s*--&gt;" />

		<!-- EXT -->
		<if><bool><not><equals arg1="${amp.enabled}" arg2="true"/></not></bool>
		  
		  <dcff file="installer/antinstall-config.xml" startTag="&lt;!-- start amp --&gt;" endTag="&lt;!-- end amp --&gt;" />
		  <dcff file="installer/build.xml" startTag="&lt;!-- start amp --&gt;" endTag="&lt;!-- end amp --&gt;" />
		</if>
		<!-- /EXT -->
		<!-- put the concrete values of things in the config -->
		<rsr>
			<fileset dir="${basedir}/installer" includes="*.xml"/>
			<job pattern="@version@" replacement="${version}${version-extra}"/>
			<job pattern="@component.size.core@" replacement="${component.size.core}"/>
			<job pattern="@component.size.tomcat@" replacement="${component.size.tomcat}"/>
			<job pattern="@component.size.imagemagick@" replacement="${component.size.imagemagick}"/>
			<job pattern="@component.size.ghostscript@" replacement="${component.size.ghostscript}"/>
			<job pattern="@component.size.httpd@" replacement="${component.size.httpd}"/>
			<job pattern="@java.min.version@" replacement="${java.min.version}"/>
			<job pattern="@java.extracted@" replacement="${java.extracted}"/>
		</rsr>

		<!-- If this is a FLAX installer then we need to change a few things -->
		<if><bool><equals arg1="${forward.install.flax}" arg2="true"/></bool>
			<dcff file="installer/build.xml" startTag="&lt;!-- NO FLAX START --&gt;" endTag="&lt;!-- NO FLAX END --&gt;" />
			<dcff file="installer/antinstall-config.xml" startTag="&lt;!-- NO FLAX START --&gt;" endTag="&lt;!-- NO FLAX END --&gt;" />
		<else>
			<dcff file="installer/build.xml" startTag="&lt;!-- FLAX START --&gt;" endTag="&lt;!-- FLAX END --&gt;" />
			<dcff file="installer/antinstall-config.xml" startTag="&lt;!-- FLAX START --&gt;" endTag="&lt;!-- FLAX END --&gt;" />
		</else>
		</if>

		<!-- If we don't want GLI/GEMS -->
		<if><bool><equals arg1="${with.gli.and.gems}" arg2="false" /></bool>
			<dcff file="installer/build.xml" startTag="&lt;!-- GLI START --&gt;" endTag="&lt;!-- GLI END --&gt;" />
		</if>	

		<!-- EXT -->
		<if><bool><equals arg1="${amp.enabled}" arg2="true"/></bool>
		  <rsr>
		    <fileset dir="${basedir}/installer" includes="*.xml"/>
		    <job pattern="@component.size.amp@" replacement="${component.size.amp}"/>
		  </rsr>
		</if>
		<!-- /EXT -->

		<!-- put the concrete values of things in the text -->
		<rsr>
			<fileset dir="${basedir}/installer/classes/resources" includes="*.properties"/>
			<job pattern="@version@" replacement="${version}${version-extra}"/>
			<job pattern="@version.major@" replacement="${version.major}"/>
			<job pattern="@java.min.version@" replacement="${java.min.version}"/>
			<job pattern="@bundled.version.imagemagick@" replacement="${bundled.version.imagemagick}"/>
			<job pattern="@bundled.version.ghostscript@" replacement="${bundled.version.ghostscript}"/>
			<job pattern="@bundled.version.tomcat@" replacement="${bundled.version.tomcat}"/>
			<job pattern="@bundled.version.windows-java@" replacement="${bundled.version.windows-java}"/>
			<job pattern="@bundled.version.linux-java@" replacement="${bundled.version.linux-java}"/>
			<job pattern="@bundled.version.mac-java@" replacement="${bundled.version.mac-java}"/>
		</rsr>

		<!-- now ready to run "compile-*-installer" targets -->

		<antcall target="compile-binary-installer"/>

	</target>

	<target name="compile-binary-installer">

		<!-- strip out lines for the cdrom installer -->
		<copy file="installer/antinstall-config.xml" tofile="installer/antinstall-config-binary.xml" overwrite="true"/>
		<dcff file="installer/antinstall-config-binary.xml" startTag=".*&lt;!-- start cdrom --&gt;.*" endTag=".*&lt;!-- end cdrom --&gt;.*" />
		<copy file="installer/build.xml" tofile="installer/build-binary.xml" overwrite="true"/>
		<dcff file="installer/build-binary.xml" startTag=".*&lt;!-- start cdrom --&gt;.*" endTag=".*&lt;!-- end cdrom --&gt;.*" />

		<!-- create insaller jar with ant-installer's ant task -->
		<installer file="installer/Greenstone-${version}${version-extra}-${os.suffix}.jar"
			compress="true"
			extractType="NonExtractor"
			installConfig="installer/antinstall-config-binary.xml"
			buildFile="installer/build-binary.xml"
			antInstallLib="${rk.home}/shared/core/ant-installer/lib"
			validateConfig="true"
			icons="bluecurve">
		  <!--
			antLib="${ant.home}/lib"
		  -->

		  <!-- 
		       The follow (below) is a replacement for doing the follow up in the <installer> element (above):

		         antib="${ant.home}/lib"
		       
		       The change was needed to support newer versions of ant, where the underlying taskdef FileSet API 
		       now has stricker checks, and no longer allows AbstractFileSet.setFile() to be called more than once for
		       an instantiated object instance of FileSet.  In the case of 'antlLib' in the ant-istaller taskdef, this is
		       exactly what triggered by specifying this attribute is specified.  The workaround is to create the
		       fileset explicitly below: done through <zipgroupfileset>, this form can have multiple values in it
		       and has the additional property of unzipping/unjarring the specificed jar files
		  -->

		  <zipgroupfileset dir="${ant.home}/lib" includes="ant.jar,ant-launcher.jar" excludes="META-INF/*.SF"/>

		  <zipfileset dir="installer/classes" includes="**/*" />
		  <zipfileset dir="components" includes="*.lzma"/>
		  
		</installer>
		
	</target>

	<target name="compile-cdrom-installer">

		<!-- strip out lines for the binary(web) installer -->
		<copy file="installer/antinstall-config.xml" tofile="installer/antinstall-config-cdrom.xml"/>
		<dcff file="installer/antinstall-config-cdrom.xml" startTag=".*&lt;!-- start web --&gt;.*" endTag=".*&lt;!-- end web --&gt;.*" />
		<copy file="installer/build.xml" tofile="installer/build-cdrom.xml"/>
		<dcff file="installer/build-cdrom.xml" startTag=".*&lt;!-- start web --&gt;.*" endTag=".*&lt;!-- end web --&gt;.*" />

		<installer file="installer/cdrom.jar"
			compress="true"
			extractType="NonExtractor"
			installConfig="installer/antinstall-config-cdrom.xml"
			buildFile="installer/build-cdrom.xml"
			antInstallLib="${rk.home}/shared/core/ant-installer/lib"
			validateConfig="true"
			icons="bluecurve">

		  <!--
		      The following (which had been in the element above):

			antLib="${ant.home}/lib"

		      now needs to be done explicitly (below) due to changes in newer implementation of ant tasks
		      See longer note on the topic in the target before this one
		  -->
		  <zipgroupfileset dir="${ant.home}/lib" includes="ant.jar,ant-launcher.jar" excludes="META-INF/*.SF"/>

		  <zipfileset dir="installer/classes" includes="**/*" />
		</installer>
		
	</target>

</project>
