<?xml version="1.0" encoding="utf-8" ?>
<!--
    .......................................................... 
    March 2009
    CD Release Kit for Greenstone2 (cdrk2)
    Oran Fry
    ..........................................................
  -->

<project name="cdrk2-build" default="cdrk2">

	<!-- IMPORT OTHER ANT SCRIPTS -->
	<import file="${rk.home}/shared/core/ant-scripts/shared.xml"/>
	<import file="${rk.home}/shared/greenstone2/ant-scripts/greenstone2-shared.xml"/>

	<!-- THE MAIN TARGET -->
	<target name="cdrk2" depends="init">

		<property name="linux.file" value="null"/>
		<property name="mac.file" value="null"/>
		<property name="windows.file" value="null"/>
		<property name="doc.file" value="null"/>

		<if><bool><not><equals arg1="${linux.file}" arg2="null"/></not></bool>
			<property name="software.archive.linux" value="file://${linux.file}"/>
		</if>
		
		<if><bool><not><equals arg1="${mac.file}" arg2="null"/></not></bool>
			<property name="software.archive.mac" value="file://${mac.file}"/>
		</if>
		
		<if><bool><not><equals arg1="${windows.file}" arg2="null"/></not></bool>
			<property name="software.archive.windows" value="file://${windows.file}"/>
		</if>
		
		<if><bool><not><equals arg1="${doc.file}" arg2="null"/></not></bool>
			<property name="software.archive.documented-examples" value="file://${doc.file}"/>
		</if>
		
		<!-- figure out where the compiled software is coming from -->
		<property name="software.archive.linux" value="https://www.greenstone.org/caveat-emptor/cdrom-components-${version}-candidate-${date}-linux.tar.gz"/>
		<property name="software.archive.windows" value="https://www.greenstone.org/caveat-emptor/cdrom-components-${version}-candidate-${date}-windows.tar.gz"/>
		<property name="software.archive.mac" value="https://www.greenstone.org/caveat-emptor/cdrom-components-${version}-candidate-${date}-mac.tar.gz"/>
		<property name="software.archive.documented-examples" value="https://www.greenstone.org/caveat-emptor/Greenstone-documented-examples-${date}.tar.gz"/>
		
		<!-- show the properties we're using -->
		<if><bool><not><equals arg1="${no.linux}" arg2="true"/></not></bool>
			<echo message="software.archive.linux:                    ${software.archive.linux}"/>
		</if>

		<if><bool><not><equals arg1="${no.windows}" arg2="true"/></not></bool>
			<echo message="software.archive.windows:                  ${software.archive.windows}"/>
		</if>

		<if><bool><not><equals arg1="${no.mac}" arg2="true"/></not></bool>
			<echo message="software.archive.mac:                      ${software.archive.mac}"/>
		</if>

		<if><bool><not><equals arg1="${no.doc}" arg2="true"/></not></bool>
			<echo message="software.archive.documented-examples:      ${software.archive.documented-examples}"/>
		</if>

		<!-- create a directory for the cdrom contents -->
		<mkdir dir="cdrom"/>

		<!-- checkout and compile documentation java/fop code -->
		<if><bool><equals arg1="${post.2.83.version}" arg2="true"/></bool>
			<exec executable="svn">
				<arg value="export"/>
				<arg value="${svn.root}/documentation/${branch.path}/shared"/>
				<arg value="documentation/shared"/>
			</exec>
		  
		<else>
			<exec executable="svn">
				<arg value="export"/>
				<arg value="${svn.root}/main/${branch.path}/documentation/shared"/>
				<arg value="documentation/shared"/>
			</exec>
		</else>
		</if>

		<javac srcdir="documentation/shared" destdir="documentation/shared" debug="on" classpath="documentation/shared/xalan.jar">
			<include name="*.java"/>
		</javac>

		<!-- checkout tutorials and build them -->
		<if><bool><equals arg1="${post.2.83.version}" arg2="true"/></bool>  
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/documentation/${branch.path}/tutorials"/>
				<arg value="documentation/tutorials"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/gli"/>
				<arg value="documentation/gli"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/greenstone2/perllib"/>
				<arg value="documentation/perllib"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/greenstone2/macros"/>
				<arg value="documentation/macros"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/documentation/${branch.path}/tutorial_sample_files"/>
				<arg value="documentation/tutorial_sample_files"/>
			</exec>
		  
		<else>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/documentation/tutorials"/>
				<arg value="documentation/tutorials"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/gli"/>
				<arg value="documentation/gli"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/gsdl/perllib"/>
				<arg value="documentation/perllib"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/gsdl/macros"/>
				<arg value="documentation/macros"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/documentation/tutorial_sample_files"/>
				<arg value="documentation/tutorial_sample_files"/>
			</exec>
		</else>
		</if>

		<delete dir="documentation/tutorials/html"/>
		<exec dir="documentation/tutorials" executable="/bin/bash">
			<arg value="generate-html.sh"/>
			<env key="GSDLHOME" value="${basedir}/documentation"/>
		</exec>
		<delete dir="cdrom/Tutorial Exercises"/>
		<mkdir dir="cdrom/Tutorial Exercises/sample_files"/>

		<!-- copy tutorial html into place on the cd -->
		<copy todir="cdrom/Tutorial Exercises/English"><fileset dir="documentation/tutorials/html/en"/></copy>
		<copy todir="cdrom/Tutorial Exercises/French"><fileset dir="documentation/tutorials/html/fr"/></copy>
		<copy todir="cdrom/Tutorial Exercises/Spanish"><fileset dir="documentation/tutorials/html/es"/></copy>
		<copy todir="cdrom/Tutorial Exercises/Russian"><fileset dir="documentation/tutorials/html/ru"/></copy>

		<!-- create tutorial sample files zips in place on the cdrom -->
		<copy todir="cdrom/Tutorial Exercises/sample_files">
			<fileset dir="documentation/tutorial_sample_files"/>
		</copy>
		
		<!--<zip destfile="cdrom/Tutorial Exercises/sample_files/beatles.zip" basedir="documentation/tutorial_sample_files/beatles"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/custom.zip" basedir="documentation/tutorial_sample_files/custom"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/demo_NewFiles.zip" basedir="documentation/tutorial_sample_files/demo_NewFiles"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/dspace.zip" basedir="documentation/tutorial_sample_files/dspace"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/images.zip" basedir="documentation/tutorial_sample_files/images"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/isis.zip" basedir="documentation/tutorial_sample_files/isis"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/marc.zip" basedir="documentation/tutorial_sample_files/marc"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/niupepa.zip" basedir="documentation/tutorial_sample_files/niupepa"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/oai.zip" basedir="documentation/tutorial_sample_files/oai"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/simple_html.zip" basedir="documentation/tutorial_sample_files/simple_html"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/tudor.zip" basedir="documentation/tutorial_sample_files/tudor"/>
		<zip destfile="cdrom/Tutorial Exercises/sample_files/Word_and_PDF.zip" basedir="documentation/tutorial_sample_files/Word_and_PDF"/>-->

		<!-- checkout manuals and build them -->
		<if><bool><equals arg1="${post.2.83.version}" arg2="true"/></bool>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/documentation/${branch.path}/manuals"/>
				<arg value="documentation/manuals"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/documentation/${branch.path}/shared"/>
				<arg value="documentation/shared"/>
			</exec>   
		<else>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/documentation/manuals"/>
				<arg value="documentation/manuals"/>
			</exec>
			<exec executable="svn">
				<arg value="checkout"/>
				<arg value="${svn.root}/main/${branch.path}/documentation/shared"/>
				<arg value="documentation/shared"/>
			</exec>
		</else>
		</if>

		<exec executable="svn">
			<arg value="checkout"/>
			<arg value="${svn.root}/main/${branch.path}/gli"/>
			<arg value="documentation/gli"/>
		</exec>

		<unzip src="documentation/shared/fop.zip" dest="documentation/shared"/>
		<chmod perm="a+x" file="documentation/shared/fop/fop.sh"/>

		<antcall target="generate-gli-chapter"><param name="language" value="en"/></antcall>
		<antcall target="generate-gli-chapter"><param name="language" value="fr"/></antcall>
		<antcall target="generate-gli-chapter"><param name="language" value="es"/></antcall>
		<antcall target="generate-gli-chapter"><param name="language" value="ru"/></antcall>

		<delete dir="documentation/manuals/build"/>
		<exec dir="documentation/manuals" executable="/bin/bash">
			<arg value="generate-pdf.sh"/>
			<arg value="all"/>
			<arg value="en es ru fr"/>
		</exec>

		<exec spawn="true" dir="documentation/manuals" executable="/bin/bash">
			<arg value="generate-pdf.sh"/>
			<arg value="Install"/>
			<arg value="ar"/>
		</exec>

		<!-- put manuals in place on cdrom -->
		<delete dir="cdrom/Documentation"/>
		<copy todir="cdrom/Documentation/English"><fileset dir="documentation/manuals/build/en/pdf"/></copy>
		<copy todir="cdrom/Documentation/French"><fileset dir="documentation/manuals/build/fr/pdf"/></copy>
		<copy todir="cdrom/Documentation/Spanish"><fileset dir="documentation/manuals/build/es/pdf"/></copy>
		<copy todir="cdrom/Documentation/Russian"><fileset dir="documentation/manuals/build/ru/pdf"/></copy>
		<copy todir="cdrom/Documentation/Arabic"><fileset dir="documentation/manuals/build/ar/pdf"/></copy>

		<!-- top off manuals with a few more from the web -->
		<antcall target="get-doc"><param name="docsrc" value="http://wiki.greenstone.org/wiki/gsdoc/others/CDS-ISIS_to_DL.pdf"/><param name="docdest" value="cdrom/Documentation/English/CDS-ISIS_to_DL.pdf"/></antcall>
		<antcall target="get-doc"><param name="docsrc" value="https://www.greenstone.org/docs/inside_greenstone.pdf"/><param name="docdest" value="cdrom/Documentation/English/inside_greenstone.pdf"/></antcall>
		<antcall target="get-doc"><param name="docsrc" value="https://www.greenstone.org/manuals/gsdl2/ar/pdf/Users_Guide_Arabic.doc"/><param name="docdest" value="cdrom/Documentation/Arabic/Users_Guide_Arabic.doc"/></antcall>
		<antcall target="get-doc"><param name="docsrc" value="https://www.greenstone.org/manuals/gsdl2/ar/pdf/Install_ar.pdf"/><param name="docdest" value="cdrom/Documentation/Arabic/Install_ar.pdf"/></antcall>

		<mkdir dir="cdrom/Documentation/Brazilian Portuguese"/>
		<antcall target="get-doc"><param name="docsrc" value="https://www.greenstone.org/manuals/gsdl2/pt-br/pdf/Install_pt-br.pdf"/><param name="docdest" value="cdrom/Documentation/Brazilian Portuguese/Install_pt-br.pdf"/></antcall>

		<mkdir dir="cdrom/Documentation/Vietnamese"/>
		<antcall target="get-doc">
          <param name="docsrc" value="http://ftp.heanet.ie/disk1/sourceforge/g/project/gr/greenstone/OldFiles/Install-2.39-vi.pdf"/>
		  <param name="docdest" value="cdrom/Documentation/Vietnamese/Install-2.39-vi.pdf"/>
        </antcall>	
		<antcall target="get-doc">
          <param name="docsrc" value="http://ftp.heanet.ie/disk1/sourceforge/g/project/gr/greenstone/OldFiles/User-2.39-vi.pdf"/>
          <param name="docdest" value="cdrom/Documentation/Vietnamese/User-2.39-vi.pdf"/>
        </antcall>
		<antcall target="get-doc">
          <param name="docsrc" value="http://ftp.heanet.ie/disk1/sourceforge/g/project/gr/greenstone/OldFiles/Paper-2.39-vi.pdf"/>
          <param name="docdest" value="cdrom/Documentation/Vietnamese/Paper-2.39-vi.pdf"/>
        </antcall>

		<mkdir dir="cdrom/Documentation/Kazakh"/>
		<antcall target="get-doc">
          <param name="docsrc" value="http://ftp.heanet.ie/disk1/sourceforge/g/project/gr/greenstone/OldFiles/Install-2.39-kz.pdf"/>
          <param name="docdest" value="cdrom/Documentation/Kazakh/Install-2.39-kz.pdf"/>
        </antcall>
		<antcall target="get-doc">
          <param name="docsrc" value="http://ftp.heanet.ie/disk1/sourceforge/g/project/gr/greenstone/OldFiles/User-2.39-kz.pdf"/>
          <param name="docdest" value="cdrom/Documentation/Kazakh/User-2.39-kz.pdf"/>
        </antcall>
		<antcall target="get-doc">
          <param name="docsrc" value="http://ftp.heanet.ie/disk1/sourceforge/g/project/gr/greenstone/OldFiles/Paper-2.39-kz.pdf"/>
          <param name="docdest" value="cdrom/Documentation/Kazakh/Paper-2.39-kz.pdf"/>
        </antcall>

		<!-- insert the documented examples onto cdrom -->
		<if><bool><not><equals arg1="${no.doc}" arg2="true"/></not></bool>
			<mkdir dir="archives"/>
			<get src="${software.archive.documented-examples}" dest="archives/documented-examples.zip"/>		  
			<delete dir="cdrom/Documented Examples"/>
			<copy toDir="cdrom">
				<fileset dir="archives">
					<include name="documented-examples.zip"/>
				</fileset>
			</copy>
			<!--<exec executable="tar" dir="cdrom/Documented Examples"><arg value="-xzf"/><arg value="../../archives/documented-examples.tar.gz"/></exec>-->
		</if>

		<!-- insert readmes -->
		<copy todir="cdrom"><fileset dir="${rk.home}/shared/greenstone2/docs"/></copy>
		<antcall target="gsdl-set-dates-in-readmes"><param name="gsdl.basedir" value="${basedir}/cdrom"/></antcall>

		<!-- insert linux software -->
		<if><bool><not><equals arg1="${no.linux}" arg2="true"/></not></bool>
			<mkdir dir="cdrom"/>
			<mkdir dir="archives"/>
			<get src="${software.archive.linux}" dest="archives/linux.tar.gz"/>
			<exec executable="tar" dir="cdrom"><arg value="-xzf"/><arg value="../archives/linux.tar.gz"/></exec>
		</if>

		<!-- insert windows software -->
		<if><bool><not><equals arg1="${no.windows}" arg2="true"/></not></bool>
			<mkdir dir="cdrom"/>
			<mkdir dir="archives"/>
			<get src="${software.archive.windows}" dest="archives/windows.tar.gz"/>
			<exec executable="tar" dir="cdrom"><arg value="-xzf"/><arg value="../archives/windows.tar.gz"/></exec>
		</if>

		<!-- insert mac software -->
		<if><bool><not><equals arg1="${no.mac}" arg2="true"/></not></bool>
			<mkdir dir="cdrom"/>
			<mkdir dir="archives"/>
			<get src="${software.archive.mac}" dest="archives/mac.tar.gz"/>
			<exec executable="tar" dir="cdrom"><arg value="-xzf"/><arg value="../archives/mac.tar.gz"/></exec>
		</if>

		<!-- create autorun (windows) -->
		<exec executable="cp">
			<arg value="${rk.home}/kits/${rk.name}/resources/AUTORUN.INF"/>
			<arg value="cdrom"/>
		</exec>

		<!-- finally create the cdrom image -->
		<mkdir dir="products"/>
		<exec executable="mkisofs">
			<arg value="-V"/>
			<arg value="Greenstone-${version}"/>
			<arg value="-input-charset"/>
			<arg value="utf-8"/>
			<arg value="-o"/>
			<arg value="products/Greenstone-${version}-cdrom.iso"/>
			<arg value="-J"/>
			<arg value="-R"/>
			<arg value="-D"/>
			<arg value="cdrom"/>
		</exec>
		<exec executable="/bin/bash" dir="products">
			<arg value="-c"/>
			<arg value="cat Greenstone-${version}-cdrom.iso | gzip > Greenstone-${version}-cdrom.iso.gz"/>
		</exec>
		<exec executable="/bin/bash" dir="products">
			<arg value="-c"/>
			<arg value="zip Greenstone-${version}-cdrom.zip Greenstone-${version}-cdrom.iso"/>
		</exec>
	</target>

	<target name="properties" depends="core-properties">
		<echo>no.linux                                    (optional)  specifies that this cd should not use files from the linux distribution</echo>
		<echo>no.windows                                  (optional)  specifies that this cd should not use files from the windows distribution</echo>
		<echo>no.mac                                      (optional)  specifies that this cd should not use files from the mac distribution</echo>
		<echo>no.doc                                      (optional)  specifies that this cd should not use the documented examples</echo>
		<echo>linux.file                                  (optional)  use this if you would like to specify a file path to the linux software archive rather than a URL</echo>
		<echo>windows.file                                (optional)  use this if you would like to specify a file path to the windows software archive rather than a URL</echo>
		<echo>mac.file                                    (optional)  use this if you would like to specify a file path to the mac software archive rather than a URL</echo>
		<echo>doc.file                                    (optional)  use this if you would like to specify a file path to the documented examples software archive rather than a URL</echo>
		<echo>software.archive.linux                      (optional)  the URL to the linux archive of components</echo>
		<echo>software.archive.windows                    (optional)  the URL to the windows archive of components</echo>
		<echo>software.archive.mac                        (optional)  the URL to the mac archive of components</echo>
		<echo>software.archive.documented-examples        (optional)  the URL to the archive of documented examples</echo>
	</target>

	<!-- target to generate the manual chapter about gli in a given language -->
	<target name="generate-gli-chapter">
		<path id="documentation.compile.classpath">
			<fileset dir="documentation">
				<include name="**/*.jar"/>
			</fileset>
			<pathelement path="documentation/gli/classes"/>
			<pathelement path="documentation/shared"/>
			<pathelement path="documentation/manuals"/>
		</path>
		<java classname="ApplyXSLT" classpathref="documentation.compile.classpath" output="documentation/manuals/xml-source/${language}/help-${language}.xml">
			<arg value="${language}" />
			<arg value="documentation/manuals/processing/gen-gli-help-to-manual-chapter.xsl" />
			<arg value="documentation/gli/help/${language}/help.xml" />
			<arg value="compiled" />
		</java>
		<copy file="documentation/manuals/xml-source/${language}/help-${language}.xml" todir="documentation/manuals"/>
	</target>

	<target name="get-doc">
		<try>
			<get src="${docsrc}" dest="${docdest}"/>
		<catch>
		</catch>
		</try>
	</target>
</project>
