### config headers

configure_file( mars_client_version.h.in  mars_client_version.h )
configure_file( mars_client_version.c.in  mars_client_version.c )

# compile definitions

add_definitions( -DR64 )

if( ECMWF )
    add_definitions( -DECMWF )
endif()

# ecbuild automatically test endiness and sets variable EC_BIG_ENDIAN or EC_LITTLE_ENDIAN
if( EC_BIG_ENDIAN )
	message( STATUS "System is big endian" )
else()
	message( STATUS "System is little endian" )
	add_definitions(-DLITTLE_END)
endif()

if( NOT HAVE_FDB )
    add_definitions(-DNOFDB)
endif()

if( NOT HAVE_FDB5 )
    add_definitions(-DNOFDB5)
endif()

if( NOT HAVE_ODB )
	add_definitions(-DNOODB)
else()
	add_definitions(-DODB_SUPPORT )
	include_directories( ${ODB_API_INCLUDE_DIRS} ${ECKIT_INCLUDE_DIRS} )
endif()

if( HAVE_ODBDIRECT )
  add_definitions(-DODBDIRECT)
  include_directories( ${MARS_SERVER_INCLUDE_DIRS} ${ODB_API_INCLUDE_DIRS} ${ECKIT_INCLUDE_DIRS} )
endif()

if( NOT HAVE_PPROC )
	add_definitions(-DNOPPROC)
endif()

if( NOT CURL_FOUND )
	add_definitions(-DNOCURL)
endif()

if(HAVE_ECMWF_DEVELOPMENT)
  add_definitions(-DECMWF)
  add_definitions(-DNOEMS)
  add_definitions(-DNOAUTH)
  add_definitions(-DNOSCHEDULE)
endif()

# sources

list( APPEND mars_client_src_files
account.c
archive.c
authenticate.c
base.c
bufr.c
calc.c
certify.c
check.c
control.c
cos.c
ecaccess.c
eccert.c
environ.c
expand.c
extargs.c
externf.c
fdb5base.cc
forwardbase.c
odbdirect.cc
feedtask.c
field.c
filebase.c
files.c
flatfilebase.c
free.c
gribbase.c
grib.c
guess.c
handler.c
hash.c
hidden.c
hypercube.c
ibmblk.c
index.c
langy.c
list.c
lock.c
logfile.c
marsxdr.c
mcs.c
memory.c
dhsbase.c
metadata.c
msbase.c
multibase.c
netbase.c
nfdbbase.c
nullbase.c
odbbase.c
options.c
pproc.c
queue.c
rdb.c
remove.c
request.c
restricted.c
retrieve.c
schedule.c
server.c
service.c
sh2ll.c
statistics.c
stream.c
target.c
tcp.c
time.c
timer.c
tools.c
udp.c
variable.c
version.c
json.c
odb.cc
api.c
apibase.c
wind.c)

# targets

configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/rpcmars.x ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.x COPYONLY )

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.h
	COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.h
	COMMAND rpcgen -h -o ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.h ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.x
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.x )

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/marsxdr.c
	COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_BINARY_DIR}/marsxdr.c
	COMMAND rpcgen -c -o ${CMAKE_CURRENT_BINARY_DIR}/marsxdr.c ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.x
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.x )

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/langl.c
	COMMAND lex -l ${CMAKE_CURRENT_SOURCE_DIR}/langl.l\; sed -e s/yy/yy_mars/g < lex.yy.c > ${CMAKE_CURRENT_BINARY_DIR}/langl.c
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/langl.l)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/langy.c
	COMMAND yacc -t ${CMAKE_CURRENT_SOURCE_DIR}/langy.y\; sed -e s/yy/yy_mars/g < y.tab.c > ${CMAKE_CURRENT_BINARY_DIR}/langy.c
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/langy.y)

add_custom_target(marsxdr DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.h ${CMAKE_CURRENT_BINARY_DIR}/marsxdr.c)
add_custom_target(marslex DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/langy.c ${CMAKE_CURRENT_BINARY_DIR}/langl.c)

#### TODO: Mechanism to choose ecCodes over grib_api -- REVIEW
set( grib_handling_pkg grib_api )
if( HAVE_ECCODES )
    set( grib_handling_pkg eccodes )
endif()
####

ecbuild_add_library(
    TARGET      marsclient
    TYPE        STATIC
    GENERATED   mars_client_version.c
	SOURCES     ${mars_client_src_files} mars_client_version.c
	DEPENDS     marsxdr marslex
    PRIVATE_INCLUDES
        ${ECKIT_INCLUDE_DIRS}
        ${MARS_SERVER_INCLUDE_DIRS}
        ${GRIB_API_INCLUDE_DIRS}
        ${CURL_INCLUDE_DIRS}
    DEFINITIONS ${GRIB_API_DEFINITIONS}
    LIBS        ${grib_handling_pkg} ${MARS_FEATURE_LIBRARIES} ${CURL_LIBRARIES} ${CMATH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} )

set_directory_properties( ADDITIONAL_MAKE_CLEAN_FILES rpcmars.h marsxdr.c langl.c langy.c )

ecbuild_add_executable(
	TARGET    mars.bin
  INCLUDES  ${GRIB_API_INCLUDE_DIRS}
	SOURCES		mars.c
  LIBS		  marsclient
)

add_subdirectory( tools )
