set(MACDEPLOYQT_EXECUTABLE "@MACDEPLOYQT_EXECUTABLE@")
set(CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@")
set(CMAKE_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@")
set(DLT_APP_DIR_NAME "@DLT_APP_DIR_NAME@")
set(DLT_PLUGIN_INSTALLATION_PATH "@DLT_PLUGIN_INSTALLATION_PATH@")

# See CMAKE_INSTALL_PREFIX
execute_process(COMMAND ${CMAKE_COMMAND} "--install" "." "--prefix" "${CMAKE_INSTALL_PREFIX}"
    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
    RESULT_VARIABLE STATUS)

if(STATUS AND NOT STATUS EQUAL 0)
    message(SEND_ERROR "Failure: ${STATUS}")
else()
    message(STATUS "Success.")
endif()

# execute_process(COMMAND ls -l
#     WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
#     RESULT_VARIABLE STATUS)

# execute_process(COMMAND tree
#     WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
#     RESULT_VARIABLE STATUS)

file(GLOB DLT_PLUGINS_SO "${CMAKE_INSTALL_PREFIX}/${DLT_PLUGIN_INSTALLATION_PATH}/*.so")
list(TRANSFORM DLT_PLUGINS_SO PREPEND "-executable=")

message(STATUS "Call ${MACDEPLOYQT_EXECUTABLE} ${DLT_APP_DIR_NAME} ${DLT_PLUGINS_SO}")
execute_process(COMMAND
    "${MACDEPLOYQT_EXECUTABLE}"
    "${DLT_APP_DIR_NAME}"
    -verbose=1
    -always-overwrite
    -dmg
    -libpath=${CMAKE_INSTALL_PREFIX}/${DLT_APP_DIR_NAME}/Contents/Frameworks
    ${DLT_PLUGINS_SO}
    WORKING_DIRECTORY "${CMAKE_INSTALL_PREFIX}"
    RESULT_VARIABLE STATUS)

if(STATUS AND NOT STATUS EQUAL 0)
    message(SEND_ERROR "Failure: ${STATUS}")
else()
    message(STATUS "Success.")
endif()
