find_program( GRIB_COMPARE grib_compare )

if( NOT GRIB_COMPARE AND TARGET grib_compare )
  get_target_property( GRIB_COMPARE grib_compare LOCATION )
endif()

if( NOT GRIB_COMPARE )
  message( WARNING "Could not find grib_compare executable -- turning off some tests!\nTo enable them place 'grib_compare' on the PATH and re-run cmake" )
endif()

# define the grib environment
if( grib_api_BASE_DIR )
  set( _grib_environment
    GRIB_DEFINITION_PATH=${grib_api_BASE_DIR}/share/grib_api/definitions/
    GRIB_SAMPLES_PATH=${grib_api_BASE_DIR}/share/grib_api/samples/ )
endif()

# configure Fortran-based targets for double precision (compilation & linking)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${emos_double_precision_flags}")

configure_file( include.sh.in include.sh @ONLY )

##################### INTERPOLATION #######################

ecbuild_add_test(
  TARGET  interpolation_example_grib_api
  SOURCES interpolation_example_grib_api.F
  LIBS    emos ${GRIB_API_LIBRARIES}
  ARGS    -i ${PROJECT_SOURCE_DIR}/data/latlon.grib -o interpolation_example_grib_api.grib
  LINKER_LANGUAGE Fortran
  ENVIRONMENT
    "${_grib_environment}"
    MARS_LSM_PATH=${CMAKE_BINARY_DIR}/${INSTALL_DATA_DIR}/tables/interpol/
    ECMWF_LOCAL_TABLE_PATH=${PROJECT_SOURCE_DIR}/gribtables/
    LOCAL_DEFINITION_TEMPLATES=${PROJECT_SOURCE_DIR}/gribtemplates/ )

ecbuild_add_test(
  TARGET  interpolation_example_wind
  SOURCES interpolation_example_wind.F
  LIBS    emos ${GRIB_API_LIBRARIES}
  ARGS    -i ${PROJECT_SOURCE_DIR}/data/latlon.grib -o interpolation_example_wind.grib
  LINKER_LANGUAGE Fortran
  ENVIRONMENT
    "${_grib_environment}"
    MARS_LSM_PATH=${CMAKE_BINARY_DIR}/${INSTALL_DATA_DIR}/tables/interpol/
    ECMWF_LOCAL_TABLE_PATH=${PROJECT_SOURCE_DIR}/gribtables/
    LOCAL_DEFINITION_TEMPLATES=${PROJECT_SOURCE_DIR}/gribtemplates/ )

ecbuild_add_executable(
  TARGET  interpolation NOINSTALL
  SOURCES interpolation.F
  LIBS    emos ${GRIB_API_LIBRARIES} )

ecbuild_add_test(
  TARGET      interpolation
  TYPE        SCRIPT
  CONDITION   GRIB_COMPARE
  COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/interpolation.sh
  RESOURCES   data_ref/spectral-ll.grib data_ref/spectral-rg.grib
  ENVIRONMENT "${_grib_environment}"
)

################### G R I B E X ###########################

if( HAVE_GRIBEX_ABORT )
  ecbuild_add_test(
    TARGET  gribex_abort
    SOURCES gribex_abort.F
    LIBS    emos ${GRIB_API_LIBRARIES}
    LINKER_LANGUAGE Fortran )
  set_tests_properties( gribex_abort PROPERTIES WILL_FAIL TRUE )
else()
  ecbuild_add_test(
    TARGET  interpolation_example_gribex
    SOURCES interpolation_example_gribex.F
    LIBS    emos ${GRIB_API_LIBRARIES}
    ARGS    -i ${PROJECT_SOURCE_DIR}/data/latlon.grib -o interpolation_example_gribex.grib
    LINKER_LANGUAGE Fortran
    ENVIRONMENT
      "${_grib_environment}"
      MARS_LSM_PATH=${CMAKE_BINARY_DIR}/${INSTALL_DATA_DIR}/tables/interpol/
      ECMWF_LOCAL_TABLE_PATH=${PROJECT_SOURCE_DIR}/gribtables/
      LOCAL_DEFINITION_TEMPLATES=${PROJECT_SOURCE_DIR}/gribtemplates/ )
endif()

##################### B U F R #############################

if( ENABLE_FORTRAN90 )
  ecbuild_add_test(
    TARGET  bufr_demo
    SOURCES bufr_demo.f90
    LIBS    emos ${GRIB_API_LIBRARIES}
    LINKER_LANGUAGE Fortran
    ARGS    ${PROJECT_SOURCE_DIR}/data/temp_101.bufr
    ENVIRONMENT BUFR_TABLES=${PROJECT_SOURCE_DIR}/bufrtables )
endif( ENABLE_FORTRAN90 )

ecbuild_add_test(
  TARGET  bufr_decode_all
  COMMAND ${CMAKE_BINARY_DIR}/bin/bufr_decode_all
  LIBS    emos ${GRIB_API_LIBRARIES}
  LINKER_LANGUAGE Fortran
  ARGS    -i ${PROJECT_SOURCE_DIR}/data/temp_101.bufr
  ENVIRONMENT BUFR_TABLES=${PROJECT_SOURCE_DIR}/bufrtables )

#ecbuild_add_test(
#  TARGET  decode_bufr
#  SOURCES bufr/decode_bufr.F
#  LIBS    emos ${GRIB_API_LIBRARIES}
#  ARGS    -i ${PROJECT_SOURCE_DIR}/data/ISMD01_OKPR.bufr
#  LINKER_LANGUAGE Fortran )

#if( ENABLE_INSTALL_TOOLS )
#  ecbuild_add_test(
#    TARGET  tc_tracks
#    TYPE    SCRIPT
#    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tc_tracks.sh
#    ENVIRONMENT BUFR_TABLES=${PROJECT_SOURCE_DIR}/bufrtables/ )
#endif( ENABLE_INSTALL_TOOLS )

