find_program( GRIB_COMPARE grib_compare )

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

if( 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
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/ )

# 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 #######################

set(tests interpolation_example_grib_api interpolation_example_gribex interpolation_example_wind)

foreach( test ${tests} )
  ecbuild_add_test( TARGET ${test}
            SOURCES   ${test}.F
            LIBS      emos ${GRIB_API_LIBRARIES}
            ARGS      -i ${PROJECT_SOURCE_DIR}/data/latlon.grib -o ${test}.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/
            )
endforeach()


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}
                  }
                    
)


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

if( ENABLE_FORTRAN90 )

ecbuild_add_test( TARGET bufr_demo
            SOURCES   bufr_demo.f90
            LIBS      emos ${GRIB_API_LIBRARIES}
            ARGS      ${PROJECT_SOURCE_DIR}/data/synop_4.bufr
            LINKER_LANGUAGE Fortran )

endif( ENABLE_FORTRAN90 )

#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 )

#ecbuild_add_test( TARGET bufr_decode_all
#            SOURCES    bufr_decode_all.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_TABLE=${PROJECT_SOURCE_DIR}/bufrtables/
#)

endif( ENABLE_INSTALL_TOOLS )
