message("Running Windows deploy script.")
set(WINDEPLOYQT "@WINDEPLOYQT@")
set(CYGCHECK "@CYGCHECK@")
message("Found windeployqt: ${WINDEPLOYQT}")
set(MERKAARTOR_BINARY "${CPACK_TEMPORARY_INSTALL_DIRECTORY}/bin/merkaartor.exe")
message("Working on binary: ${MERKAARTOR_BINARY}")

# Copy whole packages of gdal and proj, as they need other files than dlls to work!
# Also copy openssl directly, as it appears to be preinstalled by github actions in system paths and ignored by the toling below
# Also copy opencl-icd, as is overridden by the system files
execute_process(COMMAND pacman -Qlq mingw-w64-x86_64-openssl mingw-w64-x86_64-opencl-icd mingw-w64-x86_64-gdal mingw-w64-x86_64-proj OUTPUT_VARIABLE DEP_PKGS_FILES OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "\n" ";" DEP_PKGS_FILES_LIST ${DEP_PKGS_FILES})
foreach(DEP_PKGS_FILE IN LISTS DEP_PKGS_FILES_LIST)
  if (${DEP_PKGS_FILE} MATCHES "/include/|/man/|/doc/|/$|.a$|.pc$|.cmake$")
    message("skipping file '${DEP_PKGS_FILE}'")
  else()
    string(REGEX REPLACE "^/mingw64/" "" DEP_PKGS_FILE_DEST "${DEP_PKGS_FILE}")
    string(REGEX REPLACE "/[^/]*$" "" DEP_PKGS_FILE_DEST "${DEP_PKGS_FILE_DEST}")
    execute_process(COMMAND cygpath -w "${DEP_PKGS_FILE}" OUTPUT_VARIABLE DEP_PKGS_FILE_W OUTPUT_STRIP_TRAILING_WHITESPACE)
    string(REGEX REPLACE "\\\\" "/" DEP_PKGS_FILE_W "${DEP_PKGS_FILE_W}")
    message("installing file '${DEP_PKGS_FILE}' (${DEP_PKGS_FILE_W}) into ${DEP_PKGS_FILE_DEST}")
    file(COPY ${DEP_PKGS_FILE_W} DESTINATION ${CPACK_TEMPORARY_INSTALL_DIRECTORY}/${DEP_PKGS_FILE_DEST})
  endif()
endforeach()

message("Running windeployqt:")
# Run Windeployqt to handle Qt libraries, including plugins
execute_process(COMMAND ${WINDEPLOYQT} --compiler-runtime ${MERKAARTOR_BINARY} RESULT_VARIABLE WINDEPLOYQT_RESULT)
if ( NOT (${WINDEPLOYQT_RESULT} EQUAL "0") )
    message(FATAL_ERROR "Windeployqt failed with result: ${WINDEPLOYQT_RESULT}.")
endif()

# Copy all required dlls into the package, skipping those residing in Windows installation
execute_process(COMMAND ${CYGCHECK} ${MERKAARTOR_BINARY} OUTPUT_VARIABLE MERKAARTOR_DLL_DEPS OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "\n" ";" MERKAARTOR_DLL_DEPS_LIST ${MERKAARTOR_DLL_DEPS})
message("Dependencies: ${MERKAARTOR_DLL_DEPS_LIST}")
foreach(DLL_FILE IN LISTS MERKAARTOR_DLL_DEPS_LIST)
  string(TOUPPER "${DLL_FILE}" DLL_FILE_UPPER)
  if (${DLL_FILE_UPPER} MATCHES "WINDOWS")
    message("skipping file '${DLL_FILE}'")
  else()
    set(DLL_FILE_DEST "${CPACK_TEMPORARY_INSTALL_DIRECTORY}/bin/")
    string(STRIP ${DLL_FILE} DLL_FILE)
    string(REPLACE "\\\\" "/" DLL_FILE ${DLL_FILE})
    message("copying new dependency '${DLL_FILE}' into ${DLL_FILE_DEST}")
    file( COPY ${DLL_FILE} DESTINATION ${DLL_FILE_DEST} )
  endif()
endforeach()

# Remove when done debugging
get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
    message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
#message("Prefix is: ${CMAKE_INSTALL_PREFIX}")
#execute_process(COMMAND ls ${CMAKE_INSTALL_PREFIX})
