
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  set(CENTER_ARCH "amd64")
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
  set(CENTER_ARCH "i686")
else()
  set(CENTER_ARCH ${CMAKE_SYSTEM_PROCESSOR})
endif()

if (WIN32)
  set(CPACK_GENERATOR "NSIS")
  set(CPACK_SOURCE_GENERATOR "ZIP")
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  set(CPACK_GENERATOR "PackageMake")
  set(CPACK_SOURCE_GENERATOR "TGZ")
else ()
  set(CPACK_GENERATOR "DEB")
  set(CPACK_SOURCE_GENERATOR "TGZ")
endif ()


set(CPACK_CREATE_DESKTOP_LINKS ${CENTER_OUTPUT_NAME})
set(CPACK_STRIP_FILES TRUE)
set(CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION TRUE)

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROGRAM_DESCRIPTION})
set(CPACK_PACKAGE_EXECUTABLES ${CENTER_OUTPUT_NAME} ${CENTER_OUTPUT_NAME})
set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CENTER_OUTPUT_NAME})
set(CPACK_PACKAGE_VERSION_MAJOR 0)
set(CPACK_PACKAGE_VERSION_MINOR 0)
set(CPACK_PACKAGE_VERSION_PATCH 0)
set(CPACK_PACKAGE_CHECKSUM "SHA256")
set(CPACK_PACKAGE_CONTACT "Andy Rapatas <https://github.com/rapatas>")
set(CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/rapatas/center")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE.txt")
set(CPACK_RESOURCE_FILE_README  "${CMAKE_SOURCE_DIR}/README.md")

if(APPLE)
  set(CPACK_PACKAGE_VENDOR "Organisation")
else()
  set(CPACK_PACKAGE_VENDOR ${CPACK_PACKAGE_HOMEPAGE_URL})
endif()

string(CONCAT CPACK_PACKAGE_FILE_NAME
  ${CENTER_OUTPUT_NAME}
  "_"
  ${CENTER_VERSION}
  "_"
  ${CENTER_ARCH}
)

# Source
set(CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_FILE_NAME})
set(
  CPACK_SOURCE_IGNORE_FILES
  ".drone.yml"
  ".cache"
  ".gitignore"
  "compile_commands.json"
  ".*.swp"
  "*.md"
  "build-*"
  "/[.]git/"
)

# NSIS (Windows .exe installer)
set(CPACK_NSIS_MUI_ICON "${CMAKE_SOURCE_DIR}/resources/images/center.ico")
set(CPACK_NSIS_INSTALLED_ICON_NAME "bin/center.exe")
set(CPACK_NSIS_COMPRESSOR "/SOLID lzma")
set(CPACK_NSIS_ENABLE_UNINSTALL_BEFORE_INSTALL "ON")
set(CPACK_NSIS_MODIFY_PATH "ON")
set(CPACK_NSIS_HELP_LINK ${CPACK_PACKAGE_HOMEPAGE_URL})
set(CPACK_NSIS_URL_INFO_ABOUT ${CPACK_PACKAGE_HOMEPAGE_URL})
set(CPACK_NSIS_MENU_LINKS "${CPACK_PACKAGE_HOMEPAGE_URL}" "center Homepage")
set(CPACK_NSIS_PACKAGE_NAME ${CENTER_NAME})
set(CPACK_NSIS_DISPLAY_NAME ${CENTER_NAME})
set(CPACK_NSIS_CONTACT ${CPACK_PACKAGE_CONTACT})
set(CPACK_NSIS_COMPRESSOR "${CPACK_NSIS_COMPRESSOR}\n  SetCompressorDictSize 64")
set(CPACK_NSIS_COMPRESSOR "${CPACK_NSIS_COMPRESSOR}\n  BrandingText '${CPACK_PACKAGE_DESCRIPTION_SUMMARY}'")
set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "\
  WriteRegStr SHCTX 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\App Paths\\\\center.exe' '' '$INSTDIR\\\\bin\\\\center.exe'\n\
  WriteRegStr SHCTX 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\App Paths\\\\center.exe' 'Path' '$INSTDIR\\\\bin'")
set(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "\
  DeleteRegKey SHCTX 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\App Paths\\\\center.exe'")

# DEB (Linux .deb bundle)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "")
set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${CPACK_PACKAGE_CONTACT}")
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "${PROGRAM_DESCRIPTION}")

include(CPack)
