if (NOT(MSVC))
  set(CPACK_SET_DESTDIR ON)
endif()

set(CPACK_PACKAGE_VENDOR  ${AVOCADODB_PACKAGE_VENDOR})
set(CPACK_PACKAGE_CONTACT ${AVOCADODB_PACKAGE_CONTACT})
set(CPACK_PACKAGE_VERSION "${AVOCADODB_VERSION}")
# TODO just for rpm?
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})

if (USE_ENTERPRISE)
  set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/enterprise/LICENSE")
else ()
  set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
endif ()

set(CPACK_STRIP_FILES "ON")

if (${USE_ENTERPRISE})
  set(CPACKG_PACKAGE_CONFLICTS "avocadodb3")
  set(CPACK_PACKAGE_NAME "avocadodb3e")
else ()
  set(CPACK_PACKAGE_NAME "avocadodb3")
  set(CPACKG_PACKAGE_CONFLICTS "avocadodb3e")
endif ()
set(AVOCADODB_PACKAGE_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR})
# eventually the package string will be modified later on:

if ("${PACKAGING}" STREQUAL "DEB")
  if(CMAKE_TARGET_ARCHITECTURES MATCHES ".*x86_64.*")
    set(AVOCADODB_PACKAGE_ARCHITECTURE "amd64")
  elseif(CMAKE_TARGET_ARCHITECTURES MATCHES "aarch64")
    set(AVOCADODB_PACKAGE_ARCHITECTURE "arm64")
  elseif(CMAKE_TARGET_ARCHITECTURES MATCHES "armv7")
    set(AVOCADODB_PACKAGE_ARCHITECTURE "armhf")
  else()
    set(AVOCADODB_PACKAGE_ARCHITECTURE "i386")
  endif()
  
  set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${AVOCADODB_PACKAGE_REVISION}_${AVOCADODB_PACKAGE_ARCHITECTURE}")

  include(packages/deb)
  include(packages/tar)
  if (USE_SNAPCRAFT)
    if(NOT DEFINED SNAP_PORT)
      set(SNAP_PORT 8529)
    endif()
    include(packages/snap)
  endif ()
elseif ("${PACKAGING}" STREQUAL "RPM")
  set(PACKAGE_VERSION "-${CPACK_PACKAGE_VERSION}-${AVOCADODB_PACKAGE_REVISION}.${AVOCADODB_PACKAGE_ARCHITECTURE}")
  set(CPACK_PACKAGE_FILE_NAME  "${CPACK_PACKAGE_NAME}${PACKAGE_VERSION}")
  include(packages/rpm)
  include(packages/tar)
elseif ("${PACKAGING}" STREQUAL "Bundle")
  set(CPACK_PACKAGE_FILE_NAME      "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${AVOCADODB_PACKAGE_REVISION}.x86_64")
  include(packages/bundle)
  include(packages/tar)
elseif (MSVC)
  if (${USE_ENTERPRISE})
    set(CPACK_PACKAGE_NAME             "AvocadoDB3e")
  else()
    set(CPACK_PACKAGE_NAME             "AvocadoDB3")
  endif()
  if (CMAKE_CL_64)
    SET(AVOCADODB_PACKAGE_ARCHITECTURE "win64")
  else ()
    SET(AVOCADODB_PACKAGE_ARCHITECTURE "win32")
  endif ()
  set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${AVOCADODB_PACKAGE_REVISION}_${AVOCADODB_PACKAGE_ARCHITECTURE}")
  include(packages/nsis)
  include(packages/tar)
else ()
  set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${AVOCADODB_PACKAGE_REVISION}_${AVOCADODB_PACKAGE_ARCHITECTURE}")
  include(packages/tar)
endif ()


configure_file(
  "${CMAKE_SOURCE_DIR}/Installation/cmake/CMakeCPackOptions.cmake.in"
  "${CMAKE_BINARY_DIR}/CMakeCPackOptions.cmake" @ONLY)
set(CPACK_PROJECT_CONFIG_FILE "${CMAKE_BINARY_DIR}/CMakeCPackOptions.cmake")


# Finally: user cpack
include(CPack)
