# - Configurable script to fix up a VR Juggler bundle
#
# Original Author:
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright 2009-2010, Iowa State University
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file ../../LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
# SPDX-License-Identifier: BSL-1.0

function(gp_resolve_item_override _context _item _exepath _dirs _resolved_item _resolved)
	if(NOT ${_resolved})
		set(ri "ri-NOTFOUND")
		if("${_item}" MATCHES "@executable_path")
			get_filename_component(itemname "${_item}" NAME)
			message(STATUS "Was incompletely embedded, now looking for ${itemname}")
			find_file(ri "${itemname}" ${_exepath} ${_dirs} NO_DEFAULT_PATH)
			find_file(ri "${itemname}" ${_exepath} ${_dirs} /usr/lib)
			if(ri)
				get_filename_component(ri "${ri}" REALPATH)
				message(STATUS "override info: 'find_file' on filename '${itemname} in exepath/dirs (${ri})")
				set(${_resolved} 1 PARENT_SCOPE)
				set(${_resolved_item} "${ri}" PARENT_SCOPE)
				set(ri "ri-NOTFOUND")
			endif()
		endif()
	endif()

endfunction()


set(CMAKE_MODULE_PATH @CMAKE_MODULE_PATH@)

include(BundleUtilities)
set(IS_XCODE @IS_XCODE@)
if(IS_XCODE)
	set(APP "$ENV{TARGET_BUILD_DIR}/@_target@.app")
else()
	set(APP "@TARGET_LOCATION@")
endif()
#set(APP "@_target@")

set(BUNDLE_LIBS_INPUT "@BUNDLE_LIBS@")
foreach(lib ${BUNDLE_LIBS_INPUT})
	if(IS_ABSOLUTE "${lib}")
		list(APPEND BUNDLE_LIBS "${lib}")
		message(STATUS "  ${lib}")
	else()
		message(STATUS "* ${CMAKE_INSTALL_PREFIX}/${lib}")
		list(APPEND BUNDLE_LIBS "${CMAKE_INSTALL_PREFIX}/${lib}")
	endif()
endforeach()

fixup_bundle("${CMAKE_INSTALL_PREFIX}/${APP}" "${BUNDLE_LIBS}" "@BUNDLE_LIB_DIRS@")
