#--------------------------------------------
#               CamiTK SDK
#
# You should probably NOT modify this file...
# Please edit CEPDescription.cmake instead
#--------------------------------------------
include(SDKConfig.cmake) 

# Configure CamiTK test coverage report generation (if and only if CAMITK_TEST_COVERAGE is set to TRUE)
include(camitk/test/CamiTKTestCoverage)

message(STATUS "Compilation build type: ${CMAKE_BUILD_TYPE}")
if (CAMITK_TEST_COVERAGE)
    message(STATUS "Test coverage: enabled")
else()
    message(STATUS "Test coverage: disabled")
endif()

# include global install of SDK
# This is a SDK build => global install is done in CMAKE_INSTALL_PREFIX
add_custom_target(camitk-ce-global-install
    COMMAND ${CMAKE_COMMAND} -E echo "Global Installation in ${CMAKE_INSTALL_PREFIX}"
    # First remove the WIN32 system runtime libraries dependencies
    COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_SYSTEM_RUNTIME_LIBS:STRING=" " ${CMAKE_BINARY_DIR}
    COMMAND ${CMAKE_COMMAND} -E echo "Configuration for ${CMAKE_INSTALL_PREFIX} done."
    # Second cmake to install
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target install --config ${CMAKE_CFG_INTDIR}
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target camitk-ce-copy-camitkdirtxt --config ${CMAKE_CFG_INTDIR}
    COMMAND ${CMAKE_COMMAND} -E echo "File installation done."
    COMMAND ${CMAKE_COMMAND} -E echo "Global installation in ${CMAKE_INSTALL_PREFIX} done."
    COMMENT "Global installation"
)

# The definition of SDK_TOP_LEVEL_SOURCE_DIR and SDK_TOP_LEVEL_BINARY_DIR are
# are needed to store the current source dir to pass as an argument to the ConfigureCamiTKDirTxt.cmake script in camitk-ce-copy-camitkdirtxt
set(SDK_TOP_LEVEL_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SDK_TOP_LEVEL_BINARY_DIR ${CMAKE_BINARY_DIR})
# specific top-level target that prepare and copy the CamiTKDir.txt file in ${CAMITK_USER_DIR}
add_custom_target(camitk-ce-copy-camitkdirtxt
    # prepare the CamiTKConfig.cmake for installation
    COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
                             -DCAMITK_USER_DIR=${CAMITK_USER_DIR}
                             -DSDK_TOP_LEVEL_SOURCE_DIR=${SDK_TOP_LEVEL_SOURCE_DIR}
                             -DSDK_TOP_LEVEL_BINARY_DIR=${SDK_TOP_LEVEL_BINARY_DIR}
                             -P ${CMAKE_CURRENT_SOURCE_DIR}/ConfigureCamiTKDirTxt.cmake
    # Make sure the user dir exists
    COMMAND ${CMAKE_COMMAND} -E make_directory "${CAMITK_USER_DIR}"
    # Remove the previous version
    COMMAND ${CMAKE_COMMAND} -E remove -f ${CAMITK_USER_DIR}/CamiTKDir.txt
    # Copy the CamiTKDir.txt file in the ${CMAKE_USER_DIR} directory
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/CamiTKDir.txt ${CAMITK_USER_DIR}
    COMMAND ${CMAKE_COMMAND} -E echo "CamiTKDir.txt [${CMAKE_INSTALL_PREFIX}] copied in ${CAMITK_USER_DIR}"
)

# camitk-ce-local-install == install in User Config directory
add_custom_target(camitk-ce-local-install
    COMMAND ${CMAKE_COMMAND} -E echo "Local Installation in ${CAMITK_USER_DIR}"
    # First cmake to redefine install prefix
    COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=${CAMITK_USER_DIR} ${CMAKE_BINARY_DIR}
    COMMAND ${CMAKE_COMMAND} -E echo "Configuration for ${CMAKE_INSTALL_PREFIX} done."    
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target install --config ${CMAKE_CFG_INTDIR}
    COMMAND ${CMAKE_COMMAND} -E echo "File installation done."
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target camitk-ce-copy-camitkdirtxt --config ${CMAKE_CFG_INTDIR}
    COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} ${CMAKE_BINARY_DIR}
    COMMAND ${CMAKE_COMMAND} -E echo "Local Installation in ${CAMITK_USER_DIR} done."
    COMMENT "Local installation"
)

# camitk-ce-update-translate == configure all the .qrc files for the different extensions of the SDK.
# .qrc files consider up to date .ts resources files for translation
add_custom_target(camitk-ce-update-translate
    COMMAND ${CMAKE_COMMAND} -E echo "Updating translation for CamiTK Community Edition"
    COMMAND ${CMAKE_COMMAND} -DCAMITK_TRANSLATE=TRUE ${CMAKE_BINARY_DIR}
    COMMAND ${CMAKE_COMMAND} -DCAMITK_TRANSLATE=FALSE ${CMAKE_BINARY_DIR}
    COMMAND ${CMAKE_COMMAND} -E echo "Translation updated for CamiTK Community Edition"    
)

# make sure a copy of the CamiTKDir.txt file is written to ${CAMITK_USER_DIR}
# even if the user is just invoking the "install" target (from the IDE or command line)
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target camitk-ce-copy-camitkdirtxt)")

# include CamiTK auto-documentation generation
include(CamiTKApiDoc)

# python binding option
option(PYTHON_BINDING "Generate CamiTK Python Module" OFF)
if (PYTHON_BINDING)
    add_definitions(-DPYTHON_BINDING)
    message(STATUS "Generating CamiTK Python Module")
endif()

# initialize subproject management
camitk_init_manifest_data()

# initialize automatic tests parsing
camitk_parse_test_init()

# Try to find camitk-extensiongenerator binary in usual/recommanded path/variable/user directories
find_program(CAMITK_EXTENSION_GENERATOR_EXECUTABLE
    NAMES camitk-extensiongenerator-debug camitk-extensiongenerator
    PATH_SUFFIXES "bin"
    PATHS
        #-- 0. from CAMITK_DIR environment or cache variable
        "${CAMITK_DIR}"
        "$ENV{CAMITK_DIR}"

        #-- 1. Build path
        # Read from the CMakeSetup registry entries.  It is likely that
        # CamiTK will have been recently built.
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild1]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild2]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild3]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild4]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild5]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild6]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild7]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild8]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild9]
        [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild10]

        # For camitk communityedition compilation
        "${CMAKE_BINARY_DIR}"

        #-- 2. installed path
        # Unix/MacOS
        "/usr"
        "/usr/local"
        "/opt/CamiTK"
        "$ENV{HOME}/Dev/CamiTK"
        "$ENV{HOME}/Dev/camitk"
        
        # Windows
        "C:/Programs "
        "C:/Programs/CamiTK"
        "C:/Program Files/CamiTK"
        "C:/Programs/camitk"
        "C:/Program Files/camitk"
        "C:/Dev"
        "C:/Dev/CamiTK"
        "C:/Dev/camitk"

        # From last installation
        "${FIND_CAMITK_USER_DIR_LAST_INSTALL}"
)

if (NOT CAMITK_EXTENSION_GENERATOR_EXECUTABLE)
    # Fist build extension generator during the build stage so that it can be
    # reused during the normal phase.
    # see https://stackoverflow.com/a/36084786
    if (BUILD_EXTENSION_GENERATOR)
            # build the library and executable
            add_subdirectory(libraries/extensiongenerator)
            add_subdirectory(applications/extensiongenerator)   

            # and that's it, stop processing here
            return()
    endif()

    message("-- First configure: building camitk-extensiongenerator...")

    # Nested CMake run.
    execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
    -DBUILD_EXTENSION_GENERATOR=ON
    -DVTK_DIR=${VTK_DIR}
    -DCAMITK_HOST_MULTIARCH=${CMAKE_LIBRARY_PATH}
    ${CMAKE_SOURCE_DIR}
    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")

    # Build extensiongenerator lib and app
    execute_process(COMMAND ${CMAKE_COMMAND} --build . --target library-camitkextensiongenerator application-extensiongenerator
    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")

else()
    message("-- camitk-extensiongenerator already exists. Skipping first build.")
endif()


# add all subprojects, in dependencies order
#
# Note: testapplications are easier to manage if they are in a separate directory
# As testapplications are required to test actions and components but depends on library core,
# it is easier to keep them in a separate (specific) folder.
# -> dependencies are satisfied (thanks to camitk_extension macros)
# -> parallel build is not broken
camitk_add_subdirectory(libraries)
camitk_add_subdirectory(viewers)
camitk_add_subdirectory(testapplications)
camitk_add_subdirectory(components)
camitk_add_subdirectory(actions)
camitk_add_subdirectory(applications)
camitk_add_subdirectory(cmake) # for cmake module installation

# Validate project.xml file for CDash
camitk_write_manifest_data()

# Validate CamiTKTestsLevel.txt file for wiki
camitk_parse_test_validate()
