<?xml version="1.0"?>

<project name="agavi" basedir="." default="main">

	<!-- set up some stuff -->
	<property environment="env"/>
	<property file="build.properties"/>
	<property name="package"  value="${phing.project.name}" override="true" />
	<property name="srcdir"   value="${project.basedir}" override="true" />
	
	<taskdef classname="etc.phing.AgaviVersionTask" name="agaviversion" />
	<taskdef classname="etc.phing.AgaviPackageTask" name="agavipackage" />
	<taskdef classname="etc.phing.AgaviOlsonCompileTask" name="agaviolsoncompile" />

	<!-- fileset for -dist files -->
	<fileset dir="." id="distfiles">
		<include name="**/*-dist"/>
	</fileset>

	<!-- main target -->
	<target name="main" description="main target">
		<echo msg="available targets: " />
		<echo msg=" docs             : regenerate all documentation" />
		<echo msg="  apidocs         : regenerate API docs" />
		<echo msg="  manuals         : regenerate manuals (-Doutput=html|html-onepage|fo)" />
		<echo msg=" packages         : generate all packages" />
		<echo msg="  package-pear    : generate PEAR package" />
		<echo msg="  package-release : generate API docs package" />
		<echo msg=" test    : run unit test suite" />
		<echo msg=" clean   : clean out cruft" />
	</target>

	<target name="docs" description="generate API docs and manuals">
		<phingcall target="apidocs" />
		<phingcall target="manuals" />
	</target>

	<target name="apidocs" description="API docs">
		<phpdoc title="Agavi Documentation" destdir="apidocs" output="HTML:frames:DOM/earthli">
			<fileset dir="./src">
				<include name="**/*.php" />
				<exclude name="vendor/**" />
				<exclude name="translation/data/**" />
			</fileset>
		</phpdoc>
	</target>

	<target name="manuals" description="generate all manuals">
		<property name="output" value="html" override="false" />
		<phingcall target="cookbook-${output}" />
		<phingcall target="introduction-${output}" />
		<phingcall target="guide-${output}" />
	</target>

	<!--target name="manual-cookbook">
		<phingcall target="cookbook-${output}" />
	</target>

	<target name="manual-introduction">
		<phingcall target="introduction-${output}" />
	</target>

	<target name="manual-guide">
		<phingcall target="guide-${output}" />
	</target-->

	<!-- Agavi Cookbook -->
	<target name="cookbook-html" >
		<mkdir dir="manuals/cookbook/html/multipage" />
		<copy file="docs/lib/agavi-manual.css" todir="manuals/cookbook/html/multipage" overwrite="true" />
		<xslt file="docs/docbook/cookbook.xml" todir="manuals/cookbook/html/multipage" style="docs/lib/agavi-manual-html.xsl">
			<param name="onechunk" expression="0" />
			<param name="base.dir" expression="manuals/cookbook/html/multipage/" /><!-- end slash required -->
			<param name="root.filename" expression="index" />
			<param name="html.stylesheet" expression="agavi-manual.css" />
			<param name="chunker.output.encoding" expression="UTF-8" />
			<param name="id.warnings" expression="0" />
			<param name="header.rule" expression="0" />
			<param name="footer.rule" expression="0" />
			<param name="suppress.navigation" expression="1" />
		</xslt>
	</target>

	<target name="cookbook-html-onepage" >
		<mkdir dir="manuals/cookbook/html/onepage" />
		<copy file="docs/lib/agavi-manual.css" todir="manuals/cookbook/html/onepage" overwrite="true" />
		<xslt file="docs/docbook/cookbook.xml" todir="manuals/cookbook/html/onepage" style="docs/lib/agavi-manual-html-onepage.xsl">
			<param name="onechunk" expression="1" />
			<param name="base.dir" expression="manuals/cookbook/html/onepage/" /><!-- end slash required -->
			<param name="root.filename" expression="index" />
			<param name="html.stylesheet" expression="agavi-manual.css" />
			<param name="chunker.output.encoding" expression="UTF-8" />
			<param name="id.warnings" expression="0" />
			<param name="header.rule" expression="0" />
			<param name="footer.rule" expression="0" />
			<param name="suppress.navigation" expression="1" />
		</xslt>
	</target>

	<!-- Introduction to Agavi -->
	<target name="introduction-html" >
		<mkdir dir="manuals/introduction/html/multipage" />
		<copy file="docs/lib/agavi-manual.css" todir="manuals/introduction/html/multipage" overwrite="true" />
		<xslt file="docs/docbook/introduction.xml" todir="manuals/introduction/html/multipage" style="docs/lib/agavi-manual-html.xsl">
			<param name="onechunk" expression="0" />
			<param name="base.dir" expression="manuals/introduction/html/multipage/" /><!-- end slash required -->
			<param name="root.filename" expression="index" />
			<param name="html.stylesheet" expression="agavi-manual.css" />
			<param name="chunker.output.encoding" expression="UTF-8" />
			<param name="id.warnings" expression="0" />
			<param name="header.rule" expression="0" />
			<param name="footer.rule" expression="0" />
			<param name="suppress.navigation" expression="1" />
		</xslt>
	</target>

	<target name="introduction-html-onepage" >
		<mkdir dir="manuals/introduction/html/onepage" />
		<copy file="docs/lib/agavi-manual.css" todir="manuals/introduction/html/onepage" overwrite="true" />
		<xslt file="docs/docbook/introduction.xml" todir="manuals/introduction/html/onepage" style="docs/lib/agavi-manual-html-onepage.xsl">
			<param name="onechunk" expression="1" />
			<param name="base.dir" expression="manuals/introduction/html/onepage/" /><!-- end slash required -->
			<param name="root.filename" expression="index" />
			<param name="html.stylesheet" expression="agavi-manual.css" />
			<param name="chunker.output.encoding" expression="UTF-8" />
			<param name="id.warnings" expression="0" />
			<param name="header.rule" expression="0" />
			<param name="footer.rule" expression="0" />
			<param name="suppress.navigation" expression="1" />
		</xslt>
	</target>

	<!-- Definitive Guide -->
	<target name="guide-html" >
		<mkdir dir="manuals/guide/html/multipage" />
		<copy file="docs/lib/agavi-manual.css" todir="manuals/guide/html/multipage" overwrite="true" />
		<xslt file="docs/docbook/manual.xml" todir="manuals/guide/html/multipage" style="docs/lib/agavi-manual-html.xsl">
			<param name="onechunk" expression="0" />
			<param name="base.dir" expression="manuals/guide/html/multipage/" /><!-- end slash required -->
			<param name="root.filename" expression="index" />
			<param name="html.stylesheet" expression="agavi-manual.css" />
			<param name="chunker.output.encoding" expression="UTF-8" />
			<param name="id.warnings" expression="0" />
			<param name="header.rule" expression="0" />
			<param name="footer.rule" expression="0" />
			<param name="suppress.navigation" expression="1" />
		</xslt>
	</target>

	<target name="guide-html-onepage" >
		<mkdir dir="manuals/guide/html/onepage" />
		<copy file="docs/lib/agavi-manual.css" todir="manuals/guide/html/onepage" overwrite="true" />
		<xslt file="docs/docbook/manual.xml" todir="manuals/guide/html/onepage" style="docs/lib/agavi-manual-html-onepage.xsl">
			<param name="onechunk" expression="1" />
			<param name="base.dir" expression="manuals/guide/html/onepage/" /><!-- end slash required -->
			<param name="root.filename" expression="index" />
			<param name="html.stylesheet" expression="agavi-manual.css" />
			<param name="chunker.output.encoding" expression="UTF-8" />
			<param name="id.warnings" expression="0" />
			<param name="header.rule" expression="0" />
			<param name="footer.rule" expression="0" />
			<param name="suppress.navigation" expression="1" />
		</xslt>
	</target>

	<!-- manuals FO -->

	<target name="cookbook-fo" >
		<mkdir dir="manuals/guide/fo" />
		<xslt file="docs/docbook/manual.xml" tofile="manuals/guide/fo/cookbook.fo" style="docs/lib/agavi-manual-fo.xsl" >
			<param name="base.dir" expression="manuals/guide/fo/" />
			<param name="paper.type" expression="A4" />
		</xslt>
	</target>

	<target name="introduction-fo" >
		<mkdir dir="manuals/guide/fo" />
		<xslt file="docs/docbook/manual.xml" tofile="manuals/guide/fo/introduction.fo" style="docs/lib/agavi-manual-fo.xsl" >
			<param name="base.dir" expression="manuals/guide/fo/" />
			<param name="paper.type" expression="A4" />
		</xslt>
	</target>

	<target name="guide-fo" >
		<mkdir dir="manuals/guide/fo" />
		<xslt file="docs/docbook/manual.xml" tofile="manuals/guide/fo/guide.fo" style="docs/lib/agavi-manual-fo.xsl" >
			<param name="base.dir" expression="manuals/guide/fo/" />
			<param name="paper.type" expression="A4" />
		</xslt>
	</target>
	
	<target name="generate-properties">
		<agaviversion /> 
	</target>

	<target name="package-zip">
		<!-- zip a release package -->
	</target>

	<target name="package-apidocs">
		<phingcall target="apidocs" />
		<!-- now zip that -->
	</target>

	<target name="package-manuals">
		<phingcall target="manuals" />
		<!-- now zip that -->
	</target>

	<target name="package-pear" depends="generate-properties" description="build pear package">
		<!-- prepare -->
		<property name="basedir" value="pear-build" override="true" />
		<property name="builddir" value="${basedir}/agavi-${agavi.pear.version}" override="true" />
		
		<delete dir="${builddir}" includeemptydirs="true" verbose="false" failonerror="true" />
		<delete file="agavi-${agavi.pear.version}.tgz" verbose="false" failonerror="true" />
		
		<mkdir dir="${builddir}" />
		
		<copy todir="${builddir}/samples">
			<fileset dir="samples/">
				<include name="**/*" />
			</fileset>
		</copy>
		<copy file="API_CHANGELOG" todir="${builddir}" />
		<copy file="CHANGELOG" todir="${builddir}" />
		<copy file="COPYRIGHT" todir="${builddir}" />
		<copy file="INSTALL" todir="${builddir}" />
		<copy file="KNOWN_ISSUES" todir="${builddir}" />
		<copy file="LICENSE" todir="${builddir}" />
		<copy file="LICENSE-AGAVI" todir="${builddir}" />
		<copy file="LICENSE-ICU" todir="${builddir}" />
		<copy file="LICENSE-SCHEMATRON" todir="${builddir}" />
		<copy file="LICENSE-UNICODE_CLDR" todir="${builddir}" />
		<copy file="RELEASE_NOTES" todir="${builddir}" />
		<copy file="TODO" todir="${builddir}" />
		<copy file="UPGRADING" todir="${builddir}" />

		<copy todir="${builddir}">
			<fileset dir="src/">
				<include name="**/*" />
			</fileset>
		</copy>

		<copy todir="${builddir}/bin">
			<fileset dir="bin/">
				<include name="**/*" />
			</fileset>
		</copy>

		<!-- generate package.xml -->
		<agavipackage baseDir="${basedir}" buildDir="${builddir}" />
		
		<!-- create .tgz file -->
		<echo msg="Creating gzip file for PEAR." />
		<tar destfile="agavi-${agavi.pear.version}.tgz" basedir="${basedir}" compression="gzip" />
	</target>
	
	<target name="package-release" depends="generate-properties">
		<delete dir="./package-build" includeemptydirs="true" verbose="false" failonerror="true" />
		<delete file="${agavi.pear.version}.tgz" verbose="false" failonerror="true" />
		<delete file="${agavi.pear.version}.zip" verbose="false" failonerror="true" />
		
		<mkdir dir="./package-build/agavi-${agavi.pear.version}" />
		
		<copy todir="./package-build/agavi-${agavi.pear.version}">
			<fileset dir=".">
				<exclude name="*-build/**" />
				<exclude name="*.tgz*" />
				<exclude name="*.zip*" />
				<include name="**/*" />
			</fileset>
		</copy>
		
		<tar destfile="${agavi.pear.version}.tgz" compression="gzip">
			<fileset dir="./package-build" />
		</tar>
		
		<zip destfile="${agavi.pear.version}.zip">
			<fileset dir="./package-build" />
		</zip>
	</target>
	
	<target name="packages" depends="package-pear,package-release" />

	<target name="clean" description="clean out the cruft">
		<delete dir="manuals" includeemptydirs="true" failonerror="true" />
		<delete dir="pear-build" includeemptydirs="true" failonerror="true" />
		<delete dir="apidocs" includeemptydirs="true" quiet="true" failonerror="false" />
		<delete file="tests.html" includeemptydirs="true" quiet="true" failonerror="false" />
	</target>

	<target name="olson" description="build timezone datebase">
		<agaviolsoncompile olsonDir="${srcdir}/etc/olson/tzdata" outputDir="${srcdir}/src/translation/data/timezones" />
	</target>

</project>
