
#### NOTE: rpcmars.h rpcmars.c should not be in the distro!!!

# the 'plain' filenames of the MARS source code
set(_libMars_srcs
    tcp.c server.c request.c expand.c
	hash.c memory.c logfile.c options.c
	api.c apibase.c base.c netbase.c netcdf.c hdf5.c nullbase.c fdb5base.cc forwardbase.c
	gribbase.c nfdbbase.c dhsbase.c odb.cc odbbase.c
	multibase.c archive.c retrieve.c ibmblk.c
	lock.c files.c sh2ll.c guess.c hypercube.c
	check.c environ.c
	handler.c target.c grib.c calc.c field.c
	list.c tools.c hidden.c index.c bufr.c
	externf.c service.c xservice.c
	udp.c queue.c variable.c
	filebase.c account.c cos.c pproc.h pproc.cc pproc_none.cc restricted.c
	wind.c control.c stream.c
	remove.c authenticate.c flatfilebase.c time.c timer.c json.c
	version.c statistics.c metadata.c ecaccess.c free.c
	schedule.c

    # add the header files because we use this list to help create the tarball later
	api.h base.h bufr.h control.h cos.h ecaccess.h field.h globals.h grib.h hypercube.h
	ibmblk.h index.h json.h lang.h mars.h mcs.h
	ncmerge.h ncschema.h nctarget.h nctypes.h netbase.h
	netbase.h odb.h proto.h queue.h restricted.h tools.h variable.h 
)


# extra files which will be needed in the tarball
set(extra_files_for_tarball
    ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
    ${MARS_SOURCE}/rpcmars.x
    ${MARS_SOURCE}/langy.y
    ${MARS_SOURCE}/langl.l
    ${MARS_SOURCE}/CMakeLists.txt            # needed for bundle tarball
    ${MARS_SOURCE}/mars_client_version.c.in  # needed for bundle tarball
    ${MARS_SOURCE}/mars_client_version.h.in  # needed for bundle tarball
    ${MARS_SOURCE}/mars_client_config.h.in   # needed for bundle tarball
)    


if( HAVE_PPROC_EMOS )
    list( APPEND _libMars_srcs pproc_emos.cc )
endif()

if( HAVE_PPROC_MIR )
    list( APPEND _libMars_srcs pproc_mir.cc )
endif()

if(HAVE_NETCDF)
  list(APPEND _libMars_srcs
    nctarget.c
    nctypes.c
    ncschema.c
    ncmerge.c
    )
endif()



# add the path to where the source files are located
foreach( file ${_libMars_srcs} )
    list( APPEND libMars_srcs ${MARS_SOURCE}/${file} )
endforeach()


ecbuild_generate_config_headers()
configure_file( ${MARS_SOURCE}/mars_client_config.h.in    mars_client_config.h   )


# get the MARS client version number (just invent it for now since we don't use it)

set(MARS_CLIENT_MAJOR_VERSION 0)
set(MARS_CLIENT_MINOR_VERSION 0)
set(MARS_CLIENT_PATCH_VERSION 0)
configure_file( ${MARS_SOURCE}/mars_client_version.h.in  mars_client_version.h )
configure_file( ${MARS_SOURCE}/mars_client_version.c.in  mars_client_version.c )



# copy all the MARS source files to a subdirectory of the build directory; this is
# so that we have a clean version of the MARS client source code in its own directory
# which can be packaged into the export tarball.
# NO - see code at the end of this file for the real solution
#foreach( file ${libMars_srcs} )
#    get_filename_component( basefile ${file} NAME )
#    configure_file(${file} "${CMAKE_BINARY_DIR}/src/libMarsClient/${basefile}" COPYONLY) # copy to binary dir
#endforeach()

# add the local and generated files - they are not in the MARS source directory
list( APPEND libMars_srcs ${CMAKE_CURRENT_BINARY_DIR}/langy.c ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.c)


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

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

find_package(CMath)



# rpcgen, when creating the .c file, has an include for the header file;
# unfortunately, it does not know where the header file has been generated, so it
# assumes it will be in the source directory. This is wrong, it should be the
# binary directory. Therefore, we first copy the .x file to the binary directory
# and work on it there. Note: will this cause a problem with in-source builds?
# If so, we may need to first check whether the file already exists.

if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
    configure_file(${MARS_SOURCE}/rpcmars.x ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.x)
endif()

ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.h
	COMMAND ${RPCGEN_EXECUTABLE} -h -o ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.h ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.x
    DEPENDS ${MARS_SOURCE}/rpcmars.x)

ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.c
	COMMAND ${RPCGEN_EXECUTABLE} -c -o ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.c ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.x
    DEPENDS ${MARS_SOURCE}/rpcmars.x)

ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/langl.c
	COMMAND lex -l ${MARS_SOURCE}/langl.l\; sed -e s/yy/yy_mars/g < lex.yy.c | egrep -v "^#line" > ${CMAKE_CURRENT_BINARY_DIR}/langl.c
    DEPENDS ${MARS_SOURCE}/langl.l)

ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/langy.c
	COMMAND yacc -t ${MARS_SOURCE}/langy.y\; sed -e s/yy/yy_mars/g < y.tab.c | egrep -v "^#line" > ${CMAKE_CURRENT_BINARY_DIR}/langy.c
    DEPENDS ${MARS_SOURCE}/langy.y)

ADD_CUSTOM_TARGET(mvmarsxdr DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.h ${CMAKE_CURRENT_BINARY_DIR}/rpcmars.c)
ADD_CUSTOM_TARGET(mvmarslex DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/langy.c   ${CMAKE_CURRENT_BINARY_DIR}/langl.c)

###set( libMars_srcs ${libMars_srcs} PARENT_SCOPE )

include_directories(BEFORE   ${CMAKE_CURRENT_BINARY_DIR} ${METVIEW_STANDARD_INCLUDES} ${METVIEW_ODB_API_INCLUDE_DIRS})



if (ODB_API_FOUND)
    set(ODB_FLAG ODB_SUPPORT)
else()
    set(ODB_FLAG NOODB)
endif()


ecbuild_add_library( TARGET      MvMars
                     TYPE        SHARED
                     GENERATED   mars_client_version.c
                     SOURCES     ${libMars_srcs} mars_client_version.c
                     TEMPLATES   ${common_templates}
                     DEFINITIONS ${METVIEW_EXTRA_DEFINITIONS} ${ODB_FLAG}
                     LIBS        ${MARS_FEATURE_LIBRARIES} ${METVIEW_EXTRA_LIBRARIES} ${METVIEW_ODB_API_LIBRARIES} ${CURL_LIBRARIES}
                     PRIVATE_INCLUDES
                         ${NETCDF_INCLUDE_DIRS}
                         ${MIR_INCLUDE_DIRS}
                    )

# we generate a separate MARS library which is static, hopefully making
# the inline C and Fortran easier to handle (fewer dependencies this way)
# ecbuild_add_library( TARGET      MvMarsStatic
#                      TYPE        STATIC
#                      GENERATED   mars_client_version.c
#                      SOURCES     ${libMars_srcs} mars_client_version.c
#                      TEMPLATES   ${common_templates}
#                      DEFINITIONS ${METVIEW_EXTRA_DEFINITIONS} ${ODB_FLAG}
#                      LIBS        ${MARS_FEATURE_LIBRARIES} ${METVIEW_EXTRA_LIBRARIES} ${METVIEW_ODB_API_LIBRARIES} ${CURL_LIBRARIES}
#                      PRIVATE_INCLUDES
#                          ${NETCDF_INCLUDE_DIRS}
#                          ${MIR_INCLUDE_DIRS}
#                     )
# 
ADD_DEPENDENCIES(MvMars       mvmarsxdr mvmarslex)
# ADD_DEPENDENCIES(MvMarsStatic mvmarsxdr mvmarslex)



if(NOT DEFINED METVIEW_BUNDLE)

    # for the tarball, we want to include the MARS client source, but only the files that we actually
    # use in Metview. So we create a list of all files in the source, then subtract from it the list
    # of source files defined previously as libMars_srcs.

    # first, list all the files in the MARS source directory - these are the files we will NOT pack
    file(GLOB mars_unwanted_src_files   "${MARS_SOURCE}/*")
    file(GLOB mars_unwanted_tools_files "${MARS_SOURCE}/tools/*")
    list(APPEND mars_unwanted_src_files ${mars_unwanted_tools_files})

    # remove each wanted source file from the list
    foreach( file ${libMars_srcs} ${extra_files_for_tarball})
        list(REMOVE_ITEM mars_unwanted_src_files ${file})
    endforeach()

    # these filenames will be treated as regular expressions, so we need to be more
    # explicit and put a dollar sign at the end so that they don't match with too much
    set(mars_real_unwanted_src_files "")
    foreach( file ${mars_unwanted_src_files} )
        list(APPEND mars_real_unwanted_src_files "${file}\$")
    endforeach()

    # add our revised list to ECBUILD_DONT_PACK_FILES
    # - we don't use ecbuild_dont_pack() because it makes everything relative to the
    #   source directory
    list( APPEND ECBUILD_DONT_PACK_FILES ${mars_real_unwanted_src_files})
    set( ECBUILD_DONT_PACK_FILES ${ECBUILD_DONT_PACK_FILES} CACHE INTERNAL "" )

endif()


# soon to be redundant - remove unwanted files from libMars/MARS
# list of files (regular expressions) to exclude from the source tarball
# note "MARS/m$" - the dollar is required because otherwise CPACK will exclude any
# file beginning with an 'm'!
ecbuild_dont_pack(REGEX "MARS/config*")
ecbuild_dont_pack(REGEX "MARS/make*")
ecbuild_dont_pack(FILES "MARS/mars.test;MARS/m$;MARS/gnumake_target;MARS/Makefile;MARS/account_check.c")
ecbuild_dont_pack(FILES "MARS/blk.c;MARS/certify.c;MARS/cpli.h;MARS/cvers.h;MARS/dce.c;MARS/dce.h;MARS/dcebase.c;MARS/dceclient.c")
ecbuild_dont_pack(FILES "MARS/dceremote.c;MARS/dceserver.c;MARS/eccert.c;MARS/eccert.h;MARS/ecopt.h;MARS/emoscycle.c;MARS/extargs.c;")
ecbuild_dont_pack(FILES "MARS/feedtask.c;MARS/gpl-3.0.txt;MARS/grib_api_release.c;MARS/htest.c;MARS/lckfile.c")
ecbuild_dont_pack(FILES "MARS/licence_header;MARS/mars.c;MARS/marsdce.c;MARS/marsdce.h;MARS/marsfdb.c;MARS/marsmon.c;MARS/marsversion.c")
ecbuild_dont_pack(FILES "MARS/mcs.c;MARS/mrpc.x;MARS/msbase.c;MARS/multidef.c;MARS/multidef.h;MARS/ocean.c")
ecbuild_dont_pack(FILES "MARS/rdb.c;MARS/reqmerge.c;MARS/source.c;MARS/source.h;MARS/test.c;MARS/testh.c;MARS/teststats.c")


