SET(CPACK_STRIP_FILES TRUE)
SET(CMAKE_INSTALL_PREFIX "/usr")

#MESSAGE("CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}")

SET( CPACK_PACKAGE_GROUP "${HTTRAQT_CPACK_PACKAGE_GROUP}" )
SET( CPACK_PACKAGE_LICENSE "${HTTRAQT_CPACK_PACKAGE_LICENSE}" )
SET( CPACK_PACKAGE_SHLIBDEPS "${HTTRAQT_CPACK_PACKAGE_SHLIBDEPS}" )
# SET( CPACK_PACKAGE_DEPENDS "${HTTRAQT_CPACK_PACKAGE_DEPENDS}" )

INSTALL(PROGRAMS "${CURRENT_BUILD_DIR}/httraqt" DESTINATION bin COMPONENT applications)
INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/icons" DESTINATION "/usr/share/" FILES_MATCHING PATTERN "*.*") 
INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/lang" DESTINATION "/usr/share/httraqt/" FILES_MATCHING PATTERN "*.*") 
INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/help" DESTINATION "/usr/share/httraqt/" FILES_MATCHING PATTERN "*.*") 
# MESSAGE("PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}")

SET( desktop "${PROJECT_SOURCE_DIR}/desktop/httraqt.desktop")

INSTALL(FILES ${desktop}
        DESTINATION "/usr/share/applications/"
        PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)
        
SET(CPACK_INSTALL_CMAKE_PROJECTS "${HTTRAQT_CPACK_INSTALL_CMAKE_PROJECTS}")

SET(HTTRAQT_PACKAGE_DIRECTORY "${CURRENT_BUILD_DIR}")
# FILE(MAKE_DIRECTORY "${HTTRAQT_PACKAGE_DIRECTORY}")
# MESSAGE("CMAKE_CURRENT_SOURCE_DIR posix ${CMAKE_CURRENT_SOURCE_DIR}")
# MESSAGE("DIR: ${CMAKE_CURRENT_SOURCE_DIR}")

# if not defined
IF(NOT DEBIAN_ARCHITECTURE)
    execute_process(
      COMMAND dpkg --print-architecture
      OUTPUT_VARIABLE DEBIAN_ARCHITECTURE
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )
  #SET(DEBIAN_ARCHITECTURE i386) #dpkg --print-architecture is always i386 on intel
ENDIF(NOT DEBIAN_ARCHITECTURE)

IF(DEBIAN_ARCHITECTURE)
    MESSAGE("DEBIAN_ARCHITECTURE ${DEBIAN_ARCHITECTURE}")
    SET( HTTRAQT_CPACK_PACKAGE_FILE_NAME "httraqt-${HTTRAQT_VERSION}-${DEBIAN_ARCHITECTURE}" )
    
    CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/package-binary-deb.cpack.in ${CMAKE_CURRENT_BINARY_DIR}/package-binary-deb.cpack)
    ADD_CUSTOM_TARGET(package-binary-deb
        WORKING_DIRECTORY "${HTTRAQT_PACKAGE_DIRECTORY}"
        COMMAND ${CMAKE_CPACK_COMMAND} --config ${CMAKE_CURRENT_BINARY_DIR}/package-binary-deb.cpack
        )
ENDIF()

IF(NOT RPM_ARCHITECTURE)
    FIND_PROGRAM(RPMBUILD
        NAMES rpmbuild
        PATHS "/usr/bin")

    IF ( RPMBUILD )
        GET_FILENAME_COMPONENT(RPMBUILD_PATH ${RPMBUILD} ABSOLUTE)
        MESSAGE(STATUS "Found rpmbuild : ${RPMBUILD_PATH}")
        SET(RPMBUILD_FOUND "YES")
        execute_process(
           COMMAND arch
           OUTPUT_VARIABLE RPM_ARCHITECTURE
           OUTPUT_STRIP_TRAILING_WHITESPACE
        )
    ENDIF ( RPMBUILD )
ENDIF()

IF(RPM_ARCHITECTURE)
    MESSAGE("RPM_ARCHITECTURE ${RPM_ARCHITECTURE}")
    SET( HTTRAQT_CPACK_PACKAGE_FILE_NAME "httraqt-${HTTRAQT_VERSION}-${RPM_ARCHITECTURE}" )
    
    CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/package-binary-rpm.cpack.in ${CMAKE_CURRENT_BINARY_DIR}/package-binary-rpm.cpack)
    ADD_CUSTOM_TARGET(package-binary-rpm
        WORKING_DIRECTORY "${HTTRAQT_PACKAGE_DIRECTORY}"
        COMMAND ${CMAKE_CPACK_COMMAND} --config ${CMAKE_CURRENT_BINARY_DIR}/package-binary-rpm.cpack
        )
ENDIF()

# Generate self-extracting tarballs
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/package-binary-stgz.cpack.in ${CMAKE_CURRENT_BINARY_DIR}/package-binary-stgz.cpack)
ADD_CUSTOM_TARGET(package-binary-stgz
        WORKING_DIRECTORY "${HTTRAQT_PACKAGE_DIRECTORY}"
        COMMAND ${CMAKE_CPACK_COMMAND} --config ${CMAKE_CURRENT_BINARY_DIR}/package-binary-stgz.cpack
        )

# Generate tarballs
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/package-binary-tbz2.cpack.in ${CMAKE_CURRENT_BINARY_DIR}/package-binary-tbz2.cpack)
ADD_CUSTOM_TARGET(package-binary-tbz2
        WORKING_DIRECTORY "${HTTRAQT_PACKAGE_DIRECTORY}"
        COMMAND ${CMAKE_CPACK_COMMAND} --config ${CMAKE_CURRENT_BINARY_DIR}/package-binary-tbz2.cpack
        )

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/package-binary-tgz.cpack.in ${CMAKE_CURRENT_BINARY_DIR}/package-binary-tgz.cpack)
ADD_CUSTOM_TARGET(package-binary-tgz
        WORKING_DIRECTORY "${HTTRAQT_PACKAGE_DIRECTORY}"
        COMMAND ${CMAKE_CPACK_COMMAND} --config ${CMAKE_CURRENT_BINARY_DIR}/package-binary-tgz.cpack
        )

# Note ... we intentionally don't provide upload capability for Posix packages, and leave distribution to the experts

