<!--

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 1997-2017 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://oss.oracle.com/licenses/CDDL+GPL-1.1
    or LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<project name="book" default="main" basedir=".">
  <!--
  This buildfile is designed to minimize development
  time for developing applications using SAAJ. It 
  creates the initial directory structure for 
  web apps. It also creates a war file which
  can be deployed in tomcat or any other container.
  
  You must customize this file or call it with 
  -Dname=value arguments.
  
  The values to customize for each application:

  appname: the name of the application ( "book" ). Defaults to 
           project name, if set.


  Global properties you must set( can be set in saaj.properties )

  saaj.home: location of saaj
  servlet.jar: location of servlet.jar

  Other properties, with defaults:

  webapps.dir: directory where the app will be built. 
          Default to in-place ( very usefull if you create your app under webapps/ ).

  wars.dir: directory where the .war file will be constructed. 
            If not set, no war will be generated.

  compile.debug
  compile.deprecation
  compile.optimize: javac customization

  -->
  <property file="${user.home}/build.properties"/>   <!-- User local        -->
  <property file="${user.home}/.ant.properties"/>    <!-- User local        -->
  <property file="build.properties"/>                <!-- Component local   -->
  <property file="../../build.properties"/>          <!-- For all webapps -->
  <property file="../../../build.properties"/>             
 
  <property name="appname" value="${ant.project.name}"/>

  <property name="appdir" location=".."/>

  <property name="compile.debug" value="true" />
  <property name="compile.optimize" value="false" />
  <property name="compile.deprecation" value="false" />
  <property name="saaj-api.jar" value="${saaj.home}/lib/saaj-api.jar" />
  <property name="saaj-coms.jar" value="${webapps.dir}/saajservlet/lib/saaj-coms.jar" />

  <!-- Classpath -->

  <!-- Main saaj files -->
  <fileset id="saaj-classpath" dir="${saaj.home}/lib" >
    <include name="activation.jar" />
    <include name="mail.jar" />
    <include name="saaj-api.jar" />
    <include name="servlet.jar" />
    <include name="jax-qname.jar" />
  </fileset>

  <!-- ==================== Init targets ==================== -->
  <target name="init" depends="checks,build.dir.webapps,build.dir.local"
	  description="Initialization and checkup" >
    <echo message="-------- Building ${appname} to ${build.dir} "/>
  </target>
  
  <target name="prepare.build" 
	  if="webapps.dir"
	  description="Preparing the webapp dir if not in-place" >
    <mkdir dir="${webapps.dir}/${appname}"/>
    <mkdir dir="${webapps.dir}/${appname}/WEB-INF"/>
    <mkdir dir="${webapps.dir}/${appname}/WEB-INF/src"/>
    <mkdir dir="${webapps.dir}/${appname}/WEB-INF/lib"/>
    <copy todir="${webapps.dir}/${appname}/WEB-INF/lib" >
          <fileset dir="${saaj-coms.jar}/../" 
      		includes="saaj-coms.jar"/>
    </copy>
    <mkdir dir="${webapps.dir}/${appname}/WEB-INF/classes"/>
    <copy todir="${webapps.dir}/${appname}" >
          <fileset dir="${appdir}" 
      		excludes="src/**"/>
    </copy>
  </target>

  <target name="prepare" depends="init,prepare.build">
    <mkdir dir="${build.dir}/${appname}/WEB-INF/classes"/>
  </target>
  
  <!-- ==================== Build ==================== -->

  <target name="main" depends="prepare,compile" />

  <target name="compile" if="src.available" >
    <javac srcdir="${appdir}/WEB-INF/src"
           destdir="${build.dir}/${appname}/WEB-INF/classes"
	   debug="${compile.debug}"
	   deprecation="${compile.deprecation}"
	   optimize="${compile.optimize}">
      <classpath >
	<fileset refid="saaj-classpath" />
	<pathelement location="${servlet.jar}" />
        <pathelement location="${saaj-coms.jar}" />
      </classpath>
    </javac>

    <copy todir="${build.dir}/${appname}/WEB-INF/classes">
      <fileset dir="${appdir}/WEB-INF/src">
	<include name="**/*.properties" />
	<include name="**/*.dtd" />
	<include name="**/*.xml" />
      </fileset>
    </copy>
  </target>

  <!-- ==================== Cleanup & Packaging ==================== -->

  <target name="clean" depends="init">
    <delete dir="{build.dir}/${appname}/WEB-INF/classes"/>
  </target>

  <target name="war" 
	  depends="main" 
	  description="Creating the WAR file" 
	  if="wars.dir" >
    <mkdir dir="${wars.dir}" />
    <jar jarfile="${wars.dir}/${appname}.war"
	 basedir="${build.dir}/${appname}" >
      <include name="**"/>
    </jar>
  </target>

  <!-- ==================== Logic and Guessing ==================== -->

  <target name="checks" depends="set.available,check.saaj,check.appname,check.servlet" />

  <target name="set.available" >
    <!-- Environement check -->
    <available property="saaj.available" file="${saaj-api.jar}" />
    <available property="saaj.servlet.available" file="${saaj-coms.jar}" />
    <property name="src.base" location="../.." />
    <available property="appname.correct" file="../../${appname}/WEB-INF/build.xml" />
    <available property="servlet.available" file="${servlet.jar}" />
    <available property="src.available" file="src" />
  </target>
  
  <target name="check.saaj" unless="saaj.available" >
    <fail message="SAAJ not found at ${saaj.home}/lib/saaj-api.jar, make sure saaj.home is set" />
  </target>

  <target name="check.servlet" unless="servlet.available" >
    <fail message="Servlet.jar not found at ${servlet.jar}, make sure servlet.jar is set" />
  </target>

  <target name="check.appname" unless="appname.correct" >
    <fail message="Appname not set, couldn't find ${appname} in ${src.base}" />
  </target>

  <target name="check.saaj.servlet" unless="saaj.servlet.available" >
    <fail message="SAAJServlet not found at ${build.dir}/samples/saajservlet/lib/saaj-coms.jar, make sure saaj-coms.jar exists" />
  </target>
  
  <target name="build.dir.webapps" if="webapps.dir" >
    <property name="build.dir" value="${webapps.dir}" />
  </target>

  <target name="build.dir.local" unless="webapps.dir" >
    <property name="build.dir" location="../.." />
  </target>

</project>
