include(OdalaunchCopyLibs)
include(OdamexTargetSettings)

# API
set(API_DIR ../odalpapi)
file(GLOB API_HEADERS ${API_DIR}/*.h ${API_DIR}/threads/*.h)
file(GLOB API_SOURCES ${API_DIR}/*.cpp ${API_DIR}/threads/*.cpp)

# Odalaunch
set(LAUNCH_DIR res src)
file(GLOB LAUNCH_HEADERS res/*.h src/*.h)
file(GLOB LAUNCH_SOURCES src/*.cpp)

if(WIN32)
  configure_file(res/odalaunch.rc.in "${CMAKE_CURRENT_BINARY_DIR}/res/odalaunch.rc" @ONLY)
  set(LAUNCH_WIN32_RESOURCES "${CMAKE_CURRENT_BINARY_DIR}/res/odalaunch.rc")
endif()

# Build Resource Directory
# This file is generated by the build process.
set(XRCRES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/res")
set(XRCRES_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/res")
set(XRCRES_HEADER "${XRCRES_BINARY_DIR}/xrc_resource.h")

# Odalaunch definitions
include_directories(${API_DIR} ${LAUNCH_DIR} ${XRCRES_BINARY_DIR})

# wxWidgets configuration
# [AM] Putting this "find" in libraries would only work (from a scope
#      perspective) if we got a target out of it, which we don't for
#      wxWidgets.  So we do the ugly downloading bits in libraries and do
#      the "find" here.
find_package(wxWidgets COMPONENTS core base adv net xrc)
if(wxWidgets_FOUND)
  include( "${wxWidgets_USE_FILE}" )
else()
  if(wxWidgets_ROOT_DIR)
    message(WARNING "wxWidgets root directory exists but no compiled libraries were found, odalaunch target not generated.")
  else()
    message(WARNING "wxWidgets not found, odalaunch target not generated.")
  endif()
endif()

# Odalaunch target
if(wxWidgets_FOUND)
  file(MAKE_DIRECTORY "${XRCRES_BINARY_DIR}")

  # Attempt to compile all of the XRC files with custom commands.
  add_custom_command(
    OUTPUT "${XRCRES_HEADER}"
    COMMAND "${wxWidgets_wxrc_EXECUTABLE}" -c -o "${XRCRES_HEADER}"
    "${XRCRES_DIR}/icons.xrc" "${XRCRES_DIR}/xrc_resource.xrc"
    VERBATIM)

  add_executable(odalaunch MACOSX_BUNDLE WIN32
    ${API_SOURCES} ${API_HEADERS}
    ${LAUNCH_SOURCES} ${LAUNCH_HEADERS}
    ${XRCRES_HEADER} ${LAUNCH_WIN32_RESOURCES})
  odamex_target_settings(odalaunch)

  target_link_libraries(odalaunch ${wxWidgets_LIBRARIES})

  if(WIN32)
    target_link_libraries(odalaunch ws2_32)
  endif()

  odalaunch_copy_libs(odalaunch)

  set(MACOSX_BUNDLE_BUNDLE_NAME OdaLaunch)
  set(MACOSX_BUNDLE_INFO_STRING "ODAMEX and its likeness © ${PROJECT_COPYRIGHT} Odamex team.")
  set(MACOSX_BUNDLE_LONG_VERSION_STRING "${PROJECT_VERSION}")
  set(MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION}")
  set(MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}")
  set(MACOSX_BUNDLE_ICON_FILE odalaunch.icns)

  if(APPLE)
    add_custom_command(
      TARGET odalaunch
      POST_BUILD
      COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/odalaunch.app/Contents/Resources"
      COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_SOURCE_DIR}/media/${MACOSX_BUNDLE_ICON_FILE}" "${CMAKE_CURRENT_BINARY_DIR}/odalaunch.app/Contents/Resources/"
      VERBATIM
    )

    set_directory_properties(
      PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES
      "${CMAKE_CURRENT_BINARY_DIR}/odalaunch.app/Contents/Resources/${MACOSX_BUNDLE_ICON_FILE}"
    )
  elseif(WIN32)
    install(TARGETS odalaunch
      RUNTIME DESTINATION .
      COMPONENT odalaunch)
  else()
    install(TARGETS odalaunch
      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
      COMPONENT odalaunch)
  endif()
endif()

if(BUILD_OR_FAIL AND NOT TARGET odalaunch)
  message(FATAL_ERROR "Odalaunch target could not be generated")
endif()
