<?xml version="1.0"?>
<!--
   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.
  
   $Header:$
 -->

<!--
  An Ant build file for compiling the netui-jsf Beehive sample web application.
-->
<project name="Beehive/Samples/NetUI-JSF-Samples" default="usage" basedir=".">

    <property environment="os"/>
    <property file="build.properties"/>

    <import file="${beehive.home}/beehive-imports.xml"/>

    <property name="build.dir" location="build"/>
    <property name="web.dir" location="web"/>
    <property name="src.dir" location="src"/>

    <!-- Define the classpath used to build the webapp -->
    <path id="webapp.classpath">
        <path refid="velocity.dependency.path"/>
        <path refid="netui-compiler.dependency.path"/>

        <pathelement location="${servlet-api.jar}"/>
        <pathelement location="${jsp-api.jar}"/>
        <pathelement location="${build.dir}/WEB-INF/classes"/>
        <fileset dir="${build.dir}/WEB-INF/lib" includes="*.jar"/>
    </path>    

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

        <deploy-netui webappDir="${build.dir}"/>

        <copy todir="${build.dir}/" includeEmptyDirs="false">
            <fileset dir="${web.dir}"/>
        </copy>

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

    <target name="build-internal">
        <taskdef name="apt" 
                 classname="org.apache.beehive.controls.runtime.generator.AptTask" 
                 classpathref="webapp.classpath"
                 onerror="fail"/>

        <apt srcdir="${src.dir}"
             destdir="${build.dir}/WEB-INF/classes"
             gendir="${build.dir}/WEB-INF/.tmpbeansrc"
             classpathref="webapp.classpath"
             srcExtensions="*.java"
             debug="true"
             processorOptions="web.content.root=${web.dir}"
             nocompile="false"/>
    </target>

    <target name="copy-jsf-jars"
            depends="init">
        <available property="jsf-api.jar.available" file="${jsf-api.jar}" type="file"/>
        <fail unless="jsf-api.jar.available" message="Can't find the JSF API JAR ${jsf-api.jar}"/>

        <available property="jsf-impl.jar.available" file="${jsf-impl.jar}" type="file"/>
        <fail unless="jsf-impl.jar.available" message="Can't find the JSF implementation JAR ${jsf-impl.jar}"/>

        <copy file="${jsf-api.jar}" todir="${build.dir}/WEB-INF/lib"/>
        <copy file="${jsf-impl.jar}" todir="${build.dir}/WEB-INF/lib"/>
    </target>

    <!-- 
        Myfaces 1.1.2 or later is dependent on commons-collections and
        commons-lang.
    -->
    <target name="copy-commons-collections" depends="init"
            if="commons-collections.jar">
        <copy file="${commons-collections.jar}" todir="${build.dir}/WEB-INF/lib"/>
    </target>

    <target name="copy-commons-lang" depends="init"
            if="commons-lang.jar">
        <copy file="${commons-lang.jar}" todir="${build.dir}/WEB-INF/lib"/>
    </target>

    <target name="init-myfaces" depends="init"
            description="set build/WEB-INF/web.xml for use with MyFaces">
        <copy file="${web.dir}/WEB-INF/web.xml.myfaces" tofile="${build.dir}/WEB-INF/web.xml"/>
    </target>

    <target name="init-jsf-ri" depends="init"
            description="set build/WEB-INF/web.xml for use with the JSF Reference Implementation">
        <copy file="${web.dir}/WEB-INF/web.xml.jsf-ri" tofile="${build.dir}/WEB-INF/web.xml"/>
    </target>

    <target name="build-myfaces"
            depends="copy-jsf-jars,copy-commons-collections,copy-commons-lang,init-myfaces,build-internal"
            description="Build the netui-jsf webapp, for use with MyFaces"/>

    <target name="build-jsf-ri"
            depends="copy-jsf-jars,init-jsf-ri,build-internal"
            description="Build the netui-jsf webapp, for use with the JSF Reference Implementation"/>

    <target name="build" depends="build-myfaces"
            description="Build the webapp with MyFaces"/>

    <target name="clean" description="Clean the netui-jsf webapp">
        <delete dir="${build.dir}"/>
    </target>

    <target name="war" description="Build a compressed WAR file that can be deployed to an application container">
        <property name="archive.dir" value="${build.dir}/.."/>

        <!-- this does *not* exclude source from the archive -->
        <war destfile="${archive.dir}/${context.path}.war" webxml="${build.dir}/WEB-INF/web.xml">
            <fileset dir="${build.dir}">
                <exclude name="WEB-INF/web.xml"/>
            </fileset>
        </war>
    </target>

    <target name="usage" description="Print usage information for this build file">
        <echo>
Welcome to the Beehive netui-jsf webapp!

ant clean          -- cleans the netui-jsf webapp
ant build-myfaces  -- builds the netui-jsf webapp against MyFaces (v1.0.9 or later)
ant build-jsf-ri   -- builds the netui-jsf webapp against the JSF Reference Implementation (v1.1.01)
ant war            -- creates a .war archive to deploy; build-myfaces or build-jsf-ri must be called first
        </echo>

        <java fork="no" classname="org.apache.tools.ant.Main">
            <arg line="-f ${ant.file} -projecthelp"/>
        </java>
    </target>

</project>
