<?xml version="1.0"?>
<!--
This is the build.xml run by AntInstaller for the installer
-->
<project name="Installation">
  <!-- if windows -->
  <macrodef name="shortcut">
    <attribute name="file"/>
    <attribute name="execute"/>
    <attribute name="workingDirectory" default=""/>
    <attribute name="iconFile" default=""/>
    <attribute name="iconIndex" default="0"/>

    <sequential>
        <exec executable="cscript.exe">
            <arg value="//NoLogo"/>
            <arg value="${basedir}/create-shortcut.vbs"/>
            <arg value="@{file}"/>
            <arg value="@{execute}"/>
            <arg value="@{workingDirectory}"/>
            <arg value="@{iconFile},@{iconIndex}"/>
        </exec>
    </sequential>
  </macrodef>

  <exec executable="powershell" outputproperty="programs.menu">
    <arg value="-NoProfile"/>
    <arg value="-Command"/>
    <arg value="[Environment]::GetFolderPath('StartMenu') + '\Programs'"/>
  </exec>

  <property name="win32.personal.programs_menu" value="${programs.menu}"/>
  <!-- /if -->
	<!-- this is required to pick up the properties generated during the install pages -->
	<property file="${basedir}/ant.install.properties"/>

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

	<typedef name="rsr" classname="org.greenstone.anttasks.RegexSearchReplace" classpathref="project.classpath"/>	
	<!--<typedef name="adduser" classname="org.greenstone.anttasks.AddGreenstone3UserToDatabase" classpathref="project.classpath"/>-->
	<!-- if linux|mac -->
	<taskdef name="sevenzip" classname="org.apache.tools.ant.SevenZip" classpathref="project.classpath"/>
	<!-- /if -->
	<!-- 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 -->
	
	<!-- 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 -->
	

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

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

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

	<!-- target to copy the bundled jdk into place -->
	<target name="Initialising" if="bundled.java.exists">
		<mkdir dir="${installDir}"/>
		<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>

	<!-- target to load in the messages left by the last installation -->
	<target name="Looking For Previous Installation" if="is.overinstall">
		<property file="${installDir}\installation.properties"/>
		<delete dir="${installed.startmenu.path}" failonerror="false"/>
	</target>

	<target name="Installing Core System">

		<!-- rip out build.xml to the temp dir so we can fake and <antcall> with <ant> -->
		<unzip src="${antinstaller.jar}" dest="${basedir}"><patternset><include name="build.xml"/></patternset></unzip>	


		<!-- if windows -->
		<echo>Figuring out the start menu path</echo>
		<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"/>

		<echo>Extracting 7za tool</echo>
		<unzip src="${antinstaller.jar}" dest="${basedir}">
			<patternset><include name="7za.exe"/></patternset>
		</unzip>
		<echo>Extracting create-shortcut script</echo>
		<unzip src="${antinstaller.jar}" dest="${basedir}">
		  <patternset><include name="create-shortcut.vbs"/></patternset>
		</unzip>
		<!-- /if -->

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

		<echo>Setting binaries to executable</echo>
		<chmod dir="${installDir}" includes="*.sh" perm="775"/>
		<chmod dir="${installDir}/bin/script" includes="*" perm="775"/>
		<chmod dir="${installDir}/bin" includes="*" perm="775"/>
		<chmod dir="${installDir}/ext/solr9/bin/script" includes="*" perm="775"/>
		<chmod file="${installDir}/ext/solr9/solr/bin/solr" perm="775"/>
		<chmod dir="${installDir}/web/WEB-INF/cgi/" includes="*.pl" perm="775"/>
		<!-- if linux -->
		<chmod dir="${installDir}/gs2build/bin/linux" includes="*" perm="775"/>
		<!-- No longer grabbing prebuilt 32 bit wvWare. We now use the one release-kit was already locally building,
		     now that the libpng dependency is resolved. So 64 bit wvWare binary on 64 bit linux and 32 bit on 32 bit linux. -->
		<!--<chmod dir="${installDir}/gs2build/bin/linux/wv/bin" includes="*" perm="775"/>-->
		<chmod dir="${installDir}/gs2build/bin/linux/xpdf-tools/bin" includes="*" perm="775"/>
		<chmod dir="${installDir}/gs2build/bin/linux/perl/bin" includes="*" perm="775"/>
		<chmod dir="${installDir}/bin/linux" includes="*" perm="775"/>
		<chmod dir="${installDir}/bin/linux/openssl/bin" includes="*" perm="775"/>
		<!-- /if -->
		<!-- if mac -->
		<chmod dir="${installDir}/gs2build/bin/darwin" includes="**/*" excludes="ghostscript/** imagemagick/**" perm="775"/>
		<chmod dir="${installDir}/bin/darwin" includes="*" perm="775"/>
		<chmod dir="${installDir}/bin/darwin/openssl/bin" includes="*" perm="775"/>
		<!-- /if -->
		<chmod dir="${installDir}/gs2build/bin/script" includes="**/*" perm="775"/>
		<!-- GLI START -->
		<chmod dir="${installDir}/gli" includes="*.sh" perm="775"/>
		<!-- GLI END -->

		<chmod file="${installDir}/packages/ant/bin/ant" perm="775"/>
		<!--
		source is not included for now
		<chmod dir="${installDir}/gs2build" includes="**/configure" perm="775"/>
		<chmod dir="${installDir}/gs2build/common-src/packages/gdbm/gdbm-1.8.3" includes="mkinstalldirs" perm="775"/>
		<chmod file="${installDir}/src/packages/javagdbm/configure" perm="775"/>
		-->
		

		<echo>Correcting perl shebangs in perl scripts</echo>
		<ant dir="${installDir}" antfile="build.xml" target="set-perl-shebangs" inheritAll="false">
			<!-- if windows -->
			<property name="perl.path" value="${installDir}\gs2build\bin\windows\strawberry-perl\perl\bin\"/>
			<!-- /if -->
			<!-- if linux -->
			<property name="perl.path" value="${installDir}/gs2build/bin/linux/perl/bin"/>
			<!-- /if -->
			<!-- if mac -->
			<!--<property name="perl.path" value="${installDir}/gs2build/bin/darwin/perl/bin"/>-->
			<!-- /if -->
		</ant>


		<echo>Setting tomcat ports</echo>		
		<property name="tomcat.server" value="localhost"/>			
		<property name="tomcat.port" value="8383"/>		
		<property name="tomcat.shutdown.port" value="8305"/>
		<rsr file="${installDir}/build.properties">
			<job pattern="^(tomcat\.server[=:]).*" replacement="$1${tomcat.server}" />
			<job pattern="^(tomcat\.port[=:]).*" replacement="$1${tomcat.port}" />
			<job pattern="^(tomcat\.shutdown.port[=:]).*" replacement="$1${tomcat.shutdown.port}" />
		</rsr>

		<echo>Setting up global properties</echo>
		<copy file="${installDir}/resources/web/global.properties.in" tofile="${installDir}/web/WEB-INF/classes/global.properties" overwrite="true"/>
		<rsr file="${installDir}/web/WEB-INF/classes/global.properties">
			<job pattern="^(gsdl3\.home[:=]).*" replacement="$1${installDir.unix}/web" />
			<job pattern="^(tomcat.server[:=]).*" replacement="$1${tomcat.server}" />
			<job pattern="^(tomcat\.port[:=]).*" replacement="$1${tomcat.port}" />
		</rsr>

		<echo>Setting up log4j properties</echo>
		<copy file="${installDir}/resources/web/log4j.properties.in" tofile="${installDir}/web/WEB-INF/classes/log4j.properties" overwrite="true"/>
		<rsr file="${installDir}/web/WEB-INF/classes/log4j.properties" pattern="@gsdl3home@" replacement="${installDir.unix}/web" />

		<!-- if windows -->
		<echo message="Setting GSDLHOME in gs2build\setup.bat"/>
		<!-- No quotes around GSDLHOME filepath in setup.bat. since the set operations of batchfiles don't want spaces in their assignments, and where quotes are needed in setup.bat, setup.bat handles this itself -->
		<rsr file="${installDir}/gs2build/setup.bat" pattern="&quot;?\*\*GSDLHOME\*\*&quot;?" replacement="${installDir.local}\gs2build" />
		<!-- /if -->

		<!-- if windows|linux -->
		<echo>Deleting mac shortcuts</echo>
		
		<!-- from now on, we have 64 bit .app as well as 32 bit .app files in their own separate folders.
		  One or more of these folders or .app files won't exist, so don't fail on error if they can't be deleted for that reason -->
		<delete failonerror="false" dir="${installDir}/MacShortcuts64bit"/>
		<delete failonerror="false" dir="${installDir}/MacShortcuts32bit"/>
		<!-- /if -->		
		
		<!-- if mac -->
		<echo>Installing Mac Shortcuts</echo>
		<!-- convert template document.wflow.in files inside the .app mac shortcuts into their live files -->
	  	<ant dir="${installDir}" antfile="build.xml" target="gen-mac-shortcuts" inheritAll="false" />

		<!-- need to give executable permissions to the Mac shortcuts -->
		<chmod file="${installDir}/gs3-server.app/Contents/MacOS/gs2-server" perm="775"/><!--called gs2-server, since the gs3-server.app is a copy of the one generated for gs2-->
		<chmod file="${installDir}/gli.app/Contents/MacOS/gli" perm="775"/>
		<chmod file="${installDir}/client-gli.app/Contents/MacOS/client-gli" perm="775"/>
		<chmod file="${installDir}/gems.app/Contents/MacOS/gems" perm="775"/>

		<!-- From Catalina (Darwin kernel version 19.x onwards) the executables are called: Application Stub -->		
		<chmod file="${installDir}/gs3-server.app/Contents/MacOS/Automator Application Stub" perm="775"/>
		<chmod file="${installDir}/gli.app/Contents/MacOS/Application Stub" perm="775"/>
		<chmod file="${installDir}/client-gli.app/Contents/MacOS/Application Stub" perm="775"/>
		<chmod file="${installDir}/gems.app/Contents/MacOS/Application Stub" perm="775"/>

		<!--<chmod dir="${installDir}" includes="*.app" perm="755"/>
		<chmod dir="${installDir}" includes="*.app/**/*" perm="755"/>-->

		<!-- can get rid of the shortcuts folders -->
		<delete failonerror="false" dir="${installDir}/MacShortcuts64bit"/>
		<delete failonerror="false" dir="${installDir}/MacShortcuts32bit"/>
		<!-- /if -->

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

		<echo>Creating usage.txt</echo>
		<echo file="${installDir}/gs2build/etc/usage.txt"></echo>

		<echo>Creating installation properties file</echo>
		<echo file="${installDir}\installation.properties">installed.startmenu.path:${startmenu.path.unix}</echo>

		<echo>Finished</echo>

	</target>

	<!-- Dummy target to redirect display of subtargets resulting from ant calls to the gen-mac-shortcuts
	     and set-perl-shebangs to this dummy target instead of to the Installing ImageMagick target below -->
	<target name="Adjusting Permissions" />
	
	<target name="Installing ImageMagick">

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

		<echo>Setting binaries to executable</echo>
		<!-- if linux -->
		<chmod dir="${installDir}/gs2build/bin/linux/imagemagick/bin" includes="*" perm="775"/>
		<!-- /if -->
		<!-- if mac -->
		<chmod dir="${installDir}/gs2build/bin/darwin/imagemagick/bin" includes="*" perm="775"/>
		<!-- /if -->

	</target>

	<!-- NO FLAX START -->

	<target name="Installing Ghostscript">

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

		<echo>Setting binaries to executable</echo>
		<!-- if linux -->
		<chmod dir="${installDir}/gs2build/bin/linux/ghostscript/bin" includes="*" perm="775"/>
		<!-- /if -->
		<!-- if mac -->
		<chmod dir="${installDir}/gs2build/bin/darwin/ghostscript/bin" includes="*" perm="775"/>
		<!-- /if -->

	</target>

	<!-- NO FLAX END -->

	<!-- start amp -->

	<target name="Installing AMP">
	  <echo>Installing Apache, MySQL and PHP</echo>
	  <unzip src="${antinstaller.jar}" dest="${basedir}"><patternset><include name="amp.lzma"/></patternset></unzip>
	  <!-- if windows --><exec dir="${basedir}" executable="${basedir}/7za.exe"><arg line="x amp.lzma"/></exec><!-- /if -->
	  <!-- if linux|mac --><sevenzip task="decode" input="${basedir}/amp.lzma" output="${basedir}/amp.comp"/><!-- /if -->
	  <delete file="amp.lzma"/>
	  <unzip src="${basedir}/amp.comp" dest="${installDir}/ext/amp"/>
	  <delete file="amp.comp"/>
	  
	  <!-- if linux|mac -->
	  <echo>Setting binaries to executable</echo>
	  <chmod dir="${installDir}/ext/amp" includes="*" perm="775"/>
	  <!-- /if -->
	</target>

	<!-- end amp -->

	<target name="Installing Tomcat">

		<echo>Installing Tomcat (packages/tomcat)</echo>
		<mkdir dir="packages"/>
		<unzip src="${antinstaller.jar}" dest="${basedir}"><patternset><include name="tomcat.lzma"/></patternset></unzip>
		<!-- if windows --><exec dir="${basedir}" executable="${basedir}/7za.exe"><arg line="x tomcat.lzma"/></exec><!-- /if -->
		<!-- if linux|mac --><sevenzip task="decode" input="${basedir}/tomcat.lzma" output="${basedir}/tomcat.comp"/><!-- /if -->
		<delete file="tomcat.lzma"/>
		<unzip src="${basedir}/tomcat.comp" dest="${installDir}"/>
		<delete file="tomcat.comp"/>


		<echo>Changing tomcat ports tomcat's server.xml</echo>
		<copy file="${installDir}/resources/tomcat/server_tomcat8.xml.in" tofile="${installDir}/packages/tomcat/conf/server.xml" overwrite="true"/>
		<rsr file="${installDir}/packages/tomcat/conf/server.xml">
			<job pattern="@port@" replacement="${tomcat.port}"/>
			<job pattern="@shutdown-port@" replacement="${tomcat.shutdown.port}"/>
		</rsr>

		<echo>Copying greenstone3.xml to tomcat directory</echo>
		<copy file="${installDir}/resources/tomcat/greenstone3.xml.in" tofile="${installDir}/packages/tomcat/conf/Catalina/localhost/greenstone3.xml" overwrite="true"/>
		<rsr file="${installDir}/packages/tomcat/conf/Catalina/localhost/greenstone3.xml" pattern="@gsdl3webhome@" replacement="${installDir.local}${file.separator}web" />
		<rsr file="${installDir}/packages/tomcat/conf/Catalina/localhost/greenstone3.xml" pattern="@privilegedattribute@" replacement="privileged='true'" />

		<!-- FLAX START -->
		<rsr file="${installDir}/packages/tomcat/conf/Catalina/localhost/greenstone3.xml" pattern="localsite" replacement="flax" />
		<!-- FLAX END -->

		<echo>Set binaries to executable</echo>
		<chmod dir="${installDir}/packages/tomcat/bin" includes="*.sh" perm="775"/>

	</target>

	<target name="Installing Start Menu Shortcuts">

		<echo>Installing Start Menu Shortcuts</echo>

		<!-- NO FLAX START -->

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

		<shortcut
			file="${startmenu.path}\Greenstone3 Server.lnk"
			execute="${installDir}\gs3-server.bat"
			workingDirectory="${installDir}"
			iconFile="${installDir}\resources\images\server.ico"
			iconIndex="0" />

		<shortcut
			file="${startmenu.path}\Greenstone Librarian Interface (GLI).lnk"
			execute="${installDir}\gli\gli.bat"
			workingDirectory="${installDir}\gli"
			iconFile="${installDir}\resources\images\librarian.ico"
			iconIndex="0" />

		<shortcut
			file="${startmenu.path}\Remote Greenstone Librarian Interface (Client GLI).lnk"
			execute="${installDir}\gli\client-gli.bat"
			workingDirectory="${installDir}\gli"
			iconFile="${installDir}\resources\images\clientgli.ico"
			iconIndex="0" />
			
		<shortcut
			file="${startmenu.path}\Greenstone Editor for Metadata Sets (GEMS).lnk"
			execute="${installDir}\gli\gems.bat"
			workingDirectory="${installDir}\gli"
			iconFile="${installDir}\resources\images\metadata.ico"
			iconIndex="0" />

		<shortcut
			file="${startmenu.path}\Uninstall.lnk"
			execute="${installDir}\uninstall\Uninstall.bat"
			workingDirectory="${installDir}"
			iconFile="${installDir}\resources\images\uninstall.ico"
			iconIndex="0" />
	
		<shortcut
			file="${startmenu.path}\Documentation\README.lnk"
			execute="${installDir}\README.txt" />
		
		<shortcut
			file="${startmenu.path}\Documentation\Greenstone3 Users' Guide.lnk"
			execute="${installDir}\docs\manual\manual.pdf" />
		
		<shortcut
			file="${startmenu.path}\Documentation\Greenstone Wiki.lnk"
			execute="http://wiki.greenstone.org" />

		<shortcut
			file="${startmenu.path}\Documentation\Greenstone Website.lnk"
			execute="https://www.greenstone.org" />
		
		<!-- NO FLAX END -->

		<!-- FLAX START -->

		<shortcut
			file="${startmenu.path}\Start FLAX Server.lnk"
			execute="${installDir}\gs3-server.bat"
			workingDirectory="${installDir}"
			iconFile="${installDir}\resources\images\server.ico"
			iconIndex="0" />

		<shortcut
			file="${startmenu.path}\Uninstall.lnk"
			execute="${installDir}\uninstall\Uninstall.bat"
			workingDirectory="${installDir}"
			iconFile="${installDir}\resources\images\uninstall.ico"
			iconIndex="0" />

		<!-- FLAX END -->
	</target>
	
	<target name="Configuring Administration Pages" if="set.admin.pwd">
		<echo>Enabling Admin Pages</echo>
		<rsr file="${installDir}/web/sites/localsite/siteConfig.xml" pattern="&lt;!--&lt;serviceRack name='Authentication'/&gt;--&gt;" replacement="&lt;serviceRack name='Authentication'/&gt;"/>
		<echo>Creating admin user</echo>
		<!--<echo>Creating admin and demo users</echo>-->
		<ant dir="${installDir}" antfile="build.xml" target="config-admin" inheritAll="false">
		  <property name="admin.password" value="${admin.password}"/>		  
		</ant>
		<!--<adduser txt2db="org.greenstone.gsdl3.util.ChangePwdUsersDB"
			usersDb="${installDir}/web/sites/localsite/etc/usersDB"
			username="admin"
			password="${admin.password}"
			groups="administrator,colbuilder,all-collections-editor"
			comment="created at install time"
			email=""/>
		<adduser
			txt2db="org.greenstone.gsdl3.util.ChangePwdUsersDB"
			usersDb="${installDir}/web/sites/localsite/etc/usersDB"
			username="demo"
			password="demo"
			groups="demo"
			comment="Dummy 'demo' user with password 'demo' for authen-e collection"
			email=""/>-->
	</target>
	
	<!-- util target to extract the given component -->
	<!-- pass in ${component.name} as a param -->
	<!-- unused at this time as causes display errors in installer gui!
	<target name="extract-component">
		<!- - extract the component into place - ->
		<unzip src="${antinstaller.jar}" dest="${basedir}"><patternset><include name="${component.name}.lzma"/></patternset></unzip>
		<!- - if windows - ->
		<exec dir="${basedir}" executable="${basedir}/7za.exe"><arg line="x ${component.name}.lzma"/></exec>
		<!- - /if - ->
		<!- - if linux|mac - ->
		<sevenzip task="decode" input="${basedir}/${component.name}.lzma" output="${basedir}/${component.name}.comp"/>
		<!- - /if - ->
		<delete file="${component.name}.lzma"/>
		<unzip src="${basedir}/${component.name}.comp" dest="${installDir}"/>
		<delete file="${component.name}.comp"/>
	</target>
	-->

</project>
