camitk_application(NO_GUI
                   ADDITIONAL_SOURCES CommandLineOptions.cxx CommandLineOptions.hxx
                   CEP_NAME SDK
                   DESCRIPTION "Provides information about your CamiTK configuration and installation"
)

if(NOT APPLICATION_CONFIG)
    message(FATAL_ERROR "APPLICATION_CONFIG cannot be set to OFF, it is mandatory for any CamiTK installation")
else()
    # get more revision information
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CamiTKVersionInformation.h.in ${CMAKE_CURRENT_BINARY_DIR}/CamiTKVersionInformation.h @ONLY)
endif()

set(TEST_BASENAME ${APPLICATION_TARGET_NAME})
camitk_init_test(${TEST_BASENAME})

# should pass because invoking config without arguments or with help arg shows usage and exit success
camitk_add_test(PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")

camitk_add_test(EXECUTABLE_ARGS "--help" PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")

camitk_add_test(EXECUTABLE_ARGS "-h"     PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")

# test #4
# should pass because invoking camitk-config with a faulty arguments results in printing
# an error message (and exit failure)
camitk_add_test(EXECUTABLE_ARGS "-badarg" 
                PASS_REGULAR_EXPRESSION "unknown option '-badarg'"
                PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")

camitk_add_test(EXECUTABLE_ARGS "-e" 
                PASS_REGULAR_EXPRESSION "missing value for option '-e'"
                PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
                
camitk_add_test(EXECUTABLE_ARGS "-e badfile" 
                PASS_REGULAR_EXPRESSION "Error opening .camitk JSON file badfile"
                PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")

if(PYTHON_BINDING)
    # Test missing python script.
    # The following test must fails as the python scripts are not available in the directory
    # Copy a known camitk extension file
    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/testing)
    set(CAMITK_EXTENSION_FILE_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/extensiongenerator/testing/resources/multiActionsPython.camitk)
    set(CAMITK_EXTENSION_FILE_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/testing/multiActionsPython.camitk)
    # Add a custom command to copy the test file at build time
    add_custom_command(
    TARGET ${APPLICATION_TARGET_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
            ${CAMITK_EXTENSION_FILE_SOURCE}
            ${CAMITK_EXTENSION_FILE_DESTINATION}
    COMMENT "Copying camitk extension file to test for config test..."
    )
    camitk_add_test(EXECUTABLE_ARGS "-e ${CAMITK_EXTENSION_FILE_DESTINATION}"
                    # as no .py scripts where copied alongside the .camitk, python hotplug extension should fail
                    PASS_REGULAR_EXPRESSION "sdk/applications/config/testing/do_something_on_meshcomponent.py' not found"
                    PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
endif()

# Add config test
find_program (BASH_PROGRAM bash)
if(BASH_PROGRAM)
    add_test(NAME ${APPLICATION_TARGET_NAME}-bash-test 
             COMMAND ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/testing/config-test.sh -inbuild ${PROJECT_BINARY_DIR}
             WORKING_DIRECTORY ${CMAKE_BINARY_DIR} # needs to be at the top of the CamiTK module repository
    )
    # add test to this subproject (can be tested in local build dir using "ctest -L application-config")    
    set_tests_properties(${APPLICATION_TARGET_NAME}-bash-test PROPERTIES LABELS "${APPLICATION_TARGET_NAME}")
endif(BASH_PROGRAM)
