# DOS utilities for mars-nwe.
#
# Default mode: install a prebuilt net.exe from this source tree. This keeps the
# normal mars-nwe build independent from Open Watcom.
#
# Maintainer mode: configure with -DMARS_NWE_BUILD_DOSUTILS=ON to rebuild
# net.exe with Open Watcom v2 on Linux.

set(MARS_DOSUTILS_NET_EXE
    "${CMAKE_CURRENT_SOURCE_DIR}/net.exe"
    CACHE FILEPATH "Prebuilt DOS net.exe used for installation when MARS_NWE_BUILD_DOSUTILS is OFF"
)

set(MARS_DOSUTILS_PUBLIC_TOOLS
    net
    login
    profile
    spawn
    passwd
    path
    pathins
    pathdel
    map
    mapdel
    logout
    slist
    flag
    capture
    endcap
)

# Do not install slist.exe yet: func_slist() is empty and SLIST is disabled in net.c.
# Do not install tests/debug by default either; they are developer-only helpers.

if(MARS_NWE_BUILD_DOSUTILS)
    find_package(OpenWatcom REQUIRED)

    set(DOSUTILS_C_SOURCES
        kern.c
        net.c
        tools.c
        netcall.c
        ncpcall.c
        login.c
        map.c
        slist.c
        flag.c
        nwcrypt.c
        nwdebug.c
        nwtests.c
        capture.c
    )

    add_custom_command(
        OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/kern.obj"
        COMMAND "${CMAKE_COMMAND}" -E env ${OPENWATCOM_ENV}
                "${OPENWATCOM_WASM}"
                -q
                -zq
                -fo="${CMAKE_CURRENT_BINARY_DIR}/kern.obj"
                "${CMAKE_CURRENT_SOURCE_DIR}/kern_wasm.asm"
        DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/kern_wasm.asm"
        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
        VERBATIM
    )

    add_custom_command(
        OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/net.exe"
        COMMAND "${CMAKE_COMMAND}" -E env ${OPENWATCOM_ENV}
                "${OPENWATCOM_WCL}"
                -q
                -zq
                -bt=dos
                -ml
                -0
                -k32768
                -fe="${CMAKE_CURRENT_BINARY_DIR}/net.exe"
                ${DOSUTILS_C_SOURCES}
                "${CMAKE_CURRENT_BINARY_DIR}/kern.obj"
        DEPENDS
                ${DOSUTILS_C_SOURCES}
                "${CMAKE_CURRENT_BINARY_DIR}/kern.obj"
                net.h
                kern.h
                nwcrypt.h
        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
        VERBATIM
    )

    add_custom_target(dosutils_net ALL
        DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/net.exe"
    )

    set(MARS_DOSUTILS_NET_EXE "${CMAKE_CURRENT_BINARY_DIR}/net.exe")
else()
    if(NOT EXISTS "${MARS_DOSUTILS_NET_EXE}")
        message(FATAL_ERROR
            "Prebuilt DOS utility missing: ${MARS_DOSUTILS_NET_EXE}. "
            "Either commit net.exe into dosutils or configure with "
            "-DMARS_NWE_BUILD_DOSUTILS=ON and Open Watcom installed."
        )
    endif()
endif()

foreach(tool IN LISTS MARS_DOSUTILS_PUBLIC_TOOLS)
    if(tool STREQUAL "net")
        install(FILES "${MARS_DOSUTILS_NET_EXE}"
            DESTINATION "${MARS_NWE_INSTALL_FULL_FILEDIR}/SYS/public")
    else()
        install(FILES "${MARS_DOSUTILS_NET_EXE}"
            DESTINATION "${MARS_NWE_INSTALL_FULL_FILEDIR}/SYS/public"
            RENAME "${tool}.exe")
    endif()
endforeach()

install(FILES "${MARS_DOSUTILS_NET_EXE}"
    DESTINATION "${MARS_NWE_INSTALL_FULL_FILEDIR}/SYS/login"
    RENAME login.exe)

install(FILES "${MARS_DOSUTILS_NET_EXE}"
    DESTINATION "${MARS_NWE_INSTALL_FULL_FILEDIR}/SYS/login"
    RENAME map.exe)

install(FILES "${MARS_DOSUTILS_NET_EXE}"
    DESTINATION "${MARS_NWE_INSTALL_FULL_FILEDIR}/SYS/login"
    RENAME slist.exe)

