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

  <!-- postinstall.xml is shared by both the rk3 installer (build.xml and 
       antinstall-config.xml files) and the zip binary installer, zipinstall.xml. -->
  
    <!-- 
       Note that comments containing just GLI START, GLI END, FLAX START, FLAX END,
       if windows|linux|mac and /if are bookends for release-kit "macros" (originally
       used in the installer/build.xml and antinstall-config.xml).
       These rk macros control if embedded lines are stay in this file or get removed.
       
       shared/core/ant-scripts/create-installer.xml and shared.xml (for zip installation)
       are the files instructing the release-kit to process (retain/remove) these
       and other macros like start/end cdrom and start/end amp.
       We're not handling the start/end amp macros here. But installer/build.xml and
       installer/antinstall-config.xml do process start/end amp macros. -->

    <!-- 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"/>

    <!-- Properties that will be reused in later targets -->
    <property name="tomcat.server" value="localhost"/>			
	<property name="tomcat.port" value="8383"/>		
	<property name="tomcat.shutdown.port" value="8305"/>
    
  <target name="postinstall-core">
		<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/perl/bin" includes="*" perm="775"/>
	    <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>		
		<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>

  <target name="postinstall-imagemagick">
    <echo>Setting imagemagick 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>

  <target name="postinstall-ghostscript">
    <echo>Setting ghostscript 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>

  <target name="postinstall-tomcat">
    <echo>Changing tomcat ports tomcat's server.xml</echo>
	<copy file="${installDir}/resources/tomcat/server10.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>

  
</project>
