find_program( GRIB_COMPARE grib_compare )
find_program( GRIB_LS      grib_ls )

if( NOT GRIB_COMPARE AND TARGET grib_compare )
    set( GRIB_COMPARE "$<TARGET_FILE:grib_compare>" )
    set( GRIB_COMPARE "$<TARGET_FILE:grib_ls>" )
endif()

ecbuild_add_test(
    TARGET test_${V}_${T}
    ENABLED OFF
    TYPE SCRIPT
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test_${V}
    ARGS ${CMAKE_CURRENT_SOURCE_DIR}/${V}/${R}
)

if( GRIB_COMPARE )
    message( STATUS "tests will use grib_compare: ${GRIB_COMPARE}" )
else()
    message( WARNING "Could not find grib_compare executable -- turning off ${PROJECT_NAME} tests!\nTo enable them place 'grib_compare' on the PATH and re-run cmake" )
endif()

# define the grib environment

set( grib_handling_pkg eccodes )

set( GRIB_DEFINITION_PATH ${eccodes_BASE_DIR}/share/eccodes/definitions/ )
set( GRIB_SAMPLES_PATH    ${eccodes_BASE_DIR}/share/eccodes/samples/ )

set( mars_new_binary "$<TARGET_FILE:mars.bin>" )

ecbuild_configure_file( run_mars.sh.in ${CMAKE_CURRENT_BINARY_DIR}/run_mars.sh @ONLY )

# mars ems permissions
set( USER $ENV{USER} )
ecbuild_configure_file( ems_cache_prod.in ${CMAKE_CURRENT_BINARY_DIR}/ems_cache_prod @ONLY )

#### TESTS

# To add a test, just add the mars request with a .req extension
# and make sure the target=testdata which automatically gets substituted to testdata.prod and testdata.test

list( APPEND _test_reqs
compute.req
interpolation_wave.req
interpolation_gaussian.req
interpolation_l137.req
interpolation_rgg2ll.req
#interpolation_t1279_gg.req  # Creates very large coefficient file
#interpolation_t1279_sh.req  # Creates very large coefficient file
#interpolation_rotated.req   # Failing in ECMWF bundle with Intel
# interpolation_latlon.req   # comparison data no longer correct
# interpolation_sh2ll.req    # comparison data no longer correct
transform_vod2uv.req
retrieve_oper.req
retrieve_enfo.req
#retrieve_ocean.req  # MIR doesn't support GRIB localDefinitionNumber=4
retrieve_fdb_uv_pl.req
retrieve_fdb_uv_ml.req
# retrieve_ai.req
# retrieve_tigge.req
MARSC-94.req
# MARSC-100.001.req
# MARSC-100.002.req
)

if(HAVE_ODB)
    list( APPEND _test_reqs
        #odb_filter_updated.req       # As a result of ODB-159 the default codec has changed from short_real to short_real2
        odb_filter_odc.req            # ODC removed the unused Flags entries
        )
endif()

if(eccodes_HAVE_JPG)
    list( APPEND _test_reqs MARSC-86.req ) # contains jpeg-encoded data
endif()

foreach( t ${_test_reqs} )
    list( APPEND _test_data ${t}.refdata.mir ${t}.inpdata )
endforeach()

ecbuild_get_test_multidata( TARGET get_configs NAMES mars-client-baseconfigs.tar.gz:b7a2b3d3b159813d0b63dc706cc7ed76 EXTRACT )

ecbuild_get_test_multidata( TARGET get_data NAMES ${_test_data} )

ecbuild_add_resources( TARGET tests_run_mars SOURCES_PACK ${_test_reqs} )

foreach( _req ${_test_reqs} )

    string(REGEX REPLACE "[^A-Za-z0-9]" "_" _tst ${_req})

    ecbuild_add_test(
        TARGET test_${_tst}
        TYPE SCRIPT
        COMMAND ${CMAKE_CURRENT_BINARY_DIR}/run_mars.sh
        ARGS ${CMAKE_CURRENT_SOURCE_DIR}/${_req}
        TEST_DEPENDS get_data get_configs
        ENVIRONMENT
            TESTS_DIR=${CMAKE_CURRENT_BINARY_DIR}
            MARS_HOME=${CMAKE_CURRENT_BINARY_DIR}
            MARS_PPROC_BACKEND=MIR
            MIR_HOME=${CMAKE_CURRENT_BINARY_DIR}
    )

endforeach()
