SET(CPACK_STRIP_FILES TRUE)

SET(CPACK_SET_DESTDIR "ON")

SET(CPACK_INSTALL_PREFIX "/usr") 
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "/usr")

INCLUDE(GNUInstallDirs)

MESSAGE("UDEV_INSTALL_DIR ${UDEV_INSTALL_DIR}")

#SET(UDEV_INSTALL_DIR "/usr/lib/udev/rules.d")
IF(NOT EXISTS "${UDEV_INSTALL_DIR}" OR NOT IS_DIRECTORY "${UDEV_INSTALL_DIR}")
    SET(UDEV_INSTALL_DIR "/lib/udev/rules.d")
ENDIF()

MESSAGE("CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}")

# SET( CPACK_PACKAGE_GROUP "${PONYPROG_CPACK_PACKAGE_GROUP}" )
SET( CPACK_PACKAGE_LICENSE "${PONYPROG_CPACK_PACKAGE_LICENSE}" )
SET( CPACK_PACKAGE_SHLIBDEPS "${PONYPROG_CPACK_PACKAGE_SHLIBDEPS}" )
# SET( CPACK_PACKAGE_DEPENDS "${PONYPROG_CPACK_PACKAGE_DEPENDS}" )

INSTALL(PROGRAMS "${CURRENT_BUILD_DIR}/ponyprog"
        DESTINATION "${CMAKE_INSTALL_BINDIR}"
        COMPONENT "application"
        PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
        )

INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/icons/"
        DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons"
        COMPONENT "application"
        DIRECTORY_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
        FILES_MATCHING PATTERN "*.png"
        )

INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/lang/"
        DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/ponyprog/lang/"
        COMPONENT "lang files"
        DIRECTORY_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
        FILES_MATCHING PATTERN "*.utf"
        )

INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/distribution/innosetup/"
        DESTINATION "${CMAKE_INSTALL_DOCDIR}"
        COMPONENT "help files" 
        DIRECTORY_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
        FILES_MATCHING PATTERN "*.jpg" PATTERN "*.html"
        )

INSTALL(FILES "${PROJECT_SOURCE_DIR}/copyright"
        DESTINATION "${CMAKE_INSTALL_DOCDIR}"
        COMPONENT "application"
        PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
        ) 
# MESSAGE("PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}")

INSTALL(FILES "${PROJECT_SOURCE_DIR}/udev/rules.d/90-ponyprog.rules"
        DESTINATION "${UDEV_INSTALL_DIR}"
        COMPONENT "application"
        PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
        )

INSTALL(FILES "${PROJECT_SOURCE_DIR}/desktop/ponyprog.desktop"
        DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications/"
        COMPONENT "application"
        PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
        )

SET(CPACK_INSTALL_CMAKE_PROJECTS "${PONYPROG_CPACK_INSTALL_CMAKE_PROJECTS}")

SET(PONYPROG_PACKAGE_DIRECTORY "${CURRENT_BUILD_DIR}")

SET( CPACK_PACKAGE_VENDOR "Lancos" )
# FILE(MAKE_DIRECTORY "${PONYPROG_PACKAGE_DIRECTORY}")
# MESSAGE("CMAKE_CURRENT_SOURCE_DIR posix ${CMAKE_CURRENT_SOURCE_DIR}")
# MESSAGE("DIR: ${CMAKE_CURRENT_SOURCE_DIR}")

SET(CPACK_COMPONENTS_ALL "application" "lang files" "help files")
SET(CPACK_COMPONENTS_INSTALL "ON")

# 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( PONYPROG_CPACK_PACKAGE_FILE_NAME "ponyprog-${APP_VERSION}-${DEBIAN_ARCHITECTURE}" )
    SET(DEBIAN_POSTINST postinst.in)

    CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/package-binary-deb.cpack.in ${CMAKE_CURRENT_BINARY_DIR}/package-binary-deb.cpack)
    CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${DEBIAN_POSTINST} ${CMAKE_CURRENT_BINARY_DIR}/packaging/debian-control/postinst)
    ADD_CUSTOM_TARGET(package-binary-deb
        WORKING_DIRECTORY "${PONYPROG_PACKAGE_DIRECTORY}"
        COMMAND ${CMAKE_CPACK_COMMAND} --config ${CMAKE_CURRENT_BINARY_DIR}/package-binary-deb.cpack
        )
ENDIF()

IF(NOT DEBIAN_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( PONYPROG_CPACK_PACKAGE_FILE_NAME "ponyprog-${APP_VERSION}-${RPM_ARCHITECTURE}" )
    SET( RPM_POSTINST postinst.in)

    CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/package-binary-rpm.cpack.in ${CMAKE_CURRENT_BINARY_DIR}/package-binary-rpm.cpack)
    CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${RPM_POSTINST} ${CMAKE_CURRENT_BINARY_DIR}/packaging/rpm-script/postinst.sh)

    ADD_CUSTOM_TARGET(package-binary-rpm
        WORKING_DIRECTORY "${PONYPROG_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 "${PONYPROG_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 "${PONYPROG_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 "${PONYPROG_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

# INCLUDE(CPack)

