<?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.
  -->
<project name="spring-sample" default="dist">

    <property name="root.base.dir" value="../.."/>

    <!-- "extend" parent build definition -->
    <import file="../../build.xml"/>

    <property name="module.name" value="spring"/>
    <property name="module.base.dir" value="${root.base.dir}/samples/spring"/>
    <property name="dist.jar.name" value="${project.name}-${module.name}.jar"/>
    <property name="dist.jar" value="${root.dist.dir}/${dist.jar.name}"/>

    <!-- Module properties -->
    <property name="dist.war.name" value="${project.name}-${module.name}.war"/>
    <property name="dist.war" value="${root.base.dir}/build/${dist.war.name}"/>

    <property name="dist.dir" value="${build.dir}/dist"/>

    <!-- Deployment properties for development -->
    <property name="deploy.dir" value="${env.CATALINA_HOME}/webapps"/>

    <path id="compile.path">
        <fileset dir="${lib.dir}/samples" includes="**/*.jar"/>
    </path>

    <!-- ===================================================================
  -  clean - clean all build remnants from this submodule
  - ==================================================================== -->
    <target name="clean" depends="clean-deploy-dir" description="Cleans the Spring build">
        <echo message="cleaning ${module.name} module"/>
        <delete file="${dist.war}"/>
        <delete dir="${build.dir}"/>
    </target>

    <target name="clean-deploy-dir" description="Removes the deployed war directory">
        <echo message="cleaning ${dist.war.name} module from ${deploy.dir}"/>
        <delete file="${deploy.dir}/${dist.war.name}"/>
        <delete dir="${deploy.dir}/${project.name}-${module.name}"/>
    </target>

    <!-- ===================================================================
 -  compile - compile Java source files
 -  =================================================================== -->
    <target name="compile" description="Compiles the Spring sample app classes">

        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${classes.dir}/META-INF"/>

        <javac destdir="${classes.dir}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               optimize="${compile.optimize}"
               failonerror="true"
               classpathref="compile.path">
            <src path="${src.dir}"/>
            <classpath location="${root.dist.dir}/jsecurity.jar"/>
        </javac>

    </target>

    <target name="jar" depends="compile" description="Creates a .jar of the Spring sample app classes">

        <mkdir dir="${dist.dir}"/>

        <jar jarfile="${dist.dir}/jsecurity-spring-sample.jar">
            <fileset dir="${classes.dir}">
                <include name="**/*.class"/>
            </fileset>
            <fileset dir="${etc.dir}">
                <include name="*.xml"/>
                <include name="*.properties"/>
            </fileset>
            <!-- grabbing logo from webroot-->
            <fileset dir="webroot">
                <include name="logo.png"/>
            </fileset>
            <manifest>
                <attribute name="Implementation-Title"
                           value="${project.name}-${module.name}"/>
                <attribute name="Implementation-Version" value="${version}"/>
                <attribute name="${project.name}-Version" value="${version}"/>
            </manifest>
        </jar>
    </target>

    <!-- ===================================================================
 -  dist - create distribution war (which will be used for deployment)
 -  =================================================================== -->
    <target name="dist" depends="compile,jar" description="Creates the sample Spring application .war">

        <property name="webstart.lib.dir" value="${build.dir}/webstart"/>
        <mkdir dir="${webstart.lib.dir}"/>
        <copy todir="${webstart.lib.dir}" preservelastmodified="true" flatten="true">
            <fileset dir="${dist.dir}" includes="jsecurity-spring-sample.jar"/>
            <fileset dir="${root.base.dir}" includes="**/jsecurity.jar"/>
            <fileset dir="${lib.dir}/samples" includes="spring.jar"/>
            <fileset dir="${lib.dir}/samples" includes="commons-*.jar"/>
        </copy>
        <signjar keystore="${etc.dir}/jsecurity-sample.jks"
                 alias="jsecurity"
                 keypass="jsecurity"
                 storepass="jsecurity">
            <fileset dir="${webstart.lib.dir}" includes="*.jar"/>
        </signjar>

        <war warfile="${dist.war}" webxml="webroot/WEB-INF/web.xml">
            <lib dir="${lib.dir}/samples" includes="*.jar" excludes="servlet-api.jar"/>
            <lib dir="${root.dist.dir}" includes="jsecurity.jar"/>
            <webinf dir="${basedir}" includes="resources/**"/>
            <fileset dir="webroot" includes="**" excludes="**/web.xml"/>

            <lib dir="${dist.dir}" includes="*.jar"/>

            <!-- Jar for webstart application -->
            <fileset dir="${webstart.lib.dir}" includes="*.jar"/>
        </war>
    </target>

    <target name="quick-deploy" description="Blindly deploys the sample Spring .war to Tomcat">
        <echo>Deploying application to Tomcat...</echo>
        <unzip src="${dist.war}" dest="${env.CATALINA_HOME}/webapps/${project.name}-${module.name}"/>
        <touch file="${env.CATALINA_HOME}/webapps/${project.name}-${module.name}/WEB-INF/web.xml"/>
    </target>

    <target name="deploy" depends="dist,quick-deploy"
            description="Creates and deploys the sample Spring .war to Tomcat"/>

</project>