cmake_minimum_required(VERSION 3.16)
project(icons VERSION 1.0 LANGUAGES C CXX)

#set(CMAKE_INCLUDE_CURRENT_DIR ON)
include(../../variables.cmake)

# install macOS files for a correct bundle
install(FILES qt.conf DESTINATION ${INSTALLROOT}/${DATADIR})
install(FILES ../../resources/icons/qlcplus.icns DESTINATION ${INSTALLROOT}/${DATADIR})
install(FILES qlcplus.entitlements DESTINATION ${INSTALLROOT}/${DATADIR} RENAME .entitlements)

if(qmlui)
    install(FILES Info.plist.qmlui DESTINATION ${INSTALLROOT} RENAME Info.plist)
else()
    install(FILES Info.plist DESTINATION ${INSTALLROOT})
endif()
install(CODE "execute_process(COMMAND sed -i -e \"s/__QLC_VERSION__/${APPVERSION}/g\" ${INSTALLROOT}/Info.plist)")

set(APPLE_CODESIGN_ENTITLEMENTS "qlcplus.entitlements")

# Starting with arm64 macOS Apple will require ad-hoc code signatures,
# which can be generated by setting the identity to a single dash (-).
# These only include a checksum for verifying integrity, not an actual
# signature.
#if(NOT APPLE_CODESIGN_IDENTITY)
#    set(APPLE_CODESIGN_IDENTITY -)
#endif()

# install Qt library frameworks
#set(QT_FRAMEWORKS_DIR ${_qt5_root_dir}/../../Frameworks) # homebrew
set(QT_FRAMEWORKS_DIR $ENV{QTDIR}/lib)
set(QT_FRAMEWORK_NAMES
    "QtCore"
    "QtDBus"
    "QtGui"
    "QtMultimedia"
    "QtMultimediaWidgets"
    "QtNetwork"
    "QtOpenGL"
    "QtPrintSupport"
    "QtSerialPort"
    "QtSvg"
    "QtWidgets"
    "QtWebSockets"
)
if(QT_MAJOR_VERSION GREATER 5)
    list(APPEND QT_FRAMEWORK_NAMES "QtQml")
else()
    list(APPEND QT_FRAMEWORK_NAMES "QtScript")
endif()

if(qmlui)
    list(APPEND QT_FRAMEWORK_NAMES
    "QtLabsFolderListModel"
    "QtMultimediaQuick"
    "QtQmlModels"
    "QtQmlMeta"
    "QtQmlWorkerScript"
    "QtQuick"
    "QtQuickLayouts"
    "QtQuickControls2"
    "QtQuickControls2Impl"
    "QtQuickControls2Basic"
    "QtQuickControls2BasicStyleImpl"
    "QtQuickControls2Fusion"
    "QtQuickControls2FusionStyleImpl"
    "QtQuickTemplates2"
    "QtQuickDialogs2"
    "QtQuickDialogs2QuickImpl"
    "QtQuickDialogs2Utils"
    "Qt3DCore"
    "Qt3DExtras"
    "Qt3DInput"
    "Qt3DLogic"
    "Qt3DAnimation"
    "Qt3DQuick"
    "Qt3DQuickExtras"
    "Qt3DQuickInput"
    "Qt3DQuickRender"
    "Qt3DQuickScene3D"
    "Qt3DRender"
    "QtConcurrent"
    )
endif()

# Loop through each framework and copy it to installation
foreach(FW_NAME IN LISTS QT_FRAMEWORK_NAMES)
    # install each framework Version file
    install(DIRECTORY ${QT_FRAMEWORKS_DIR}/${FW_NAME}.framework
            DESTINATION ${INSTALLROOT}/${LIBSDIR}
            PATTERN "Headers" EXCLUDE)
endforeach()

# install Qt plugins
set(QT_PLUGINS_DIR $ENV{QTDIR}/plugins)
set(QT_QML_DIR $ENV{QTDIR}/qml)

# imageformats
install(FILES ${QT_PLUGINS_DIR}/imageformats/libqgif.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/imageformats)
install(FILES ${QT_PLUGINS_DIR}/imageformats/libqjpeg.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/imageformats)
install(FILES ${QT_PLUGINS_DIR}/imageformats/libqsvg.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/imageformats)
# multimedia
if(QT_MAJOR_VERSION GREATER 5)
    install(FILES ${QT_PLUGINS_DIR}/multimedia/libdarwinmediaplugin.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/multimedia)
    install(FILES ${QT_PLUGINS_DIR}/multimedia/libffmpegmediaplugin.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/multimedia)
else()
    install(FILES ${QT_PLUGINS_DIR}/audio/libqtaudio_coreaudio.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/audio)
    install(FILES ${QT_PLUGINS_DIR}/mediaservice/libqavfmediaplayer.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/mediaservice)
    install(FILES ${QT_PLUGINS_DIR}/mediaservice/libqtmedia_audioengine.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/mediaservice)
endif()
# platforms
install(FILES ${QT_PLUGINS_DIR}/platforms/libqcocoa.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/platforms)
# styles
install(FILES ${QT_PLUGINS_DIR}/styles/libqmacstyle.dylib DESTINATION ${INSTALLROOT}/${PLUGINDIR}/styles)

if(qmlui)
    install(FILES ${QT_PLUGINS_DIR}/geometryloaders/libdefaultgeometryloader.dylib  DESTINATION ${INSTALLROOT}/${PLUGINDIR}/geometryloaders)
    install(FILES ${QT_PLUGINS_DIR}/renderers/libopenglrenderer.dylib  DESTINATION ${INSTALLROOT}/${PLUGINDIR}/renderers)
    install(FILES ${QT_PLUGINS_DIR}/sceneparsers/libassimpsceneimport.dylib  DESTINATION ${INSTALLROOT}/${PLUGINDIR}/sceneparsers)
endif()

# install support libraries
set(FFTW_LIBNAME "libfftw3.3.dylib")
set(OGG_LIBNAME "libogg.0.dylib")
set(FLAC_LIBNAME "libFLAC.12.dylib")
set(OPUS_LIBNAME "libopus.0.dylib")
set(MPG123_LIBNAME "libmpg123.0.dylib")
set(LAME_LIBNAME "libmp3lame.0.dylib")
set(VORBIS_LIBNAME "libvorbis.0.dylib")
set(VORBISENC_LIBNAME "libvorbisenc.2.dylib")

install(FILES ${FFTW3_LIBDIR}/${FFTW_LIBNAME} DESTINATION ${INSTALLROOT}/${LIBSDIR})
install(FILES ${LIBFTDI1_libftdi1_LIBDIR}/libftdi1.2.dylib DESTINATION ${INSTALLROOT}/${LIBSDIR})
install(FILES ${LIBFTDI1_libftdi1_LIBDIR}/libftdi1.2.5.0.dylib DESTINATION ${INSTALLROOT}/${LIBSDIR})
install(FILES ${LIBUSB1_LIBDIR}/libusb-1.0.0.dylib DESTINATION ${INSTALLROOT}/${LIBSDIR})
#install(FILES ${MAD_LIBDIR}/libmad.0.dylib DESTINATION ${INSTALLROOT}/${LIBSDIR})
install(FILES ${SNDFILE_LIBDIR}/libsndfile.1.dylib DESTINATION ${INSTALLROOT}/${LIBSDIR})
install(FILES ${SNDFILE_LIBDIR}/libsndfile.1.0.37.dylib DESTINATION ${INSTALLROOT}/${LIBSDIR})

#if(qmlui)
#    set(qmldeps_files
#        ${QT_QML_DIR}/Qt
#        ${QT_QML_DIR}/QtQml
#        ${QT_QML_DIR}/QtQuick
#        ${QT_QML_DIR}/Qt3D
#        ${QT_QML_DIR}/QtMultimedia
#    )
#    install(DIRECTORY ${qmldeps_files} DESTINATION ${INSTALLROOT}/${DATADIR}/qml)
#endif()