# Variables to manipulate packages:
# PKG_VERSION - version provided in form 1.2.34
# PKG_NAME - package prefix name
# PKG_REQUIRES - package dependecies in format "intel-igc-core >= 1.0.2490"
# TAG_PREFIX - git tag prefix to detect revision
# PKG_DESCRIPTION - package description
# PKG_SUMMARY - short package description
set(TAG_PREFIX "cmemu-" CACHE STRING "")
set(PKG_NAME "intel-cmemu" CACHE STRING "")
set(PKG_DESCRIPTION "CM Emulation library is a open source project that implements C for Metal language CPU emulation" CACHE STRING "")
set(PKG_SUMMARY "CM Emulation library" CACHE STRING "")

if(NOT DEFINED PKG_VERSION )

  find_package(Git)
  execute_process(
    COMMAND ${GIT_EXECUTABLE} describe --long --tags
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_TAG
    RESULT_VARIABLE EXIT_STATUS
    ERROR_VARIABLE GET_TAG_ERROR
  )
  if(NOT EXIT_STATUS EQUAL 0)
    message(WARNING "[PKG] Could not get package revision: ${GET_TAG_ERROR}")
    set(GIT_TAG "${TAG_PREFIX}1.0.1-0-00000000")
  endif()

  string(REGEX MATCH "^${TAG_PREFIX}([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)-" PVERSION "${GIT_TAG}")
  if(CMAKE_MATCH_COUNT EQUAL 4)
    set(PKG_MAJOR ${CMAKE_MATCH_1})
    set(PKG_MINOR ${CMAKE_MATCH_2})
    set(BASE_PATCH ${CMAKE_MATCH_3})
    set(ADD_PATCH ${CMAKE_MATCH_4})
    math(EXPR PKG_PATCH "${BASE_PATCH} + ${ADD_PATCH}")
  else()
    message(WARNING "[PKG] Could not understand package version: ${GIT_TAG}")
    set(PKG_MAJOR 1)
    set(PKG_MINOR 0)
    set(PKG_PATCH 1)
  endif()
  set(PKG_VERSION "${PKG_MAJOR}.${PKG_MINOR}.${PKG_PATCH}")
endif()
message("[PKG] Will use version ${PKG_VERSION}")

set(CPACK_PACKAGE_ARCHITECTURE "x86_64")
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Intel")
set(CPACK_DEBIAN_COMPRESSION_TYPE "xz")
set(CPACK_RPM_PACKAGE_ARCHITECTURE "x86_64")
set(CPACK_RPM_PACKAGE_RELEASE 1)
set(CPACK_RPM_COMPRESSION_TYPE "xz")

#set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE.TXT)

if(NOT DEFINED DISTRO_NAME)
  find_program(LSB_RELEASE_EXEC lsb_release)
  execute_process(COMMAND ${LSB_RELEASE_EXEC} -is
      OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT
      OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  string(TOLOWER "${LSB_RELEASE_ID_SHORT}" DISTRO_NAME)
    execute_process(COMMAND ${LSB_RELEASE_EXEC} -rs
    OUTPUT_VARIABLE DISTRO_VERSION
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
endif()

if("${DISTRO_NAME}" STREQUAL "ubuntu")
set(DISTRO_INITIAL "u")
elseif("${DISTRO_NAME}" STREQUAL "fedora")
set(DISTRO_INITIAL "f")
elseif("${DISTRO_NAME}" STREQUAL "clear-linux-os")
set(DISTRO_INITIAL "cl")
elseif("${DISTRO_NAME}" STREQUAL "centos")
set(DISTRO_INITIAL "ce")
else()
set(DISTRO_INITIAL "generic")
endif()

string(TOLOWER "${CMAKE_BUILD_TYPE}" PACKAGE_BUILD_TYPE)

set(UMD_PACKAGE_TYPE "${DISTRO_INITIAL}${DISTRO_VERSION}-${PACKAGE_BUILD_TYPE}.${CPACK_RPM_PACKAGE_ARCHITECTURE}")

set(CPACK_DEBIAN_PACKAGE_NAME "${PKG_NAME}")
set(CPACK_RPM_PACKAGE_NAME "${PKG_NAME}")
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "${PKG_DESCRIPTION}")
set(CPACK_RPM_PACKAGE_DESCRIPTION "${PKG_DESCRIPTION}")
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION_SUMMARY "${PKG_SUMMARY}")
set(CPACK_RPM_PACKAGE_DESCRIPTION_SUMMARY "${PKG_SUMMARY}")
set(CPACK_DEBIAN_FILE_NAME "${PKG_NAME}-${PKG_VERSION}.${UMD_PACKAGE_TYPE}.deb")
set(CPACK_RPM_FILE_NAME "${PKG_NAME}-${PKG_VERSION}.${UMD_PACKAGE_TYPE}.rpm")
set(CPACK_PACKAGE_FILE_NAME "${PKG_NAME}-${PKG_VERSION}.${UMD_PACKAGE_TYPE}")
set(CPACK_PACKAGE_VERSION "${PKG_VERSION}")

if(DEFINED PKG_REQUIRES)
  set(CPACK_DEBIAN_PACKAGE_DEPENDS "${PKG_REQUIRES}")
  set(CPACK_RPM_PACKAGE_REQUIRES "${PKG_REQUIRES}")
endif()

set(CPACK_COMPONENT_INSTALL ON)
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)

#set(CPACK_COMPONENTS_ALL )
set(CPACK_GENERATOR "RPM" "DEB" "TXZ")
include(CPack)
