# -*- conding: utf-8; cmake-tab-width: 4; indent-tabs-mode: nil; -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

macro(set_testing_path)
    if( WIN32 )
        foreach( dir ${NDS_CLIENT_LIBRARY_DIRS} )
            get_filename_component( dir "${dir}/../bin" ABSOLUTE )
            file( TO_NATIVE_PATH ${dir} dir )
            list( APPEND TPATH ${dir} )
        endforeach( )
        list( APPEND TPATH  $ENV{PATH} )
        string( REPLACE ";" "\\;" TPATH "${TPATH}" )
        # message( FATAL_ERROR "DEBUG: TPATH: ${TPATH}" )
    else( WIN32 )
        set(TPATH "$ENV{PATH}")
    endif( WIN32 )
endmacro()

if ( ENABLE_SWIG_JAVA AND ENABLE_SWIG_MATLAB )
    get_filename_component( NDS_JAR "${CMAKE_BINARY_DIR}/swig/java/module/nds2.jar" ABSOLUTE )

    set( MATLAB_TEST_FLAGS "" )
    set( MATLAB_TEST_DISPLAY_FLAGS "-nodisplay")
    if ( MATLAB_NO_NODISPLAY )
        set ( MATLAB_TEST_FLAGS "--no-nodisplay" )
        set ( MATLAB_TEST_DISPLAY_FLAGS "-nodesktop" "-nosplash" )
    endif ( MATLAB_NO_NODISPLAY )

    function(matlab_test name command)
        if ( ARGV2 )
            set( cmd_prefix ${ARGV2} )
        else ( ARGV2 )
            set( cmd_prefix ${CMAKE_CURRENT_SOURCE_DIR} )
        endif ( ARGV2 )
        if ( PROG_MATLAB )
            set_testing_path( )
            if (NOT IS_ABSOLUTE ${command})
                if ( EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${command}")
                    set( command "${CMAKE_CURRENT_BINARY_DIR}/${command}")
                elseif ( EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${command}")
                    set( command "${CMAKE_CURRENT_SOURCE_DIR}/${command}")
                endif ( EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${command}")
            endif (NOT IS_ABSOLUTE ${command})
            add_test(
                NAME ${name}
                COMMAND
                    "${PYTHON_DFLT_EXECUTABLE}" "${PROG_NDS_TESTER}" ${NDS_TESTER_FLAGS}
                    --matlab=${PROG_MATLAB} ${MATLAB_TEST_FLAGS}
                    --null-server
                    "${command}" )
            set( env 
                "PATH=${TPATH}"
                ${NDS_EXTEND_JAVA_BOOT_LIBRARY_PATH} )
            set_tests_properties( ${name}
                PROPERTIES
                    ENVIRONMENT "${env}"
                    SKIP_RETURN_CODE 77 )
            set( NDS_CUSTOM_MEMCHECK_IGNORE
                ${NDS_CUSTOM_MEMCHECK_IGNORE} ${name}
                CACHE INTERNAL "" )
        endif ( PROG_MATLAB )
    endfunction(matlab_test)

    function(mock_server_test name command)
        if ( PROG_MATLAB )
            set_testing_path( )
            add_test(
                NAME ${name}
                COMMAND
                    "${PYTHON_DFLT_EXECUTABLE}" "${PROG_NDS_TESTER}" ${NDS_TESTER_FLAGS}
                    --matlab=${PROG_MATLAB} ${MATLAB_TEST_FLAGS}
                    --nds1-mock-server
                    "${CMAKE_CURRENT_SOURCE_DIR}/${command}" )
            set( env 
                "PATH=${TPATH}"
                ${NDS_EXTEND_JAVA_BOOT_LIBRARY_PATH} )
            set_tests_properties( ${name}
                PROPERTIES
                    ENVIRONMENT "${env}"
                    SKIP_RETURN_CODE 77 )
            set( NDS_CUSTOM_MEMCHECK_IGNORE
                ${NDS_CUSTOM_MEMCHECK_IGNORE} ${name}
                CACHE INTERNAL "" )
        endif ( PROG_MATLAB )
    endfunction(mock_server_test)

    function(replay_server_test name command json)
        if ( PROG_MATLAB AND REPLAY_BLOB_CACHE_DIR )
            set_testing_path( )
            set_nds_protocols(protocols ${name})
            foreach ( proto ${protocols} )
                set(test_name "${name}${proto}_")
                add_test(
                    NAME ${test_name}
                    COMMAND
                        "${PYTHON_DFLT_EXECUTABLE}" "${PROG_NDS_TESTER}" ${NDS_TESTER_FLAGS}
                        --matlab=${PROG_MATLAB} ${MATLAB_TEST_FLAGS}
                        --replay-server
                        --replay-json-filename ${json}
                        --replay-protocol ${proto}
                        "${CMAKE_CURRENT_SOURCE_DIR}/${command}" ${ARGN} )
                set( env 
                    "PATH=${TPATH}"
                    ${NDS_EXTEND_JAVA_BOOT_LIBRARY_PATH}
                    "REPLAY_BLOB_REMOTE_CACHE=${REPLAY_BLOB_CACHE_DIR}"
                    )
                set_tests_properties( ${test_name}
                    PROPERTIES
                        ENVIRONMENT "${env}"
                        SKIP_RETURN_CODE 77 )
                set( NDS_CUSTOM_MEMCHECK_IGNORE
                    ${NDS_CUSTOM_MEMCHECK_IGNORE} ${test_name}
                    CACHE INTERNAL "" )
            endforeach ( proto )
        endif ( PROG_MATLAB AND REPLAY_BLOB_CACHE_DIR )
    endfunction(replay_server_test)

    if( PYTHON_DFLT_EXECUTABLE )
        configure_file(
            ${CMAKE_CURRENT_SOURCE_DIR}/test_nds2_version.m.in
            ${CMAKE_CURRENT_BINARY_DIR}/test_nds2_version.m
            @ONLY )

        #================================================================
        # These tests require a minimum of Python
        #================================================================
        # Final variable substitution before use
        #----------------------------------------------------------------
        string(CONFIGURE "${NDS_TESTER_FLAGS}" NDS_TESTER_FLAGS )
        #----------------------------------------------------------------
        matlab_test( _matlab_is-trend-type_
            test_is_trend_type.m )
        matlab_test( _matlab_version_
            test_nds2_version.m
            "${CMAKE_CURRENT_BINARY_DIR}" )
        #----------------------------------------------------------------
        mock_server_test( _matlab_mockup_ test_mockup.m )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_availability_
            test_nds2_availability.m
            nds2-availability.json )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds1_get-epochs_
            test_get_epochs.m
            nds1-get-epoch.json -proto-1 )
        replay_server_test(
            _matlab_nds2_get-epochs_
            test_get_epochs.m
            nds2-get-epoch.json -proto-2 )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_dissimilar-gaps_
            test_dissimilar_gaps.m
            nds2-dissimilar_gaps.json )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds1_iterate-with-gaps_
            test_iterate_with_gaps.m
            nds1-iterate_with_leading_gap.json
            -proto-1 )
        replay_server_test(
            _matlab_nds2_iterate-with-gaps_
            test_iterate_with_gaps.m
            nds2-iterate_with_leading_gap.json
            -proto-2 )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds1_iterate-with-synth-gaps_
            test_iterate_with_synth_gaps.m
            nds1-iterate-with-gaps-synthetic.json
            -proto-1
            )
        replay_server_test(
            _matlab_nds1_iterate-with-synth-gaps-nogaphandler_
            test_iterate_with_synth_gaps.m
            nds1-iterate-with-gaps-synthetic.json
            -proto-1 -no-gap
            )
        replay_server_test(
            _matlab_nds1_iterate-with-synth-gaps-defaultgaphandler_
            test_iterate_with_synth_gaps.m
            nds1-iterate-with-gaps-synthetic.json
            -proto-1 -default-gap-handling
            )
        #----------------------------------------------------------------
        replay_server_test(
                _matlab_nds1_restart_failure_
                test_nds1_restart_failure.m
                nds1-restart-failure.json
        )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_iterate-live-data-bounded_
            test_iterate_live_data_bounded.m
            nds2-iterate-live-data-bounded-synthetic.json
            -proto-2
            )
        replay_server_test(
            _matlab_nds1_iterate-live-data-bounded_
            test_iterate_live_data_bounded.m
            nds1-iterate-live-data-bounded-synthetic-multiconn.json
            -proto-1
            )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_iterate-live-data_
            test_iterate_live_data.m
            nds2-iterate-live-data-synthetic.json
            -proto-2
            )
        replay_server_test(
            _matlab_nds1_iterate-live-data_
            test_iterate_live_data.m
            nds1-iterate-live-data-synthetic.json
            -proto-1
            )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_iterate-all-data_
            test_iterate_all_data.m
            nds2-iterate-all-data-synthetic.json
            -proto-2
            )
        replay_server_test(
            _matlab_nds1_iterate-all-data_
            test_iterate_all_data.m
            nds1-iterate-all-data-synthetic.json
            -proto-1
            )
        replay_server_test(
            _matlab_nds2_iterate-all-data-nogaphandler_
            test_iterate_all_data.m
            nds2-iterate-all-data-synthetic.json
            -proto-2 -no-gap
            )
        replay_server_test(
            _matlab_nds1_iterate-all-data-nogaphandler_
            test_iterate_all_data.m
            nds1-iterate-all-data-synthetic.json
            -proto-1 -no-gap
            )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds1_count-channels_
            test_count_channels.m
            nds1-count-channels-synthetic.json
            -proto-1 )
        replay_server_test(
            _matlab_nds2_count-channels_
            test_count_channels.m
            nds2-count-channels-synthetic.json
            -proto-2 )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds1_timeout_
            test_nds1_timeout.m
            nds1-timeout.json )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_ticket-169_
            test_ticket_169.m
            nds2-ticket-169.json )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_ticket-246_
            test_ticket_246.m
            nds2-ticket-246.json )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds1_ticket-260_
            test_nds1_ticket_260.m
            nds1-ticket-260.json )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_ticket-288_
            test_ticket_288.m
            nds2-ticket-288.json )
        #----------------------------------------------------------------
        replay_server_test(
            _matlab_nds2_ticket-289_
            test_ticket_289.m
            nds2-ticket-289.json )
    endif( PYTHON_DFLT_EXECUTABLE )
endif ( ENABLE_SWIG_JAVA AND ENABLE_SWIG_MATLAB )
