option(DETHRACE_REPLAY_DEBUG "Debug replay" OFF)

# Create our main game binary.
if(MSVC_42_FOR_RECCMP)
    add_executable(dethrace WIN32)

    set(CMAKE_C_FLAGS_DEBUG "/Oi /Zi /MLd /G5 /w")

    add_compile_definitions(_DEBUG)
    set_target_properties(dethrace PROPERTIES OUTPUT_NAME "CARM95")
    target_link_options(dethrace PRIVATE /INCREMENTAL:NO /subsystem:windows /ENTRY:mainCRTStartup)
    target_link_libraries(dethrace PRIVATE ws2_32)

    file(GLOB DETHRACE_COMMON_C_FILES "${CMAKE_CURRENT_SOURCE_DIR}/common/*.c")
    set_source_files_properties(${DETHRACE_COMMON_C_FILES} PROPERTIES COMPILE_FLAGS "/FAs")

    reccmp_add_target(dethrace ID CARM95)
    reccmp_configure()
else()
    add_executable(dethrace
        WIN32
        ${CMAKE_SOURCE_DIR}/packaging/macos/dethrace.icns
        ${CMAKE_SOURCE_DIR}/packaging/windows/dethrace.rc
    )
    get_property(build_rpaths GLOBAL PROPERTY DETHRACE_BUILD_RPATHS)
    set_property(TARGET dethrace APPEND PROPERTY BUILD_RPATH "${build_rpaths}")

    if(MSVC)
        target_link_libraries(dethrace PRIVATE dbghelp)
        target_link_options(dethrace PRIVATE /subsystem:windows /ENTRY:mainCRTStartup)
        target_compile_definitions(dethrace PRIVATE -D_CRT_SECURE_NO_WARNINGS -DSDL_MAIN_HANDLED -DWIN32_LEAN_AND_MEAN)
    endif()
endif()

target_include_directories(dethrace
    PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
    common
    pd
)

if(DETHRACE_ASAN)
    target_compile_options(dethrace PUBLIC -fsanitize=address)
    target_link_options(dethrace PUBLIC -fsanitize=address)
endif()

target_link_libraries(dethrace PRIVATE smackw32 harness BRender::Full BRender::DDI s3 compile_with_werror)

if(MSVC_42_FOR_RECCMP)
# do nothing
elseif(MSVC)
    target_compile_definitions(dethrace PRIVATE -D_CRT_SECURE_NO_WARNINGS)
    target_compile_options(dethrace PRIVATE
        /wd4101
        /wd4996
    )
else()
    target_compile_options(dethrace PRIVATE
        $<$<COMPILE_LANGUAGE:C,CXX>:-Wall>
    )
    add_compile_flags_if_supported(dethrace
        -Wformat
        -Wno-format-overflow
        -Wno-unused-but-set-variable
        -Wno-unused-variable
        -Wno-unused-result
        -Wno-absolute-value
        -Wstrict-prototypes
    )
endif()

target_compile_definitions(dethrace PRIVATE INSIDE_DETHRACE)

if(DETHRACE_FIX_BUGS)
    target_compile_definitions(dethrace PRIVATE DETHRACE_FIX_BUGS)
endif()

if(DETHRACE_3DFX_PATCH)
    target_compile_definitions(dethrace PRIVATE DETHRACE_3DFX_PATCH)
endif()

if(IS_BIGENDIAN)
    target_compile_definitions(dethrace PRIVATE BR_ENDIAN_BIG=1)
else()
    target_compile_definitions(dethrace PRIVATE BR_ENDIAN_LITTLE=1)
endif()

if(DETHRACE_REPLAY_DEBUG)
    target_compile_definitions(dethrace PRIVATE DETHRACE_REPLAY_DEBUG)
endif()

if(MINGW)
    target_compile_options(dethrace PRIVATE -Wno-error=incompatible-pointer-types)
endif()

target_sources(dethrace PRIVATE
    common/skidmark.c
    common/opponent.c
    common/oil.c
    common/racesumm.c
    common/replay.c
    common/loading.c
    common/piping.c
    common/powerup.c
    common/netgame.c
    common/world.c
    common/network.c

    common/brucetrk.c
    common/brucetrk.h
    common/car.c
    common/car.h
    common/controls.c
    common/controls.h
    common/crush.c
    common/crush.h
    common/cutscene.c
    common/cutscene.h
    common/demo.c
    common/demo.h
    common/depth.c
    common/depth.h
    common/displays.c
    common/displays.h
    common/drfile.c
    common/drfile.h
    common/drmem.c
    common/drmem.h
    common/errors.c
    common/errors.h
    common/finteray.c
    common/finteray.h
    common/flicplay.c
    common/flicplay.h
    common/globvars.c
    common/globvars.h
    common/globvrbm.c
    common/globvrbm.h
    common/globvrkm.c
    common/globvrkm.h
    common/globvrme.c
    common/globvrme.h
    common/globvrpb.c
    common/globvrpb.h
    common/grafdata.c
    common/grafdata.h
    common/graphics.c
    common/graphics.h
    common/init.c
    common/init.h
    common/input.c
    common/input.h
    common/intrface.c
    common/intrface.h
    common/loadsave.c
    common/loadsave.h
    common/main.c
    common/main.h
    common/mainloop.c
    common/mainloop.h
    common/mainmenu.c
    common/mainmenu.h

    common/newgame.c
    common/newgame.h
    common/oppocar.c
    common/oppocar.h
    common/oppoproc.c
    common/oppoproc.h
    common/options.c
    common/options.h
    common/pedestrn.c
    common/pedestrn.h
    common/pratcam.c
    common/pratcam.h
    common/racestrt.c
    common/racestrt.h
    common/raycast.c
    common/raycast.h

    common/sound.c
    common/sound.h
    common/spark.c
    common/spark.h
    common/structur.c
    common/structur.h
    common/trig.c
    common/trig.h
    common/utility.c
    common/utility.h

    constants.h
    dr_types.h
    pd/net.h
    pd/sys.h

    pc-all/allsys.c

    # original win95 sys and network
    # pc-win95/win95sys.c
    # pc-win95/ssdx.c
    # pc-win95/ssdx.h

    # original dos sys and network
    # pc-dos/dosnet.c
    # pc-dos/dossys.c
)

if(MSVC_42_FOR_RECCMP)
    target_sources(dethrace PRIVATE pc-win95/win95net.c)
else()
    # dethrace-added cross platform sys and network implementation
    target_sources(dethrace PRIVATE pc-all/allnet.c)
endif()
target_sources(dethrace PRIVATE main.c)

if(DETHRACE_IDE_ROOT_DIR)
    set_target_properties(dethrace PROPERTIES
        VS_DEBUGGER_ENVIRONMENT "DETHRACE_ROOT_DIR=${DETHRACE_IDE_ROOT_DIR}"
    )
endif()

if(DETHRACE_IDE_ARGUMENTS)
    message(STATUS "before: ${DETHRACE_IDE_ARGUMENTS}")
    separate_arguments(DETHRACE_IDE_ARGUMENTS NATIVE_COMMAND "${DETHRACE_IDE_ARGUMENTS}")
    message(STATUS "after: ${DETHRACE_IDE_ARGUMENTS}")
    set_target_properties(dethrace PROPERTIES
        VS_DEBUGGER_COMMAND_ARGUMENTS "${DETHRACE_IDE_ARGUMENTS}"
    )
endif()

if(DETHRACE_INSTALL)
    if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND DETHRACE_USE_LSB_GAMES)
        install(TARGETS dethrace RUNTIME DESTINATION games)
    else()
        install(TARGETS dethrace
            BUNDLE DESTINATION . COMPONENT Runtime
            RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT Runtime
        )
    endif()
    if(APPLE)
        set_target_properties(dethrace PROPERTIES
            BUNDLE True
            MACOSX_BUNDLE True
            MACOSX_BUNDLE_DISPLAY_NAME "Deth Race"
            OUTPUT_NAME "Deth Race"
            MACOSX_BUNDLE_BUNDLE_VERSION ${DETHRACE_VERSION}
            MACOSX_BUNDLE_SHORT_VERSION_STRING ${DETHRACE_VERSION}
            MACOSX_BUNDLE_ICON_FILE dethrace.icns
        )
        set_source_files_properties(${CMAKE_SOURCE_DIR}/packaging/macos/dethrace.icns PROPERTIES
            MACOSX_PACKAGE_LOCATION "Resources"
        )
        target_sources(dethrace PRIVATE
            ${CMAKE_SOURCE_DIR}/packaging/macos/dethrace.icns
        )
    endif()

    install(TARGETS dethrace
        BUNDLE DESTINATION . COMPONENT Runtime
        RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT Runtime
    )

    if(APPLE)
        if(DETHRACE_PLATFORM_SDL_DYNAMIC)
            if(DETHRACE_PLATFORM_SDL2)
                install(CODE "
                    set(app_root \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}\")
                    set(app \"\${app_root}/Deth Race.app\")
                    if(NOT EXISTS \"\${app}\")
                        set(app \"\${app_root}/${CMAKE_INSTALL_BINDIR}/Deth Race.app\")
                    endif()
                    file(INSTALL
                        DESTINATION \"\${app}/Contents/MacOS\"
                        TYPE FILE
                        FOLLOW_SYMLINK_CHAIN
                        FILES \"$<TARGET_FILE:SDL2::SDL2>\"
                    )
                    "
                    COMPONENT Runtime
                )
            endif()

            if(DETHRACE_PLATFORM_SDL3)
                install(CODE "
                    set(app_root \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}\")
                    set(app \"\${app_root}/Deth Race.app\")
                    if(NOT EXISTS \"\${app}\")
                        set(app \"\${app_root}/${CMAKE_INSTALL_BINDIR}/Deth Race.app\")
                    endif()
                    file(INSTALL
                        DESTINATION \"\${app}/Contents/MacOS\"
                        TYPE FILE
                        FOLLOW_SYMLINK_CHAIN
                        FILES \"$<TARGET_FILE:SDL3::SDL3>\"
                    )
                    file(CREATE_LINK
                        \"libSDL3.0.dylib\"
                        \"\${app}/Contents/MacOS/libSDL3.dylib\"
                        SYMBOLIC
                    )
                    "
                    COMPONENT Runtime
                )
            endif()
        endif()

        install(CODE "
            include(BundleUtilities)
            set(app_root \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}\")
            set(app \"\${app_root}/Deth Race.app\")
            if(NOT EXISTS \"\${app}\")
                set(app \"\${app_root}/${CMAKE_INSTALL_BINDIR}/Deth Race.app\")
            endif()
            fixup_bundle(\"\${app}\" \"\" \"/Library/Frameworks\")
            execute_process(COMMAND
                codesign -s - -f --deep \"\${app}\"
            )
            "
            COMPONENT Runtime
        )
    endif()

    if(MSVC)
        install(FILES $<TARGET_PDB_FILE:dethrace>
            DESTINATION "."
            OPTIONAL
        )

        if(DETHRACE_PLATFORM_SDL_DYNAMIC)
            if(DETHRACE_PLATFORM_SDL1)
                get_filename_component(sdl1_lib_dir "${SDL_LIBRARY}" DIRECTORY)
                get_filename_component(sdl1_root_dir "${SDL_LIBRARY}" PATH)
                set(sdl1_bin_dir "${sdl1_root_dir}/bin" PATH)
                find_file(SDL1DLL_PATH NAMES "SDL.dll" PATHS ${sdl1_lib_dir} ${sdl1_root_dir} PATH_SUFFIXES "bin" REQUIRED)

                if(SDL1DLL_PATH)
                    install(FILES "${SDL1DLL_PATH}"
                        DESTINATION "."
                        OPTIONAL
                    )
                endif()
            endif()

            if(DETHRACE_PLATFORM_SDL2)
                install(FILES "$<TARGET_FILE:SDL2::SDL2>"
                    DESTINATION "."
                    OPTIONAL
                )
            endif()

            if(DETHRACE_PLATFORM_SDL3)
                install(FILES "$<TARGET_FILE:SDL3::SDL3>"
                    DESTINATION "."
                    OPTIONAL
                )
            endif()
        endif()
    endif()
endif()
