# 1osg - bundle OSG & osgEarth frameworks
# ! cmakecache vars not available to external scripts
#   so we configure it first to do substitutions
#   make sure to use @varname@

# kill boolean warnings
CMAKE_POLICY (SET CMP0012 NEW)

IF (@OSGEARTH_FOUND@)

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

SET (OSG_PLUGINS_PATH "@OSG_PLUGINS_PATH@")

# list of osg frameworks to bundle

SET (OSGLIST OpenThreads osg osgAnimation osgDB osgGA osgViewer osgEarth osgEarthAnnotation osgEarthFeatures osgEarthSymbology osgEarthUtil osgFX osgManipulator osgParticle osgPresentation osgShadow osgSim osgTerrain osgText osgUtil osgVolume osgWidget)
IF (@HAVE_OSGEARTHQT@)
    SET (OSGLIST ${OSGLIST} osgQt osgEarthQt)
ENDIF (@HAVE_OSGEARTHQT@)
#SET (PYOSGLIST future python bindings?)
MYMESSAGE ("OSG list: ${OSGLIST}")

# extract OSG framework path from OSGEARTH_LIBRARY, assume all in same prefix

STRING (REPLACE "/osgEarth.framework" "" OSG_FW_DIR "@OSGEARTH_LIBRARY@")

### copy OSG

MESSAGE (STATUS "Copying OSG and osgEarth...")

# osg frameworks
FOREACH (OFW ${OSGLIST})
	IF (NOT IS_DIRECTORY "${QFWDIR}/${OFW}.framework")
		COPY_FRAMEWORK("${OSG_FW_DIR}" ${OFW} "${QFWDIR}")
	ENDIF ()
ENDFOREACH (OFW)

# osg plugins

IF (OSG_PLUGINS_PATH)
	FILE (GLOB OSGPLUGLIST RELATIVE ${OSG_PLUGINS_PATH} ${OSG_PLUGINS_PATH}/*.so)
	EXECUTE_PROCESS (COMMAND mkdir -p "${QPLUGDIR}/../osgPlugins")
	FOREACH (OP ${OSGPLUGLIST})
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${OSG_PLUGINS_PATH}/${OP}" "${QPLUGDIR}/../osgPlugins/${OP}")
	ENDFOREACH (OP)
ENDIF ()


# update lib paths

MESSAGE (STATUS "Updating OSG library paths...")

FOREACH (OFW ${OSGLIST})
    # get install names from installed in case bundled copy already changed
    # from a previous install attempt
    GET_INSTALL_NAME ("${OSG_FW_DIR}/${OFW}.framework/${OFW}" ${OFW}.framework OO)
    SET (OFW_CHG "${OO}")
    SET (OFW_CHG_TO "${ATEXECUTABLE}/@QGIS_FW_SUBDIR@/${OFW}.framework/${OFW}")
    # qgis app
    INSTALLNAMETOOL_CHANGE ("${OFW_CHG}" "${OFW_CHG_TO}" "${QAPPDIR}/${QGIS_APP_NAME}")

	SET (LIBPOST "${OFW}.framework/${OFW}")
    # osg frameworks
    IF (@OSX_HAVE_LOADERPATH@)
        SET (OFW_CHG_TO "${ATLOADER}/../../../${LIBPOST}")
    ENDIF ()
    FOREACH (OF ${OSGLIST})
        INSTALLNAMETOOL_CHANGE ("${OFW_CHG}" "${OFW_CHG_TO}" "${QFWDIR}/${OF}.framework/${OF}")
    ENDFOREACH (OF)
    # osg plugins
    IF (@OSX_HAVE_LOADERPATH@)
        SET (OFW_CHG_TO "${ATLOADER}/@QGIS_PLUGIN_SUBDIR_REV@/@QGIS_FW_SUBDIR@/${LIBPOST}")
    ENDIF ()
    FOREACH (OP ${OSGPLUGLIST})
        INSTALLNAMETOOL_CHANGE ("${OFW_CHG}" "${OFW_CHG_TO}" "${QPLUGDIR}/../osgPlugins/${OP}")
    ENDFOREACH (OP)
    # globe plugin
    INSTALLNAMETOOL_CHANGE ("${OFW_CHG}" "${OFW_CHG_TO}" "${QPLUGDIR}/libglobeplugin.so")
ENDFOREACH (OFW)

IF (@HAVE_OSGEARTHQT@)
    SET (OSGLIST ${OSGLIST} osgQt osgEarthQt)
ENDIF (@HAVE_OSGEARTHQT@)

# osgEarth exernal libs (2.4+, e.g. tinyxml)
GET_INSTALL_NAME ("${QFWDIR}/osgEarth.framework/osgEarth" "libtinyxml" TINY)
# MESSAGE (STATUS "libtinyxml path: ${TINY}")
IF (EXISTS "${TINY}")
    SET (TINY_CHG "${TINY}")
    SET (TINY_CHG_TO "${ATEXECUTABLE}/@QGIS_LIB_SUBDIR@/libtinyxml.dylib")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${TINY_CHG}" "${QLIBDIR}/libtinyxml.dylib")

    # osg frameworks
    FOREACH (OF ${OSGLIST})
        INSTALLNAMETOOL_CHANGE ("${TINY_CHG}" "${TINY_CHG_TO}" "${QFWDIR}/${OF}.framework/${OF}")
    ENDFOREACH (OF)
    # osg plugins
    FOREACH (OP ${OSGPLUGLIST})
        INSTALLNAMETOOL_CHANGE ("${TINY_CHG}" "${TINY_CHG_TO}" "${QPLUGDIR}/../osgPlugins/${OP}")
    ENDFOREACH (OP)
    # globe plugin
    INSTALLNAMETOOL_CHANGE ("${TINY_CHG}" "${TINY_CHG_TO}" "${QPLUGDIR}/libglobeplugin.so")
ENDIF ()

GET_INSTALL_NAME ("${OSG_PLUGINS_PATH}/osgdb_kml.so" "libminizip" MINI)
# MESSAGE (STATUS "libminizip path: ${MINI}")
IF (EXISTS "${MINI}")
    SET (MINI_CHG "${MINI}")
    SET (MINI_CHG_TO "${ATEXECUTABLE}/@QGIS_LIB_SUBDIR@/libminizip.dylib")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${MINI_CHG}" "${QLIBDIR}/libminizip.dylib")
    INSTALLNAMETOOL_CHANGE ("${MINI_CHG}" "${MINI_CHG_TO}" "${QPLUGDIR}/../osgPlugins/osgdb_kml.so")
ENDIF ()

ENDIF (@OSGEARTH_FOUND@)