cmake_minimum_required(VERSION 3.10)
project(dropshell VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_C_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set default build type to Release if not specified
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build (Debug or Release)" FORCE)
endif()

# Configure build-specific compiler flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG")

# Configure version information
string(TIMESTAMP CURRENT_YEAR "%Y")
string(TIMESTAMP CURRENT_MONTH "%m")
string(TIMESTAMP CURRENT_DAY "%d")
string(TIMESTAMP CURRENT_HOUR "%H")
string(TIMESTAMP CURRENT_MINUTE "%M")
set(PROJECT_VERSION "${CURRENT_YEAR}.${CURRENT_MONTH}${CURRENT_DAY}.${CURRENT_HOUR}${CURRENT_MINUTE}")
string(TIMESTAMP RELEASE_DATE "%Y-%m-%d")

# Configure version.hpp file
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/src/version.hpp.in"
    "${CMAKE_CURRENT_BINARY_DIR}/src/autogen/version.hpp"
    @ONLY
)

# Set CMAKE_MODULE_PATH to include our custom find modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# Auto-detect source files
file(GLOB_RECURSE SOURCES "src/*.cpp")
file(GLOB_RECURSE HEADERS "src/*.hpp")

# Add custom target to run make_createagent.sh at the start of the build process
add_custom_target(run_createagent ALL
    COMMAND ${CMAKE_COMMAND} -E echo "Running make_createagent.sh..."
    COMMAND ${CMAKE_COMMAND} -E env bash ${CMAKE_CURRENT_SOURCE_DIR}/make_createagent.sh
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "Running make_createagent.sh before build"
)

# Add executable
add_executable(dropshell ${SOURCES})
add_dependencies(dropshell run_createagent)

# Mark the generated files as GENERATED so CMake knows they'll be created during build
set_source_files_properties(
    ${CMAKE_CURRENT_SOURCE_DIR}/src/autogen/_agent-remote.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/autogen/_agent-local.cpp
    PROPERTIES GENERATED TRUE
)

# Explicitly add the generated agent files, as they might not be in the source directory when globbed at the start.
target_sources(dropshell PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src/autogen/_agent-remote.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/autogen/_agent-local.cpp
)

# Set include directories
# build dir goes first so that we can use the generated version.hpp
target_include_directories(dropshell PRIVATE 
    $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src/autogen>
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/src/utils
    ${CMAKE_CURRENT_SOURCE_DIR}/src/contrib
    ${CMAKE_CURRENT_SOURCE_DIR}/src/commands
    ${CMAKE_CURRENT_SOURCE_DIR}/src/autogen
)

if(WIN32)
  add_custom_command(
    TARGET dropshell POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    $<TARGET_FILE_DIR:dropshell>
  )
endif()

# Link libraries
target_link_libraries(dropshell PRIVATE
)

# Install targets
install(TARGETS dropshell
    RUNTIME DESTINATION bin
)

# Create symbolic link 'ds' pointing to 'dropshell'
install(CODE "
    message(STATUS \"Checking if 'ds' command already exists...\")
    execute_process(
        COMMAND which ds
        RESULT_VARIABLE DS_NOT_EXISTS
        OUTPUT_QUIET
        ERROR_QUIET
    )
    if(DS_NOT_EXISTS)
        message(STATUS \"Command 'ds' does not exist. Creating symlink.\")
        execute_process(
            COMMAND ${CMAKE_COMMAND} -E create_symlink 
            \${CMAKE_INSTALL_PREFIX}/bin/dropshell
            \${CMAKE_INSTALL_PREFIX}/bin/ds
        )
    else()
        message(STATUS \"Command 'ds' already exists. Skipping symlink creation.\")
    endif()
")

# Install completion script
install(FILES src/dropshell-completion.bash
    DESTINATION /etc/bash_completion.d
    RENAME dropshell
)

# Create a symlink for the completion script to work with 'ds' command
install(CODE "
    # First check if 'ds' command exists after our installation
    execute_process(
        COMMAND which ds
        RESULT_VARIABLE DS_NOT_EXISTS
        OUTPUT_VARIABLE DS_PATH
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    
    # Only proceed if 'ds' exists
    if(NOT DS_NOT_EXISTS)
        # Check if 'ds' is a symlink pointing to dropshell
        execute_process(
            COMMAND readlink -f \${DS_PATH}
            RESULT_VARIABLE READLINK_FAILED
            OUTPUT_VARIABLE REAL_PATH
            ERROR_QUIET
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )
        
        # Get the path to our dropshell binary
        set(DROPSHELL_PATH \${CMAKE_INSTALL_PREFIX}/bin/dropshell)
        
        # Check if the real path is our dropshell binary
        if(NOT READLINK_FAILED AND \"\${REAL_PATH}\" STREQUAL \"\${DROPSHELL_PATH}\")
            message(STATUS \"Command 'ds' exists and points to dropshell. Creating completion script symlink.\")
            execute_process(
                COMMAND ${CMAKE_COMMAND} -E create_symlink 
                /etc/bash_completion.d/dropshell
                /etc/bash_completion.d/ds
            )
        else()
            message(STATUS \"Command 'ds' exists but doesn't point to dropshell. Skipping completion symlink.\")
        endif()
    else()
        message(STATUS \"Command 'ds' not found. Skipping completion symlink.\")
    endif()
")