<!--
  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="cxf  build file" default="build">


    <property environment="env"/>
    <property name="build.dir" location ="${basedir}/build"/>
    <property name="build.classes.dir" location ="${build.dir}/classes"/>
    <available property="src.dir" value="${basedir}/src/main/java"   file="${basedir}/src/main/java" type="dir"/>
    <property name="src.dir" location ="${basedir}/src"/>
    <property name="build.src.dir" location ="${build.dir}/src"/>
    <property name="build.var.dir" location ="${build.dir}/var"/>
    <property name="codegen.timestamp.file" value="${build.src.dir}/.CODEGEN_DONE"/>
    <property name="war.dir" location="${build.dir}/war"/>

    <property name="host" value="localhost"/>
    <property name="port" value="8080"/>
    <property name="base.url" value="http://${host}:${port}"/>

    <available property="wsdl.dir" value="${basedir}/wsdl"
        file="${basedir}/wsdl"/>
    <property name="wsdl.dir" location="."/>

    <!-- Setup for building in a source build-->
    <available property="srcbuild_paths.file"
        value="../../../../../target/srcbuild_env.ant"
        file="target/srcbuild_env.ant"
        filepath="../../../../.."/>
    <available property="srcbuild_paths.file"
       value="../../../../../../target/srcbuild_env.ant"
       file="target/srcbuild_env.ant"
       filepath="../../../../../.."/>
    <property name="srcbuild_paths.file" value="${build.dir}/notsrcbuildpath"/>
    <mkdir dir="${build.dir}"/>
    <touch file="${build.dir}/notsrcbuildpath" verbose="false"/>
    <loadfile property="srcbuild.classpath" srcFile="${srcbuild_paths.file}" failonerror="false"/>

    <uptodate property="codegen.notrequired" value="true">
        <srcfiles dir="${wsdl.dir}" includes="**/*.wsdl"/>
        <srcfiles dir="${wsdl.dir}" includes="**/*.xsd"/>
        <mapper type="merge" to="${codegen.timestamp.file}"/>
    </uptodate>

    <!-- find the tools jar -->
    <available property="tools.jar" value="${env.JAVA_HOME}/lib/tools.jar"
       file="${env.JAVA_HOME}/lib/tools.jar"/>
    <path id="jdk.tools.classpath">
        <pathelement location="${tools.jar}"/>
    </path>

    <condition property="is.java.version.15">
        <or>
            <equals arg1="${ant.java.version}" arg2="1.5"/>
            <equals arg1="${ant.java.version}" arg2="1.6"/>
            <equals arg1="${ant.java.version}" arg2="1.7"/>
        </or>
    </condition>

    <fail message="Apache CXF requires Java version 1.5 or higher. You are currently using Java version ${ant.java.version}."
        unless="is.java.version.15"/>



    <!-- Determine cxf.home, either from the environment variable CXF_HOME
       - or using ../..
       -->
    <condition property="cxf.home" value="${env.CXF_HOME}">
        <isset property="env.CXF_HOME"/>
    </condition>
    <condition property="cxf.home" value="../..">
        <available file="../../etc" type="dir" property=""/>
    </condition>
    <condition property="cxf.home" value="../../..">
        <available file="../../../etc" type="dir" property=""/>
    </condition>
    <property name="cxf.home" location="../.."/>
    <property name="cxf.etc.dir" location="${cxf.home}/etc"/>

    <condition property="cxf.endorsed.dir" value="${cxf.home}/lib/endorsed">
        <available file="${cxf.home}/lib/endorsed" type="dir" property=""/>
    </condition>
    <condition property="cxf.endorsed.flag" value="-Djava.endorsed.dirs=${cxf.endorsed.dir}">
        <available file="${cxf.home}/lib/endorsed" type="dir" property=""/>
    </condition>
    <property name="cxf.endorsed.dir" location="${cxf.home}"/>
    <property name="cxf.endorsed.flag" value="-Dnone=none"/>

    <path id="cxf.classpath">
        <pathelement location="${basedir}"/>
        <pathelement location="${build.classes.dir}"/>
        <pathelement path="${srcbuild.classpath}"/>
        <pathelement location="${cxf.home}/lib/cxf-manifest.jar"/>
        <fileset dir="${cxf.endorsed.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>
    <condition property="thirdparty.classpath" value="">
        <not>
            <isset property="thirdparty.classpath"/>
        </not>
    </condition>

    <target name="maybe.generate.code" unless="codegen.notrequired">
        <antcall target="generate.code"/>
        <touch file="${codegen.timestamp.file}"/>
    </target>

    <target name="compile" depends="maybe.generate.code">
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${build.src.dir}"/>

        <javac destdir="${build.classes.dir}" debug="true" fork="true" encoding="utf-8">
            <compilerarg line="-J${cxf.endorsed.flag}"/>
            <src path="${src.dir}"/>
            <src path="${build.src.dir}"/>
            <classpath>
                <path refid="cxf.classpath"/>
                <pathelement path="${thirdparty.classpath}"/>
            </classpath>
        </javac>

        <copy todir="${build.classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java" />
            <fileset dir="${build.src.dir}" includes="**/*.java" />
        </copy>
        <available property="maven.resources.dir.exists" file="${basedir}/src/main/resources" type="dir" />
        <antcall target="copy.maven.resources"/>
    </target>

    <target name="copy.maven.resources" if="maven.resources.dir.exists">
        <copy todir="${build.classes.dir}">
            <fileset dir="${basedir}/src/main/resources" includes="**/*" />
        </copy>
    </target>

    <target name="checkstyle">
        <checkstyle config="${checkstyle.config.file}">
            <fileset dir="${basedir}/src" includes="**/*.java"/>
            <classpath>
                <path refid="cxf.classpath"/>
                <pathelement path="${thirdparty.classpath}"/>
            </classpath>
        </checkstyle>
    </target>

    <target name="clean">
        <delete dir="${build.classes.dir}"/>
        <delete dir="${build.src.dir}"/>
        <delete file="${codegen.timestamp.file}"/>
        <delete file="demo.log"/>
        <delete dir="${build.var.dir}"/>
        <delete dir="${activemq.home}/var"/>
        <delete dir="${build.dir}"/>
    </target>


    <target name="build"  depends="compile" description="build demo client and server"/>

    <macrodef name="cxfrun">
        <attribute name="logging-properties-file" default="${cxf.etc.dir}/logging.properties"/>
        <attribute name="classname"/>
        <attribute name="param1" default=""/>
        <attribute name="param2" default=""/>
        <attribute name="param3" default=""/>
        <attribute name="param4" default=""/>
        <attribute name="param5" default=""/>
        <attribute name="jvmarg1" default="-D' '"/>
        <attribute name="jvmarg2" default="-D' '"/>
        <attribute name="jvmarg3" default="-D' '"/>
        <attribute name="jvmarg4" default="-D' '"/>
        <attribute name="jvmarg5" default="-D' '"/>
        <sequential>
            <java classname="@{classname}" fork="yes">
                <classpath>
                    <path refid="cxf.classpath"/>
                    <pathelement path="${thirdparty.classpath}"/>
                    <path refid="jdk.tools.classpath"/>
                </classpath>
                <arg value="@{param1}"/>
                <arg value="@{param2}"/>
                <arg value="@{param3}"/>
                <arg value="@{param4}"/>
                <arg value="@{param5}"/>
                <jvmarg value="-Dcxf.home=${cxf.home}"/>
                <jvmarg value="${cxf.endorsed.flag}"/>
                <jvmarg value="@{jvmarg1}"/>
                <jvmarg value="@{jvmarg2}"/>
                <jvmarg value="@{jvmarg3}"/>
                <jvmarg value="@{jvmarg4}"/>
                <jvmarg value="@{jvmarg5}"/>
                <!--assertions>
                    <enable package="org.apache.cxf"/>
                </assertions-->
                <sysproperty key="java.util.logging.config.file" value="@{logging-properties-file}"/>
            </java>
        </sequential>
    </macrodef>

    <macrodef name="wsdl2java">
        <attribute name="srcdestdir" default="${build.src.dir}"/>
        <attribute name="destdir" default="${build.classes.dir}"/>
        <attribute name="file"/>
        <attribute name="servicename.arg" default=""/>
        <attribute name="bindingfile" default=""/>
        <attribute name="databinding" default=""/>
        <attribute name="dir" default="${wsdl.dir}"/>
        <attribute name="package" default="NOT_SPECIFIED"/>
        <sequential>
            <mkdir dir="@{destdir}"/>
            <mkdir dir="@{srcdestdir}"/>
            <condition property="package.arg.@{file}" value="-p @{package}">
                <not>
                    <equals arg1="@{package}" arg2="NOT_SPECIFIED"/>
                </not>
            </condition>
            <property name="package.arg.@{file}" value=""/>
            <condition property="binding.arg" value='-b "@{bindingfile}"'>
                <not>
                    <equals arg1="@{bindingfile}" arg2=""/>
                </not>
            </condition>
            <property name="binding.arg" value=""/>
            <condition property="databinding.arg" value='-db "@{databinding}"'>
                <not>
                    <equals arg1="@{databinding}" arg2=""/>
                </not>
            </condition>
            <property name="databinding.arg" value=""/>
            <java failonerror="true" classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="yes">
                <classpath>
                    <path refid="cxf.classpath" />
                </classpath>
                <jvmarg value="${cxf.endorsed.flag}"/>
                <sysproperty key="java.util.logging.config.file" value="${cxf.etc.dir}/logging.properties"/>
                <sysproperty key="exitOnFinish" value="true"/>
                <arg line="@{servicename.arg}"/>
                <arg line="${package.arg.@{file}}"/>
                <arg line="${databinding.arg}"/>
                <arg line="${binding.arg}"/>
                <arg value="-verbose"/>
                <arg value="-d"/>
                <arg value="@{srcdestdir}"/>
                <arg value="@{dir}/@{file}"/>
            </java>
        </sequential>
    </macrodef>
		

    <property name="war-lib" value="${basedir}/libs" />
    <property name="config.dir" value="${basedir}/config" />
		

    <macrodef name="cxfwar">
        <attribute name="filename"/>
        <attribute name="wsdl" default=""/>
        <attribute name="dir" default="${war.dir}"/>
        <attribute name="classesdir" default="${build.classes.dir}"/>
        <attribute name="webxml" default="${cxf.home}/etc/web.xml" />
        <sequential>
            <mkdir dir="@{dir}"/>
            <mkdir dir="${war-lib}"/>
            <mkdir dir="${config.dir}"/>

            <antcall target="copy-war-libs"/>
            
            <available property="has.config.webinf" file="${basedir}/webapp/WEB-INF" type="dir" />
            <available property="has.config.metainf" file="${basedir}/webapp/META-INF" type="dir" />
            <antcall target="copy-config-files-webinf" />
            <antcall target="copy-config-files-metainf" />

            <delete file="@{dir}/@{filename}" />
            <war destfile="@{dir}/@{filename}" webxml="@{webxml}">
                <classes dir="@{classesdir}"/>
                <webinf dir="${wsdl.dir}">
                    <include name="cxf-servlet.xml"/>
                </webinf>
                <webinf dir="${wsdl.dir}/..">
                    <include name="wsdl/@{wsdl}"/>
                </webinf>
                <webinf dir="${config.dir}">
                    <include name="*.*" />
                    <exclude name="context.xml"/>
                </webinf>

                <metainf dir="${config.dir}">
                    <include name="context.xml" />
                </metainf>

                <lib dir="${war-lib}">
                    <include name="*.jar"/>
                </lib>
            </war>
            

            <delete dir="${war-lib}" />
            <delete dir="${config.dir}" />
        </sequential>
    </macrodef>
    

    <target name="copy-war-libs" unless="without.libs">
        <copy todir="${war-lib}">
            <fileset dir="${cxf.home}/lib">
                <exclude name="servlet-api-*.jar" />
                <exclude name="geronimo-servlet_*.jar" />
                <exclude name="jetty-*.jar"/>
                <exclude name="WHICH_JARS" />
            </fileset>
        </copy>
    </target>
    
    <target name="copy-config-files-webinf" if="has.config.webinf">
        <copy todir="${config.dir}" >
            <fileset dir="${basedir}/webapp/WEB-INF">
                <include name="*.xml" />
                <exclude name="web.xml" />
            </fileset>
        </copy>
    </target>
    <target name="copy-config-files-metainf" if="has.config.metainf">
        <copy todir="${config.dir}" >
            <fileset dir="${basedir}/webapp/META-INF">
                <include name="context.xml" />
            </fileset>
        </copy>
    </target>

    <target name="deploy" description="deploy the application into the container">
        <antcall target="war"/>
        <antcall target="deploy-tomcat"/>
    </target>
    <target name="undeploy" depends="undeploy-tomcat" description="undeploy the application from the container"/>

    <target name="deploy-tomcat">
        <antcall target="validate-tomcat"/>
        <copy file="${war.dir}/${cxf.war.file.name}.war"
              todir="${env.CATALINA_HOME}/webapps"/>
    </target>

    <target name="undeploy-tomcat">
        <antcall target="validate-tomcat"/>
        <delete file="${env.CATALINA_HOME}/webapps/${cxf.war.file.name}.war"/>
        <delete dir="${env.CATALINA_HOME}/webapps/${cxf.war.file.name}"/>
    </target>

    <target name="validate-tomcat">
        <fail unless="env.CATALINA_HOME" message="You should set the CATALINA_HOME, if you want to deploy into tomcat"/>
    </target>

</project>
