# Make sure this CMake has BundleUtilities.cmake:
#
if(NOT EXISTS "${CMAKE_ROOT}/Modules/BundleUtilities.cmake")
  message(FATAL_ERROR "error: BundleUtilities.cmake not found. Use CMake 2.6.4 or later.")
endif(NOT EXISTS "${CMAKE_ROOT}/Modules/BundleUtilities.cmake")
 
 
# Avoid following symlinks encountered during FILE GLOB_RECURSE calls:
#
if(COMMAND CMAKE_POLICY)
  cmake_policy(SET CMP0009 NEW)
endif(COMMAND CMAKE_POLICY)
 
# Allow include to do cmake_policy push/pops:
#
if(COMMAND CMAKE_POLICY)
  cmake_policy(SET CMP0011 NEW)
endif(COMMAND CMAKE_POLICY)

 
# gp_resolved_file_type_override resolved_file type_var
#
function(gp_resolved_file_type_override resolved_file type_var)
  if(file MATCHES "^/usr//lib")
    message("resolving ${file} as system")
    set(${type_var} system PARENT_SCOPE)
  endif()
endfunction()


# gp_item_default_embedded_path_override item default_embedded_path_var
#
# Return the path that others should refer to the item by when the item
# is embedded inside a bundle.
#
# This is a project-specific override of BundleUtilities.cmake's
# gp_item_default_embedded_path
#
function(gp_item_default_embedded_path_override item default_embedded_path_var)
  # By default, embed items as set by gp_item_default_embedded_path:
  #
  set(path "${${default_embedded_path_var}}")
 
  # But for ParaView...
  #
  # ...embed *.dylib in the Libraries folder:
  #
  if(item MATCHES "\\.dylib$")
    set(path "@executable_path/../Libraries")
  endif(item MATCHES "\\.dylib$")
 
  # ...embed Plugins from ${PluginList} in the Plugins folder:
  #
  list(FIND PluginList ${item} pluginFound)
  if(pluginFound GREATER -1)
    set(path "@executable_path/../Plugins")
  endif(pluginFound GREATER -1)

  # ...embed libqsqlite.dylib in the Plugins/sqldrivers folder:
  #
  if(item MATCHES "libqsqlite\\.dylib$")
    set(path "@executable_path/../Plugins/sqldrivers")
  endif(item MATCHES "libqsqlite\\.dylib$")
 
  # ...embed *.so in the Plugins folder:
  #
  if(item MATCHES "\\.so$")
    set(path "@executable_path/../Plugins")
  endif(item MATCHES "\\.so$")
 
  # ...embed *Python.so in the Libraries folder:
  #
  if(item MATCHES "Python\\.so$")
    set(path "@executable_path/../Libraries")
  endif(item MATCHES "Python\\.so$")
 
  set(${default_embedded_path_var} "${path}" PARENT_SCOPE)
endfunction(gp_item_default_embedded_path_override)
 
 
# Copy the .app bundle from the build tree to the install tree.
# Set up the InstallPrefix ENV var and execute the shell script:
#

# Optional subdirectory inside the bundle where the app can be placed.
set(ParaView_BUNDLE_LOCATION "@ParaView_BUNDLE_LOCATION@")

if(ParaView_BUNDLE_LOCATION)
  set(InstallPrefix "${CMAKE_INSTALL_PREFIX}/${ParaView_BUNDLE_LOCATION}")
else()
  set(InstallPrefix "${CMAKE_INSTALL_PREFIX}")
endif()
 
set(ENV{InstallPrefix} "${InstallPrefix}")
 
execute_process(COMMAND "@OSX_MAKE_STANDALONE_BUNDLE_BASH_SCRIPT@")
set(bundle "${InstallPrefix}/ParaView @PARAVIEW_VERSION_FULL@.app")
 
if(NOT EXISTS "${bundle}")
  message(FATAL_ERROR "error: shell script did not put ParaView .app bundle in expected location: ${bundle}")
endif()
 
 
# Fixup the .app bundle in the install tree:
#
include(BundleUtilities)
 
# Additional libs may be found in:
#
set(libs_path "@LIBRARY_OUTPUT_PATH@")
 
set(FFMPEG_avcodec_LIBRARY "@FFMPEG_avcodec_LIBRARY@")
if(FFMPEG_avcodec_LIBRARY)
  get_filename_component(ffmpeg_lib_path "${FFMPEG_avcodec_LIBRARY}" PATH)
  set(libs_path ${libs_path} ${ffmpeg_lib_path})
endif()
 
set(QT_QTCLUCENE_LIBRARY "@QT_QTCLUCENE_LIBRARY@")
if(QT_QTCLUCENE_LIBRARY)
  foreach(i ${QT_QTCLUCENE_LIBRARY})
    if(i MATCHES ".*\\.dylib$")
      get_filename_component(qtlucene_lib_path "${i}" PATH)
      list(APPEND libs_path ${qtlucene_lib_path})
    endif()
  endforeach(i)
endif()
 
list(APPEND libs_path "@QT_PLUGINS_DIR@/sqldrivers")
list(APPEND libs_path "@QT_PLUGINS_DIR@/imageformats")
list(APPEND libs_path "@PARAVIEW_FIXUP_BUNDLE_SEARCH_PATHS@")
 
list(REMOVE_DUPLICATES libs_path)


# GLOB the list of Python.so files and plugins (for fixup_bundle
# purposes since they will not be pulled in automatically by
# dependency analysis)
#
set(libs "")
set(suffix "@CMAKE_SHARED_LIBRARY_SUFFIX@")

file(GLOB_RECURSE candidates
  "${bundle}/Contents/Plugins/*${suffix}"
  "${bundle}/Contents/Libraries/*Python.so"
  "${bundle}/Contents/Libraries/sqldrivers/*${suffix}"
  )
foreach(lib ${candidates})
  if(NOT lib MATCHES "(_debug|d[0-9])${suffix}$")
    set(libs ${libs} "${lib}")
  endif()
endforeach()

message("libs=${libs}")

# Fix it!
#
fixup_bundle(
  "${bundle}"
  "${libs}"
  "${libs_path}"
  )
