cmake_minimum_required(VERSION 3.21)

# Set your project name here
project(izanagi C)

set(CMAKE_C_STANDARD 11)

if (WIN32)
  # TODO set arch on target instead of generator
  set(PLATFORM WINDOWS)
  if (CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(PROJECT_ARCH x64)
    add_definitions(
      -DIZ_WIN64
    )
  else ()
    set(PROJECT_ARCH x86)
    add_definitions(
      -DIZ_WIN32
    )
  endif ()
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  set(PLATFORM MACOS)
  add_definitions(
    -DIZ_MACOS
  )
else()
  set(PLATFORM UNIX)
  add_definitions(
    -DIZ_UNIX
  )
endif ()

add_definitions(
  -DIZ_APP_NAME="Izanagi"
  -DIZ_APP_DESCRIPTION="Run and gun game"
  -DIZ_APP_SERVER_DESCRIPTION="Dedicated server"
  -DIZ_PLAYERS=1
)

if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
  add_definitions(-DIZ_DEBUG)
endif()

include_directories(
  "${PROJECT_SOURCE_DIR}/dependencies/SDL2/include"
  "${PROJECT_SOURCE_DIR}/dependencies/SDL2_image"
  "${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/include"
  "${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/include"
  "${PROJECT_SOURCE_DIR}/dependencies/minIni/dev"
  "${PROJECT_SOURCE_DIR}/dependencies/bdd-for-c"
  "${PROJECT_SOURCE_DIR}/dependencies/portmidi/pm_common"
  "${PROJECT_SOURCE_DIR}/dependencies/spine-runtimes/spine-c/spine-c/include"
  "${PROJECT_SOURCE_DIR}/dependencies/getopt-for-windows"
  "${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/include"
  "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/include"
  "${PROJECT_SOURCE_DIR}/dependencies/sqlite"
)

if (WIN32)
  link_directories(
    "${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}"
    "${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/lib/${PROJECT_ARCH}"
    "${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/lib/${PROJECT_ARCH}"
    "${PROJECT_SOURCE_DIR}/dependencies/portmidi/Release"
    "${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/lib/Release"
    "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/lib"
  )
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  include_directories(
    /usr/local/include
    /usr/local/include/SDL2
    /usr/local/include/openssl
  )

  link_directories(
    /usr/local/lib
  )
endif()

add_executable(
  game
    dependencies/minIni/dev/minIni.h
    dependencies/minIni/dev/minIni.c
    dependencies/getopt-for-windows/getopt.h
    dependencies/getopt-for-windows/getopt.c
        src/packages/game/output/video/IZ_video.h
        src/packages/game/output/video/IZ_video.c
        src/packages/common/IZ_common.h
    src/packages/game/input/IZ_action.h
    src/packages/game/IZ_app.h
    src/packages/game/IZ_app.c
    src/packages/game/main.c
    src/packages/game/input/IZ_joystick.c
    src/packages/game/input/IZ_joystick.h
    src/packages/game/input/IZ_keyboard.c
    src/packages/game/input/IZ_keyboard.h
        src/packages/config/IZ_config.c
        src/packages/config/IZ_config.h
        src/packages/game/geometry/IZ_vector2d.c
    src/packages/game/geometry/IZ_vector2d.h
    src/packages/game/geometry/IZ_rect.c
    src/packages/game/geometry/IZ_rect.h
    src/packages/game/core/IZ_object.c
    src/packages/game/core/IZ_object.h
    src/packages/game/core/IZ_creature.c
    src/packages/game/core/IZ_creature.h
    src/packages/game/core/IZ_entity.c
    src/packages/game/core/IZ_entity.h
    src/packages/game/memory/IZ_pool.c
    src/packages/game/memory/IZ_pool.h
    src/packages/game/input/IZ_input.c
    src/packages/game/input/IZ_input.h
    src/packages/game/input/IZ_midi.c
    src/packages/game/input/IZ_midi.h
    src/packages/game/data/IZ_list.c
    src/packages/game/data/IZ_list.h
        src/packages/net/svc/IZ_wsclient.c
        src/packages/net/svc/IZ_wsclient.h
        src/packages/log/IZ_log.c
        src/packages/log/IZ_log.h
    src/packages/game/util/IZ_midi.c
    src/packages/game/util/IZ_midi.h
        src/packages/net/core/IZ_websocket.h
        src/packages/net/core/IZ_websocket.c
        src/packages/net/IZ_net_client.c src/packages/net/IZ_net_client.h src/packages/game/IZ_app_net.c src/packages/game/IZ_app_net.h src/packages/game/IZ_app_video.c src/packages/game/IZ_app_video.h src/packages/game/IZ_subsystem.h src/packages/game/IZ_app_input.c src/packages/game/IZ_app_input.h src/packages/game/IZ_app_config.c src/packages/game/IZ_app_config.h src/packages/game/asset/IZ_asset.c src/packages/game/asset/IZ_asset.h)

if (WIN32)
  target_link_libraries(
    game
      SDL2main
      SDL2
      SDL2_image
      SDL2_ttf
      portmidi
      libcrypto
      libssl
      websockets
  )
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  target_link_libraries(
    game
    SDL2main
    SDL2
    SDL2_image
    SDL2_ttf
    portmidi
    #    libcrypto
    #    libssl
    websockets
  )
endif()

add_executable(
  game-test-geometry
    dependencies/bdd-for-c/bdd-for-c.h
    src/packages/test/IZ_mock.h
    src/packages/test/IZ_test.h

        src/packages/game/geometry/IZ_rect.h
    src/packages/game/geometry/IZ_rect.c
    src/packages/game/geometry/IZ_vector2d.h
    src/packages/game/geometry/IZ_vector2d.c
    src/packages/game/geometry/geometry.test.c
)

add_executable(
  game-test-input
    dependencies/bdd-for-c/bdd-for-c.h
    src/packages/test/IZ_mock.h
    src/packages/test/IZ_test.h

    __mocks__/minIni.mock.h
    __mocks__/SDL_keyboard.mock.h
    __mocks__/SDL_events.mock.h
    __mocks__/SDL_joystick.mock.h
    __mocks__/SDL_stdinc.mock.h
    __mocks__/portmidi.mock.h

        src/packages/config/IZ_config.h

    src/packages/game/input/IZ_keyboard.h
    src/packages/game/input/IZ_keyboard.c

    src/packages/game/input/IZ_joystick.h
    src/packages/game/input/IZ_joystick.c

    src/packages/game/input/IZ_midi.h
    src/packages/game/input/IZ_midi.c

    src/packages/game/util/IZ_midi.c
    src/packages/game/util/IZ_midi.h

    src/packages/game/input/input.test.c
)

add_executable(
  game-test-output
    dependencies/bdd-for-c/bdd-for-c.h
    src/packages/test/IZ_mock.h
    src/packages/test/IZ_test.h

    __mocks__/minIni.mock.h
    __mocks__/SDL_stdinc.mock.h
    __mocks__/SDL_render.mock.h

        src/packages/config/IZ_config.h

        src/packages/game/output/video/IZ_video.h
        src/packages/game/output/video/IZ_video.c
    src/packages/game/output/output.test.c
)

add_executable(
  game-test-memory
    dependencies/bdd-for-c/bdd-for-c.h
    src/packages/test/IZ_mock.h
    src/packages/test/IZ_test.h

    src/packages/game/memory/IZ_pool.h
    src/packages/game/memory/IZ_pool.c
    src/packages/game/memory/memory.test.c
    src/packages/game/data/IZ_list.c
    src/packages/game/data/IZ_list.h
)

add_executable(
  game-test-data
    dependencies/bdd-for-c/bdd-for-c.h
    src/packages/test/IZ_mock.h
    src/packages/test/IZ_test.h

    src/packages/game/data/IZ_list.h
    src/packages/game/data/IZ_list.c
    src/packages/game/data/data.test.c
)

add_executable(
  server
    dependencies/sqlite/sqlite3.h
    dependencies/sqlite/sqlite3.c
    dependencies/minIni/dev/minIni.h
    dependencies/minIni/dev/minIni.c
    src/packages/log/IZ_log.h
    src/packages/log/IZ_log.c
    src/packages/server/main.c
    src/packages/server/IZ_app.c
    src/packages/server/IZ_app.h
    src/packages/server/IZ_app_config.h
    src/packages/server/IZ_app_config.c
    src/packages/net/svc/IZ_wsserver.c
    src/packages/net/svc/IZ_wsserver.h
    src/packages/net/core/IZ_websocket.h
    src/packages/net/core/IZ_websocket.c
    src/packages/config/IZ_config.h
    src/packages/config/IZ_config.c
    src/packages/net/IZ_net_server.c
    src/packages/net/IZ_net_server.h
    src/packages/server/db/IZ_repo.c
    src/packages/server/db/IZ_repo.h
    src/packages/server/IZ_subsystem.h
)

add_executable(
  asset-inv
    dependencies/minIni/dev/minIni.h
    dependencies/minIni/dev/minIni.c
    src/packages/common/IZ_common.h
    src/packages/asset-inv/main.c
)

target_link_libraries(
  server
    SDL2main
    SDL2
    libcrypto
    libssl
    websockets
)

if (WIN32)
  add_custom_command(TARGET game POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:game>)                 # <--this is out-file path

  add_custom_command(TARGET game POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/lib/${PROJECT_ARCH}/SDL2_image.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:game>)                 # <--this is out-file path

  add_custom_command(TARGET game POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/lib/${PROJECT_ARCH}/SDL2_ttf.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:game>)                 # <--this is out-file path

  add_custom_command(TARGET game POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/portmidi/Release/portmidi.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:game>)                 # <--this is out-file path

  add_custom_command(TARGET game POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:game>)                 # <--this is out-file path

  add_custom_command(TARGET game POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:game>)                 # <--this is out-file path

  add_custom_command(TARGET game POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:game>)                 # <--this is out-file path

  add_custom_command(TARGET server POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:server>)                 # <--this is out-file path

  add_custom_command(TARGET server POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:server>)                 # <--this is out-file path

  add_custom_command(TARGET server POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:server>)                 # <--this is out-file path

  add_custom_command(TARGET server POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll"      # <--this is in-file
    $<TARGET_FILE_DIR:server>)                 # <--this is out-file path
endif ()

add_custom_command(TARGET game POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_directory  # which executes "cmake - E copy_if_different..."
  "${PROJECT_SOURCE_DIR}/assets"      # <--this is in-file
  $<TARGET_FILE_DIR:game>/assets)                 # <--this is out-file path

add_custom_command(TARGET asset-inv POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_directory  # which executes "cmake - E copy_if_different..."
  "${PROJECT_SOURCE_DIR}/assets"      # <--this is in-file
  $<TARGET_FILE_DIR:game>/assets)                 # <--this is out-file path
