<?xml version="1.0"?>

<!-- ====================================================================== 
     July 2006                                                   

     Greenstone3 building build and install script
              
     So far I have just moved the gs3build related targets out of main 
     greenstone 3 build.xml and put them in here. It needs tidying up to 
     make it useful.     
     kjdon                                                                
     ====================================================================== -->
<project name="gs3build" default="usage" basedir=".">

  <!-- ============= Base dirs for each package and component ============ -->
  <property name="mysql.home" value="${packages.home}/mysql"/>
  
  <!-- can be set in build.properties if need be -->
  <property name="mysql.datadir" value="./data"/>

<!--  ==================== Compilation Control Options ==================== -->

<!--

  These properties control option settings on the Javac compiler when it
  is invoked using the <javac> task.

  compile.debug        Should compilation include the debug option?

  compile.deprecation  Should compilation include the deprecation option?

  compile.optimize     Should compilation include the optimize option?

-->

  <property name="compile.debug"       value="true"/>
  <property name="compile.deprecation" value="true"/>
  <property name="compile.optimize"    value="true"/>


<target name="prepare" depends="prepare-mysql"/>

  <!-- this sets up some initial properties -->
  <target name="init">
    <condition property="java.too.old">
      <or>
	<equals arg1="1.1" arg2="${ant.java.version}"/>
	<equals arg1="1.2" arg2="${ant.java.version}"/>
	<equals arg1="1.3" arg2="${ant.java.version}"/>
      </or>
    </condition>
    <fail if="java.too.old" message="You need Java 1.4 or greater to run Greenstone 3"/>
    <available file="${packages.home}/mysql/bin" property="mysql.present"/>
    <condition property="mysql.islocal">
      <or>
	<not>
	  <isset property="mysql.installed.path"/>
	</not>
	<equals arg1="" arg2="${mysql.installed.path}"/>
      </or>
    </condition>
  </target>

  <target name="prepare-collections" depends="init">
    <property name="collect.dir" value="${web.home}/sites/localsite/collect"/>
    <!-- gs3mgdemo -->
    <unzip src="${collect.dir}/gs3mgdemo/import.zip" 
      dest="${collect.dir}/gs3mgdemo"/>
    <unzip src="${collect.dir}/gs3mgdemo/archives.zip" 
      dest="${collect.dir}/gs3mgdemo"/>
    <unzip src="${collect.dir}/gs3mgdemo/index/index.zip" 
      dest="${collect.dir}/gs3mgdemo/index"/>
    <delete file="${collect.dir}/gs3mgdemo/import.zip"/>
    <delete file="${collect.dir}/gs3mgdemo/archives.zip"/>
    <delete file="${collect.dir}/gs3mgdemo/index/index.zip"/>
  </target>

  <target name="configure-web" depends="init,get-mysql-reader-password,get-mysql-admin-password"

        <!-- just in case these haven't been defined, we need something to write to
    the file -->
    <property name="mysql.admin.password" value=" "/>
    <property name="mysql.reader.password" value=" "/>
    <filter token="mysql.port" value="${mysql.port}"/>
    <filter token="mysql.server" value="${mysql.server}"/>
    <filter token="mysql.admin.pword" value="${mysql.admin.password}"/>
    <filter token="mysql.reader.pword" value="${mysql.reader.password}"/>
  </target>

<!-- ======================= Mysql Targets =========================== -->

  <!-- this is one target that we only want to run once. -->
  <target name="prepare-mysql" depends="init,init-mysql-vars,install-mysql,start-mysql,setup-mysql,stop-mysql-no-password-force"/>

  <target name="init-mysql-vars">
    <condition property="mysql.islocal.linux">
      <and>
	<isset property="mysql.islocal"/>
	<isset property="current.os.isunixnotmac"/>
      </and>
    </condition>
    <condition property="mysql.islocal.mac">
      <and>
	<isset property="mysql.islocal"/>
	<isset property="current.os.ismac"/>
      </and>
    </condition>	   
    <condition property="mysql.islocal.windows">
      <and>
	<isset property="mysql.islocal"/>
	<isset property="current.os.iswindows"/>
      </and>
    </condition>
    <condition property="mysql.islocal.usepassword">
      <and>
	<isset property="mysql.islocal"/>
	<isset property="mysql.use.passwords"/>
	<equals arg1="yes" arg2="${mysql.use.passwords}"/>
      </and>
    </condition>
    <condition property="mysql.islocal.usepassword.windows">
      <and>
	<isset property="mysql.islocal.usepassword"/>
	<isset property="current.os.iswindows"/>
      </and>
    </condition>
     <condition property="mysql.islocal.usepassword.unix">
      <and>
	<isset property="mysql.islocal.usepassword"/>
	<isset property="current.os.isunix"/>
      </and>
    </condition>
   <condition property="mysql.islocal.nopassword">
      <and>
	<isset property="mysql.islocal"/>
	<or>
	  <not>
	    <isset property="mysql.use.passwords"/>
	  </not>
	  <equals arg1="no" arg2="${mysql.use.passwords}"/>
	</or>
      </and>
    </condition>
   </target>
 
  <target name="configure-mysql" depends="init,init-mysql-vars,start-mysql,set-mysql-account-passwords,stop-mysql" description="sets up the mysql account passwords"/>
  
  <target name="get-mysql-root-password" depends="init,init-mysql-vars" if="mysql.islocal.usepassword" unless="mysql.root.password">
    <echo>Enter password in the Java popup. (If you do not have a window server running, please set the mysql.root.password in the build.properties file.)</echo>
    <getuserandpassword message="Please specify a password for the root mysql user: this is to secure your database."  username="root" pwordproperty="mysql.root.password"/>
  </target>
  
  <target name="get-mysql-root-password-windows" depends="init,init-mysql-vars" if="mysql.islocal.usepassword.windows" unless="mysql.root.password">
    <echo>(Enter password in the Java popup)</echo>
    <getuserandpassword message="Please enter the mysql root password"  username="root" pwordproperty="mysql.root.password"/>
  </target>
  
  <target name="get-mysql-reader-password" depends="init,init-mysql-vars" if="mysql.islocal.usepassword" unless="mysql.reader.password" >
    <echo>Enter password in the Java popup. (If you do not have a window server running, please set the mysql.reader.password in the build.properties file.)</echo>
    <getuserandpassword message="Please specify the password for the gsdl3reader mysql user: this is used by greenstone" username="gsdl3reader" pwordproperty="mysql.reader.password"/>
  </target>
  
  <target name="get-mysql-admin-password" depends="init,init-mysql-vars" if="mysql.islocal.usepassword" unless="mysql.admin.password" >
    <echo>Enter password in the Java popup. (If you do not have a window server running, please set the mysql.admin.password in the build.properties file.)</echo>
    <getuserandpassword message="Please specify the password for the gsdl3admin mysql user: this is used by greenstone" username="gsdl3admin" pwordproperty="mysql.admin.password"/>
  </target>

 <target name="install-mysql" depends="init,init-mysql-vars,install-mysql-linux,install-mysql-windows,install-mysql-mac"/>
  
  <!-- install the database : linux-->
  <target name="install-mysql-linux" depends="init,init-mysql-vars" if="mysql.islocal.linux" unless="mysql.present">
    <get src="http://www.greenstone.org/gs3files/mysql-standard-4.1.15-pc-linux-gnu-i686.tar.gz"
      dest="${packages.home}/mysql-standard-4.1.15-pc-linux-gnu-i686.tar.gz"
      usetimestamp="true"/>
    <untar src="${packages.home}/mysql-standard-4.1.15-pc-linux-gnu-i686.tar.gz"
      dest="${packages.home}"
      compression="gzip"/>
    <move todir="${packages.home}/mysql">
      <fileset dir="${packages.home}/mysql-standard-4.1.15-pc-linux-gnu-i686"/>
    </move>
    <!-- file permissions are screwed up, so make executables executable -->
    <chmod file="${mysql.home}/scripts/mysql_install_db" perm="a+x"/>
    <chmod perm="a+x">
      <fileset dir="${mysql.home}/bin" />
    </chmod>
    <mkdir dir="${mysql.home}/var/log/"/>
    <chmod file="${mysql.home}/var/" perm="777" type="dir"/>
    <chmod file="${mysql.home}/var/log/" perm="777" type="dir"/>
    
    <exec executable="${mysql.home}/scripts/mysql_install_db" dir="${mysql.home}">
      <arg value="--datadir=${mysql.datadir}"/>
      <arg value="--basedir=."/>
      <arg value="--user=root"/>
      <arg value="--force"/>
    </exec>
  </target>

  <!-- install the database : mac-->
  <target name="install-mysql-mac" depends="init,init-mysql-vars" if="mysql.islocal.mac" unless="mysql.present">
    <get src="http://www.greenstone.org/gs3files/mysql-standard-4.1.15-apple-darwin7.9.0-powerpc.tar.gz"
      dest="${packages.home}/mysql-standard-4.1.15-apple-darwin7.9.0-powerpc.tar.gz"
      usetimestamp="true"/>
    <untar src="${packages.home}/mysql-standard-4.1.15-apple-darwin7.9.0-powerpc.tar.gz"
      dest="${packages.home}"
      compression="gzip"/>
    <move todir="${packages.home}/mysql">
      <fileset dir="${packages.home}/mysql-standard-4.1.15-apple-darwin7.9.0-powerpc"/>
    </move>    
    <!-- file permissions are screwed up, so make executables executable -->
    <chmod file="${mysql.home}/scripts/mysql_install_db" perm="a+x"/>
    <chmod perm="a+x">
      <fileset dir="${mysql.home}/bin" />
    </chmod>
    <mkdir dir="${mysql.home}/var/log/"/>
    <chmod file="${mysql.home}/var/" perm="777" type="dir"/>
    <chmod file="${mysql.home}/var/log/" perm="777" type="dir"/>
    <exec executable="${mysql.home}/scripts/mysql_install_db" dir="${mysql.home}">
      <arg value="--datadir=${mysql.datadir}"/>
      <arg value="--basedir=."/>
      <!--<arg value="- -user=root"/>
      <arg value="- -force"/>-->
    </exec>
  </target>

  <!-- install the database : windows -->
  <target name="install-mysql-windows" depends="init,init-mysql-vars" if="mysql.islocal.windows" unless="mysql.present">
    <get src="http://www.greenstone.org/gs3files/mysql-noinstall-4.1.15-win32.zip"
      dest="${packages.home}/mysql-noinstall-4.1.15-win32.zip"
      usetimestamp="true"/>
    <!-- can we run the installer?? -->
    <unzip src="${packages.home}/mysql-noinstall-4.1.15-win32.zip"
      dest="${packages.home}"/>
    <move todir="${packages.home}/mysql">
      <fileset dir="${packages.home}/mysql-4.1.15-win32"/>
    </move>
  </target>

  <target name="setup-mysql" depends="init" if="mysql.islocal">
    <!-- setup the gsdl3 accounts -->
    <exec executable="${mysql.home}/bin/mysql" dir="${mysql.home}">
      <arg value="--user=root"/>
      <arg value="--execute=GRANT SELECT,INSERT,DELETE,UPDATE,DROP,CREATE ON *.* TO gsdl3admin@localhost"/>
    </exec>
    <exec executable="${mysql.home}/bin/mysql" dir="${mysql.home}">
      <arg value="--user=root"/>
      <arg value="--execute=GRANT SELECT ON *.* TO gsdl3reader@localhost"/>
    </exec>
    <!--<exec executable="${mysql.home}/bin/mysql" dir="${mysql.home}">
      <arg value="- -user=root"/>
      <arg value="- -execute=GRANT SELECT,INSERT,DELETE,UPDATE,DROP,CREATE ON *.* TO gsdl3admin@'%';"/>
    </exec>-->

    <!-- remove the anonymous user -->
    <exec executable="${mysql.home}/bin/mysql" dir="${mysql.home}">
      <arg value="--user=root"/>
      <arg value="--execute=delete from mysql.user where Host='localhost' and User='';"/>
    </exec>
    <!-- load in the demo collection database -->
    <exec executable="${mysql.home}/bin/mysql" dir="${mysql.home}">
      <arg value="--user=root"/>
      <arg value="--execute=create database localsite_gs3mgdemo;"/>
    </exec>
    <exec executable="${mysql.home}/bin/mysql" dir="${mysql.home}"
      input="${web.home}/sites/localsite/collect/gs3mgdemo/mysqldatadump.sql">
      <arg value="--user=root"/>
      <arg value="localsite_gs3mgdemo"/>
    </exec>
  </target>

  <target name="set-mysql-account-passwords" depends="init,init-mysql-vars,get-mysql-root-password,get-mysql-reader-password,get-mysql-admin-password" if="mysql.islocal.usepassword">
    <exec executable="${mysql.home}/bin/mysql" dir="${mysql.home}">
      <arg value="--user=root"/>
      <arg value="--execute=set password for 'root'@'localhost' = PASSWORD('${mysql.root.password}'); set password for 'gsdl3reader'@'localhost' = PASSWORD('${mysql.reader.password}'); set password for 'gsdl3admin'@'localhost' = PASSWORD('${mysql.admin.password}');  flush privileges;"/>
    </exec>    
  </target>

  <target name="start-mysql" depends="init,init-mysql-vars" if="mysql.islocal"
    description="Startup only mysql">
    <echo>Starting up the mysql server</echo>
    <exec executable="${mysql.home}/bin/mysqld_safe" dir="${mysql.home}"
      spawn="true" os="${os.linux},${os.mac}">
      <arg value="--datadir=${mysql.datadir}"/>
      <arg value="--basedir=."/>
      <arg value="--pid_file=gsdl3.pid"/>
      <arg value="--socket=/tmp/mysql.sock"/>
      <arg value="--port=${mysql.port}"/>
      <arg value="--err-log=./var/log/mysql.log"/>
    </exec>
   <exec executable="${mysql.home}/bin/mysqld" dir="${mysql.home}" spawn="true" os="${os.windows}">
      <arg value="--port=${mysql.port}"/>   
    </exec>
    <sleep seconds="2"/>
  </target>

  <target name="stop-mysql" depends="init,init-mysql-vars,stop-mysql-use-password-unix,stop-mysql-use-password-windows,stop-mysql-no-password" if="mysql.islocal"
    description="Shutdown only mysql">
    <echo>MYSQL database server shutdown successfully</echo>
  </target>
  
  <target name="stop-mysql-no-password" depends="init" if="mysql.islocal.nopassword">
    <exec executable="${mysql.home}/bin/mysqladmin" dir="${mysql.home}"
      failonerror="true" os="${os.linux},${os.mac}">
      <arg value="--user=root"/>
      <arg value="--socket=/tmp/mysql.sock"/>
      <arg value="shutdown"/>
    </exec>
    <exec executable="${mysql.home}/bin/mysqladmin" dir="${mysql.home}"
      failonerror="true" os="${os.windows}">
      <arg value="--user=root"/>
      <arg value="shutdown"/>
    </exec>
  </target>
  
  <target name="stop-mysql-no-password-force" depends="init" if="mysql.islocal">
    <exec executable="${mysql.home}/bin/mysqladmin" dir="${mysql.home}"
      failonerror="true" os="${os.linux},${os.mac}">
      <arg value="--user=root"/>
      <arg value="--socket=/tmp/mysql.sock"/>
      <arg value="shutdown"/>
    </exec>
    <exec executable="${mysql.home}/bin/mysqladmin" dir="${mysql.home}"
      failonerror="true" os="${os.windows}">
      <arg value="--user=root"/>
      <arg value="shutdown"/>
    </exec>
  </target>
  
  <target name="stop-mysql-use-password-unix" depends="init" if="mysql.islocal.usepassword.unix">
    <echo>At the password prompt, enter the mysql root password.</echo>
    <exec executable="${mysql.home}/bin/mysqladmin" dir="${mysql.home}"
      failonerror="true" os="${os.linux},${os.mac}">
      <arg value="--user=root"/>
      <arg value="-p"/>
      <arg value="--socket=/tmp/mysql.sock"/>
      <arg value="shutdown"/>
    </exec>
  </target>
  
  <target name="stop-mysql-use-password-windows" depends="init,get-mysql-root-password-windows" if="mysql.islocal.usepassword.windows">
    <!-- on windows it doesn't work unless the password is specified here -->
    <exec executable="${mysql.home}/bin/mysqladmin" dir="${mysql.home}"
      failonerror="true" os="${os.windows}">
      <arg value="--user=root"/>
      <arg value="-p${mysql.root.password}"/>
      <arg value="shutdown"/>
    </exec>
  </target>

  <target name="restart-mysql" description="Shutdown and restart only mysql" depends="init,stop-mysql,start-mysql"/>

  <target name="prepare-for-dist-2" depends="init">
    <antcall target="prepare-mysql"/>
    <delete file="${packages.home}/mysql-standard-4.1.15-pc-linux-gnu-i686.tar.gz"/>
    <delete file="${packages.home}/mysql-standard-4.1.15-apple-darwin7.9.0-powerpc.tar.gz"/>
    <delete file="${packages.home}/mysql-noinstall-4.1.15-win32.zip"/>
    <delete dir="${packages.home}/mysql/sql-bench"/>
    <delete dir="${packages.home}/mysql/tests"/>
    <delete dir="${packages.home}/mysql/mysql-test"/>
    <delete file="${packages.home}/mysql/var/log/mysql.log"/>
  </target>
