<!--
BEGIN COPYRIGHT BLOCK
Copyright (C) 2005 Red Hat, Inc.
All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation version
2.1 of the License.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
END COPYRIGHT BLOCK
-->

<project name="console" default="build_jars" basedir=".">
  <!-- Load properties -->
  <property file="build.properties"/>

  <!-- Paths -->
  <property name="built.dir"        value="${console.root}/../built"/>
  <property name="built.classdir"   value="${built.dir}/classes"/>
  <property name="release.dir"      value="${built.dir}/release"/>
  <property name="built.jardir"     value="${release.dir}/jars"/>
  <property name="package.dir"      value="${release.dir}/package"/>
  <property name="dist.dir"         value="${release.dir}/dist"/>
  <property name="lib.dir"          value="${package.dir}/lib"/>
  <property name="java.dir"         value="${package.dir}/java"/>
  <property name="javadoc.dir"      value="${built.dir}/doc"/>
  <property name="imports.dir"      value="${console.root}/../imports"/>
  <property name="java.src.dir"     value="src/main/java"/>
  <property environment="env"/>

  <!-- Prepare -->
  <target name="prepare_build" depends="prepare_imports">
    <!-- Create directories -->
    <mkdir dir="${built.classdir}"/>

    <!-- Classdest is the location you intend to install the Console jars -->
    <property name="classdest"              value="/usr/share/java"/>

    <!-- Override these properties to use components from other system locations -->
    <property name="ldapjdk.local.location" value="/usr/share/java"/>
    <property name="jss.local.location"     value="/usr/lib/java"/>
    <property name="jsslib.local.location"  value="/usr/lib"/>
    <property name="nss.local.location"     value="/usr/lib"/>
    <property name="nspr.local.location"    value="/usr/lib"/>

    <!-- Construct classpath -->
    <path id="console.classpath">
      <pathelement location="${ldapjdk.local.location}/${ldapjdk.jar.name}"/>
      <pathelement location="${jss.local.location}/${jss.jar.name}"/>
    </path>

    <!-- Generate version property file -->
    <exec executable="${console.root}/buildnum.pl" vmlauncher="true">
      <arg line="-p ${built.dir}"/>
    </exec>

    <exec executable="${console.root}/src/main/java/com/netscape/management/client/console/genverinfo.sh">
      <arg value="${console.root}/src/main/java/com/netscape/management/client/console/versioninfo.properties"/>
      <arg value="${console.dotversion}"/>
      <arg value="${console.dotgenversion}"/>
      <arg value="${built.dir}/buildnum.dat"/>
    </exec>
  </target>

  <target name="prepare_imports" if="imports.file">
    <mkdir dir="${imports.dir}"/>

    <!-- Load imports file -->
    <fail message="Imports file does not exist. (${imports.file})">
      <condition>
        <not>
          <available file="${imports.file}"/>
        </not>
      </condition>
    </fail>
    <property file="${imports.file}"/>

    <!-- Set local component locations -->
    <property name="ldapjdk.local.location" value="${imports.dir}/ldapjdk"/>
    <property name="jss.local.location"     value="${imports.dir}/jss"/>
    <property name="jsslib.local.location"  value="${imports.dir}/jss/lib"/>
    <property name="nss.local.location"     value="${imports.dir}/nss/lib"/>
    <property name="nspr.local.location"    value="${imports.dir}/nspr/lib"/>

    <!-- Check if components exist -->
    <available file="${imports.dir}/ldapjdk/${ldapjdk.import}" property="ldapjdk.import.exists"/>
    <available file="${imports.dir}/jss/${jss.jar.import}"     property="jss.jar.import.exists"/>
    <available file="${imports.dir}/jss/${jss.lib.import}"     property="jss.lib.import.exists"/>
    <available file="${imports.dir}/nss/${nss.import}"         property="nss.import.exists"/>
    <available file="${imports.dir}/nspr/${nspr.import}"       property="nspr.import.exists"/>
  </target>

  <target name="prepare_jars">
    <!-- Create directories -->
    <mkdir dir="${package.dir}"/>
    <mkdir dir="${built.jardir}"/>
  </target>

  <target name="prepare_package">
    <!-- Create directories -->
    <mkdir dir="${lib.dir}"/>
    <mkdir dir="${dist.dir}"/>
  </target>

  <target name="prepare_doc">
    <!-- Create directories -->
    <mkdir dir="${javadoc.dir}"/>
  </target>

  <!-- Imports -->
  <target name="import_ldapjdk" depends="prepare_imports" if="imports.file" unless="ldapjdk.import.exists">
    <mkdir dir="${imports.dir}/ldapjdk"/>
    <get src="${ldapjdk.location}/${ldapjdk.import}"
         dest="${imports.dir}/ldapjdk/${ldapjdk.import}"/>
  </target>

  <target name="import_jss_jar" depends="prepare_imports" if="imports.file" unless="jss.jar.import.exists">
    <mkdir dir="${imports.dir}/jss"/>
    <get src="${jss.jar.location}/${jss.jar.import}"
         dest="${imports.dir}/jss/${jss.jar.import}"/>
    <copy file="${imports.dir}/jss/${jss.jar.import}" tofile="${imports.dir}/jss/${jss.jar.name}"/>
  </target>

  <target name="import_jss_lib" depends="prepare_imports" if="imports.file" unless="jss.lib.import.exists">
    <mkdir dir="${imports.dir}/jss"/>
    <get src="${jss.lib.location}/${jss.lib.import}"
         dest="${imports.dir}/jss/${jss.lib.import}"/>
    <unjar src="${imports.dir}/jss/${jss.lib.import}" dest="${imports.dir}/jss"/>
  </target>

  <target name="import_nss_lib" depends="prepare_imports" if="imports.file" unless="nss.import.exists">
    <mkdir dir="${imports.dir}/nss"/>
    <get src="${nss.location}/${nss.import}"
         dest="${imports.dir}/nss/${nss.import}"/>
    <unjar src="${imports.dir}/nss/${nss.import}" dest="${imports.dir}/nss"/>
  </target>

  <target name="import_nspr_lib" depends="prepare_imports" if="imports.file" unless="nspr.import.exists">
    <mkdir dir="${imports.dir}/nspr"/>
    <get src="${nspr.location}/${nspr.import}"
         dest="${imports.dir}/nspr/${nspr.import}"/>
    <unjar src="${imports.dir}/nspr/${nspr.import}"  dest="${imports.dir}/nspr"/>
  </target>

  <!-- Build -->
  <target name="build" depends="prepare_build, import_ldapjdk, import_jss_jar">
    <!-- Verify that the required jars exist -->
    <fail message="LDAP JDK (${ldapjdk.jar.name}) does not exist in ${ldapjdk.local.location}">
      Please set the correct location with -Dldapjdk.local.location=[path]
      <condition>
        <not>
          <available file="${ldapjdk.local.location}/${ldapjdk.jar.name}"/>
        </not>
      </condition>
    </fail>
    <fail message="JSS (${jss.jar.name}) does not exist in ${jss.local.location}">
      Please set the correct location with -Djss.local.location=[path]
      <condition>
        <not>
          <available file="${jss.local.location}/${jss.jar.name}"/>
        </not>
      </condition>
    </fail>

    <!-- compile regular java code -->
    <javac   srcdir="${java.src.dir}"
             destdir="${built.classdir}"
             debug="${compile.debug}"
             debuglevel="lines,vars,source"
             deprecation="${compile.deprecation}"
             optimize="${compile.optimize}"
             verbose="no"
             fork="true"
             excludes="**/CVS/**">
             <classpath refid="console.classpath" />
    </javac>
  </target>

  <!-- Package -->
  <target name="build_jars" depends="build,prepare_jars">
    <!-- Create jar files -->
    <jar jarfile="${built.jardir}/${mcc.core}.jar" >
      <fileset dir="${built.classdir}">
         <include name="com/netscape/management/client/**/*.class" />
         <include name="sun/net/www/protocol/https/**/*.class" />
         <exclude name="com/netscape/management/client/topology/IServerObject.class"/>
         <exclude name="com/netscape/management/client/topology/IRemovableServerObject.class"/>
         <exclude name="com/netscape/management/client/topology/IProductObject.class"/>
         <exclude name="com/netscape/management/client/topology/AbstractServerObject.class"/>
         <exclude name="com/netscape/management/client/topology/ServerNode.class"/>
         <exclude name="com/netscape/management/client/IPage.class"/>
         <exclude name="com/netscape/management/client/IFramework.class"/>
         <exclude name="com/netscape/management/client/IFrameworkInitializer.class"/>
         <exclude name="com/netscape/management/client/FrameworkInitializer.class"/>
         <exclude name="com/netscape/management/client/IResourceObject.class"/>
         <exclude name="com/netscape/management/client/ResourceObject.class"/>
         <exclude name="com/netscape/management/client/util/ClassLoaderUtil.class"/>
         <exclude name="com/netscape/management/client/util/LocalJarClassLoader.class"/>
         <exclude name="com/netscape/management/client/util/KingpinClassLoader.class"/>
         <exclude name="com/netscape/management/client/util/UtilConsoleGlobals.class"/>
         <exclude name="com/netscape/management/client/console/ConsoleInfo.class"/>
         <exclude name="com/netscape/management/client/console/VersionInfo.class"/>
      </fileset>
    </jar>
    <jar jarfile="${built.jardir}/${mcc.core}_${lang}.jar" >
      <fileset dir="${java.src.dir}">
         <include name="com/netscape/management/client/**/*.properties" />
         <include name="com/netscape/management/client/**/*.gif" />
         <include name="com/netscape/management/client/**/*.jpg" />
      </fileset>
    </jar>
    <jar jarfile="${built.jardir}/${nmclf.core}.jar" >
      <fileset dir="${built.classdir}">
         <include name="com/netscape/management/nmclf/**/*.class" />
      </fileset>
    </jar>
    <jar jarfile="${built.jardir}/${nmclf.core}_${lang}.jar" >
      <fileset dir="${java.src.dir}">
         <include name="com/netscape/management/nmclf/**/*.properties" />
         <include name="com/netscape/management/nmclf/**/*.gif" />
         <include name="com/netscape/management/nmclf/**/*.jpg" />
      </fileset>
    </jar>
    <jar jarfile="${built.jardir}/${base.core}.jar" >
      <fileset dir="${built.classdir}">
        <include name="com/netscape/management/client/topology/IServerObject.class"/>
        <include name="com/netscape/management/client/topology/IRemovableServerObject.class"/>
        <include name="com/netscape/management/client/topology/IProductObject.class"/>
        <include name="com/netscape/management/client/topology/AbstractServerObject.class"/>
        <include name="com/netscape/management/client/topology/ServerNode.class"/>
        <include name="com/netscape/management/client/IPage.class"/>
        <include name="com/netscape/management/client/IFramework.class"/>
        <include name="com/netscape/management/client/IFrameworkInitializer.class"/>
        <include name="com/netscape/management/client/FrameworkInitializer.class"/>
        <include name="com/netscape/management/client/IResourceObject.class"/>
        <include name="com/netscape/management/client/ResourceObject.class"/>
        <include name="com/netscape/management/client/util/ClassLoaderUtil.class"/>
        <include name="com/netscape/management/client/util/LocalJarClassLoader.class"/>
        <include name="com/netscape/management/client/util/KingpinClassLoader.class"/>
        <include name="com/netscape/management/client/util/UtilConsoleGlobals.class"/>
        <include name="com/netscape/management/client/console/ConsoleInfo.class"/>
        <include name="com/netscape/management/client/console/VersionInfo.class"/>
      </fileset>
    </jar>
  </target>

  <!-- Package -->
  <target name="package" depends="build_jars,prepare_package,import_jss_lib,import_nss_lib,import_nspr_lib">
    <!-- Copy built jars to packaging area -->
    <copy file="${built.jardir}/${mcc.core}.jar" todir="${java.dir}"/>
    <copy file="${built.jardir}/${mcc.core}_${lang}.jar" todir="${java.dir}"/>
    <copy file="${built.jardir}/${nmclf.core}.jar" todir="${java.dir}"/>
    <copy file="${built.jardir}/${nmclf.core}_${lang}.jar" todir="${java.dir}"/>
    <copy file="${built.jardir}/${base.core}.jar" todir="${java.dir}"/>

    <!-- Copy components into packaging area -->
    <copy file="${ldapjdk.local.location}/${ldapjdk.jar.name}" todir="${java.dir}"/>
    <copy file="${jss.local.location}/${jss.jar.name}" todir="${java.dir}"/>
    <copy todir="${lib.dir}">
      <fileset dir="${jsslib.local.location}">
        <include name="libjss*"/>
      </fileset>
      <fileset dir="${nss.local.location}">
        <include name="libnss*"/>
        <include name="libssl*"/>
        <include name="libsmime*"/>
        <include name="libsoftokn*"/>
        <include name="libfreebl*"/>
        <exclude name="libnssckbi*"/>
        <exclude name="lib*.a"/>
      </fileset>
      <fileset dir="${nspr.local.location}">
        <include name="libnspr*"/>
        <include name="libpl*"/>
        <exclude name="lib*.a"/>
      </fileset>
    </copy>

    <!-- Copy start script into packaging area -->
    <copy file="startconsole-sandbox" tofile="${package.dir}/startconsole"/>
    <chmod file="${package.dir}/startconsole" perm="755"/>

    <!-- Create a bundle of the Console -->
    <tar destfile="${dist.dir}/${console.brand}-console-${console.dotversion}.tar.gz"
         compression="gzip">
      <tarfileset dir="${package.dir}"
                  prefix="${console.brand}-console-${console.dotversion}"
                  mode="755">
        <include name="startconsole"/>
      </tarfileset>
      <tarfileset dir="${package.dir}"
                  prefix="${console.brand}-console-${console.dotversion}">
        <include name="**"/>
        <exclude name="startconsole"/>
      </tarfileset>
    </tar>
  </target>

  <!-- Javadoc -->
  <target name="javadoc" depends="prepare_doc">
    <javadoc
      packagenames="com.netscape.management.*"
      sourcepath="src/main/java"
      destdir="${javadoc.dir}"
      author="true"
      version="true"
      use="true"
      windowtitle="Console API"
      doctitle="Console API" />
  </target>

  <!-- Cleanup -->
  <target name="clean_all" depends="clean,clean_imports"/>

  <target name="clean">
    <delete dir="${built.dir}"/>
    <delete file="${console.root}/src/main/java/com/netscape/management/client/console/versioninfo.properties"/>
  </target>

  <target name="clean_imports">
    <delete dir="${imports.dir}"/>
  </target>

</project>
