diff --git a/.gitignore b/.gitignore
index 04b67cde0da59a3751f3818eed3355ce3416d35b..d103ea4e032beaccf19fd4151eafef1b0965966b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -228,3 +228,4 @@ vmgr/vmgr_oracle_ifce.lis
 vmgr/vmgrsettag
 xrootd/XrdTransfer
 *.xml
+00build
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000000000000000000000000000000000000..d7e5171f9b8f94ba4cdfa3a5d1e1534e05d42b50
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,46 @@
+#
+#  Copyright (C) 1993-2013 by CERN/IT/DSS
+#  All rights reserved
+#
+# CMakeLists.txt for the CASTOR project.
+#
+
+###########################################################################
+# In source build forbidding
+###########################################################################
+CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
+SET(CMAKE_DISABLE_SOURCE_CHANGES ON)
+SET(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
+
+###########################################################################
+# project definition section
+###########################################################################
+PROJECT(castor)
+LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
+INCLUDE(cmake/CASTORVersion.cmake)
+FIND_PACKAGE(Oracle)
+
+###########################################################################
+# Configure header file
+###########################################################################
+CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/h/patchlevel.h.in ${CMAKE_BINARY_DIR}/h/patchlevel.h)
+INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}/h)
+
+###########################################################################
+# Include the subdirectories of the project
+###########################################################################
+add_subdirectory(tape)
+
+###########################################################################
+# Packaging step (replacing the maketar)
+# See http://www.vtk.org/Wiki/CMakeUserUseRPMTools
+###########################################################################
+SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CASTOR_VERSION}-${CASTOR_RELEASE}")
+INCLUDE(CPack)
+INCLUDE(cmake/UseRPMTools.cmake)
+IF(RPMTools_FOUND)
+  EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.spec.in.head  ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.spec.in)
+  EXECUTE_PROCESS(COMMAND ./makespec.in.sh ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.spec.in
+       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
+  RPMTools_ADD_RPM_TARGETS(${PROJECT_NAME} ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.spec.in)
+ENDIF(RPMTools_FOUND)
\ No newline at end of file
diff --git a/CASTOR.spec b/castor.spec.in.head
similarity index 81%
rename from CASTOR.spec
rename to castor.spec.in.head
index 55300186eb83ee27b1b5314bda3f56b17fc4e124..bd738586a30021c67fe92a7f2c31c66a46d6d984 100644
--- a/CASTOR.spec
+++ b/castor.spec.in.head
@@ -1,13 +1,13 @@
 # Generic macros
 #---------------
 %define name castor
-%define castorVersion __A__.__B__.__C__
-%define castorRelease __D__
+%define castorVersion @CASTOR_VERSION@
+%define castorRelease @CASTOR_RELEASE@
 
 # Partial compilations
 #---------------------
-%{expand:%define compiling_nostk  %(if [ -z $CASTOR_NOSTK ]; then echo 0; else echo 1; fi)}
-%{expand:%define compiling_client %(if [ -z $CASTOR_CLIENT ]; then echo 0; else echo 1; fi)}
+%define compiling_nostk  @COMPILING_NOSTK@
+%define compiling_client @COMPILING_CLIENT@
 
 # General settings
 #-----------------
@@ -24,12 +24,16 @@ BuildRoot: %{_builddir}/%{name}-%{version}-root
 %if %compiling_client
 %define debug_package %{nil}
 %endif
+%if %compiling_client
+%else
+BuildRequires: oracle-instantclient-devel
+%endif
 
 %description
 The CASTOR Project stands for CERN Advanced STORage Manager, and its goal is to handle LHC data in a fully distributed environment.
 
 %prep
-%setup -q
+%setup -q -n %{name}-%{version}-%{release}
 
 %build
 # rpmbuild --define="compilenostk 1" ...
@@ -47,8 +51,8 @@ export ORACOMPILE_OPTIONS="--quiet"
 %endif
 
 # define castor version (modified by maketar.sh to put the exact version)
-MAJOR_CASTOR_VERSION=__MAJOR_CASTOR_VERSION__
-MINOR_CASTOR_VERSION=__MINOR_CASTOR_VERSION__
+MAJOR_CASTOR_VERSION=@MAJOR_CASTOR_VERSION@
+MINOR_CASTOR_VERSION=@MINOR_CASTOR_VERSION@
 export MAJOR_CASTOR_VERSION
 export MINOR_CASTOR_VERSION
 ./configure
@@ -62,8 +66,8 @@ echo "Only compiling client part"
 
 %install
 # define castor version (modified by maketar.sh to put the exact version)
-MAJOR_CASTOR_VERSION=__MAJOR_CASTOR_VERSION__
-MINOR_CASTOR_VERSION=__MINOR_CASTOR_VERSION__
+MAJOR_CASTOR_VERSION=@MAJOR_CASTOR_VERSION@
+MINOR_CASTOR_VERSION=@MINOR_CASTOR_VERSION@
 export MAJOR_CASTOR_VERSION
 export MINOR_CASTOR_VERSION
 %{__rm} -rf ${RPM_BUILD_ROOT}
diff --git a/cmake/CASTORVersion.cmake b/cmake/CASTORVersion.cmake
new file mode 100644
index 0000000000000000000000000000000000000000..e70af0bb5b70602c8854bb3f6c3be50a06167d4a
--- /dev/null
+++ b/cmake/CASTORVersion.cmake
@@ -0,0 +1,68 @@
+# Guess the CASTOR version from the various criteria in the environement:
+# - Get MINOR_CASTOR_VERSION and MAJOR_CASTOR_VERSION from the environement, if 
+# they exists.
+# - If not, guess them from the file debian/changelog
+# - The major is expected in the form: A.B (fixed to 2.1 for ages)
+# - The minor is expected to be in the for C.D.
+# - The package version (in rpm) will be A.B.C-D
+# - If the variable VCS_VERSION is defined (from command line:
+#  "cmake -DVCS_VERSION:STRING=git-6789abc")
+
+# Extract the versions from changelog anyway
+SET(VERSION_RE "^castor \\(([0-9]+)\\.([0-9]+)\\.([0-9]+)-([0-9]+)\\).*")
+FILE(STRINGS debian/changelog CHANGELOG_HEAD
+     REGEX ${VERSION_RE}
+     LIMIT_COUNT 1)
+# MESSAGE("Changelog line: ${CHANGELOG_HEAD}")
+# MESSAGE("VERSION_RE ${VERSION_RE}")
+STRING(REGEX REPLACE "${VERSION_RE}" "\\1" A ${CHANGELOG_HEAD})
+STRING(REGEX REPLACE "${VERSION_RE}" "\\2" B ${CHANGELOG_HEAD})
+STRING(REGEX REPLACE "${VERSION_RE}" "\\3" C ${CHANGELOG_HEAD})
+STRING(REGEX REPLACE "${VERSION_RE}" "\\4" D ${CHANGELOG_HEAD})
+# MESSAGE("A ${A} B ${B} C ${C} D ${D}")
+
+# Get versions from environment, if availble
+# MAJOR
+IF(NOT $ENV{MAJOR_CASTOR_VERSION} STREQUAL "")
+  SET(MAJOR_CASTOR_VERSION $ENV{MAJOR_CASTOR_VERSION})
+  MESSAGE("Got MAJOR_CASTOR_VERSION from environment: ${MAJOR_CASTOR_VERSION}")
+ELSE(NOT $ENV{MAJOR_CASTOR_VERSION} STREQUAL "")
+  SET(MAJOR_CASTOR_VERSION "${A}.${B}")
+  MESSAGE("Got MAJOR_CASTOR_VERSION from debian/changelog: ${MAJOR_CASTOR_VERSION}")
+ENDIF(NOT $ENV{MAJOR_CASTOR_VERSION} STREQUAL "")
+
+# MINOR
+IF(NOT $ENV{MINOR_CASTOR_VERSION} STREQUAL "")
+  SET(MINOR_CASTOR_VERSION $ENV{MINOR_CASTOR_VERSION})
+  MESSAGE("Got MINOR_CASTOR_VERSION from environment: ${MINOR_CASTOR_VERSION}")
+ELSE(NOT $ENV{MINOR_CASTOR_VERSION} STREQUAL "")
+  SET(MINOR_CASTOR_VERSION "${C}.${D}")
+  MESSAGE("Got MINOR_CASTOR_VERSION from debian/changelog: ${MINOR_CASTOR_VERSION}")
+ENDIF(NOT $ENV{MINOR_CASTOR_VERSION} STREQUAL "")
+
+# Change the release number if VCS version is provided
+IF(DEFINED VCS_VERSION)
+  STRING(REGEX REPLACE "\\..*" ".${VCS_VERSION}" MINOR_CASTOR_VERSION ${MINOR_CASTOR_VERSION})
+  MESSAGE("Replaced VCS_VERSION in MINOR_CASTOR_VERSION: ${MINOR_CASTOR_VERSION}")
+ENDIF(DEFINED VCS_VERSION)
+
+# Generate derived version from result. Almost all combinaisons are needed at some point!
+STRING(REGEX REPLACE "(.*)\\..*" "${MAJOR_CASTOR_VERSION}.\\1" CASTOR_VERSION ${MINOR_CASTOR_VERSION})
+STRING(REGEX REPLACE ".*\\." "" CASTOR_RELEASE ${MINOR_CASTOR_VERSION})
+STRING(REGEX REPLACE "\\..*" "" MAJOR_CASTOR_VERSION_TOP ${MAJOR_CASTOR_VERSION})
+STRING(REGEX REPLACE ".*\\." "" MAJOR_CASTOR_VERSION_BOTTOM ${MAJOR_CASTOR_VERSION})
+STRING(REGEX REPLACE "\\..*" "" MINOR_CASTOR_VERSION_TOP ${MINOR_CASTOR_VERSION})
+STRING(REGEX REPLACE ".*\\." "" MINOR_CASTOR_VERSION_BOTTOM ${MINOR_CASTOR_VERSION})
+MESSAGE("Generated derived versions: CASTOR_VERSION: ${CASTOR_VERSION} CASTOR_RELEASE: ${CASTOR_RELEASE}")
+MESSAGE("MAJOR_CASTOR_VERSION_TOP=${MAJOR_CASTOR_VERSION_TOP} MAJOR_CASTOR_VERSION_BOTTOM=${MAJOR_CASTOR_VERSION_BOTTOM}")
+MESSAGE("MINOR_CASTOR_VERSION_TOP=${MINOR_CASTOR_VERSION_TOP} MINOR_CASTOR_VERSION_BOTTOM=${MINOR_CASTOR_VERSION_BOTTOM}")
+
+# Generate the compilation variables, if needed
+IF(NOT DEFINED COMPILING_NOSTK)
+  SET(COMPILING_NOSTK 0)
+  MESSAGE("Setting option COMPILING_NOSTK to default (0). Override with option -DCOMPILING_NOSTK:STRING=1")
+ENDIF(NOT DEFINED COMPILING_NOSTK)
+IF(NOT DEFINED COMPILING_CLIENT)
+  SET(COMPILING_CLIENT 0)
+  MESSAGE("Setting option COMPILING_CLIENT to default (0). Override with option -DCOMPILING_CLIENT:STRING=1")
+ENDIF(NOT DEFINED COMPILING_CLIENT)
diff --git a/cmake/FindOracle.cmake b/cmake/FindOracle.cmake
new file mode 100644
index 0000000000000000000000000000000000000000..ea51eb897669eda419c634f95d613a366f2848f9
--- /dev/null
+++ b/cmake/FindOracle.cmake
@@ -0,0 +1,91 @@
+###############################################################################
+#
+# CMake module to search for Oracle client library (OCI)
+#
+# On success, the macro sets the following variables:
+# ORACLE_FOUND = if the library found
+# ORACLE_LIBRARY = full path to the library
+# ORACLE_LIBRARIES = full path to the library
+# ORACLE_INCLUDE_DIR = where to find the library headers also defined,
+# but not for general use are
+# ORACLE_VERSION = version of library which was found, e.g. "1.2.5"
+#
+# Copyright (c) 2009-2013 Mateusz Loskot <mateusz@loskot.net>
+#
+# Developed with inspiration from Petr Vanek <petr@scribus.info>
+# who wrote similar macro for TOra - http://torasql.com/
+#
+# Module source: http://github.com/mloskot/workshop/tree/master/cmake/
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+#
+###############################################################################
+
+set(ORACLE_OCI_NAMES clntsh libclntsh oci)
+set(ORACLE_NNZ_NAMES nnz10 libnnz10 nnz11 libnnz11 ociw32)
+set(ORACLE_OCCI_NAMES libocci occi oraocci10 oraocci11)
+
+# If ORACLE_HOME not defined, use it.
+if(DEFINED ENV{ORACLE_HOME})
+  set(ORACLE_HOME $ENV{ORACLE_HOME})
+  message("ORACLE_HOME=${ORACLE_HOME}")
+  find_path(ORACLE_INCLUDE_DIR
+    NAMES oci.h
+    PATHS
+    ${ORACLE_HOME}/rdbms/public
+    ${ORACLE_HOME}/include
+    ${ORACLE_HOME}/sdk/include # Oracle SDK
+    ${ORACLE_HOME}/OCI/include # Oracle XE on Windows
+  )
+  set(ORACLE_LIB_DIR
+    ${ORACLE_HOME}/lib
+    ${ORACLE_HOME}/sdk/lib # Oracle SDK
+    ${ORACLE_HOME}/sdk/lib/msvc
+    ${ORACLE_HOME}/OCI/lib/msvc # Oracle XE on Windows
+  )
+  find_library(ORACLE_OCI_LIBRARY NAMES ${ORACLE_OCI_NAMES} PATHS ${ORACLE_LIB_DIR})
+  find_library(ORACLE_OCCI_LIBRARY NAMES ${ORACLE_OCCI_NAMES} PATHS ${ORACLE_LIB_DIR})
+  find_library(ORACLE_NNZ_LIBRARY NAMES ${ORACLE_NNZ_NAMES} PATHS ${ORACLE_LIB_DIR})
+
+  if(APPLE)
+    set(ORACLE_OCIEI_NAMES libociei ociei)
+
+    find_library(ORACLE_OCIEI_LIBRARY
+      NAMES libociei ociei
+      PATHS ${ORACLE_LIB_DIR})
+
+    if(ORACLE_OCIEI_LIBRARY)
+      set(ORACLE_LIBRARY ${ORACLE_LIBRARY} ${ORACLE_OCIEI_LIBRARY})
+    else(ORACLE_OCIEI_LIBRARY)
+      message(STATUS
+        "libociei.dylib is not found. It may cause crash if you are building BUNDLE")
+    endif()
+  endif()
+
+else(DEFINED ENV{ORACLE_HOME})
+  # Attempt known locations in Linux
+  file(GLOB ORACLE_INCLUDE_DIR "/usr/include/oracle/*/client/")
+  message("ORACLE_INCLUDE_DIR=${ORACLE_INCLUDE_DIR}")
+  find_path(ORACLE_INCLUDE_DIR
+    NAMES oci.h
+    PATHS
+    ${ORACLE_INCLUDE_DIR}) # Oracle instant client 11 on Linux
+  message("ORACLE_INCLUDE_DIR=${ORACLE_INCLUDE_DIR}")
+  set(ORACLE_LIB_DIR
+    /usr/lib64/oracle/*/client/lib64
+    /usr/lib/oracle/*/client/lib)
+  find_library(ORACLE_OCI_LIBRARY NAMES ${ORACLE_OCI_NAMES} PATHS ${ORACLE_LIB_DIR})
+  find_library(ORACLE_OCCI_LIBRARY NAMES ${ORACLE_OCCI_NAMES} PATHS ${ORACLE_LIB_DIR})
+  find_library(ORACLE_NNZ_LIBRARY NAMES ${ORACLE_NNZ_NAMES} PATHS ${ORACLE_LIB_DIR})
+endif(DEFINED ENV{ORACLE_HOME})
+
+set(ORACLE_LIBRARY ${ORACLE_OCI_LIBRARY} ${ORACLE_OCCI_LIBRARY} ${ORACLE_NNZ_LIBRARY})
+set(ORACLE_LIBRARIES ${ORACLE_LIBRARY})
+
+# Handle the QUIETLY and REQUIRED arguments and set ORACLE_FOUND to TRUE
+# if all listed variables are TRUE
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(ORACLE DEFAULT_MSG ORACLE_LIBRARY ORACLE_INCLUDE_DIR)
+
+mark_as_advanced(ORACLE_INCLUDE_DIR ORACLE_LIBRARY)
\ No newline at end of file
diff --git a/cmake/UseRPMTools.cmake b/cmake/UseRPMTools.cmake
new file mode 100644
index 0000000000000000000000000000000000000000..cea0b82ef2b1377ce846c5f99f623eae324e7b99
--- /dev/null
+++ b/cmake/UseRPMTools.cmake
@@ -0,0 +1,176 @@
+#
+# - Find tools needed for building RPM Packages
+#   on Linux systems and defines macro that helps to
+#   build source or binary RPM, the MACRO assumes
+#   CMake 2.4.x which includes CPack support.
+#   CPack is used to build tar.gz source tarball
+#   which may be used by a custom user-made spec file.
+#
+# - Define RPMTools_ADD_RPM_TARGETS which defines
+#   two (top-level) CUSTOM targets for building
+#   source and binary RPMs
+#
+# Those CMake macros are provided by the TSP Developer Team
+# https://savannah.nongnu.org/projects/tsp
+#
+
+IF (WIN32)  
+  MESSAGE(STATUS "RPM tools not available on Win32 systems")
+ENDIF(WIN32)
+
+IF (UNIX)
+  # Look for RPM builder executable
+  FIND_PROGRAM(RPMTools_RPMBUILD_EXECUTABLE 
+    NAMES rpmbuild
+    PATHS "/usr/bin;/usr/lib/rpm"
+    PATH_SUFFIXES bin
+    DOC "The RPM builder tool")
+  
+  IF (RPMTools_RPMBUILD_EXECUTABLE)
+    MESSAGE(STATUS "Looking for RPMTools... - found rpmuild is ${RPMTools_RPMBUILD_EXECUTABLE}")
+    SET(RPMTools_RPMBUILD_FOUND "YES")
+    GET_FILENAME_COMPONENT(RPMTools_BINARY_DIRS ${RPMTools_RPMBUILD_EXECUTABLE} PATH)
+  ELSE (RPMTools_RPMBUILD_EXECUTABLE) 
+    SET(RPMTools_RPMBUILD_FOUND "NO")
+    MESSAGE(STATUS "Looking for RPMTools... - rpmbuild NOT FOUND")
+  ENDIF (RPMTools_RPMBUILD_EXECUTABLE) 
+  
+  # Detect if CPack was included or not
+  IF (NOT DEFINED "CPACK_PACKAGE_NAME") 
+    MESSAGE(FATAL_ERROR "CPack was not included, you should include CPack before Using RPMTools")
+  ENDIF (NOT DEFINED "CPACK_PACKAGE_NAME")
+  
+  IF (RPMTools_RPMBUILD_FOUND)
+    SET(RPMTools_FOUND TRUE)    
+    #
+    # - first arg  (ARGV0) is RPM name
+    # - second arg (ARGV1) is the RPM spec file path [optional]
+    # - third arg  (ARGV2) is the RPM ROOT DIRECTORY used to build RPMs [optional]
+    #
+    MACRO(RPMTools_ADD_RPM_TARGETS RPMNAME)
+
+      #
+      # If no spec file is provided create a minimal one
+      #
+      IF ("${ARGV1}" STREQUAL "")
+	SET(SPECFILE_PATH "${CMAKE_BINARY_DIR}/${RPMNAME}.spec")
+      ELSE ("${ARGV1}" STREQUAL "")
+	SET(SPECFILE_PATH "${ARGV1}")
+      ENDIF("${ARGV1}" STREQUAL "")
+      
+      # Verify whether if RPM_ROOTDIR was provided or not
+      IF("${ARGV2}" STREQUAL "") 
+	SET(RPM_ROOTDIR ${CMAKE_BINARY_DIR}/RPM)
+      ELSE ("${ARGV2}" STREQUAL "")
+	SET(RPM_ROOTDIR "${ARGV2}")	
+      ENDIF("${ARGV2}" STREQUAL "")
+      MESSAGE(STATUS "RPMTools:: Using RPM_ROOTDIR=${RPM_ROOTDIR}")
+
+      # Prepare RPM build tree
+      FILE(MAKE_DIRECTORY ${RPM_ROOTDIR})
+      FILE(MAKE_DIRECTORY ${RPM_ROOTDIR}/tmp)
+      FILE(MAKE_DIRECTORY ${RPM_ROOTDIR}/BUILD)
+      FILE(MAKE_DIRECTORY ${RPM_ROOTDIR}/RPMS)
+      FILE(MAKE_DIRECTORY ${RPM_ROOTDIR}/SOURCES)
+      FILE(MAKE_DIRECTORY ${RPM_ROOTDIR}/SPECS)
+      FILE(MAKE_DIRECTORY ${RPM_ROOTDIR}/SRPMS)
+
+      #
+      # We check whether if the provided spec file is
+      # to be configure or not.
+      # 
+      IF ("${ARGV1}" STREQUAL "")
+	SET(SPECFILE_PATH "${RPM_ROOTDIR}/SPECS/${RPMNAME}.spec")
+	SET(SPECFILE_NAME "${RPMNAME}.spec")
+	MESSAGE(STATUS "No Spec file given generate a minimal one --> ${RPM_ROOTDIR}/SPECS/${RPMNAME}.spec")
+      	FILE(WRITE ${RPM_ROOTDIR}/SPECS/${RPMNAME}.spec
+	  "# -*- rpm-spec -*-
+Summary:        ${RPMNAME}
+Name:           ${RPMNAME}
+Version:        ${PACKAGE_VERSION}
+Release:        1
+License:        Unknown
+Group:          Unknown
+Source:         ${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar.gz
+BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root
+BuildRequires:	cmake
+
+%define prefix /opt/${RPMNAME}-%{version}
+%define rpmprefix $RPM_BUILD_ROOT%{prefix}
+%define srcdirname %{name}-%{version}-Source
+
+%description
+${RPMNAME} : No description for now
+
+%prep
+%setup -q -n %{srcdirname}
+
+%build
+cd ..
+rm -rf build_tree
+mkdir build_tree
+cd build_tree
+cmake -DCMAKE_INSTALL_PREFIX=%{rpmprefix} ../%{srcdirname}
+make
+  
+%install 
+cd ../build_tree
+make install
+
+%clean
+rm -rf %{srcdirname}
+rm -rf build_tree
+
+%files
+%defattr(-,root,root,-)
+%dir %{prefix}
+%{prefix}/*
+
+%changelog
+* Wed Feb 28 2007 Erk <eric.noulard@gmail.com>
+  Generated by CMake UseRPMTools macros"
+	)
+
+      ELSE ("${ARGV1}" STREQUAL "")
+	SET(SPECFILE_PATH "${ARGV1}")
+	
+	GET_FILENAME_COMPONENT(SPECFILE_EXT ${SPECFILE_PATH} EXT)      
+	IF ("${SPECFILE_EXT}" STREQUAL ".spec")
+	  # This is a 'ready-to-use' spec file which does not need to be CONFIGURED
+	  GET_FILENAME_COMPONENT(SPECFILE_NAME ${SPECFILE_PATH} NAME)
+	  MESSAGE(STATUS "Simple copy spec file <${SPECFILE_PATH}> --> <${RPM_ROOTDIR}/SPECS/${SPECFILE_NAME}>")
+	  CONFIGURE_FILE(
+	    ${SPECFILE_PATH} 
+	    ${RPM_ROOTDIR}/SPECS/${SPECFILE_NAME}
+	    COPYONLY)
+	ELSE ("${SPECFILE_EXT}" STREQUAL ".spec")
+	  # This is a to-be-configured spec file
+	  GET_FILENAME_COMPONENT(SPECFILE_NAME ${SPECFILE_PATH} NAME_WE)
+	  SET(SPECFILE_NAME "${SPECFILE_NAME}.spec")
+	  MESSAGE(STATUS "Configuring spec file <${SPECFILE_PATH}> --> <${RPM_ROOTDIR}/SPECS/${SPECFILE_NAME}>")
+	  CONFIGURE_FILE(
+	    ${SPECFILE_PATH} 
+	    ${RPM_ROOTDIR}/SPECS/${SPECFILE_NAME}
+	    @ONLY)
+	ENDIF ("${SPECFILE_EXT}" STREQUAL ".spec")
+      ENDIF("${ARGV1}" STREQUAL "")
+            
+      ADD_CUSTOM_TARGET(${RPMNAME}_srpm
+	COMMAND cpack -G TGZ --config CPackSourceConfig.cmake
+	COMMAND ${CMAKE_COMMAND} -E copy ${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar.gz ${RPM_ROOTDIR}/SOURCES    
+	COMMAND ${RPMTools_RPMBUILD_EXECUTABLE} -bs --define=\"_topdir ${RPM_ROOTDIR}\"  --define '_source_filedigest_algorithm md5' --define '_binary_filedigest_algorithm md5' --nodeps --buildroot=${RPM_ROOTDIR}/tmp ${RPM_ROOTDIR}/SPECS/${SPECFILE_NAME} 
+	)
+      
+      ADD_CUSTOM_TARGET(${RPMNAME}_rpm
+	COMMAND cpack -G TGZ --config CPackSourceConfig.cmake
+	COMMAND ${CMAKE_COMMAND} -E copy ${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar.gz ${RPM_ROOTDIR}/SOURCES    
+	COMMAND ${RPMTools_RPMBUILD_EXECUTABLE} -bb --define=\"_topdir ${RPM_ROOTDIR}\" --buildroot=${RPM_ROOTDIR}/tmp ${RPM_ROOTDIR}/SPECS/${SPECFILE_NAME} 
+	)  
+    ENDMACRO(RPMTools_ADD_RPM_TARGETS)
+
+  ELSE (RPMTools_RPMBUILD_FOUND)
+    SET(RPMTools FALSE)
+  ENDIF (RPMTools_RPMBUILD_FOUND)  
+  
+ENDIF (UNIX)
+  
diff --git a/h/patchlevel.h b/h/patchlevel.h
deleted file mode 100644
index df920f21f800b80ad7ee2cab79c05fda6eb1440b..0000000000000000000000000000000000000000
--- a/h/patchlevel.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * $Id: patchlevel.h,v 1.8 2007/12/07 17:19:31 itglp Exp $
- */
-
-/*
- */
-
-/*
- * Copyright (C) 1990-2000 by CERN IT-PDP/CS
- * All rights reserved
- */
-
-/* patchlevel.h                 Patch level                             */
-
-#ifndef _PATCHLEVEL_H_INCLUDED_
-#define _PATCHLEVEL_H_INCLUDED_
-
-#define MAJORVERSION    __MAJORVERSION__
-#define MINORVERSION    __MINORVERSION__
-#define MAJORRELEASE    __MAJORRELEASE__
-#define MINORRELEASE    __MINORRELEASE__
-#define BASEVERSION     __BASEVERSION__
-
-/* For untagged builds (e.g. from CVS) we redefine the replacement macros */
-/* warning: there must be ONLY ONE space between define and the macro here to work! */
-#define __MAJORVERSION__ 0
-#define __MINORVERSION__ 0
-#define __MAJORRELEASE__ 0
-#define __MINORRELEASE__ 0
-
-#define PATCHLEVEL      MINORRELEASE
-
-#endif /* _PATCHLEVEL_H_INCLUDED_ */
diff --git a/h/patchlevel.h.in b/h/patchlevel.h.in
new file mode 100644
index 0000000000000000000000000000000000000000..996517f6b45739fdad65d711598f771d07aa8957
--- /dev/null
+++ b/h/patchlevel.h.in
@@ -0,0 +1,26 @@
+/*
+ * $Id: patchlevel.h,v 1.8 2007/12/07 17:19:31 itglp Exp $
+ */
+
+/*
+ */
+
+/*
+ * Copyright (C) 1990-2000 by CERN IT-PDP/CS
+ * All rights reserved
+ */
+
+/* patchlevel.h                 Patch level                             */
+
+#ifndef _PATCHLEVEL_H_INCLUDED_
+#define _PATCHLEVEL_H_INCLUDED_
+
+#define MAJORVERSION  @MAJOR_CASTOR_VERSION_TOP@
+#define MINORVERSION  @MAJOR_CASTOR_VERSION_BOTTOM@
+#define MAJORRELEASE  @MINOR_CASTOR_VERSION_TOP@
+#define MINORRELEASE  @MINOR_CASTOR_VERSION_BOTTOM@
+#define BASEVERSION  @CASTOR_VERSION@
+
+#define PATCHLEVEL      MINORRELEASE
+
+#endif /* _PATCHLEVEL_H_INCLUDED_ */
diff --git a/maketar.sh b/makespec.in.sh
old mode 100755
new mode 100644
similarity index 62%
rename from maketar.sh
rename to makespec.in.sh
index 00c4ab525ecf6c6b1151805db1fe131a052881c4..b9f047752869a75524da3e754207fe80f830185c
--- a/maketar.sh
+++ b/makespec.in.sh
@@ -1,72 +1,8 @@
 #!/bin/sh
+# Generate the spec file from a Debian style package description collection.
+output=$1
 
-if [ "x${MAJOR_CASTOR_VERSION}" = "x" ]; then
-  echo "No MAJOR_CASTOR_VERSION environment variable - guessing from changelog"
-  MAJOR_CASTOR_VERSION=`egrep "^castor" debian/changelog | awk '{print $2}' | head -1 | perl -ne 'if (/\((\d+)\.(\d+)/) {print "$1.$2\n";}'`
-  if [ "x${MAJOR_CASTOR_VERSION}" = "x" ]; then
-    echo "Unable to guess MAJOR_CASTOR_VERSION"
-    exit 1
-  fi
-  export MAJOR_CASTOR_VERSION
-  echo "... Got MAJOR_CASTOR_VERSION=${MAJOR_CASTOR_VERSION}"
-fi
-if [ "x${MINOR_CASTOR_VERSION}" = "x" ]; then
-  echo "No MINOR_CASTOR_VERSION environment variable - guessing from changelog"
-  MINOR_CASTOR_VERSION=`egrep "^castor" debian/changelog | awk '{print $2}' | head -1 | perl -ne 'if (/(\d+)\-(\d+)\)/) {print "$1.$2\n";}'`
-  if [ "x${MINOR_CASTOR_VERSION}" = "x" ]; then
-    echo "Unable to guess MINOR_CASTOR_VERSION"
-    exit 1
-  fi
-  export MINOR_CASTOR_VERSION
-  echo "... Got MINOR_CASTOR_VERSION=${MINOR_CASTOR_VERSION}"
-fi
-
-#
-## We expect ${MAJOR_CASTOR_VERSION} in the form a.b
-#
-echo ${MAJOR_CASTOR_VERSION} | egrep -q '^[0-9]+\.[0-9]+$'
-if [ $? -ne 0 ]; then
-  echo "MAJOR_CASTOR_VERSION (${MAJOR_CASTOR_VERSION}) should be in the form a.b, example: 2.1"
-  exit 1
-fi
-
-#
-## We expect ${MINOR_CASTOR_VERSION} in the form c.d
-#
-echo ${MINOR_CASTOR_VERSION} | egrep -q '^[0-9]+\.[0-9]+$'
-if [ $? -ne 0 ]; then
-  echo "MINOR_CASTOR_VERSION (${MINOR_CASTOR_VERSION}) should be in the form c.d, example: 99.1"
-  exit 1
-fi
-
-a=`echo ${MAJOR_CASTOR_VERSION} | sed 's/\..*//g'`
-b=`echo ${MAJOR_CASTOR_VERSION} | sed 's/.*\.//g'`
-c=`echo ${MINOR_CASTOR_VERSION} | sed 's/\..*//g'`
-d=`echo ${MINOR_CASTOR_VERSION} | sed 's/.*\.//g'`
-version=${a}.${b}.${c}
-fullversion=${version}-${d}
-
-echo "### Making build directory"
-
-# Go to castor-${version} and do the changes in here
-curdir=`pwd`
-cd ..
-[ -d "castor-${version}" ] && rm -rf castor-${version}
-rsync -aC --exclude '.__afs*' $curdir/ castor-${version}
-cd castor-${version}
-
-echo "### Customizing spec file"
-
-#
-# Make spec file in sync
-#
-perl -pi -e s/\__MAJOR_CASTOR_VERSION__/${MAJOR_CASTOR_VERSION}/g CASTOR.spec
-perl -pi -e s/\__MINOR_CASTOR_VERSION__/${MINOR_CASTOR_VERSION}/g CASTOR.spec
-perl -pi -e s/__A__/${a}/g CASTOR.spec
-perl -pi -e s/__B__/${b}/g CASTOR.spec
-perl -pi -e s/__C__/${c}/g CASTOR.spec
-perl -pi -e s/__D__/${d}/g CASTOR.spec
-
+echo "### Generating spec file"
 function copyInstallPermInternal {
     package=$1
     file=$2
@@ -76,7 +12,7 @@ function copyInstallPermInternal {
           sed -i "s/$g\$/%config(noreplace) $g/" $file
       done
     fi
-    cat $file >> CASTOR.spec
+    cat $file >> $output
 }
 
 function copyInstallPerm {
@@ -97,11 +33,11 @@ function copyInstallPerm {
     if [ $? -eq 0 ]; then
       copyInstallPermInternal $package debian/$package.install.perm.tmp
     else
-      echo "%ifarch x86_64" >> CASTOR.spec
+      echo "%ifarch x86_64" >> $output
       copyInstallPermInternal $package debian/$package.install.perm.64.tmp
-      echo "%else" >> CASTOR.spec
+      echo "%else" >> $output
       copyInstallPermInternal $package debian/$package.install.perm.tmp
-      echo "%endif" >> CASTOR.spec
+      echo "%endif" >> $output
     fi
 }
 
@@ -134,11 +70,11 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
       }' $package XBS-Group |
       sed 's/ //g' | sed 's/\${[^{},]*}//g' | sed 's/^,*//g' | sed 's/,,*/,/g'`
     if [ "${group}" != "Client" ]; then
-        echo "%if ! %compiling_client" >> CASTOR.spec # no a client package
+        echo "%if ! %compiling_client" >> $output # no a client package
     fi
-    echo "%package -n $actualPackage" >> CASTOR.spec
-    echo "Summary: Cern Advanced mass STORage" >> CASTOR.spec
-    echo "Group: Application/Castor" >> CASTOR.spec
+    echo "%package -n $actualPackage" >> $output
+    echo "Summary: Cern Advanced mass STORage" >> $output
+    echo "Group: Application/Castor" >> $output
     #
     ## Except for Description that is truely a multi-line thing even within debian/control, the other
     ## fields are always on a single line:
@@ -163,7 +99,7 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
       }' $package Depends |
       sed 's/^[ \t]*//' | sed 's/\${[^{},]*}//g' | sed 's/^,*//g' | sed 's/,,*/,/g'`
     if [ -n "${requires}" ]; then
-        echo "Requires: ${requires}" >> CASTOR.spec
+        echo "Requires: ${requires}" >> $output
     fi
     #
     ## Get BuildRequires
@@ -179,7 +115,7 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
         print "$this{$what}\n";
       }' $package Build-Depends`
     if [ -n "${buildrequires}" ]; then
-        echo "BuildRequires: ${buildrequires}" >> CASTOR.spec
+        echo "BuildRequires: ${buildrequires}" >> $output
     fi
     #
     ## Get Conditional BuildRequires
@@ -194,7 +130,7 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
       $this = $1;
       print "$this\n";' $package`
     if [ -n "${buildrequirescond}" ]; then
-        echo "${buildrequirescond}" >> CASTOR.spec
+        echo "${buildrequirescond}" >> $output
     fi
     #
     ## Get Provides
@@ -211,7 +147,7 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
       }' $package Provides |
       sed 's/ //g'`
     if [ -n "${provides}" ]; then
-        echo "Provides: ${provides}" >> CASTOR.spec
+        echo "Provides: ${provides}" >> $output
     fi
     #
     ## Get Conflicts
@@ -228,7 +164,7 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
       }' $package Conflicts |
       sed 's/ //g'`
     if [ -n "${conflicts}" ]; then
-        echo "Conflicts: ${conflicts}" >> CASTOR.spec
+        echo "Conflicts: ${conflicts}" >> $output
     fi
     #
     ## Get Obsoletes
@@ -245,12 +181,12 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
       }' $package XBS-Obsoletes |
       sed 's/ //g'`
     if [ -n "${obsoletes}" ]; then
-        echo "Obsoletes: ${obsoletes}" >> CASTOR.spec
+        echo "Obsoletes: ${obsoletes}" >> $output
     fi
     #
     ## Get description
     #
-    echo "%description -n $actualPackage" >> CASTOR.spec
+    echo "%description -n $actualPackage" >> $output
     cat debian/control | perl -e '
       $package=shift;
       $what=shift;
@@ -261,25 +197,25 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
       $this =~ s/\n*$//sg;
       $this =~ s/\n/ \- /sg;
       $this =~ s/  */ /sg;
-      print "$this\n";' $package >> CASTOR.spec
+      print "$this\n";' $package >> $output
     #
     ## Get file list
     #
-    echo "%files -n $actualPackage" >> CASTOR.spec
-    echo "%defattr(-,root,root)" >> CASTOR.spec
+    echo "%files -n $actualPackage" >> $output
+    echo "%defattr(-,root,root)" >> $output
     ## deal with manpages
     if [ -s "debian/$package.manpages" ]; then
         for man in `cat debian/$package.manpages | sed 's/debian\/castor\///g'`; do
-            echo "%attr(0644,root,bin) %doc /$man" >> CASTOR.spec
+            echo "%attr(0644,root,bin) %doc /$man" >> $output
         done
     fi
     ## deal with directories
     if [ -s "debian/$package.dirs" ]; then
         cat debian/$package.dirs | while read dir; do
             if [ `echo $dir | grep -c %attr` -lt 1 ]; then
-                echo "%attr(-,root,bin) %dir /$dir" >> CASTOR.spec
+                echo "%attr(-,root,bin) %dir /$dir" >> $output
             else
-                echo $dir | sed -e "s/) /) %dir \//" >> CASTOR.spec
+                echo $dir | sed -e "s/) /) %dir \//" >> $output
             fi
         done
     fi
@@ -289,30 +225,24 @@ for this in `grep Package: debian/control | awk '{print $NF}'`; do
     fi
     ## deal with scripts
     if [ -s "debian/$package.postinst" ]; then
-        echo "%post -n $actualPackage" >> CASTOR.spec
-        cat debian/$package.postinst >> CASTOR.spec
+        echo "%post -n $actualPackage" >> $output
+        cat debian/$package.postinst >> $output
     fi
     if [ -s "debian/$package.preun" ]; then
-        echo "%preun -n $actualPackage" >> CASTOR.spec
-        cat debian/$package.preun >> CASTOR.spec
+        echo "%preun -n $actualPackage" >> $output
+        cat debian/$package.preun >> $output
     fi
     if [ -s "debian/$package.pre" ]; then
-        echo "%pre -n $actualPackage" >> CASTOR.spec
-        cat debian/$package.pre >> CASTOR.spec
+        echo "%pre -n $actualPackage" >> $output
+        cat debian/$package.pre >> $output
     fi
     if [ -s "debian/$package.postun" ]; then
-        echo "%postun -n $actualPackage" >> CASTOR.spec
-        cat debian/$package.postun >> CASTOR.spec
+        echo "%postun -n $actualPackage" >> $output
+        cat debian/$package.postun >> $output
     fi
     if [ "${group}" != "Client" ]; then
-        echo "%endif" >> CASTOR.spec # end of client compilation if
+        echo "%endif" >> $output # end of client compilation if
     fi
-    echo >> CASTOR.spec
+    echo >> $output
 done
 
-cd ..
-
-echo "### Creating tarball"
-
-tar -zcf castor-${fullversion}.tar.gz --exclude '.__afs*' castor-${version}
-rm -rf castor-${version}