
macro(subdirlist result dir)
  set(ExtraMacroArgs ${ARGN})
  message("GLOB children RELATIVE ${dir} ${dir}/*")
  file(GLOB children RELATIVE ${dir} ${dir}/*)
  set(dirlist "")
  foreach(child ${children})
    if(IS_DIRECTORY ${dir}/${child})
      if(ExtraMacroArgs STREQUAL RELATIVE)
	list(APPEND dirlist ${child})
      else(ExtraMacroArgs STREQUAL RELATIVE)
	list(APPEND dirlist ${dir}/${child})
      endif(ExtraMacroArgs STREQUAL RELATIVE)
    endif(IS_DIRECTORY ${dir}/${child})
  endforeach()
  set(${result} ${dirlist})
endmacro()

#=============================================================================

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" ${CMAKE_MODULE_PATH})
#include(cpack_generator)
include(cpack_generator)

if(CPACK_GENERATOR)
  message(STATUS "Requested CPACK_GENERATOR = ${CPACK_GENERATOR}")
else(CPACK_GENERATOR)
  if(SPECIFIC_SYSTEM_PREFERED_CPACK_GENERATOR)
    message(STATUS "Setting CPACK_GENERATOR to ${SPECIFIC_SYSTEM_PREFERED_CPACK_GENERATOR}")
    set(CPACK_GENERATOR ${SPECIFIC_SYSTEM_PREFERED_CPACK_GENERATOR} CACHE STRING "")
  endif(SPECIFIC_SYSTEM_PREFERED_CPACK_GENERATOR)
endif(CPACK_GENERATOR )

#set(SPECIFIC_COMPILER_NAME "")

#=============================================================================
# SETTINGS
#=============================================================================

#=== Installer filename ======================================================

# Variable PACKAGE_ARCHITECTURE
# if(CPACK_GENERATOR STREQUAL "RPM")
#   execute_process(COMMAND uname "-m"
#     OUTPUT_VARIABLE PACKAGE_ARCHITECTURE
#     OUTPUT_STRIP_TRAILING_WHITESPACE )
# elseif(CPACK_GENERATOR STREQUAL "DEB")
#   message(STATUS "CPACK_DEBIAN_PACKAGE_ARCHITECTURE = ${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")
#   find_program(DPKG_CMD dpkg)
#   if(NOT DPKG_CMD)
#     message(STATUS "Can not find dpkg in your path, default to i386.")
#     set(PACKAGE_ARCHITECTURE i386)
#   endif(NOT DPKG_CMD)
#   execute_process(COMMAND "${DPKG_CMD}" --print-architecture
#     OUTPUT_VARIABLE PACKAGE_ARCHITECTURE
#     OUTPUT_STRIP_TRAILING_WHITESPACE) 
# else(CPACK_GENERATOR STREQUAL "RPM")
#   set(PACKAGE_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR})
# endif(CPACK_GENERATOR STREQUAL "RPM")
set(PACKAGE_ARCHITECTURE ${SPECIFIC_SYSTEM_ARCHITECTURE})
message(STATUS "PACKAGE_ARCHITECTURE=${PACKAGE_ARCHITECTURE}")

# Variable CPACK_SYSTEM_NAME
string(TOLOWER "${SPECIFIC_SYSTEM_NAME}.${SPECIFIC_SYSTEM_RELEASE}.${PACKAGE_ARCHITECTURE}" _CPACK_SYSTEM_NAME)
set(CPACK_SYSTEM_NAME ${_CPACK_SYSTEM_NAME} CACHE STRING "" FORCE)
message(STATUS "CPACK_SYSTEM_NAME=${CPACK_SYSTEM_NAME}")

# Variable CPACK_PACKAGE_FILE_NAME
# Default value for ${CPACK_PACKAGE_FILE_NAME} is:
#   ${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}
# See: cpack --help-variable CPACK_PACKAGE_FILE_NAME
#message(STATUS "CPACK_PACKAGE_FILE_NAME=${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")

#=== Components ==============================================================

set(CPACK_TGZ_COMPONENT_INSTALL 1 PARENT_SCOPE)
set(CPACK_DEB_COMPONENT_INSTALL 1 PARENT_SCOPE)
set(CPACK_RPM_COMPONENT_INSTALL 1 PARENT_SCOPE)

# One single .deb with everything: engine, TOLBase and the packages. Switching
# this back to "IGNORE" produces one .deb per component instead, which is what
# it did before, and the per-component DEPENDS variables would have to come
# back with it.
set(CPACK_COMPONENTS_GROUPING "ALL_COMPONENTS_IN_ONE" PARENT_SCOPE)
set(CPACK_COMPONENTS_ALL core headers gui packages PARENT_SCOPE)

set(CPACK_COMPONENT_CORE_DESCRIPTION "TOL Core (Time Oriented Language)" PARENT_SCOPE)
set(CPACK_COMPONENT_HEADERS_DESCRIPTION "C++ headers for TOL (Time Oriented Language)" PARENT_SCOPE)
set(CPACK_COMPONENT_GUI_DESCRIPTION "GUI for TOL (Time Oriented Language)" PARENT_SCOPE)
set(CPACK_COMPONENT_PACKAGES_DESCRIPTION "TOL packages, to be installed per user with tol-install-packages" PARENT_SCOPE)

#=== RPM =====================================================================
#!! Mirar si algunas configuraciones pueden obviarse porque por defecto usan 
#!! una configuración general, por ejemplo: CPACK_RPM_PACKAGE_VENDOR quizá
#!! ya usa: CPACK_PACKAGE_VENDOR
#!! Comprobar si los nombres de las componentes pueden ponerse en mayúsculas:
#!! CPACK_RPM_CORE_PACKAGE_DESCRIPTION en lugar de: CPACK_RPM_core_PACKAGE_DESCRIPTION

set(CPACK_RPM_PACKAGE_URL "www.tol-project.org" PARENT_SCOPE)
set(CPACK_RPM_PACKAGE_VENDOR "TOL Project" PARENT_SCOPE)
set(CPACK_RPM_PACKAGE_RELOCATABLE OFF PAREENT_SCOPE)
set(CPACK_RPM_PACKAGE_AUTOREQ " no" PARENT_SCOPE)
set(CPACK_RPM_PACKAGE_GROUP "Applications/Engineering" PARENT_SCOPE)

set(CPACK_RPM_core_PACKAGE_SUMMARY "The minimal TOL components necessary for a functional runtime" PARENT_SCOPE)
set(CPACK_RPM_core_PACKAGE_DESCRIPTION 
"
TOL = Time Oriented Language is a language and environment for statistical 
computing. It provides a natural way to handle time series data and model it.

There is a TOL package called TolRInside based on Rcpp/RInside wich R which
expose all R funcionality whithin TOL.

" PARENT_SCOPE)

set(FEDORA_core_REQUIRES "atlas, tcl >= 8.5, tcllib, boost-filesystem >= 1.41, boost-thread >= 1.41, suitesparse >= 3, fftw >= 3, gsl >= 1.13, bzip2-libs, zlib, libtool-ltdl, libcurl >= 7.19, postgresql-libs >= 8.4, mysql-libs, unixODBC")

set(CENTOS_core_REQUIRES "atlas, blas, lapack, tcl >= 8.5, tcllib, boost-atomic >= 1.41, boost-date-time >= 1.41, boost-chrono >= 1.41, boost-filesystem >= 1.41, boost-thread >= 1.41, suitesparse >= 3, fftw >= 3, gsl >= 1.13, bzip2-libs, zlib, libtool-ltdl, libcurl >= 7.19, postgresql-libs >= 8.4, mysql-libs, unixODBC")

set(CENTOS_gui_REQUIRES "dejavu-sans-fonts")

set(CPACK_RPM_core_PACKAGE_REQUIRES ${CENTOS_core_REQUIRES} PARENT_SCOPE)
set(CPACK_RPM_headers_PACKAGE_REQUIRES "tol-core = ${CPACK_PACKAGE_VERSION}"  PARENT_SCOPE)
set(CPACK_RPM_gui_PACKAGE_REQUIRES "tol-core = ${CPACK_PACKAGE_VERSION}, tk >= 8.5, bwidget"  PARENT_SCOPE)

set(CPACK_RPM_core_PACKAGE_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} PARENT_SCOPE)
set(CPACK_RPM_headers_PACKAGE_ARCHITECTURE noarch PARENT_SCOPE)
set(CPACK_RPM_gui_PACKAGE_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} PARENT_SCOPE)

#=== DEB =====================================================================

# + CPACK_DEBIAN_PACKAGE_NAME = CPACK_PACKAGE_NAME (lower case)
# + CPACK_DEBIAN_PACKAGE_VERSION = CPACK_PACKAGE_VERSION
# + CPACK_DEBIAN_PACKAGE_ARCHITECTURE
# - CPACK_DEBIAN_PACKAGE_DEPENDS
# + CPACK_DEBIAN_PACKAGE_MAINTAINER = CPACK_PACKAGE_CONTACT
# + CPACK_DEBIAN_PACKAGE_DESCRIPTION = PACK_PACKAGE_DESCRIPTION_SUMMARY
# + CPACK_DEBIAN_PACKAGE_SECTION = 'devel'
# + CPACK_DEBIAN_PACKAGE_PRIORITY = 'optional'
# - CPACK_DEBIAN_PACKAGE_HOMEPAGE
set(CPACK_DEBIAN_PACKAGE_SECTION "Mathematics" PARENT_SCOPE)
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://www.tol-prj.org" PARENT_SCOPE)

# The dependencies on shared libraries are NOT written by hand any more.
# dpkg-shlibdeps reads the binaries and derives them exactly, with their
# versions, which is what kept the old list wrong: it still named libgsl2 and
# libmysqlclient20 (Ubuntu 16.04) long after those names had gone, and
# libboost-*-dev, which are build dependencies and have no business here.
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON PARENT_SCOPE)

# What shlibdeps cannot see, and therefore has to be declared by hand:
#
#  * The Tcl/Tk extensions, loaded at run time by name, which leave no trace in
#    the ELF. blt is gone on purpose -- the tolbaseBLT chain was deleted and the
#    shipped rbc replaces it.
#
#  * What the TOL PACKAGES need. They travel as zips, so their .so files are
#    invisible to shlibdeps: it only reads binaries the .deb installs. Without
#    these two, TolIpopt and NonLinGloOpt fail to load after
#    tol-install-packages and apt never warned about anything. Their own
#    transitive needs -- mumps, scotch -- come from their Debian packages.
#    Only 64-bit .so files count: the packages also carry Linux_x86_32 and
#    Linux32 leftovers, which is where libc6-i386 and a request for the
#    long-gone libgsl.so.0 came from. TOL never loads them on a 64-bit machine,
#    because it picks the directory of its own platform.
#
# R is a dependency of the house, not of one package. TolExcel 5 reads and
# writes workbooks through it, BysMcmc needs CODA for the MCMC diagnostics, and
# the sampler asks quadprog for a feasible starting point on every constrained
# model, so any Bayesian work needs it. Nothing can be derived automatically:
# no shipped binary links libR.
#
# The list is not guessed. It comes from every RLibrary, library(), require(),
# RcheckPackage and _LoadRPackage in the TOL code of OTAN and mms-pkgs, split
# into what the core path needs and what only some packages use.
# One single list, written whole. Referring to ${CPACK_DEBIAN_PACKAGE_DEPENDS}
# here does NOT work: it was set with PARENT_SCOPE, so in this scope it is
# empty, and the field ended up starting with a comma, which dpkg rejects.
set(CPACK_DEBIAN_PACKAGE_DEPENDS "tcl (>= 8.6), tk (>= 8.6), tcllib, tklib, bwidget, tktreectrl, libtk-img, tk-table, tclodbc, tclcurl, coinor-libipopt1v5, libnlopt0, r-base-core, r-cran-rcpp, r-cran-rinside, r-cran-coda, r-cran-quadprog, r-cran-readxl, r-cran-openxlsx, r-cran-base64enc, r-cran-lubridate, r-cran-polynom" PARENT_SCOPE)

# Used by specific packages, so they travel as Recommends: apt brings them in a
# normal install and a minimal one can decline.
set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "r-cran-abind, r-cran-corpcor, r-cran-corrplot, r-cran-fbasics, r-cran-jsonlite, r-cran-mvtnorm, r-cran-numderiv, r-cran-proc, r-cran-rgl, r-cran-rglpk, r-cran-sdmtools, r-cran-snow, r-cran-xts" PARENT_SCOPE)

# SEVEN more R packages appear in the code and are NOT packaged in Ubuntu, so
# they cannot be declared and have to be installed from CRAN by whoever needs
# them: alabama, aod, DescTools, dlm, KFAS, mcgibbsit and mfx. They belong to
# specific packages, so a plain installation works without them until something
# asks.
#
# tolRlink used to be in that list and it should never have been: it is not on
# CRAN and it never will be, because it is TOL's own R package -- the R side of
# the link that TolRInside uses to talk to the engine. Nobody could install it
# from CRAN, so TolRInside stayed broken on every fresh machine. It now travels
# INSIDE the .deb, with tolBasis, which it imports, and the postinst installs
# both into R's site library. That is why base64enc, lubridate and polynom are
# hard dependencies above: they are what those two need to load.

#=============================================================================
# INSTALLATION FILES
#=============================================================================

#=== Installation Directory ==================================================

# lib or lib64 directory
if(CPACK_GENERATOR STREQUAL "RPM")
  get_property(LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
  if(LIB64 STREQUAL "TRUE")
    set(LIBSUFFIX "64")
  else(LIB64 STREQUAL "TRUE")
    set(LIBSUFFIX "")
  endif(LIB64 STREQUAL "TRUE")
else(CPACK_GENERATOR STREQUAL "RPM")
  set(LIBSUFFIX "")
endif(CPACK_GENERATOR STREQUAL "RPM")

set(PREFIX_TOL "lib${LIBSUFFIX}/tol${CPACK_PACKAGE_VERSION}")

#=== [core] ==================================================================

# core executables [BIN]
install(FILES 
  "${TOL_RUNTIME_DIR}/bin/tolcon"
  "${TOL_RUNTIME_DIR}/bin/tolsh"
  DESTINATION "${PREFIX_TOL}/bin"
  COMPONENT core
  # 755, not 777: these land in /usr and a world-writable executable there
  # means any user can replace the interpreter for everybody else.
  PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ
  GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

# The executables, linked from a directory that IS on the PATH. Without this the
# .deb leaves them in /usr/lib/tol4.1/bin, where nobody's PATH looks, so every
# user ends up making links by hand -- and pointing them, as happened on this
# machine, at a BUILD TREE instead of at the installed version. That is how you
# end up running a TOLBase from three weeks ago without noticing.
#
# The link is RELATIVE, "../lib/tol4.1/bin/tolsh", which is what Debian policy
# asks for links inside the same package: an absolute one breaks if the package
# is installed under another prefix.
#
# /usr/bin and not /usr/local/bin: policy reserves /usr/local for software built
# locally, and a package must never write there.
#
# The $ENV{DESTDIR} is not decoration. At install time CMAKE_INSTALL_PREFIX is
# /usr, and CPack redirects everything to its staging directory through DESTDIR;
# without the prefix, cpack tries to create the link in the REAL /usr/bin and
# dies with "Permission denied", which is what happened the first time.
foreach(tol_exe tolcon tolsh)
  install(CODE "
    file(MAKE_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/bin\")
    file(CREATE_LINK \"../${PREFIX_TOL}/bin/${tol_exe}\"
         \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/bin/${tol_exe}\" SYMBOLIC)
  " COMPONENT core)
endforeach(tol_exe)

# TolCore (stdlib directory)
file(TO_CMAKE_PATH "${TOL_RUNTIME_DIR}/bin/stdlib" stdlib_dir)
install(DIRECTORY ${stdlib_dir} DESTINATION "${PREFIX_TOL}/bin"
  COMPONENT core)

# libraries [LIB]
file(GLOB tol_libs  "${TOL_RUNTIME_DIR}/lib/*.so")
install(FILES ${tol_libs} DESTINATION "${PREFIX_TOL}/lib"
  COMPONENT core)

# core tcltk libraries (toltcl & TclCurl)
file(GLOB toltcl_dirs  
  "${TOL_RUNTIME_DIR}/lib/toltcl*"
  "${TOL_RUNTIME_DIR}/lib/TclCurl*")
install(DIRECTORY ${toltcl_dirs} DESTINATION "${PREFIX_TOL}/lib"
  COMPONENT core)

#=== [headers] ===============================================================

# headers [INCLUDE]
file(TO_CMAKE_PATH "${TOL_RUNTIME_DIR}/include" include_dir)
install( DIRECTORY ${include_dir} DESTINATION "${PREFIX_TOL}"
  COMPONENT headers)
  
#=== [gui] ===================================================================

# gui executables [BIN]
install(FILES 
  "${TOL_RUNTIME_DIR}/bin/tolbase.tcl"
  "${TOL_RUNTIME_DIR}/bin/tolbase"
  DESTINATION "${PREFIX_TOL}/bin"
  COMPONENT gui
  # 755, not 777: these land in /usr and a world-writable executable there
  # means any user can replace the interpreter for everybody else.
  PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ
  GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

# The link for tolbase belongs to the gui component, so it goes here and not
# with the two of core.
install(CODE "
  file(MAKE_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/bin\")
  file(CREATE_LINK \"../${PREFIX_TOL}/bin/tolbase\"
       \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/bin/tolbase\" SYMBOLIC)
" COMPONENT gui)

# gui tcltk libraries  
subdirlist(tclExtDirs "${TOL_RUNTIME_DIR}/lib")
# mimetex ships a 32-bit Linux binary and a Windows .exe, and no 64-bit build
# exists -- there is not even a source for it in the tree, only the blobs. If
# the i386 binary travels in the .deb, dpkg-shlibdeps derives libc6-i386 and
# EVERY installation then demands i386 multiarch for the sake of one optional
# LaTeX preview in the database manager. Both blobs stay out; see the pending
# item about building mimetex for 64 bits.
# Los *_noversion son las copias intactas de los iconos, antes de estamparles la
# marca de version. Estan en el arbol a proposito, para poder volver atras, pero
# NO tienen nada que hacer en una instalacion: son peso muerto y confunden a
# quien los encuentre. Se excluyen aqui y no se confia en que nadie los deje
# fuera del arbol de ejecucion a mano, porque ya viajaron dentro de los dos
# instaladores del 2026-08-20 justamente por eso.
install(DIRECTORY ${tclExtDirs} DESTINATION "${PREFIX_TOL}/lib"
  COMPONENT gui
  USE_SOURCE_PERMISSIONS
  REGEX "mimetex/(linux32|win32)" EXCLUDE
  PATTERN "*_noversion.*" EXCLUDE
  PATTERN toltcl* EXCLUDE
  PATTERN TclCurl* EXCLUDE)
##!!! cuidado que excluye bftoltcl.tcl quitar *

#=== [packages] ==============================================================

# The TOL packages travel as the zips built by TolPackage::Builder, not as an
# unpacked tree: InstallZip does more than unzip -- it registers the package
# and resolves the paths of its binary part -- so letting it do the work at
# install time is safer than shipping a tree built for somebody else's $HOME.
#
# They cannot be installed system-wide: the package area is per user by design.
# tol-install-packages materialises it for whoever runs it.
if(NOT TOL_PACKAGE_ZIPS_DIR)
  set(TOL_PACKAGE_ZIPS_DIR "$ENV{HOME}/.tol/TolPackage.5/Builder")
endif(NOT TOL_PACKAGE_ZIPS_DIR)

# Which zips travel is decided by the shared helper, because the Windows
# installer needs exactly the same rule and duplicating sixty lines of version
# comparison in two files is how the two drift apart.
include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/TolPackageZips.cmake")
# #Win... is refused here: a Windows binary variant in a .deb is dead weight at
# best and a confusing "package installed but does not load" at worst.
tol_select_package_zips(tol_package_zips "${TOL_PACKAGE_ZIPS_DIR}" "#Win")

# To bin/stdlib/DefaultPackages, not to a packages/ of its own, and that is the
# whole point: TOL already knows how to do this. Client::Initialize, in
# stdlib/TolPackage/client.tol, walks THAT directory at startup, takes every
# *.zip that is not in the catalogue yet, and installs it into the area of
# whoever is starting TOL:
#
#   Set localZips = Select(GetDir(_.defaultPackageDir)[1], ... "*.zip");
#   ... If(catalog::FindRecord(packageName), 0, { InstallZip(...) })
#
# DefaultPackages.tol only decides which ones get LOADED, not which get
# installed. So with the zips here every user has them the first time they open
# TOL, without running anything. Measured on Windows on 2026-08-19: deleting a
# package and starting tolsh prints "[TolPackage::Client] Installing default
# package RKFAS.0.6" and it is back.
#
# This does not contradict the area being per user: it still is, and it is still
# true that a root postinst cannot write into it. What changes is that the zips
# now sit where the engine looks during each user's own startup.
install(FILES ${tol_package_zips}
  DESTINATION "${PREFIX_TOL}/bin/stdlib/DefaultPackages"
  COMPONENT packages)

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/tol-install-packages"
  DESTINATION "${PREFIX_TOL}/bin"
  COMPONENT packages
  PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ
  GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

#=== [R packages] ============================================================

# TOL's own two R packages, tolBasis and tolRlink, travel as SOURCE TREES and
# the postinst installs them with R CMD INSTALL. Both are pure R -- neither
# declares NeedsCompilation -- so no compiler is needed on the target machine.
#
# They are not on CRAN and never will be, so no dependency can bring them in.
# That is why every fresh machine ended up with TolRInside warning that "es
# conveniente tener instalado en R el paquete tolRlink" and no way to get it:
# the only copy was a link on the project wiki, and it pointed at a version
# that is no longer on the server.
#
# The C++ half of the link is a different thing: tolRlink.so, built by the
# rtol project against libtol and RInside and installed into the runtime
# tree's lib/, from where the *.so glob above picks it up. The R package
# dyn.loads it by absolute path, <root>/lib/tolRlink.so, so without it the R
# side installs cleanly and then dies at the first tolInit(). Hence the check:
# a .deb built from a runtime tree that never built rtol is broken in a way
# nothing else would report.
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../rtol/Rpackages"
  r_packages_dir)
install(DIRECTORY "${r_packages_dir}/tolBasis" "${r_packages_dir}/tolRlink"
  DESTINATION "${PREFIX_TOL}/share/Rpackages"
  COMPONENT packages)

if(NOT EXISTS "${TOL_RUNTIME_DIR}/lib/tolRlink.so")
  message(WARNING
    "tolRlink.so is NOT in ${TOL_RUNTIME_DIR}/lib, so this .deb would install "
    "the R package tolRlink without the library it loads, and TolRInside "
    "would keep failing. Build and install the rtol project into the runtime "
    "tree first: see doc/rescue/02-compilar.md.")
endif()

#=== [maintainer scripts] ====================================================

# The .deb tells the user what is left to do. It cannot do it for them: the
# package area is per user and postinst runs as root, so anything it installed
# would land in root's home.
#
# What it CAN do, and now does, is install the two R packages: those go to R's
# own site library, which is system-wide, so root is the right one to write
# them. The postrm takes them out again on purge, and only the ones it wrote.
set(POSTINST_TOL_ROOT "${CMAKE_INSTALL_PREFIX}/${PREFIX_TOL}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/postinst.in"
  "${CMAKE_CURRENT_BINARY_DIR}/postinst" @ONLY)
execute_process(COMMAND chmod 755 "${CMAKE_CURRENT_BINARY_DIR}/postinst")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/postrm.in"
  "${CMAKE_CURRENT_BINARY_DIR}/postrm" @ONLY)
execute_process(COMMAND chmod 755 "${CMAKE_CURRENT_BINARY_DIR}/postrm")
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA
  "${CMAKE_CURRENT_BINARY_DIR}/postinst;${CMAKE_CURRENT_BINARY_DIR}/postrm"
  PARENT_SCOPE)

#=============================================================================

# SCRIPTS (/usr/bin)
## !!
## file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/building/Linux_32/scripts/tol*" sh_pattern)
## file(GLOB RT_SH  ${sh_pattern})
## install(FILES ${RT_SH} DESTINATION "bin" PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_WRITE GROUP_EXECUTE WORLD_READ WORLD_WRITE WORLD_EXECUTE)

# nombre del paquete instalador
#if( SPECIFIC_SYSTEM_PREFERED_CPACK_GENERATOR STREQUAL "RPM" )
#  configure_file(
#    ${CMAKE_CURRENT_SOURCE_DIR}/rpm_post_install.sh.in 
#    ${CMAKE_CURRENT_BINARY_DIR}/rpm_post_install.sh )
#  set( CPACK_RPM_core_POST_INSTALL_SCRIPT_FILE
#    "${CMAKE_CURRENT_BINARY_DIR}/rpm_post_install.sh" PARENT_SCOPE )
#endif( SPECIFIC_SYSTEM_PREFERED_CPACK_GENERATOR STREQUAL "RPM" )
# Estudiar como hacer lo mismo con CPACK_DEBIAN_core_PACKAGE_CONTROL_EXTRA
