include(BundleUtilities)

set(MACDEPLOYQT_PATH "@MACDEPLOYQT_PATH@")
set(INFO_PLIST_PATH "@CPACK_BUNDLE_PLIST@")
set(ADJUST_RIZIN_LIBS "@ADJUST_RIZIN_LIBS@")
set(CUTTER_ENABLE_CRASH_REPORTS "@CUTTER_ENABLE_CRASH_REPORTS@")
set(Breakpad_LINK_LIBRARIES "@Breakpad_LINK_LIBRARIES@")
set(CUTTER_PACKAGE_DEPENDENCIES "@CUTTER_PACKAGE_DEPENDENCIES@")
set(CUTTER_ENABLE_PYTHON "@CUTTER_ENABLE_PYTHON@")

macro(run_or_die)
	execute_process(${ARGV} RESULT_VARIABLE PROC_RESULT)
	if(PROC_RESULT)
		message(FATAL_ERROR "Failed to run ${ARGV}")
	endif()
endmacro()

get_filename_component(BUNDLE_PATH "${CMAKE_INSTALL_PREFIX}/../.." ABSOLUTE)
set(EXECUTABLE_DIR "${BUNDLE_PATH}/Contents/MacOS")
set(FRAMEWORK_DIR "${BUNDLE_PATH}/Contents/Frameworks")

# Copying the Info.plist will be done later again by CPack but we need it a bit earlier
# so macdeployqt has enough info.
file(COPY "${INFO_PLIST_PATH}" DESTINATION "${BUNDLE_PATH}/Contents")

# replace absolute path from build directory in rizin pkgconfig files with relative ones
file(GLOB RZ_PCFILES "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig/rz_*.pc")
list(APPEND RZ_PCFILES "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig/librz.pc")
foreach (_pcfile ${RZ_PCFILES})
	file(READ "${_pcfile}" _text)
	string(REGEX REPLACE "^prefix=[^\n]*\n" "prefix=\${pcfiledir}/../..\n" _text "${_text}")
	file(WRITE "${_pcfile}" "${_text}")
endforeach()
# macdeployqt would put the rz libraries into Contents/Frameworks by default, but we want to have them
# only in the prefix, so we fix the paths manually.
foreach(_lib ${ADJUST_RIZIN_LIBS})
	get_filename_component(_lib "${_lib}" REALPATH)
	get_filename_component(_name "${_lib}" NAME)
	# Cutter reference to lib
	run_or_die(COMMAND install_name_tool
		-change "${_lib}" "@rpath/${_name}"
		"${EXECUTABLE_DIR}/Cutter")
	# lib LC_ID_DYLIB
	run_or_die(COMMAND install_name_tool
			-id "@rpath/${_name}"	
			"${CMAKE_INSTALL_PREFIX}/lib/${_name}")
	# Fix every lib for every lib too
	foreach(_lib2 ${ADJUST_RIZIN_LIBS})
	get_filename_component(_lib2 "${_lib2}" REALPATH)
		get_filename_component(_name2 "${_lib2}" NAME)
		run_or_die(COMMAND install_name_tool
			-change "${_lib2}" "@rpath/${_name2}"
			"${CMAKE_INSTALL_PREFIX}/lib/${_name}")
	endforeach()
endforeach()

# Add rpaths because macdeployqt fails to do that properly
run_or_die(COMMAND install_name_tool
	-add_rpath "@executable_path/../Frameworks"
	"${EXECUTABLE_DIR}/Cutter")
run_or_die(COMMAND install_name_tool
	-add_rpath "@executable_path/../Resources/lib"
	"${EXECUTABLE_DIR}/Cutter")

set(MACDEPLOYQT_COMMAND "${MACDEPLOYQT_PATH}" "${BUNDLE_PATH}" "-verbose=2" "-libpath=${CMAKE_INSTALL_PREFIX}/lib")
message("Running macdeployqt \"${MACDEPLOYQT_COMMAND}\"")
run_or_die(COMMAND ${MACDEPLOYQT_COMMAND}) # First run
if (CUTTER_PACKAGE_DEPENDENCIES AND CUTTER_ENABLE_PYTHON)
	file(REAL_PATH "${FRAMEWORK_DIR}/Python.framework/Python" _python_lib_path)
	message("Python lib ${_python_lib_path}")
	list(APPEND MACDEPLOYQT_COMMAND "-executable=${_python_lib_path}")
endif()
# Qt plugins are not getting deployed on first macdeployqt run. Runing twice helps.
# If python is added as additional executable in first run it also breaks plugin copying.
run_or_die(COMMAND ${MACDEPLOYQT_COMMAND}) # Second run

# Clean up the mess that macdeployqt made (duplicate rz libs, we only want the ones in the prefix)
foreach(_lib ${ADJUST_RIZIN_LIBS})
	get_filename_component(_lib "${_lib}" REALPATH)
	get_filename_component(_name "${_lib}" NAME)
	file(REMOVE "${BUNDLE_PATH}/Contents/Frameworks/${_name}")
endforeach()

if (CUTTER_ENABLE_CRASH_REPORTS)
	message("Copying Breakpad ${Breakpad_LINK_LIBRARIES}")
	set(_breakpad_lib "Versions/A/Breakpad")
	copy_resolved_framework_into_bundle("${Breakpad_LINK_LIBRARIES}/${_breakpad_lib}" "${FRAMEWORK_DIR}/Breakpad.framework/${_breakpad_lib}")
endif()