From 2318cce57f4a89c3f0fbe5fb59dfa00937709637 Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Fri, 26 Jul 2013 13:04:49 +0000
Subject: [PATCH] OPENDJ-1032 RPM package should be built in a different way for more visibility.

---
 /dev/null                                                                             |  458 -------------------------
 opendj-sdk/opends/build.xml                                                           |   76 ++-
 opendj-sdk/opends/resource/rpm/files                                                  |   26 +
 opendj-sdk/opends/src/build-tools/org/opends/build/tools/Utilities.java               |    1 
 opendj-sdk/opends/resource/rpm/excludedFiles                                          |   26 +
 opendj-sdk/opends/resource/rpm/specFile                                               |  206 +++++++++++
 opendj-sdk/opends/src/build-tools/org/opends/build/tools/ProcessFilesForPackages.java |  262 ++++++++++++++
 7 files changed, 570 insertions(+), 485 deletions(-)

diff --git a/opendj-sdk/opends/build.xml b/opendj-sdk/opends/build.xml
index 09cc265..0f8027b 100644
--- a/opendj-sdk/opends/build.xml
+++ b/opendj-sdk/opends/build.xml
@@ -1336,47 +1336,71 @@
 
     <property name="prefix"    value="/opt/opendj"/>
     <property name="release"   value="1"/>
-    <taskdef name="genrpm"
-             classname="org.opends.build.tools.GenerateRpm">
+
+    <mkdir dir="${build.dir}/rpm/BUILD" />
+    <mkdir dir="${build.dir}/rpm/RPMS" />
+    <mkdir dir="${build.dir}/rpm/SOURCES" />
+    <mkdir dir="${build.dir}/rpm/SPECS" />
+    <mkdir dir="${build.dir}/rpm/SRPMS" />  
+  	 
+    <condition property="rpmRelease" value="${release}" else="${release}.${pkg_version_qualifier}">
+        <or>
+            <not>
+                <isset property="pkg_version_qualifier"/>
+            </not>
+            <equals arg1="${pkg_version_qualifier}" arg2="" />
+        </or>
+    </condition>
+  	
+    <copy todir="${build.dir}/rpm/SPECS">
+      <fileset file="resource/rpm/specFile"/>
+      <filterset begintoken="[[ " endtoken=" ]]">
+        <filter token="topDir" value="${build.dir}/rpm"/>
+        <filter token="prefix" value="${prefix}"/>
+        <filter token="pre" value="${SHORT_NAME}-${VERSION_NUMBER_STRING}"/>
+        <filter token="productName" value="${PRODUCT_NAME}"/>
+        <filter token="shortName" value="${SHORT_NAME}"/>
+        <filter token="pkgName" value="${PKG_NAME}"/>
+        <filter token="referenceURL" value="${DOC_REFERENCE_HOME}"/>
+        <filter token="sourceDirName" value="${package.dir}/${SHORT_NAME}-${VERSION_NUMBER_STRING}"/>
+        <filter token="specFileName" value="${build.dir}/rpm/SPECS/specFile"/>
+        <filter token="version" value="${pkg_version_string}"/>      	
+        <filter token="release" value="${rpmRelease}" />
+      </filterset>
+    </copy>
+
+    <taskdef name="processFilesForPackages"
+                     classname="org.opends.build.tools.ProcessFilesForPackages">
       <classpath>
         <fileset dir="${build.dir}/build-tools">
           <include name="*.jar" />
         </fileset>
       </classpath>
     </taskdef>
-
-    <mkdir dir="${build.dir}/rpm/BUILD" />
-    <mkdir dir="${build.dir}/rpm/RPMS" />
-    <mkdir dir="${build.dir}/rpm/SOURCES" />
-    <mkdir dir="${build.dir}/rpm/SPECS" />
-    <mkdir dir="${build.dir}/rpm/SRPMS" />
-
-    <genrpm
-        topDir="${build.dir}/rpm"
-        prefix="${prefix}"
-        productName="${PRODUCT_NAME}"
-        shortName="${PKG_NAME}"
-        referenceURL="${DOC_REFERENCE_HOME}"
-        sourceDirName="${package.dir}/${SHORT_NAME}-${VERSION_NUMBER_STRING}"
-        specFileName="${build.dir}/rpm/SPECS/specFile"
-        version="${pkg_version_string}"
-        release="${release}.${pkg_version_qualifier}"
-        overwrite="true">
-    </genrpm>
-
+    <processFilesForPackages sourceDirName="${package.dir}/${SHORT_NAME}-${VERSION_NUMBER_STRING}" />           
+    <copy file="resource/rpm/files" tofile="${build.dir}/rpm/SOURCES/files" />
+    
+    <replace file="${build.dir}/rpm/SOURCES/files" token="# [[ listFiles ]]" value="${listRPMFiles}" />
+    <replace file="${build.dir}/rpm/SPECS/specFile" token="# [[ docsList ]]" value="${listRPMDocFiles}" />    
+    <replace file="${build.dir}/rpm/SPECS/specFile" token="# [[ installRpmFiles ]]" value="cp -rf ${package.dir}/${SHORT_NAME}-${VERSION_NUMBER_STRING}/* . ${line.separator}" />
+    
+    <copy file="resource/rpm/excludedFiles" tofile="${build.dir}/rpm/SOURCES/excludedFiles" />
+    <replace file="${build.dir}/rpm/SOURCES/excludedFiles" token="# [[ excludedListFiles ]]" value="${excludedRPMFiles}" />
+  
     <rpm specfile="specFile"
          topdir="${build.dir}/rpm"
-         quiet="true">
+         quiet="true"
+         removeSpec="false">
     </rpm>
-
+  	
+    <mkdir dir="${build.dir}/rpm/RPMS/noarch" />
     <copy todir="${package.dir}">
       <fileset dir="${build.dir}/rpm/RPMS/noarch">
         <include name="*.rpm" />
       </fileset>
     </copy>
-
   </target>
-
+	
   <!-- Package the Directory Server for the DEB distribution. -->
   <target name="package-DEB"
           description="Package the Directory Server for DEB distribution.">
diff --git a/opendj-sdk/opends/resource/rpm/excludedFiles b/opendj-sdk/opends/resource/rpm/excludedFiles
new file mode 100644
index 0000000..d516419
--- /dev/null
+++ b/opendj-sdk/opends/resource/rpm/excludedFiles
@@ -0,0 +1,26 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License, Version 1.0 only
+# (the "License").  You may not use this file except in compliance
+# with the License.
+#
+# You can obtain a copy of the license at
+# trunk/opendj3/legal-notices/CDDLv1_0.txt
+# or http://forgerock.org/license/CDDLv1.0.html.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at
+# trunk/opendj3/legal-notices/CDDLv1_0.txt.  If applicable,
+# add the following below this CDDL HEADER, with the fields enclosed
+# by brackets "[]" replaced with your own identifying information:
+#      Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#      Copyright 2013 ForgeRock AS
+#
+# [[ excludedListFiles ]]
\ No newline at end of file
diff --git a/opendj-sdk/opends/resource/rpm/files b/opendj-sdk/opends/resource/rpm/files
new file mode 100644
index 0000000..99aa84a
--- /dev/null
+++ b/opendj-sdk/opends/resource/rpm/files
@@ -0,0 +1,26 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License, Version 1.0 only
+# (the "License").  You may not use this file except in compliance
+# with the License.
+#
+# You can obtain a copy of the license at
+# trunk/opendj3/legal-notices/CDDLv1_0.txt
+# or http://forgerock.org/license/CDDLv1.0.html.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at
+# trunk/opendj3/legal-notices/CDDLv1_0.txt.  If applicable,
+# add the following below this CDDL HEADER, with the fields enclosed
+# by brackets "[]" replaced with your own identifying information:
+#      Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#      Copyright 2013 ForgeRock AS
+#
+# [[ listFiles ]]
\ No newline at end of file
diff --git a/opendj-sdk/opends/resource/rpm/specFile b/opendj-sdk/opends/resource/rpm/specFile
new file mode 100644
index 0000000..4d778c5
--- /dev/null
+++ b/opendj-sdk/opends/resource/rpm/specFile
@@ -0,0 +1,206 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License, Version 1.0 only
+# (the "License").  You may not use this file except in compliance
+# with the License.
+#
+# You can obtain a copy of the license at
+# trunk/opendj3/legal-notices/CDDLv1_0.txt
+# or http://forgerock.org/license/CDDLv1.0.html.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at
+# trunk/opendj3/legal-notices/CDDLv1_0.txt.  If applicable,
+# add the following below this CDDL HEADER, with the fields enclosed
+# by brackets "[]" replaced with your own identifying information:
+#      Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#      Copyright 2013 ForgeRock AS
+# 
+%define _topdir [[ topDir ]]
+%define _arch noarch
+%define _prefix [[ prefix ]]
+%define _pre [[ pre ]]
+%define __os_install_post %{nil}
+
+# =========================
+# Header
+# =========================
+# Short Description
+Summary: [[ shortName ]]
+# Application Name
+Name: [[ pkgName ]]
+# Application Version
+Version: [[ version ]]
+# Packaging Revision
+Release: [[ release ]]
+# Software Licenced Under
+License: CDDL
+# RPM Group
+Group: Applications/Network
+# Link to Application web site
+URL: [[ referenceURL ]]
+# Distributing Organisation
+Vendor: ForgeRock AS
+# Build Architecture
+BuildArch: noarch
+
+Requires: jre >= 1.6
+
+# Long Description
+%Description
+OpenDJ LDAP Server
+OpenDJ is an LDAPv3 compliant directory service, developed for the Java
+platform, providing a high performance, highly available and secure store
+for the identities managed by enterprises. Its easy installation process,
+combined with the power of the Java platform makes OpenDJ one of the
+simplest and fastest directory servers to deploy and manage.
+
+
+# =========================
+# Prepare, Build, Install
+# =========================
+# %prep
+
+# %build
+
+%install
+mkdir -p "$RPM_BUILD_ROOT%{_prefix}"
+cd "$RPM_BUILD_ROOT%{_prefix}"
+# [[ installRpmFiles ]]
+# Remove the excluded files. They shouldn't be a part of this package. 
+/bin/grep -v '^#' "$RPM_SOURCE_DIR/excludedFiles" | while read excludedFiles 
+do
+    rm -r "$RPM_BUILD_ROOT%{_prefix}$excludedFiles"
+done
+
+%clean
+[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf "${RPM_BUILD_ROOT}"
+
+# =========================
+# Pre & Post Install
+# =========================
+# If the first argument to %pre is 1, the RPM operation is an initial
+#  installation. If the argument to %pre is 2, the operation is an upgrade
+#  from an existing version to a new one.
+# Similarly, the arguments to a %post are 1 and 2 for a new installation
+#  and upgrade, respectively. (%pre and %post aren't executed during
+#  an uninstallation.)
+
+# -------------------------
+# Pre Install
+%pre
+if [ "$1" == "1" ]; then
+    echo "Pre Install - initial install"
+else if [ "$1" == "2" ] ; then
+# Only if the instance has been configured
+    if [ isInstanceConfigured ]
+    then
+        echo "Pre Install - upgrade install"
+# If the server is running before upgrade, creates a file flag
+        if [ -f "%{_prefix}"/logs/server.pid ] 
+        then
+            touch "%{_prefix}"/logs/status
+        fi
+        "%{_prefix}"/bin/./stop-ds
+        fi
+    fi
+fi
+
+# -------------------------
+# Post Install
+%post
+if [ "$1" == "1" ] ; then
+    echo "Post Install - initial install"
+else if [ "$1" == "2" ] ; then
+    echo "Post Install - upgrade install"
+# Only if the instance has been configured
+    if [ isInstanceConfigured ]
+    then
+        "%{_prefix}"/./upgrade -n --acceptLicense
+# Upgrade ok 
+        if [ "$?" == "0" ] ; then 
+# Checks the server status flag for restart. 
+            if [ -f "%{_prefix}"/logs/status ] 
+            then
+                echo ""
+                echo "Restarting server..." 
+                "%{_prefix}"/./bin/start-ds 
+                echo ""
+                rm -f "%{_prefix}"/logs/status 
+            fi
+        fi
+# Upgrade fails, needs user interaction (eg. manual mode)
+        if [ "$?" == "2" ] ; then 
+            exit "0" 
+        fi 
+    else
+        echo "Instance is not configured. Upgrade aborted."
+        exit -1
+    fi
+    fi 
+fi
+
+# =========================
+# Pre & Post Uninstall
+# =========================
+# If the first argument to %preun and %postun is 0, the action is
+#  uninstallation.
+# If the first argument to %preun and %postun is 1, the action is an upgrade.
+# -------------------------
+# Pre Uninstall
+%preun
+if [ "$1" == "0" ] ; then
+    echo "Pre Uninstall - uninstall"
+# Only if the instance has been configured
+    if [ isInstanceConfigured ]
+    then
+	"%{_prefix}"/bin/./stop-ds
+    fi
+else if [ "$1" == "1" ] ; then
+    echo "Pre Uninstall - upgrade uninstall"
+    fi
+fi
+# -------------------------
+# Post Uninstall
+%postun
+if [ "$1" == "0" ] ; then
+    echo "Post Uninstall - uninstall"
+    echo "OpenDJ successfully removed."
+else if [ "$1" == "1" ] ; then
+    echo "Post Uninstall - upgrade uninstall"
+    fi
+fi
+
+# -------------------------
+# Returns true/0 if the instance has been configured.
+isInstanceConfigured() {
+    if [ -f "%{_prefix}"/config/buildinfo ] && [ "$(ls -A "%{_prefix}"/config/archived-configs)" ]
+    then
+        return 0
+    else
+        return 1
+    fi
+}
+# =========================
+# Files section
+# =========================
+%files -f "%{_sourcedir}"/files
+%defattr(-,root,root)
+# [[ docsList ]]
+
+# =========================
+# Changelog
+# =========================
+%changelog
+* Thu Jul 18 2013 ForgeRock
+- Fixed the sections' order and added a new "clean" section.
+- Added '%doc' section.
+- Added '%changelog' at the end of the file.
+- Added license to header's files.
diff --git a/opendj-sdk/opends/src/build-tools/org/opends/build/tools/GenerateRpm.java b/opendj-sdk/opends/src/build-tools/org/opends/build/tools/GenerateRpm.java
deleted file mode 100644
index c9af268..0000000
--- a/opendj-sdk/opends/src/build-tools/org/opends/build/tools/GenerateRpm.java
+++ /dev/null
@@ -1,458 +0,0 @@
-/*
- * CDDL HEADER START
- *
- * The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License").  You may not use this file except in compliance
- * with the License.
- *
- * You can obtain a copy of the license at
- * trunk/opends/resource/legal-notices/OpenDS.LICENSE
- * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
- * See the License for the specific language governing permissions
- * and limitations under the License.
- *
- * When distributing Covered Code, include this CDDL HEADER in each
- * file and include the License file at
- * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
- * add the following below this CDDL HEADER, with the fields enclosed
- * by brackets "[]" replaced with your own identifying information:
- *      Portions Copyright [yyyy] [name of copyright owner]
- *
- * CDDL HEADER END
- *
- *
- *      Copyright 2007-2008 Sun Microsystems, Inc.
- *      Portions Copyright 2012 Delta-Victor Consultants
- *      Portions Copyright 2013 ForgeRock AS
- */
-package org.opends.build.tools;
-
-import org.apache.tools.ant.Task;
-import org.apache.tools.ant.BuildException;
-
-import static org.opends.build.tools.Utilities.*;
-
-import java.io.File;
-import java.io.FileFilter;
-import java.io.PrintWriter;
-
-/**
- * Generates an RPM spec file.
- */
-public class GenerateRpm extends Task
-{
-
-  private File topDir;
-  private String topDirAbsolutePath;
-  private String sourceDirName;
-  private File destFile;
-  private String prefix;
-  private String productName;
-  private String referenceURL;
-  private String shortName;
-  private String version;
-  private String release;
-  private boolean overwrite;
-  private StringBuilder sb;
-
-  private final String filePrefix = "%{_prefix}";
-  private final String dirPrefix = "%dir %{_prefix}";
-
-  /**
-   * Sets the top directory for the rpm build.
-   *
-   * @param topDir
-   *          File representing top directory for rpm build directory
-   */
-  public void setTopDir(File topDir)
-  {
-    this.topDir = topDir;
-    topDirAbsolutePath = topDir.getAbsolutePath().replaceAll(" ", "\\\\ ");
-  }
-
-  /**
-   * Sets the prefix for the RPM.
-   *
-   * @param prefix
-   *          Used for package relocation
-   */
-  public void setPrefix(String prefix)
-  {
-    this.prefix = prefix;
-  }
-
-  /**
-   * Sets the short description for the RPM.
-   *
-   * @param productName
-   *          The RPM short description
-   */
-  public void setproductName(String productName)
-  {
-    this.productName = productName;
-  }
-
-  /**
-   * Sets the application name for the RPM.
-   *
-   * @param shortName
-   *          The RPM application name
-   */
-  public void setshortName(String shortName)
-  {
-    this.shortName = shortName;
-  }
-
-  /**
-   * Sets the reference URL for the RPM.
-   *
-   * @param referenceURL
-   *          The RPM reference URL
-   */
-  public void setreferenceURL(String referenceURL)
-  {
-    this.referenceURL = referenceURL;
-  }
-
-  /**
-   * Sets the name of the source directory.
-   *
-   * @param sourceDirName
-   *          name of the source directory.
-   */
-  public void setSourceDirName(String sourceDirName)
-  {
-    this.sourceDirName = sourceDirName;
-  }
-
-  /**
-   * Sets the RPM spec file that will be generated.
-   *
-   * @param dest
-   *          The spec file
-   */
-  public void setSpecFileName(File dest)
-  {
-    this.destFile = dest;
-  }
-
-  /**
-   * Sets the version number.
-   *
-   * @param version
-   *          The version number
-   */
-  public void setVersion(String version)
-  {
-    this.version = version;
-  }
-
-  /**
-   * Sets the release number.
-   *
-   * @param release
-   *          The release number
-   */
-  public void setRelease(String release)
-  {
-    this.release = release;
-  }
-
-  /**
-   * Indicates when true that an existing destination file will be overwritten.
-   *
-   * @param o
-   *          boolean where true means overwrite
-   */
-  public void setOverwrite(boolean o)
-  {
-    this.overwrite = o;
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public void execute() throws BuildException
-  {
-
-    try
-    {
-      if (!topDir.exists())
-      {
-        throw new BuildException("directory " + topDir.getName()
-            + " does not exist");
-      }
-      if (!topDir.isDirectory())
-      {
-        throw new BuildException(topDir.getName() + " is not a directory");
-      }
-
-      if (destFile.exists())
-      {
-        if (this.overwrite)
-        {
-          destFile.delete();
-          log("Regenerating " + destFile.getName() + " from "
-              + topDir.getName());
-        }
-        else
-        {
-          log(destFile.getName() + " has not been regenerated");
-        }
-      }
-
-      sb = new StringBuilder();
-      final File rootDir = new File(sourceDirName);
-      final String opendsDir = rootDir.getName();
-      final File[] listFiles = rootDir.listFiles(new PkgFileFilter());
-
-      // Generate the package information
-      sb.append("%define _topdir " + topDirAbsolutePath + EOL);
-      sb.append("%define _arch noarch" + EOL);
-      sb.append("%define _prefix " + prefix + EOL);
-      sb.append("%define _pre " + opendsDir + EOL);
-      sb.append("%define __os_install_post %{nil}" + EOL);
-      sb.append(EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# Header" + EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# Short Description" + EOL);
-      sb.append("Summary: " + productName + EOL);
-      sb.append("# Application Name" + EOL);
-      sb.append("Name: " + shortName + EOL);
-      sb.append("# Application Version" + EOL);
-      sb.append("Version: " + version + EOL);
-      sb.append("# Packaging Revision" + EOL);
-      // If VERSION_QUALIFIER is empty, need to remove '.' char.
-      if (release.endsWith("."))
-      {
-        release = release.substring(0, release.length() - 1);
-      }
-      sb.append("Release: " + release + EOL);
-      sb.append("# Software Licenced Under" + EOL);
-      sb.append("License: CDDL" + EOL);
-      sb.append("# RPM Group" + EOL);
-      sb.append("Group: Applications/Network" + EOL);
-      sb.append("# Link to Application web site" + EOL);
-      sb.append("URL: " + referenceURL + EOL);
-      sb.append("# Distributing Organisation" + EOL);
-      sb.append("Vendor: ForgeRock AS" + EOL);
-      sb.append("# Build Architecture" + EOL);
-      sb.append("BuildArch: noarch" + EOL);
-      sb.append(EOL);
-      sb.append("Requires: jre >= 1.6" + EOL);
-      sb.append(EOL);
-      sb.append("# Long Description" + EOL);
-      sb.append("%Description" + EOL);
-      sb.append("OpenDJ LDAP Server" + EOL);
-      sb.append("OpenDJ is an LDAPv3 compliant directory service, developed for the Java"
-          + EOL);
-      sb.append("platform, providing a high performance, highly available and secure store"
-          + EOL);
-      sb.append("for the identities managed by enterprises. Its easy installation process,"
-          + EOL);
-      sb.append("combined with the power of the Java platform makes OpenDJ one of the"
-          + EOL);
-      sb.append("simplest and fastest directory servers to deploy and manage."
-          + EOL);
-      sb.append(EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# Pre & Post Install" + EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# If the first argument to %pre is 1, the RPM operation is an initial"
-          + EOL);
-      sb.append("#  installation. If the argument to %pre is 2, the operation is an upgrade"
-          + EOL);
-      sb.append("#  from an existing version to a new one." + EOL);
-      sb.append("# Similarly, the arguments to a %post are 1 and 2 for a new installation"
-          + EOL);
-      sb.append("#  and upgrade, respectively. (%pre and %post aren't executed during"
-          + EOL);
-      sb.append("#  an uninstallation.)" + EOL);
-      sb.append(EOL);
-      sb.append("# Pre Install" + EOL);
-      sb.append("%pre" + EOL);
-      sb.append("if [ \"$1\" == \"1\" ]; then" + EOL);
-      sb.append("    echo \"Pre Install - initial install\"" + EOL);
-      sb.append("else if [ \"$1\" == \"2\" ] ; then" + EOL);
-      sb.append("# Only if the instance has been configured" + EOL);
-      sb.append("    if [ -f %{_prefix}/config/buildinfo ] && [ \"$(ls -A %{_prefix}/config/archived-configs)\" ]" + EOL);
-      sb.append("    then" + EOL);
-      sb.append("        echo \"Pre Install - upgrade install\"" + EOL);
-      sb.append("# If the server is running before upgrade, creates a file flag" + EOL);
-      sb.append("        if [ -f %{_prefix}/logs/server.pid ] " + EOL);
-      sb.append("        then" + EOL);
-      sb.append("            touch %{_prefix}/logs/status" + EOL);
-      sb.append("        fi" + EOL);
-      sb.append("        stopds=$(%{_prefix}/bin/stop-ds)" + EOL);
-      sb.append("        echo $stopds" + EOL);
-      sb.append("        fi" + EOL);
-      sb.append("    fi" + EOL);
-      sb.append("fi" + EOL);
-      sb.append(EOL);
-      sb.append("# Post Install" + EOL);
-      sb.append("%post" + EOL);
-      sb.append("if [ \"$1\" == \"1\" ] ; then" + EOL);
-      sb.append("    echo \"Post Install - initial install\"" + EOL);
-      sb.append("    [[ `java -version 2>&1 | /bin/sed 's/java version \"\\(.*\\)\\.\\(.*\\)\\.\\(.*\\)\\_\\(.*\\)\\.*\"/\\1\\2\\3\\4/; 1q'` < 16022 ]] && echo \"WARNING - For best server performance, use at least Java 1.6.0_22, which includes a major security fix for TLS.\""
-          + EOL);
-      sb.append("    echo \"\"" + EOL);
-      sb.append("else if [ \"$1\" == \"2\" ] ; then" + EOL);
-      sb.append("    echo \"Post Install - upgrade install\"" + EOL);
-      sb.append("# Only if the instance has been configured" + EOL);
-      sb.append("    if [ -f %{_prefix}/config/buildinfo ] && [ \"$(ls -A %{_prefix}/config/archived-configs)\" ]" + EOL);
-      sb.append("    then" + EOL);
-      // Starts the upgrade. The new files are automatically imported
-      // by rpm manager, which compares files between last & actual version.
-      // Copies / deletes files depending of new package.
-      sb.append("        %{_prefix}/./upgrade -n --acceptLicense" + EOL);
-      sb.append("# Upgrade ok " + EOL);
-      sb.append("        if [ \"$?\" == \"0\" ] ; then " + EOL);
-      sb.append("# Checks the server status flag for restart. " + EOL);
-      sb.append("            if [ -f %{_prefix}/logs/status ] " + EOL);
-      sb.append("            then" + EOL);
-      sb.append("                echo \"\"" + EOL);
-      sb.append("                echo \"Restarting server...\" " + EOL);
-      sb.append("                %{_prefix}/./bin/start-ds " + EOL);
-      sb.append("                echo \"\"" + EOL);
-      sb.append("                rm -f %{_prefix}/logs/status " + EOL);
-      sb.append("            fi" + EOL);
-      sb.append("        fi" + EOL);
-      sb.append("# Upgrade fails, needs user interaction (eg. manual mode)" + EOL);
-      sb.append("        if [ \"$?\" == \"2\" ] ; then " + EOL);
-      sb.append("            exit \"0\" " + EOL);
-      sb.append("        fi " + EOL);
-      sb.append("    else" + EOL);
-      sb.append("        echo \"Instance is not configured. Upgrade aborded.\"" + EOL);
-      sb.append("        exit -1" + EOL);
-      sb.append("    fi" + EOL);
-      sb.append("    fi " + EOL);
-      sb.append("fi" + EOL);
-      sb.append(EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# Pre & Post Uninstall" + EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# If the first argument to %preun and %postun is 0, the action is"
-          + EOL);
-      sb.append("#  uninstallation." + EOL);
-      sb.append("# If the first argument to %preun and %postun is 1, the action is an upgrade."
-          + EOL);
-      sb.append("# Pre Uninstall" + EOL);
-      sb.append("%preun" + EOL);
-      sb.append("if [ \"$1\" == \"0\" ] ; then" + EOL);
-      sb.append("    echo \"Pre Uninstall - uninstall\"" + EOL);
-      sb.append("# Only if the instance has been configured" + EOL);
-      sb.append("    if [ -f %{_prefix}/config/buildinfo ] && [ \"$(ls -A %{_prefix}/config/archived-configs)\" ]" + EOL);
-      sb.append("    then" + EOL);
-      sb.append("	%{_prefix}/bin/stop-ds" + EOL);
-      sb.append("    fi" + EOL);
-      sb.append("else if [ \"$1\" == \"1\" ] ; then" + EOL);
-      sb.append("    echo \"Pre Uninstall - upgrade uninstall\"" + EOL);
-      sb.append("    fi" + EOL);
-      sb.append("fi" + EOL);
-      sb.append("# Post Uninstall" + EOL);
-      sb.append("%postun" + EOL);
-      sb.append("if [ \"$1\" == \"0\" ] ; then" + EOL);
-      sb.append("    echo \"Post Uninstall - uninstall\"" + EOL);
-      sb.append("    echo \"OpenDJ successfully removed.\"" + EOL);
-      sb.append("else if [ \"$1\" == \"1\" ] ; then" + EOL);
-      sb.append("    echo \"Post Uninstall - upgrade uninstall\"" + EOL);
-      sb.append("    fi" + EOL);
-      sb.append("fi" + EOL);
-      sb.append(EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# Prepare, Build, Install" + EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# %prep" + EOL);
-      sb.append(EOL);
-      sb.append("# %build" + EOL);
-      sb.append(EOL);
-      sb.append("%install" + EOL);
-      sb.append("mkdir -p \"$RPM_BUILD_ROOT%{_prefix}\"" + EOL);
-      sb.append("cd \"$RPM_BUILD_ROOT%{_prefix}\"" + EOL);
-      for (final File f : listFiles)
-      {
-        sb.append("cp -r \"" + f.getPath() + "\" ." + EOL);
-      }
-      sb.append(EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("# Files Layout" + EOL);
-      sb.append("# =========================" + EOL);
-      sb.append("%files" + EOL);
-      sb.append(dirPrefix + EOL);
-      // In order to handle upgrades, we need to start
-      // with the contents of the rootDir rather than
-      // the actual directory, because the
-      // rootDir has the OpenDJ version number included
-      // into it
-      for (final File build : listFiles)
-      {
-        generatedLevel("", build);
-      }
-
-      // flush the spec file.
-      PrintWriter destWriter = new PrintWriter(destFile);
-      destWriter.print(sb.toString());
-      destWriter.close();
-    }
-    catch (Exception e)
-    {
-      // Don't leave a malformed file laying around. Delete
-      // it so it will be forced to be regenerated.
-      if (destFile.exists())
-      {
-        destFile.deleteOnExit();
-      }
-      e.printStackTrace();
-      throw new BuildException("Error processing " + topDir + ":  "
-          + e.getMessage());
-    }
-  }
-
-  private void generatedLevel(String parent, File source)
-  {
-    if (source.isDirectory())
-    {
-      sb.append(dirPrefix + parent + "/" + source.getName());
-      sb.append(EOL);
-      for (File child : source.listFiles())
-      {
-        generatedLevel(parent + "/" + source.getName(), child);
-      }
-    }
-    else
-    {
-      sb.append(filePrefix + parent + "/" + source.getName());
-      sb.append(EOL);
-    }
-  }
-
-  /**
-   * A file filter for the rpm. Excludes all '.bat', '.exe' and '.app' files.
-   */
-  static final class PkgFileFilter implements FileFilter
-  {
-    /** {@inheritDoc} */
-    public boolean accept(File file)
-    {
-      final String fileName = file.getName().toLowerCase();
-      if (file.isDirectory()
-          && (fileName.equals("bat") || fileName.endsWith(".app") || fileName
-              .endsWith(".bat")))
-      {
-        return false;
-      }
-      else if (file.isFile())
-      {
-        if (fileName.endsWith(".app") || fileName.endsWith(".bat")
-            || fileName.endsWith(".exe"))
-        {
-          return false;
-        }
-      }
-      return true;
-    }
-  }
-}
diff --git a/opendj-sdk/opends/src/build-tools/org/opends/build/tools/ProcessFilesForPackages.java b/opendj-sdk/opends/src/build-tools/org/opends/build/tools/ProcessFilesForPackages.java
new file mode 100644
index 0000000..169f0c6
--- /dev/null
+++ b/opendj-sdk/opends/src/build-tools/org/opends/build/tools/ProcessFilesForPackages.java
@@ -0,0 +1,262 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
+ * or http://forgerock.org/license/CDDLv1.0.html.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at legal-notices/CDDLv1_0.txt.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2013 ForgeRock AS
+ */
+package org.opends.build.tools;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+
+/**
+ * A utility class for the packaging process. It is used by the build.xml to
+ * perform the package construction.
+ */
+public class ProcessFilesForPackages extends Task
+{
+  /** The source directory name */
+  private String sourceDirName;
+
+  /** Files contained in the package */
+  private final List<File> files = new LinkedList<File>();
+
+  /** Files which should be excluded from the package */
+  private final List<File> excludedFiles = new LinkedList<File>();
+
+  /** Package documentation files */
+  private final List<File> docFiles = new LinkedList<File>();
+
+  /** The end-of-line character for this platform */
+  private static String EOL = System.getProperty("line.separator");
+
+  /**
+   * Returns the source directory name.
+   *
+   * @return The source directory name.
+   */
+  public String getSourceDirName()
+  {
+    return sourceDirName.replaceAll(" ", "\\\\ ");
+  }
+
+  /**
+   * Sets the source directory name.
+   *
+   * @param sourceDirName
+   *          The source directory name.
+   */
+  public void setSourceDirName(String sourceDirName)
+  {
+    this.sourceDirName = sourceDirName;
+  }
+
+  @Override()
+  public void execute()
+  {
+    try
+    {
+      // Process the filtering of the files contained in the given directory.
+      filterFiles(new File(getSourceDirName()));
+      // Sorts the list.
+      Collections.sort(files);
+      Collections.sort(docFiles);
+      // Returns the properties which can be used fill data.
+      getProject().setNewProperty("listRPMFiles", formatAsFileList(files));
+      getProject().setNewProperty("listRPMDocFiles",
+          formatAsDocList(docFiles));
+      getProject().setNewProperty("excludedRPMFiles",
+          formatAsExcludedList(excludedFiles));
+    }
+    catch (Exception e)
+    {
+      throw new BuildException(
+          "An error occurs during filtering package files", e);
+    }
+  }
+
+  /**
+   * Formats the file list to be supported by RPM.
+   *
+   * @param fileList
+   *          The list of files to be processed.
+   * @return A string containing the list of files which should be excluded in
+   *         the RPM source.
+   */
+  private String formatAsExcludedList(final List<File> fileList)
+  {
+    final StringBuilder sb = new StringBuilder();
+    for (final File f : fileList)
+    {
+      sb.append(relativeToSourceDirName(f)).append(EOL);
+    }
+    return sb.toString();
+  }
+
+  /**
+   * Formats the file list to be supported by RPM.
+   *
+   * @param fileList
+   *          The list of files to be processed.
+   * @return A string containing the list of files which can be included in the
+   *         RPM source.
+   */
+  private String formatAsFileList(final List<File> fileList)
+  {
+    final StringBuilder sb = new StringBuilder();
+    sb.append("%dir %{_prefix}").append(EOL);
+    for (final File f : fileList)
+    {
+      if (f.isDirectory())
+      {
+        sb.append("%dir %{_prefix}");
+      }
+      else
+      {
+        sb.append("%{_prefix}");
+      }
+      sb.append(relativeToSourceDirName(f)).append(EOL);
+    }
+    return sb.toString();
+  }
+
+  /**
+   * Formats the document list to be supported by RPM.
+   *
+   * @param fileList
+   *          The list of files to be processed.
+   * @return A string containing the formatted list doc.
+   */
+  private String formatAsDocList(final List<File> fileList)
+  {
+    final StringBuilder sb = new StringBuilder();
+    for (final File f : fileList)
+    {
+      // FIXME If directory is mentioned, files are duplicated in doc directory
+      // in us/share/opendj{version} and in the package install opt/opendj/
+      if (!f.isDirectory())
+      {
+        sb.append("%doc $RPM_BUILD_ROOT%{_prefix}").append(
+            relativeToSourceDirName(f));
+        sb.append(EOL);
+      }
+    }
+    return sb.toString();
+  }
+
+  /**
+   * Sorts all the files contained in selected directory and fills the
+   * appropriate lists.
+   *
+   * @param dir
+   *          The directory where to analyze the files.
+   * @throws Exception
+   *           If an exception occurs during the process.
+   */
+  private void filterFiles(final File dir) throws Exception
+  {
+    final ExcludeFileFilter exFilter = new ExcludeFileFilter();
+    final DocFileFilter docFilter = new DocFileFilter();
+    for (final File f : dir.listFiles())
+    {
+      if (f.isDirectory())
+      {
+        filterFiles(f);
+      }
+      // Completes the excluded files list.
+      if (exFilter.accept(f))
+      {
+        excludedFiles.add(f);
+      }
+      else
+      {
+        files.add(f);
+      }
+      // Completes the document file list.
+      if (docFilter.accept(f))
+      {
+        docFiles.add(f);
+      }
+    }
+  }
+
+  /**
+   * Returns the relative path to given source directory name.
+   *
+   * @param f
+   *          The file we want the relative path.
+   * @return A string representing the relative path to the given source
+   *         directory name.
+   */
+  private String relativeToSourceDirName(File f)
+  {
+    return f.getAbsolutePath().replace(getSourceDirName(), "");
+  }
+
+  /**
+   * A file filter which excludes all files that belong to other OS.
+   */
+  static final class ExcludeFileFilter implements FileFilter
+  {
+    /** {@inheritDoc} */
+    public boolean accept(File file)
+    {
+      final String fileName = file.getName().toLowerCase();
+      return (
+          fileName.equals("bat")
+          || fileName.endsWith(".app")
+          || fileName.endsWith(".bat")
+          || fileName.endsWith(".exe")
+          || file.getAbsolutePath().contains(".app"));
+    }
+  }
+
+  /**
+   * A file filter which accepts only documentation files.
+   */
+  static final class DocFileFilter implements FileFilter
+  {
+    /** {@inheritDoc} */
+    public boolean accept(File file)
+    {
+      final String fileName = file.getName().toLowerCase();
+      if (file.isDirectory())
+      {
+        return fileName.equals("legal-notices");
+      }
+      return (
+          !file.getAbsolutePath().contains("/template/")
+          && !file.getAbsolutePath().contains("/legals/")
+          && (fileName.endsWith(".doc")
+          || fileName.endsWith(".txt")
+          || fileName.contains("example-plugin")
+          || fileName.equals("readme")
+          || fileName.equals("opends.license")));
+    }
+  }
+}
diff --git a/opendj-sdk/opends/src/build-tools/org/opends/build/tools/Utilities.java b/opendj-sdk/opends/src/build-tools/org/opends/build/tools/Utilities.java
index e228197..f04c6c8 100644
--- a/opendj-sdk/opends/src/build-tools/org/opends/build/tools/Utilities.java
+++ b/opendj-sdk/opends/src/build-tools/org/opends/build/tools/Utilities.java
@@ -153,5 +153,4 @@
 
     return buffer.toString();
   }
-
 }

--
Gitblit v1.10.0