SET(CMAKE_MODULE_PATH @CMAKE_MODULE_PATH@)
include(linkMacro)
include(BundleUtilities)
SET(BU_CHMOD_BUNDLE_ITEMS TRUE)

FIXUP_BUNDLE("@BUNDLE@" "@all@" "@bundleDirs@" IGNORE_ITEM "python3;python3.7m;python3.7;Python;")

FILE(GLOB lbs "@BUNDLE@/Contents/MacOS/libADM_core*")
IF(lbs)
    MESSAGE(STATUS "Libs are in @BUNDLE@/Contents/MacOS")
    SET(LIBS_LOCATION "MacOS")
ELSE(lbs)
    MESSAGE(STATUS "Libs are in @BUNDLE@/Contents/Frameworks")
    SET(LIBS_LOCATION "Frameworks")
ENDIF(lbs)
# Now we can move the old libs
MESSAGE(STATUS " * Cleanup * ")
FILE(REMOVE_RECURSE "@BUNDLE@/Contents/lib")
FILE(REMOVE_RECURSE "@BUNDLE@/Contents/Resources/lib")

makeLink("MacOS" "lib" "@BUNDLE@/Contents/")

# Partially relink libADM_dm_vapoursynth.dylib
# Using default link location because versioned paths from VAPOURSYNTHSCRIPT_LDFLAGS
# would result in each minor VapourSynth upgrade breaking the demuxer.
FILE(GLOB dmvs "@BUNDLE@/Contents/MacOS/ADM_plugins6/demuxers/libADM_dm_vapoursynth.dylib")
SET(ADM_CPU_ARM64 "@ADM_CPU_ARM64@")
IF(dmvs)
    IF(ADM_CPU_ARM64)
        SET(HOMEBREW_PREFIX "/opt/homebrew")
    ELSE(ADM_CPU_ARM64)
        SET(HOMEBREW_PREFIX "/usr/local")
    ENDIF(ADM_CPU_ARM64)
    MESSAGE(STATUS "Relinking ${dmvs}")
    EXECUTE_PROCESS(COMMAND install_name_tool -change "@executable_path/../${LIBS_LOCATION}/libvapoursynth.dylib"
        "${HOMEBREW_PREFIX}/lib/libvapoursynth.dylib" "${dmvs}")
    EXECUTE_PROCESS(COMMAND install_name_tool -change "@executable_path/../${LIBS_LOCATION}/libvapoursynth-script.0.dylib"
        "${HOMEBREW_PREFIX}/lib/libvapoursynth-script.0.dylib" "${dmvs}")
ENDIF(dmvs)

# Remove Python and libvapoursynth*, the former can't be relinked and we end up using external versions anyway.
FILE(GLOB pyth "@BUNDLE@/Contents/Frameworks/Python.framework")
IF(pyth)
        MESSAGE(STATUS "Removing Python")
        FILE(REMOVE_RECURSE "${pyth}")
ENDIF(pyth)
FILE(GLOB vs "@BUNDLE@/Contents/${LIBS_LOCATION}/libvapoursynth*" "@BUNDLE@/Contents/${LIBS_LOCATION}/libzimg*")
FOREACH(v ${vs})
        MESSAGE(STATUS "Removing ${v}")
        FILE(REMOVE "${v}")
ENDFOREACH(v ${vs})

# Remove the copies of Qt lib in framework, we only keep "5"
FILE(GLOB  frameworks "@BUNDLE@/Contents/Frameworks/*")
MESSAGE(STATUS "Frameworks to clean : ${frameworks}")
FOREACH(frame ${frameworks})
        MESSAGE(STATUS "    Cleaning ${frame}")
        FILE(REMOVE_RECURSE  "${frame}/Headers")
        FILE(GLOB  qtop      "${frame}/Qt*")
        FOREACH(q ${qtop})
                MESSAGE(STATUS "  >${q}")
                FILE(REMOVE    "${q}")
        ENDFOREACH(q ${qtop})
        FILE(REMOVE_RECURSE  "${frame}/Versions/Current")
        FILE(REMOVE_RECURSE  "${frame}/Versions/@ADM_QT_VERSION@/Headers")
        # Create symlinks ?
ENDFOREACH(frame ${frameworks})

IF(ADM_CPU_ARM64)
    MESSAGE(STATUS "")
    MESSAGE(STATUS "Trying to adhoc-sign the app, script: @CMAKE_BINARY_DIR@/macos-adhoc-sign-app.sh")
    MESSAGE(STATUS "")
    EXECUTE_PROCESS(COMMAND bash "@CMAKE_BINARY_DIR@/macos-adhoc-sign-app.sh")
ENDIF(ADM_CPU_ARM64)
