#
# Before calling this, ensure that the following variables are set:
#
# VTK_DEVICE_LIBS - a path containing a wildcard suitable for globbing
# VTK_SIMULATOR_LIBS - a path containing a wildcard suitable for globbing
# VTK_INSTALLED_HEADERS - the path containing all headers for the framework
# CMAKE_FRAMEWORK_INSTALL_PREFIX - the path in which to make vtk.framework
#

# Make sure VTK is installed.
if (NOT EXISTS "@VTK_INSTALLED_HEADERS@")
  message(FATAL_ERROR
          "VTK not installed. Build the source then build the 'install' target.")
endif()

# Ensure a framework path is set
if (NOT EXISTS "@CMAKE_FRAMEWORK_INSTALL_PREFIX@")
  message(FATAL_ERROR
          "Please specifiy a CMAKE_FRAMEWORK_INSTALL_PREFIX.")
endif()

# Glob all input libs
file(GLOB VTK_LIB_GLOB
  "@VTK_DEVICE_LIBS@"
  "@VTK_SIMULATOR_LIBS@"
)

# Set framework output path
set(FRAMEWORK_PATH "@CMAKE_FRAMEWORK_INSTALL_PREFIX@/vtk.framework")

# Do these individually, as we don't want them piped
execute_process(COMMAND mkdir -p "${FRAMEWORK_PATH}")
execute_process(COMMAND mkdir -p "${FRAMEWORK_PATH}/Headers")
execute_process(COMMAND cp -r "@VTK_INSTALLED_HEADERS@/"
                              "${FRAMEWORK_PATH}/Headers/.")
execute_process(COMMAND libtool -static -o
                      "${FRAMEWORK_PATH}/vtk_lib"
                      ${VTK_LIB_GLOB})
execute_process(COMMAND lipo -create "${FRAMEWORK_PATH}/vtk_lib"
                             -output "${FRAMEWORK_PATH}/vtk")
execute_process(COMMAND rm "${FRAMEWORK_PATH}/vtk_lib")

message(STATUS "Created ${FRAMEWORK_PATH}")