include_directories(${FLTK_INCLUDE_DIR})
include_directories(${GETTEXT_INCLUDE_DIR})

if(WIN32)
  # These are used for printing pdfs only in Windows
  include_directories(${POPPLER_INCLUDE_DIR})
  include_directories(${GLIB_INCLUDE_DIR_1})
  include_directories(${GLIB_INCLUDE_DIR_2})
  include_directories(${CAIRO_INCLUDE_DIR})
endif()

include_directories(${CMAKE_SOURCE_DIR}/common)
set(VNCVIEWER_SOURCES
  tgpro_helpers.cxx
  sound_handler.cxx
  ConnectingMessage.cxx
  PrintDialog.cxx
  MagicUrlHelper.cxx
  UrlWhitelistHandler.cxx
  NativeMessagingApp.cxx
  CAutotransferHandler.cxx
  menukey.cxx
  BaseTouchHandler.cxx
  CConn.cxx
  DesktopWindow.cxx
  EmulateMB.cxx
  UserDialog.cxx
  ServerDialog.cxx
  Surface.cxx
  StreamDialog.cxx
  OptionsDialog.cxx
  PlatformPixelBuffer.cxx
  Viewport.cxx
  parameters.cxx
  keysym2ucs.c
  touch.cxx
  vncviewer.cxx)

if(WIN32)
  # Since vncviewer.rc is generated, local includes will be looking
  # in the wrong directory. We need to help it out.
  include_directories(${CMAKE_CURRENT_SOURCE_DIR})
  configure_file(vncviewer.rc.in vncviewer.rc)
  set(VNCVIEWER_SOURCES
    ${VNCVIEWER_SOURCES}
    ${CMAKE_CURRENT_BINARY_DIR}/vncviewer.rc)
endif()

if(WIN32)
  set(VNCVIEWER_SOURCES ${VNCVIEWER_SOURCES} Win32TouchHandler.cxx win32.c pdfprinter.cxx printing.cxx MagicUrlHelper.cxx)
elseif(APPLE)
  set(VNCVIEWER_SOURCES ${VNCVIEWER_SOURCES} cocoa.mm printing.cxx osx_to_qnum.c)
else()
  set(VNCVIEWER_SOURCES ${VNCVIEWER_SOURCES} GestureHandler.cxx XInputTouchHandler.cxx printing.cxx xkb_to_qnum.c)
endif()

if(WIN32)
  set(VNCVIEWER_SOURCES ${VNCVIEWER_SOURCES} Surface_Win32.cxx)
elseif(APPLE)
  set(VNCVIEWER_SOURCES ${VNCVIEWER_SOURCES} Surface_OSX.cxx)
else()
  set(VNCVIEWER_SOURCES ${VNCVIEWER_SOURCES} Surface_X11.cxx)
endif()

if(WIN32 AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
  add_executable(vncviewer WIN32 ${VNCVIEWER_SOURCES})
else()
  add_executable(vncviewer ${VNCVIEWER_SOURCES})
endif()

find_library(MPRIVACY_LIBRARY
  NAMES mprivacy libmprivacy
  HINTS "/usr/x86_64-apple-darwin19/lib/")

target_link_libraries(vncviewer rfb ${MPRIVACY_LIBRARY} network rdr os ${FLTK_LIBRARIES} ${GETTEXT_LIBRARIES})

# libmprivacy needs psapi on woe32.
if(WIN32)
  target_link_libraries(vncviewer msimg32 psapi wtsapi32 dbghelp)
elseif(APPLE)
  target_link_libraries(vncviewer "-framework Cocoa")
  target_link_libraries(vncviewer "-framework Carbon")
  target_link_libraries(vncviewer "-framework IOKit")
  target_link_libraries(vncviewer "-lfltk")
else()
  target_link_libraries(vncviewer ${X11_Xi_LIB})
endif()

if(UNIX)
#  target_link_libraries(vncviewer "-Wl,-Bstatic -lkrb5 -lkrb5support -lgssapi_krb5 -lk5crypto -Wl,-Bdynamic")
  target_link_libraries(vncviewer "-lkrb5 -lzstd")
endif()

install(TARGETS vncviewer DESTINATION ${CMAKE_INSTALL_FULL_BINDIR})
if(UNIX)
  install(FILES vncviewer.man DESTINATION ${CMAKE_INSTALL_FULL_MANDIR}/man1 RENAME vncviewer.1)

  configure_file(vncviewer.desktop.in.in vncviewer.desktop.in)
  find_program(INTLTOOL_MERGE_EXECUTABLE intltool-merge)
  if("${GETTEXT_VERSION_STRING}" VERSION_GREATER 0.18.99)
    add_custom_command(OUTPUT vncviewer.desktop
      COMMAND ${GETTEXT_MSGFMT_EXECUTABLE}
                --desktop --template vncviewer.desktop.in
                -d ${CMAKE_SOURCE_DIR}/po -o vncviewer.desktop
      DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/vncviewer.desktop.in
    )
  elseif(INTLTOOL_MERGE_EXECUTABLE)
    add_custom_command(OUTPUT vncviewer.desktop
      COMMAND sed -e 's/^Name/_Name/'
                -e 's/^GenericName/_GenericName/'
                -e 's/^Comment/_Comment/'
                vncviewer.desktop.in > vncviewer.desktop.intl
      COMMAND ${INTLTOOL_MERGE_EXECUTABLE}
                -d ${CMAKE_SOURCE_DIR}/po
                vncviewer.desktop.intl vncviewer.desktop
      DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/vncviewer.desktop.in
    )
  else()
    add_custom_command(OUTPUT vncviewer.desktop
      COMMAND cp vncviewer.desktop.in vncviewer.desktop
      DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/vncviewer.desktop.in
    )
  endif()
  add_custom_target(desktop ALL DEPENDS vncviewer.desktop)
  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/vncviewer.desktop DESTINATION ${CMAKE_INSTALL_FULL_DATADIR}/applications)

  foreach(res 16 22 24 32 48)
    install(FILES ../media/icons/tightgatevnc_${res}.png DESTINATION ${DATA_DIR}/icons/hicolor/${res}x${res}/apps RENAME tightgatevnc.png)
  endforeach()
  install(FILES ../media/icons/tightgatevnc.svg DESTINATION ${DATA_DIR}/icons/hicolor/scalable/apps)
endif()
