<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you 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.   
-->
<!-- ====================================================================== -->
<!--                                                                        -->
<!--               Ant build script for OpenBooks                           -->
<!--                                                                        -->
<!-- ====================================================================== -->
                    
<project name="OpenBooks" default="package">
	
	<!-- ================================================================== -->
	<property file="build.properties" />
	<!-- ================================================================== -->
	<property name="jpa.provider" value="openjpa" />
	<!-- ================================================================== -->
	<property name="build.mode" value="jse"/>
	<!-- ================================================================== -->
	<!-- root directory of Java Source files                                -->
	<property name="src.dir"      value="${basedir}/src/main/java"/>
	<!-- ================================================================== -->
	<!-- root directory of JSP and other web content files                  -->
	<property name="web.dir"     value="${basedir}/web"/>
	<!-- ================================================================== -->
	<!-- root directory of resource files e.g web.xml or persistence.xml    -->
	<property name="rsrc.dir"     value="${basedir}/src/main/resources"/>
	<!-- ================================================================== -->
	<!-- JPA configuration file anchored by the persistence unit name       -->
	<property name="jpa.properties"  value="META-INF/persistence.xml#OpenBooks"/>
	<!-- ================================================================== -->	
	<!-- Application/Web Server type:                                       -->
	<!-- geronimo, was, tomcat, glassfish, or empty string for default      -->
	<property name="appserver" value=""/>
	<!-- ================================================================== -->	
	<!-- Root directory of all derived artifacts                            -->
	<property name="target.dir" value="${basedir}/target"/>
	<!-- ================================================================== -->
	<!-- root directory of compiled class files                             -->
	<property name="classes.dir"  value="${target.dir}/classes"/>
	<!-- ================================================================== -->
	<!-- root directory of generated canonical meta-model source files       -->
	<property name="generated.src.dir"  value="${target.dir}/generated-metamodel/java"/>
	<!-- ================================================================== -->
	<!-- root directory of generated html for Java source files             -->
	<property name="generated.html.dir"  value="${target.dir}/generated-html"/>
	<!-- ================================================================== -->
	<!-- directory for deployable artifacts                                 -->
	<property name="deploy.dir"   value="${target.dir}/openbooks"/>          
	<!-- ================================================================== -->

	<path id="compile.classpath" 
		description="Compilation classpath requires OpenJPA specific library because of bytecode enhancement">
		<pathelement location="${src.dir}" />
		<pathelement location="${classes.dir}" />
		<pathelement location="${rsrc.dir}" />
		<fileset dir="${openjpa.lib}">
			<include name="*.jar" />
		</fileset>
	</path>
	
	<path id="code.generation.classpath" 
		description="">
		<pathelement location="${classes.dir}" />
		<fileset dir="${antlr.lib}">
			<include name="*.jar" />
		</fileset>
		<fileset dir="${antlr-runtime.lib}">
			<include name="*.jar" />
		</fileset>
	</path>
	
	
	<fileset id="canonical.metamodel" dir="${src.dir}" 
		description="Set of *.java files generated for static, cannonical domain model">
		<include name="**/*_.java"/>
	</fileset>
	
	<target name="clean" description="Removes all *.class and generated metamodel source files">
		<delete dir="${target.dir}" failonerror="false"/>
        <mkdir dir="${deploy.dir}"/>
		<delete>
			<fileset refid="canonical.metamodel"/>
		</delete>
	</target>

	<target name="info"
		description="Prints environmental variables">
		<echo>
			Builds OpenBooks demo application.
			OpenJPA Library: ${openjpa.lib}
			Build mode     : ${build.mode}
			Deploy Location: ${deploy.dir}
		</echo>
	</target>
	
	<target name="check-env" 
		    description="Checks if required libraries exist"
		    depends="info">
		<available file="${openjpa.lib}"   property="openjpa.exists"/>
		
		<fail unless="openjpa.exists">*** Error:
		   The directory for OpenJPA libraries can not be located at ${openjpa.lib}. 
		   Make sure openjpa.lib property value is correct in build.properties file.	
		</fail>
		<condition property="build.mode.is.valid">
			<or>
				<equals arg1="${build.mode}" arg2="jse" casesensitive="false" trim="true"/>
				<equals arg1="${build.mode}" arg2="jee" casesensitive="false" trim="true"/>
			</or>
		</condition>
		<fail unless="build.mode.is.valid" message="Invalid build.mode ${build.mode}"/>
		<condition property="build.mode.is.jse">
			<equals arg1="${build.mode}" arg2="jse" casesensitive="true" trim="true"/>
		</condition>
		<condition property="build.mode.is.jee">
			<equals arg1="${build.mode}" arg2="jee" casesensitive="true" trim="true"/>
		</condition>
		
		<available file="${antlr.lib}"     property="antlr.exists"/>
<!--
  <condition property="antlr.exists">
    <and>
      <isset property="antlr.lib.exists" />
      <available classname="org.antlr.Tool" />
    </and>
  </condition>
		<fail unless="antlr.exists" message="ANTLR is missing from classpath"/>
-->
	</target>
	
	<target name="generate-canonical-model" depends="check-env"
		description="Generate canonical meta-model source">
		<mkdir dir="${generated.src.dir}" />
		<mkdir dir="${classes.dir}" />
                <!--
                    javac has to be forked for maven-antrun-plugin builds
                    to work on Linux...
                -->
		<javac fork="true" srcdir="${src.dir}" destdir="${classes.dir}" debug="true" includeAntRuntime="false" >
			<classpath refid="compile.classpath" />
			<include name="openbook/domain/*.java"/>
			<compilerarg value="-Aopenjpa.metamodel=true" />
			<compilerarg value="-Aopenjpa.log=TRACE" />
			<compilerarg value="-s"/>
			<compilerarg value="${generated.src.dir}" />
		</javac>
	</target>
	
	<target name="compile" depends="generate-canonical-model"
		description="Compile classes.">
		<subant antfile="build.${build.mode}.xml" 
			target="compile" 
			inheritrefs="true" 
			inheritall="true"
			buildpath="${basedir}"/>
	</target>
	
	<target name="enhance" depends="check-env"
		description="Enhance persistent domain classes.">
		<java classname="org.apache.openjpa.enhance.PCEnhancer" fork="true">
			<arg value="-properties" />
			<arg value="${jpa.properties}" />
			<classpath refid="compile.classpath" />
		</java>
	</target>
	
	<target name="package" description="Build and Package based on JSE or JEE mode"
		depends="check-env,
	             clean,
				 generate-canonical-model,
				 compile,
				 enhance,
				 generate-source">
		<subant antfile="build.${build.mode}.xml" 
			target="package" 
			inheritrefs="true" 
			inheritall="true"
			buildpath="${basedir}"/>
	</target>
	
	<target name="build-parser" depends="check-env" if="antlr.exists">
		<java classname="org.antlr.Tool">
			<classpath refid="code.generation.classpath"/>
			<arg value="-o"/>
			<arg value="${generated.src.dir}/openbook/tools/parser"/>
			<arg value="-debug"/>
			<arg value="${rsrc.dir}/META-INF/Java.g"/>
		</java>
	</target>
	<!-- Generating viewable syntax colored Java code as html is to be split -->
	<!-- for JSE and JEE mode for some differences in Swing based browser    -->
	<!-- versus general purpose web browsers. Swing based browser treats     -->
	<!-- line breaks, white spaces differently becuase it does not recognize -->
	<!-- some critical CSS directives in this regard. This anamoly is        -->
	<!-- accounted for by customizing Java2HTMLConverter accordingly         -->
	<!-- see the respective target's options to notice the difference        -->
	<target name="generate-source" depends="check-env" if="antlr.exists">
		<javac srcdir="${src.dir}" destdir="${classes.dir}">
			<include name="openbook/tools/**/*.java"/>
			<exclude name="openbook/tools/**/Test*.java"/>
			<classpath refid="code.generation.classpath"/>
		</javac>
		<taskdef name="java2html" classname="openbook.tools.ant.Java2HTMLConverterTask">
			<classpath refid="code.generation.classpath"/>
		</taskdef>
		<delete dir="${generated.html.dir}"></delete>
		<mkdir dir="${generated.html.dir}"/>
		<copy file="${rsrc.dir}/java.css" todir="${generated.html.dir}"/>
		<subant antfile="build.${build.mode}.xml" 
			target="generate-source-with-options" 
			inheritrefs="true" 
			inheritall="true"
			buildpath="${basedir}"/>

	</target>
	
</project>
