cmake_minimum_required(VERSION 3.19)

if(NOT DEFINED VERSION)
	execute_process(
		COMMAND git describe --tags --abbrev=0
		WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
		OUTPUT_VARIABLE GIT_LAST_TAG
		OUTPUT_STRIP_TRAILING_WHITESPACE
		ERROR_QUIET
	)

	if (GIT_LAST_TAG)
		string(REGEX REPLACE "^v" "" GIT_LAST_TAG "${GIT_LAST_TAG}")

		string(REPLACE "." ";" VERSION_LIST "${GIT_LAST_TAG}")
		list(GET VERSION_LIST 0 VERSION_MAJOR)
		list(GET VERSION_LIST 1 VERSION_MINOR)

		execute_process(
			COMMAND git rev-list v${VERSION_MAJOR}.${VERSION_MINOR}.0..HEAD --count
			WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
			OUTPUT_VARIABLE VERSION_PATCH
			OUTPUT_STRIP_TRAILING_WHITESPACE
			ERROR_QUIET
		)
	endif()
endif()

if(NOT DEFINED GIT_REVISION)
	execute_process(COMMAND git rev-parse HEAD
								WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
								OUTPUT_VARIABLE GIT_REVISION
								OUTPUT_STRIP_TRAILING_WHITESPACE
				)

	if("${GIT_REVISION}" STREQUAL "")
		message(FATAL_ERROR "GIT_REVISION is not set and failed to get from git")
	endif()
endif()

set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")

project(ZShell VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
message(STATUS "ZShell version: ${VERSION}")

include(GNUInstallDirs)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(ENABLE_MODULES "plugin;shell;cli;m3shapes" CACHE STRING "Modules to build/install")

set(INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/ZShell" CACHE STRING "Library install dir")
set(INSTALL_QMLDIR "${CMAKE_INSTALL_LIBDIR}/qt6/qml" CACHE STRING "QML install dir")
set(INSTALL_QSCONFDIR "${CMAKE_INSTALL_SYSCONFDIR}/xdg/quickshell/zshell" CACHE STRING "Quickshell config install dir")
set(INSTALL_GREETERCONFDIR "${CMAKE_INSTALL_SYSCONFDIR}/xdg/quickshell/zshell-greeter" CACHE STRING "Quickshell greeter install dir")

set(CMAKE_INSTALL_MESSAGE NEVER)

add_compile_options(
	-Wall -Wextra -Wpedantic -Wshadow -Wconversion
	-Wold-style-cast -Wnull-dereference -Wdouble-promotion
	-Wformat=2 -Wfloat-equal -Woverloaded-virtual
	-Wsign-conversion -Wredundant-decls -Wswitch
	-Wunreachable-code
)


if("cli" IN_LIST ENABLE_MODULES)
	# Nuitka compilation
	set(ZSHELL_CLI_BUILD_DIR "${CMAKE_BINARY_DIR}/zshell-cli")
	set(ZSHELL_CLI_DIST "${ZSHELL_CLI_BUILD_DIR}/zshell.dist")
	set(ZSHELL_CLI_SRC "${CMAKE_SOURCE_DIR}/cli/src/zshell")

	find_program(NUITKA_EXECUTABLE nuitka)

	if(NUITKA_EXECUTABLE)
		file(GLOB_RECURSE ZSHELL_CLI_SOURCES CONFIGURE_DEPENDS
			"${ZSHELL_CLI_SRC}/*.py"
		)
		file(GLOB_RECURSE ZSHELL_CLI_ASSETS CONFIGURE_DEPENDS
			"${ZSHELL_CLI_SRC}/assets/*"
		)

		add_custom_command(
			OUTPUT "${ZSHELL_CLI_DIST}/zshell-cli"
			COMMAND ${CMAKE_COMMAND} -E make_directory "${ZSHELL_CLI_BUILD_DIR}"
			COMMAND ${CMAKE_COMMAND} -E rm -rf "${ZSHELL_CLI_DIST}"

			COMMAND
				${NUITKA_EXECUTABLE}
				--standalone
				--include-data-dir=${CMAKE_SOURCE_DIR}/cli/src/zshell/assets=zshell/assets
				--include-package=gi.overrides
				--include-package-data=solaar
				--output-dir=${ZSHELL_CLI_BUILD_DIR}
				--output-filename=zshell-cli
				${CMAKE_SOURCE_DIR}/cli/src/zshell/

			WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cli
			DEPENDS ${ZSHELL_CLI_SOURCES} ${ZSHELL_CLI_ASSETS}
		)

		add_custom_target(zshell-cli ALL DEPENDS "${ZSHELL_CLI_DIST}/zshell-cli")

		install(PROGRAMS "${ZSHELL_CLI_DIST}/zshell-cli" DESTINATION "${INSTALL_LIBDIR}/zshell-cli")
		install(DIRECTORY "${ZSHELL_CLI_DIST}/" DESTINATION "${INSTALL_LIBDIR}/zshell-cli" PATTERN "zshell-cli" EXCLUDE)

		configure_file(
			"${CMAKE_SOURCE_DIR}/Plugins/cmake/zshell-cli.cmake.in"
			"${CMAKE_BINARY_DIR}/Plugins/cmake/zshell-cli.cmake"
			@ONLY
		)
		install(SCRIPT "${CMAKE_BINARY_DIR}/Plugins/cmake/zshell-cli.cmake")
	else()
		message(STATUS "Nuitka not found, building zshell-cli as a Python wheel instead")

		find_package(Python3 COMPONENTS Interpreter REQUIRED)

		set(ZSHELL_CLI_DIR "${CMAKE_SOURCE_DIR}")
		set(ZSHELL_CLI_DIST_DIR "${ZSHELL_CLI_DIR}/dist")
		set(ZSHELL_CLI_WHEEL_STAMP "${CMAKE_BINARY_DIR}/zshell-cli/wheel.stamp")

		file(GLOB_RECURSE ZSHELL_CLI_SOURCES CONFIGURE_DEPENDS
			"${ZSHELL_CLI_DIR}/src/zshell/*.py"
		)
		file(GLOB_RECURSE ZSHELL_CLI_ASSETS CONFIGURE_DEPENDS
			"${ZSHELL_CLI_DIR}/src/zshell/assets/*"
		)

		add_custom_command(
			OUTPUT "${ZSHELL_CLI_WHEEL_STAMP}"
			COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/zshell-cli"
			COMMAND ${CMAKE_COMMAND} -E rm -rf "${ZSHELL_CLI_DIST_DIR}"
			COMMAND ${Python3_EXECUTABLE} -m build --wheel --no-isolation
			COMMAND ${CMAKE_COMMAND} -E touch "${ZSHELL_CLI_WHEEL_STAMP}"
			WORKING_DIRECTORY "${ZSHELL_CLI_DIR}"
			DEPENDS ${ZSHELL_CLI_SOURCES} ${ZSHELL_CLI_ASSETS}
			COMMENT "Building zshell-cli python wheel"
		)

		add_custom_target(zshell-cli ALL DEPENDS "${ZSHELL_CLI_WHEEL_STAMP}")

		configure_file(
			"${CMAKE_SOURCE_DIR}/Plugins/cmake/zshell-cli-python.cmake.in"
			"${CMAKE_BINARY_DIR}/Plugins/cmake/zshell-cli-python.cmake"
			@ONLY
		)
		install(SCRIPT "${CMAKE_BINARY_DIR}/Plugins/cmake/zshell-cli-python.cmake")
	endif()
endif()

if("plugin" IN_LIST ENABLE_MODULES)
	add_subdirectory(Plugins)
endif()

if("shell" IN_LIST ENABLE_MODULES)
	foreach(dir assets scripts Components Services Modules Daemons Drawers Effects Helpers Paths)
		install(DIRECTORY ${dir} DESTINATION "${INSTALL_QSCONFDIR}")
	endforeach()

	# Disable watching for changes
	file(READ shell.qml SHELL_QML)
	string(REPLACE "settings.watchFiles: true" "settings.watchFiles: false" SHELL_QML "${SHELL_QML}")
	file(WRITE "${CMAKE_BINARY_DIR}/qml/shell.qml" "${SHELL_QML}")
	install(FILES "${CMAKE_BINARY_DIR}/qml/shell.qml" DESTINATION "${INSTALL_QSCONFDIR}")

	# Greeter
	install(DIRECTORY Greeter/ DESTINATION "${INSTALL_GREETERCONFDIR}")
endif()

if("m3shapes" IN_LIST ENABLE_MODULES)
	message(STATUS "Fetching M3Shapes module")
	include(FetchContent)
	set(M3SHAPES_REV bdc327b29f95394a732baf3c9b19658ba23755b6)
	FetchContent_Declare(
		m3shapes_external
		GIT_REPOSITORY https://github.com/soramanew/m3shapes.git
		GIT_TAG        ${M3SHAPES_REV}
		SOURCE_DIR     "${CMAKE_BINARY_DIR}/_deps/m3shapes-${M3SHAPES_REV}"
	)
	FetchContent_MakeAvailable(m3shapes_external)
	message(STATUS "Done fetching M3Shapes module")

	# Fix m3shapes wrong rpath
	if(TARGET m3shapesplugin)
		set_target_properties(m3shapesplugin PROPERTIES INSTALL_RPATH "$ORIGIN")
	endif()
endif()
