<?xml version="1.0"?>
<!--
This is the build.xml run by AntInstaller for the installer
-->

<project name="Installation">

	<!-- this is required to pick up the properties generated during the install pages -->
	<property file="${basedir}/ant.install.properties"/>

	<!-- set some properties to keep track of the OS -->
	<!-- if windows -->
	<property name="rk.os" value="windows"/>
	<property name="shell.name" value="windows"/>
	<!-- /if -->
	<!-- if linux -->
	<property name="rk.os" value="linux"/>
	<property name="shell.name" value="linux"/>
	<!-- /if -->
	<!-- if mac -->
	<property name="rk.os" value="mac"/>
	<property name="shell.name" value="darwin"/>
	<!-- /if -->

	<!-- classpath -->
	<path id="project.classpath">
		<pathelement path="${antinstaller.jar}"/>
	</path>

	<!-- custom tasks -->
	<taskdef name="if" classname="ise.antelope.tasks.IfTask" classpathref="project.classpath"/>
	<typedef name="rsr" classname="org.greenstone.anttasks.RegexSearchReplace" classpathref="project.classpath"/>
	<typedef name="adduser" classname="org.greenstone.anttasks.AddGreenstoneUserToDatabase" classpathref="project.classpath"/>
	<!-- if windows -->
	<taskdef name="shortcut" classname="com.orangevolt.tools.ant.Win32ShortcutTask" classpathref="project.classpath"/>
	<taskdef name="winprops" classname="com.orangevolt.tools.ant.Win32PropertiesTask" classpathref="project.classpath"/> 
	<typedef name="getfreepath" classname="org.greenstone.anttasks.GetFreePath" classpathref="project.classpath"/>
	<!-- /if -->
	<!-- if linux|mac -->
	<taskdef name="sevenzip" classname="org.apache.tools.ant.SevenZip" classpathref="project.classpath"/>
	<!-- /if -->

	<!-- determine if a bundled java exists -->
	<condition property="bundled.java.exists">
		<or>
			<available file="../@java.extracted@/bin/java"/>
			<available file="../@java.extracted@/bin/java.exe"/>
			<available file="/tmp/jdk/bin/java"/>
		</or>
	</condition>

	<!-- determine if there is an existing installation in the installDir-->
	<available file="${installDir}\etc\installation.properties" property="is.overinstall"/>

	<!-- create a local installDir String -->
	<path id="installDir.path"><pathelement location="${installDir}"/></path>
	<property name="installDir.local" refid="installDir.path"/>

	<!-- if windows -->
	<!-- load windows properties -->
	<winprops/>
	<!-- /if -->

	<!-- load the properties of the previous installation if this is an overinstall -->
	<target name="Looking For Previous Installation" if="is.overinstall">
		<property file="${installDir}\etc\installation.properties"/>
		<delete dir="${installed.startmenu.path}" failonerror="false"/>
	</target>

	<!-- copy java -->
	<target name="Initialising" if="bundled.java.exists">
		<!-- work out whether a jdk was bundled with this installer. If it was, then for a Mac, it would live in /tmp -->
		<!-- if mac -->
		<property name="extracted.java.path" location="/tmp/jdk" relative="false" />
		<!-- /if -->
		<!-- if windows|linux -->
		<property name="extracted.java.path" location="../@java.extracted@" relative="true" basedir="${basedir}" />
		<!-- /if -->

		<mkdir dir="${installDir}/packages"/>
		<copy todir="${installDir}/packages/jdk" failonerror="false">
			<fileset dir="${extracted.java.path}"/>
		</copy>
		<chmod dir="${installDir}/packages/jdk/bin" includes="*" perm="775"/>		
		<!-- if mac -->
		<chmod dir="${installDir}/packages/jdk/lib" includes="**/*" perm="a+rx"/>		
		<!-- /if -->
	</target>

	<!-- copy java -->
	<!--
	<target name="Initialising" if="bundled.java.exists">
		<mkdir dir="${installDir}/packages"/>
		<copy todir="${installDir}/packages/jdk" failonerror="false">
			<fileset dir="../@java.extracted@"/>
		</copy>
		<chmod dir="${installDir}/packages/jdk/bin" includes="*" perm="775"/>		
	</target>
	-->


	<!-- core system -->
	<target name="Installing Core System">

		<!-- if windows -->
		<!-- figure out the start menu path -->
		<getfreepath path="${win32.personal.programs_menu}\Greenstone-@version@" property="startmenu.path"/>		
		<path id="startmenu.path.path" path="${startmenu.path}"/>
		<pathconvert targetos="unix" property="startmenu.path.unix" refid="startmenu.path.path"/>

		<!-- extract 7za tool -->
		<unzip src="${antinstaller.jar}" dest="${basedir}">
			<patternset><include name="7za.exe"/></patternset>
		</unzip>
		<!-- /if -->

		<!-- start web -->
		<unzip src="${antinstaller.jar}" dest="${basedir}"><patternset><include name="core.lzma"/></patternset></unzip>
		<!-- if linux|mac --><sevenzip task="decode" input="${basedir}/core.lzma" output="${basedir}/core.comp"/><!-- /if -->
		<!-- if windows --><exec dir="${basedir}" executable="${basedir}/7za.exe"><arg line="x core.lzma"/></exec><!-- /if -->
		<delete file="core.lzma"/>
		<unzip src="${basedir}/core.comp" dest="${installDir}"/>
		<delete file="core.comp"/>
		<!-- end web -->

		<!-- start cdrom -->
		<copy todir="${installDir}">
			<fileset dir="${orig.dir}/Software/core/all"/>
			<fileset dir="${orig.dir}/Software/core/${rk.os}"/><!-- /if -->
		</copy>
		<!-- if windows -->
		<copy todir="${installDir}/packages/jdk" failonerror="false">
			<fileset dir="${orig.dir}/Java/Windows/jdk"/>
		</copy>
		<!-- /if -->
		<!-- end cdrom -->

		<echo>Setting Binaries to Executable</echo>
		<chmod dir="${installDir}" includes="*.sh,*.bash" perm="775"/>
		<chmod dir="${installDir}/bin" includes="*" perm="775"/>
		<!-- to circumvent the Shellshock Bash bug vulnerability, 
		do not give execute permissions to bash files (*.*sh, or no file extension)
		At present, the "getior" file is the only  bash script here. 
		The other executables here have *.cgi, *.pl extensions.-->
		<chmod dir="${installDir}/cgi-bin/${shell.name}" includes="*.cgi,*.pl" perm="775"/>
		<chmod dir="${installDir}/bin/script" includes="**/*" perm="775"/>
		<chmod dir="${installDir}/gli" includes="*.sh" perm="775"/>
		<chmod dir="${installDir}/ext/solr9/bin/script" includes="*" perm="775"/>
		<chmod file="${installDir}/ext/solr9/solr/bin/solr" perm="775"/>
		<!-- if linux|mac -->
		<chmod dir="${installDir}/bin/${shell.name}" includes="**/*" perm="775"/>
		<!-- /if -->

		<echo>Filling in concrete values in config files</echo>
		<copy file="${installDir}/cgi-bin/${shell.name}/gsdlsite.cfg.in" tofile="${installDir}/cgi-bin/${shell.name}/gsdlsite.cfg"/>
		<rsr file="${installDir}/cgi-bin/${shell.name}/gsdlsite.cfg" pattern="&quot;?\*\*GSDLHOME\*\*&quot;?" replacement="&quot;${installDir.local}&quot;"/>

		<echo>Correcting perl shebangs in perl scripts</echo>
		<!-- if mac -->
		<exec executable="which" outputproperty="perl.executable"><arg value="perl"/></exec>		
		<!-- /if -->		
		<!-- if linux -->
		<property name="perl.executable" value="${installDir.local}/bin/linux/perl/bin/perl"/>
		<!-- /if -->		
		<!-- if windows -->
		<property name="perl.executable" value="${installDir.local}\bin\windows\strawberry-perl\perl\bin\perl.exe"/>
		<!-- /if -->
		
		<rsr file="${installDir}/cgi-bin/${shell.name}/gliserver.pl" pattern="^#!.*" replacement="#!${perl.executable} -w" lines="1"/>
		<rsr file="${installDir}/cgi-bin/${shell.name}/metadata-server.pl" pattern="^#!.*" replacement="#!${perl.executable} -w" lines="1"/>
		<rsr file="${installDir}/cgi-bin/${shell.name}/checksum.pl" pattern="^#!.*" replacement="#!${perl.executable} -w" lines="1"/>
		

		<echo>Creating the english dictionary</echo>
		<copy file="${installDir}/gli/classes/dictionary.properties" tofile="${installDir}/gli/classes/dictionary_en.properties"/>

		<!-- if linux|mac -->
		<echo>Set the installation locale in config files</echo>
		<rsr file="${installDir}/setup.bash" pattern="^gsdllang=.*" replacement="gsdllang=${language}"/>
		<rsr file="${installDir}/gli/gli.sh" pattern="^glilang=.*" replacement="glilang=${language}"/>
		<rsr file="${installDir}/gli/gems.sh" pattern="^glilang=.*" replacement="glilang=${language}"/>
		<!-- /if -->
		<rsr
			file="${installDir}/gli/classes/xml/config.xml"
			pattern="(&lt;Argument name=&quot;general.locale&quot;&gt;).*(&lt;/Argument&gt;)"
			replacement="$1${language}$2"/>

		<echo file="${installDir}/etc/main.cfg" append="true"
			>cgiarg          shortname=l argdefault=${language}</echo>

		<echo message="Creating installation properties file"/>
		<!-- if linux|mac -->
		<echo file="${installDir}/etc/installation.properties">#this file is just a placeholder for now</echo>
		<!-- /if -->
		<!-- if windows -->
		<echo file="${installDir}\etc\installation.properties">installed.startmenu.path:${startmenu.path.unix}</echo>
		<!-- /if -->
		
		<!-- if mac -->
		<copy file="${installDir}/gli.app/Contents/document.wflow.in" tofile="${installDir}/gli.app/Contents/document.wflow"/>
		<rsr file="${installDir}/gli.app/Contents/document.wflow" pattern="\*\*GSDLHOME\*\*" replacement="${installDir}"/>
		
		<copy file="${installDir}/gs2-server.app/Contents/document.wflow.in" tofile="${installDir}/gs2-server.app/Contents/document.wflow"/>
		<rsr file="${installDir}/gs2-server.app/Contents/document.wflow" pattern="\*\*GSDLHOME\*\*" replacement="${installDir}"/>
		
		<copy file="${installDir}/gems.app/Contents/document.wflow.in" tofile="${installDir}/gems.app/Contents/document.wflow"/>
		<rsr file="${installDir}/gems.app/Contents/document.wflow" pattern="\*\*GSDLHOME\*\*" replacement="${installDir}"/>
		
		<copy file="${installDir}/client-gli.app/Contents/document.wflow.in" tofile="${installDir}/client-gli.app/Contents/document.wflow"/>
		<rsr file="${installDir}/client-gli.app/Contents/document.wflow" pattern="\*\*GSDLHOME\*\*" replacement="${installDir}"/>

		<chmod dir="${installDir}" includes="*.app" perm="755"/>
		<chmod dir="${installDir}" includes="*.app/**/*" perm="755"/>
		<!-- /if -->
		
		<echo message="Renaming PDFv2Plugin to make it unfunctional. To make it functional, install the PDFBoxConverter extension for Greenstone and then rename the PDFv2Plugin.tmp file to PDFv2Plugin.pm"/>
		<move file="${installDir}/perllib/plugins/PDFv2Plugin.pm" tofile="${installDir}/perllib/plugins/PDFv2Plugin.tmp"/>
	</target>

	<!-- Apache HTTPD  -->
	<target name="Installing Apache Web Server">
		<echo>Installing the Apache Web Server</echo>

		<!-- start web -->
		<unzip src="${antinstaller.jar}" dest="${basedir}">
			<patternset>
				<include name="apachehttpd.lzma"/>
			</patternset>
		</unzip>

		<!-- if linux|mac -->
		<sevenzip task="decode" input="${basedir}/apachehttpd.lzma" output="${basedir}/apachehttpd.comp"/>
		<!-- /if -->
		<!-- if windows -->
		<exec dir="${basedir}" executable="${basedir}/7za.exe">
			<arg line="x apachehttpd.lzma"/>
		</exec>
		<!-- /if -->
		<unzip src="${basedir}/apachehttpd.comp" dest="${installDir}"/>
		<delete file="apachehttpd.comp"/>
		<!-- end web -->

		<!-- start cdrom -->
		<mkdir dir="${installDir}/apache-httpd"/>
		<mkdir dir="${installDir}/apache-httpd/${shell.name}"/>
		<copy todir="${installDir}/apache-httpd/${shell.name}">
			<fileset dir="${orig.dir}/Software/apache-httpd/${shell.name}"/>
		</copy>
		<!-- end cdrom -->

		<!-- if linux|mac -->
		<chmod dir="${installDir}/apache-httpd/${shell.name}/bin" includes="*" perm="775"/>
		<chmod dir="${installDir}/apache-httpd/${shell.name}" includes="*.sh" perm="775"/>
		<echo>Setting up apache web server</echo>
		<exec dir="${installDir}" executable="${installDir}/apache-httpd/${shell.name}/install-bindist.sh">
			<arg value="${installDir}/apache-httpd/${shell.name}"/>
		</exec>
		<!-- /if -->
	</target>

	<!-- ImageMagick -->
	<target name="Installing ImageMagick">
		<echo>Installing ImageMagick</echo>
		<!-- start web -->
		<unzip src="${antinstaller.jar}" dest="${basedir}"><patternset><include name="imagemagick.lzma"/></patternset></unzip>
		<!-- if linux|mac --><sevenzip task="decode" input="${basedir}/imagemagick.lzma" output="${basedir}/imagemagick.comp"/><!-- /if -->
		<!-- if windows --><exec dir="${basedir}" executable="${basedir}/7za.exe"><arg line="x imagemagick.lzma"/></exec><!-- /if -->
		<delete file="imagemagick.lzma"/>
		<unzip src="${basedir}/imagemagick.comp" dest="${installDir}"/>
		<delete file="imagemagick.comp"/>
		<!-- end web -->

		<!-- start cdrom -->
		<copy todir="${installDir}">
			<fileset dir="${orig.dir}/Software/imagemagick/${rk.os}"/>
		</copy>
		<!-- end cdrom -->

		<if><bool><available file="${installDir}/bin/${shell.name}/imagemagick" type="dir"/></bool>
			<chmod dir="${installDir}/bin/${shell.name}/imagemagick/bin" includes="*" perm="775"/>
		<else>
			<chmod dir="${installDir}/ext/imagemagick/${shell.name}/bin" includes="*" perm="775"/>
		</else>
		</if>

	</target>

	<!-- Ghostscript -->
	<target name="Installing Ghostscript" depends="">
		<echo message="Installing Ghostscript"/>
		<!-- start web -->
		<unzip src="${antinstaller.jar}" dest="${basedir}"><patternset><include name="ghostscript.lzma"/></patternset></unzip>
		<!-- if linux|mac --><sevenzip task="decode" input="${basedir}/ghostscript.lzma" output="${basedir}/ghostscript.comp"/><!-- /if -->
		<!-- if windows --><exec dir="${basedir}" executable="${basedir}/7za.exe"><arg line="x ghostscript.lzma"/></exec><!-- /if -->
		<delete file="ghostscript.lzma"/>
		<unzip src="${basedir}/ghostscript.comp" dest="${installDir}" />
		<delete file="ghostscript.comp"/>
		<!-- end web -->

		<!-- start cdrom -->
		<copy todir="${installDir}">
			<fileset dir="${orig.dir}/Software/ghostscript/${rk.os}"/>
		</copy>
		<!-- end cdrom -->

		<chmod dir="${installDir}/bin/${shell.name}/ghostscript/bin" includes="*" perm="775"/>
	</target>	

	<!-- Start menu shortcuts -->
	<target name="Installing Start Menu Shortcuts">

		<mkdir dir="${startmenu.path}\Documentation"/>

		<!-- Server shortcut -->
		<if><bool><available file="${installDir}/web/images/server.ico"/></bool>
			<property name="servericon" value="${installDir}\web\images\server.ico"/> 
			<else>
				<property name="servericon" value="${installDir}\web\images\serverico.ico"/> 
			</else>
		</if>

		<shortcut
			file="${startmenu.path}\Greenstone Server.lnk"
			execute="${installDir}\gs2-server.bat"
			workingDirectory="${installDir}"
			iconFile="${servericon}"
			iconIndex="0" />

		<!-- Librarian shortcut -->
		<if><bool><available file="${installDir}/web/images/librarian.ico"/></bool>
			<property name="libraryicon" value="${installDir}\web\images\librarian.ico"/> 
			<else>
				<property name="libraryicon" value="${installDir}\web\images\icon.ico"/>
			</else>
		</if>

		<shortcut
			file="${startmenu.path}\Librarian Interface (GLI).lnk"
			execute="${installDir}\gli\gli.bat"
			workingDirectory="${installDir}\gli"
			iconFile="${libraryicon}"
			iconIndex="0" />
			
		<!-- Remote Server shortcut -->
		<if><bool><available file="${installDir}/web/images/clientgli.ico"/></bool>
			<property name="clientgliicon" value="${installDir}\web\images\clientgli.ico"/> 
			<else>
				<property name="clientgliicon" value="${installDir}\web\images\icon.ico"/>
			</else>
		</if>

		<shortcut
			file="${startmenu.path}\Remote Librarian Interface (Client GLI).lnk"
			execute="${installDir}\gli\client-gli.bat"
			workingDirectory="${installDir}\gli"
			iconFile="${clientgliicon}"
			iconIndex="0" />

		<!-- GEMS shortcut -->
		<if><bool><available file="${installDir}/web/images/metadata.ico"/></bool>
			<property name="gemsicon" value="${installDir}\web\images\metadata.ico"/> 		
			<else>
				<property name="gemsicon" value="${installDir}\web\images\gems.ico"/> 	
			</else>
		</if>

		<shortcut
			file="${startmenu.path}\Metadata Set Editor (GEMS).lnk"
			execute="${installDir}\gli\gems.bat"
			workingDirectory="${installDir}\gli"
			iconFile="${gemsicon}"
			iconIndex="0" />

		<!-- Uninstall shortcut -->
		<if><bool><available file="${installDir}/web/images/newuninstall.ico"/></bool>
			<property name="uninstallicon" value="${installDir}\web\images\newuninstall.ico"/> 		
			<else>
				<property name="uninstallicon" value="${installDir}\web\images\uninstall.ico"/> 	
			</else>
		</if>
		
		<shortcut
			file="${startmenu.path}\Uninstall.lnk"
			execute="${installDir}\uninstall\Uninstall.bat"
			workingDirectory="${installDir}\uninstall"
			iconFile="${uninstallicon}"
			iconIndex="0" />

		<!-- Misc shortcuts -->
		<shortcut
			file="${startmenu.path}\Documentation\Greenstone2 Users' Guide.lnk"
			execute="${installDir}\docs\manual\gs2_user_en.pdf" />
		
		<shortcut
			file="${startmenu.path}\Documentation\READMEen.lnk"
			execute="${installDir}\READMEen.txt" />

		<shortcut
			file="${startmenu.path}\Documentation\Greenstone Wiki.url"
			url="http://wiki.greenstone.org" />

		<shortcut
			file="${startmenu.path}\Documentation\Greenstone Website.url"
			url="https://www.greenstone.org" />

	</target>

	<target name="Configuring Administration Pages" if="enable.admin.pages">
		<echo>Enabling Admin Pages</echo>
		<rsr file="${installDir}/etc/main.cfg" pattern="^\s*status\s+.*" replacement="status enabled"/>
		<echo>Creating admin and demo users</echo>
		<adduser txt2db="${installDir}/bin/${shell.name}/txt2db"
			usersDb="${installDir}/etc/users.gdb"
			username="admin"
			password="${admin.password}"
			groups="administrator,colbuilder,all-collections-editor"
			comment="created at install time"/>
		<adduser
			txt2db="${installDir}/bin/${shell.name}/txt2db"
			usersDb="${installDir}/etc/users.gdb"
			username="demo"
			password="demo"
			groups="demo"
			comment="Dummy 'demo' user with password 'demo' for authen-e collection"/>
	</target>

	<target name="Installing Documented Examples">
		<unzip src="${orig.dir}/documented-examples.zip" dest="${installDir}/collect"/>
	</target>

</project>
