<?xml version="1.0" encoding="UTF-8"?>

<!--
  Copyright 2005 The Apache Software Foundation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<!--
    Main build targets for the bricks-cms cocoon app.
    $Id$
-->

<project name="bricks-cms" default="explain">

	<!--
      Override default settings provided by user.properties, which is stored in SVN),
      with settings from local_user.properties, which is not in SVN and can be used
      to set local values.

      And yes, this is the correct order of <property> elements to do this.
  -->
	<property file="local.user.properties"/>
	<property file="user.properties"/>

	<!-- setup paths -->
	<property name="src" value="${basedir}/src"/>
	<property name="src.java" value="${src}/java"/>
	<property name="src.cocoon" value="${src}/cocoon"/>
	<property name="src.webapp" value="${src.cocoon}/webapp"/>
	<property name="src.config" value="${src.cocoon}/config"/>
	<property name="src.build-targets" value="${src}/build-targets"/>
	<property name="src.misc" value="${src}/misc"/>
	<property name="build" value="${basedir}/build"/>
	<property name="build.classes" value="${build}/classes"/>
	<property name="build.meta-inf" value="${build}/classes/META-INF"/>
	<property name="warfile" value="${build}/${ant.project.name}.war"/>

	<!-- ${basedir}/ ensures that the absolute path is part of the properties;
       this is needed for property 'cocoon.webapp' in the 'cocoon:run' target -->
	<property name="lib" value="${basedir}/lib"/>
	<property name="utilities" value="${basedir}/utilities"/>

	<property name="cocoon-build.properties" value="${src.cocoon}/local.build.properties"/>
	<property name="cocoon.xconf.dir" value="${src.cocoon}/xconf"/>
	<property name="cocoon.tool.dir" value="${build}/cocoon"/>
	<property name="cocoon.webapp" value="${cocoon.tool.dir}/webapp"/>
	<property name="cocoon.lib" value="${cocoon.webapp}/WEB-INF/lib"/>
	<property name="cocoon.classes" value="${cocoon.webapp}/WEB-INF/classes"/>
	<property name="jetty.workdir" value="${cocoon.webapp}/WEB-INF/work"/>

	<!-- import additional build files -->
	<import file="${src.build-targets}/build-cocoon-targets.xml"/>
	<import file="${src.build-targets}/derby-build-targets.xml"/>

	<!-- Common paths -->
	<path id="all.cp">
		<pathelement location="${build.classes}"/>
		<fileset dir="${cocoon.lib}">
			<include name="*.jar"/>
		</fileset>
		<fileset dir="${lib}">
			<include name="**/*.jar"/>
		</fileset>
	</path>

	<!-- Main targets -->

	<target name="explain">
		<echo>See README.TXT for how to setup and use this module</echo>
	</target>

	<target name="init">
		<mkdir dir="${build.classes}"/>
	</target>

	<target name="compile" depends="init,-cocoon:check">
		<javac srcdir="${src.java}" destdir="${build.classes}" debug="true">
			<classpath refid="all.cp"/>
			<include name="**/*.java"/>
		</javac>
	</target>

	<!-- Compile and configure -->
	<target name="copy-configs" depends="init" description="copy required configuration files for the webapp">

    <!-- log4j: delete first to make sure it is updated if the log4j.user.properties name has changed -->
    <property name="log4j.cocoon.config" value="${cocoon.classes}/log4j.properties"/>
    <delete file="${log4j.cocoon.config}"/>
    <copy file="${log4j.user.properties}" tofile="${log4j.cocoon.config}" />

    <!-- copy hivemodule.xml -->
		<copy todir="${build.meta-inf}">
			<fileset dir="${src.misc}/hivemind/">
				<include name="**"/>
			</fileset>
		</copy>

    <!-- copy OJB properties and XML files -->
		<copy todir="${build.classes}">
			<fileset dir="${src.misc}/ojb">
				<include name="*.*"/>
			</fileset>
		</copy>

	</target>

	<target name="webapp" depends="compile, cocoon:xconf, copy-configs, derby:setup" description="build and configure the webapp">
		<copy todir="${cocoon.lib}" flatten="true">
			<fileset dir="${lib}">
				<include name="**/*.jar"/>
			</fileset>
		</copy>
		<copy todir="${cocoon.classes}">
			<fileset dir="${build.classes}"/>
		</copy>
		<copy todir="${cocoon.webapp}">
			<fileset dir="${src.webapp}"/>
		</copy>

		<!-- make sure to overwrite the welcome.xml generated by cocoon:get -->
		<copy todir="${cocoon.webapp}" overwrite="true">
			<fileset dir="${src.webapp}">
				<include name="welcome.xml"/>
			</fileset>
		</copy>

		<!-- Jetty uses WEB-INF/work as a work dir if it exists, clean it up -->
		<delete dir="${jetty.workdir}"/>
		<mkdir dir="${jetty.workdir}"/>
	</target>

	<target name="clean" depends="init" description="cleanup all generated files, including the cocoon:get build">
		<echo>To delete the database, use "ant derby:clean" - it is not deleted by this build target</echo>
		<delete dir="${build}"/>
	</target>

  <target name="clean-classes" depends="init" description="cleanup our classes only, not the cocoon:get build">
    <echo>To delete the database, use the derby:clean target</echo>
    <delete dir="${build.classes}"/>
  </target>

	<target name="run-message">
		<echo>WARNING: the database must be initialized by doing "ant derby:clean derby:create", this is not automated for now</echo>
	</target>

	<target name="run" depends="webapp,run-message,cocoon:run" description="build and start with built-in Jetty"/>

	<target name="war" depends="webapp" description="create a war file for deployment">
		<jar destfile="${warfile}" basedir="${cocoon.webapp}"/>
	</target>

</project>
