cmake_minimum_required(VERSION 4.1)
project(seagull)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)

find_package(Python3 REQUIRED COMPONENTS Interpreter)
set(sys_python ${Python3_EXECUTABLE})
find_package(Cargo)

find_program(RSYNC NAMES rsync)
find_program(ZIP NAMES zip)

file(GLOB_RECURSE pydepends RELATIVE "${CMAKE_SOURCE_DIR}"
     "${CMAKE_SOURCE_DIR}/app/*.py")
file(GLOB_RECURSE basepakdepends RELATIVE "${CMAKE_SOURCE_DIR}"
     "${CMAKE_SOURCE_DIR}/app/basepak/*")
file(GLOB_RECURSE pakargs RELATIVE "${CMAKE_SOURCE_DIR}/pak"
     "${CMAKE_SOURCE_DIR}/pak/*")
file(GLOB_RECURSE pakdepends RELATIVE "${CMAKE_SOURCE_DIR}"
     "${CMAKE_SOURCE_DIR}/pak/*")

file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/dist")
add_custom_target(pak ALL
                  DEPENDS ${pakdepends}
                  WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/pak"
                  COMMENT "Creating seagull.pak"
                  COMMAND ${ZIP} -7r "${CMAKE_CURRENT_BINARY_DIR}/dist/seagull.pak" .)

# wordlists
set(WORDLIST_SOURCE_PATH "${CMAKE_SOURCE_DIR}/ext/imsky/wordlists" CACHE PATH "Source location of wordlists to use.")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/basepak/rant")
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/basepak/rant/wordlist.rant"
                   COMMENT "Rendering wordlists"
                   COMMAND "${sys_python}" "${CMAKE_SOURCE_DIR}/render-wordlists.py" -i "${WORDLIST_SOURCE_PATH}" -o rant "${CMAKE_CURRENT_BINARY_DIR}/basepak/rant/wordlist.rant")

# build rant
set(rant_path "${CMAKE_CURRENT_BINARY_DIR}/opt/rant")
add_custom_command(OUTPUT "${rant_path}"
                   COMMENT "Compiling rant"
                   COMMAND "${CARGO_EXECUTABLE}" install rant --version 4.0.0-alpha.33 --root "${rant_path}" --features cli)

# venv
set(venv_path "${CMAKE_CURRENT_BINARY_DIR}/pyvenv")
execute_process(COMMAND "${Python3_EXECUTABLE}" -m venv "${venv_path}" --upgrade-deps
                COMMAND_ERROR_IS_FATAL ANY)
set(ENV{VIRTUAL_ENV} "${venv_path}")
set(Python3_FIND_VIRTUALENV ONLY)
unset(Python3_EXECUTABLE)
# re-find python to use venv
find_package(Python3 REQUIRED COMPONENTS Interpreter)
if (sys_python STREQUAL Python3_EXECUTABLE)
    message(FATAL_ERROR "Still using Python3 after venv activation")
endif()

execute_process(COMMAND "${Python3_EXECUTABLE}" -m pip install -r "${CMAKE_SOURCE_DIR}/app/requirements.txt")
execute_process(COMMAND "${Python3_EXECUTABLE}" -m pip install -r "${CMAKE_SOURCE_DIR}/app/requirements-desktop.txt")
execute_process(COMMAND "${Python3_EXECUTABLE}" -m pip install -r "${CMAKE_SOURCE_DIR}/app/requirements-build-desktop.txt")
execute_process(COMMAND "${Python3_EXECUTABLE}" -m pip install -r "${CMAKE_SOURCE_DIR}/app/requirements-desktop-linux.txt")

find_program(PYINSTALLER NAMES pyinstaller HINTS "${venv_path}/bin" REQUIRED)
mark_as_advanced(PYINSTALLER)

if (WIN32)
    set(binname "seagull.exe")
else()
    set(binname "seagull")
endif()

configure_file("seagull.spec.in" "seagull.spec")
add_custom_command(OUTPUT "dist/${binname}"
                   DEPENDS "${rant_path}" "${CMAKE_CURRENT_BINARY_DIR}/basepak/rant/wordlist.rant" ${pydepends} ${basepakdepends}
                   COMMENT "Building ${binname}"
                   BYPRODUCTS "pyinstaller"
                   COMMAND ${PYINSTALLER} --distpath "${CMAKE_CURRENT_BINARY_DIR}/dist" --workpath "${CMAKE_CURRENT_BINARY_DIR}/pyinstaller" seagull.spec)
add_custom_target(desktop ALL DEPENDS "dist/${binname}" pak)
add_custom_target(docker DEPENDS pak
                  DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/basepak/rant/wordlist.rant" ${basepakdepends}
                  COMMENT "Building Docker container"
                  COMMAND docker build --target dev -t seagull-game:latest ${CMAKE_SOURCE_DIR})