# 2lib - bundle shared libraries (but not standard frameworks)
# ! cmakecache vars not available to external scripts
#   so we configure it first to do substitutions
#   make sure to use @varname@

# for now, just libs not available as frameworks
#   libpq
#   libfcgi (non-system)

# kill boolean warnings
CMAKE_POLICY (SET CMP0012 NEW)

INCLUDE ("@CMAKE_BINARY_DIR@/mac/0vars.cmake")
INCLUDE ("@CMAKE_SOURCE_DIR@/cmake/MacBundleMacros.cmake")

# Postgres

IF ("@POSTGRES_LIBRARY@" MATCHES ".*libpq.dylib")
    MESSAGE (STATUS "Copying libpq and updating library paths...")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@POSTGRES_LIBRARY@" "${QLIBDIR}/libpq.dylib")
    GET_INSTALL_NAME ("@POSTGRES_LIBRARY@" "libpq" PQLIB)
    UPDATEQGISPATHS (${PQLIB} libpq.dylib)
    # may have been built with libintl
    GET_INSTALL_NAME ("@POSTGRES_LIBRARY@" "libintl" INTLLIB)
    IF (INTLLIB)
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${INTLLIB}" "${QLIBDIR}/libintl.dylib")
        SET (LIB_CHG_TO "${ATEXECUTABLE}/@QGIS_LIB_SUBDIR@/libintl.dylib")
        IF (@OSX_HAVE_LOADERPATH@)
            SET (LIB_CHG_TO "${ATLOADER}/libintl.dylib")
        ENDIF (@OSX_HAVE_LOADERPATH@)
        INSTALLNAMETOOL_CHANGE ("${INTLLIB}" "${LIB_CHG_TO}" "${QLIBDIR}/libpq.dylib")
    ENDIF (INTLLIB)
    IF (EXISTS "${QPLUGDIR}/../sqldrivers/libqsqlpsql.dylib")
        FILE (RELATIVE_PATH _relpath "${QPLUGDIR}/../sqldrivers" "${QLIBDIR}/libpq.dylib")
        INSTALLNAMETOOL_CHANGE ("${PQLIB}" "${ATLOADER}/${_relpath}" "${QPLUGDIR}/../sqldrivers/libqsqlpsql.dylib")
    ENDIF ()
ENDIF ()

# libspatialindex

IF ("@SPATIALINDEX_LIBRARY@" MATCHES ".*libspatialindex.dylib")
    MESSAGE (STATUS "Copying libspatialindex and updating library paths...")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@SPATIALINDEX_LIBRARY@" "${QLIBDIR}/libspatialindex.dylib")
    GET_INSTALL_NAME ("@SPATIALINDEX_LIBRARY@" "libspatialindex" SPILIB)
    UPDATEQGISPATHS (${SPILIB} libspatialindex.dylib)
    IF (@WITH_QSPATIALITE@ AND EXISTS "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
        FILE (RELATIVE_PATH _relpath "${QPLUGDIR}/../sqldrivers" "${QLIBDIR}/libspatialindex.dylib")
        INSTALLNAMETOOL_CHANGE ("${SPILIB}" "${ATLOADER}/${_relpath}" "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
    ENDIF ()
ENDIF ()

# libfcgi (non-system)

IF ("@FCGI_LIBRARY@" MATCHES ".*libfcgi.dylib" AND NOT "@FCGI_LIBRARY@" MATCHES "/usr/lib/.*")
    MESSAGE (STATUS "Copying libfcgi and updating library paths...")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@FCGI_LIBRARY@" "${QLIBDIR}/libfcgi.dylib")
    GET_INSTALL_NAME ("@FCGI_LIBRARY@" "libfcgi" FCGILIB)
    UPDATEQGISPATHS (${FCGILIB} libfcgi.dylib)
ENDIF ()