# (C) Copyright 2011- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

#################################################################################################################################################################

cmake_minimum_required( VERSION 3.12 FATAL_ERROR )

find_package( ecbuild 3.4 REQUIRED HINTS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../ecbuild)

project( mars_client LANGUAGES C CXX )

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#################################################################################################################################################################
### dependencies

option( ECMWF "build the client as for ECMWF"  OFF )

### eccodes

ecbuild_find_package( NAME eccodes VERSION 0.14 REQUIRED )

### eckit

ecbuild_add_option( FEATURE UDP_STATS
  DESCRIPTION "Send statistics over UDP"
  DEFAULT ON
  REQUIRED_PACKAGES "NAME eckit VERSION 1.7" )

if( HAVE_UDP_STATS )
    list( APPEND MARS_FEATURE_LIBRARIES eckit )
endif()

### mir

ecbuild_add_option( FEATURE PPROC_MIR
    DESCRIPTION "compile with MIR interpolation"
    DEFAULT ON
    REQUIRED_PACKAGES "NAME mir VERSION 1.5" )

if( HAVE_PPROC_MIR )
    list( APPEND MARS_FEATURE_LIBRARIES mir )
endif()

### libemos

ecbuild_add_option( FEATURE PPROC_EMOS
    DESCRIPTION "compile with EMOSLIB interpolation"
    DEFAULT ON
    REQUIRED_PACKAGES "NAME libemos" )

if( HAVE_PPROC_EMOS )
    list( APPEND MARS_FEATURE_LIBRARIES emos )
endif()


ecbuild_add_option( FEATURE BUFR_TOOLS
    DESCRIPTION "compile BUFR tools"
    DEFAULT ON
    REQUIRED_PACKAGES "NAME libemos" )

### odb

ecbuild_add_option( FEATURE ODB
    DESCRIPTION "compile with ODB support"
    DEFAULT ON
    REQUIRED_PACKAGES
        "NAME odc VERSION 1.0.3"
        "NAME metkit VERSION 1.4.0" )

if( HAVE_ODB )
  list( APPEND MARS_FEATURE_LIBRARIES odccore metkit )
endif()


ecbuild_add_option( FEATURE BUILD_TOOLS
    DEFAULT ON
    DESCRIPTION "Build the command line tools"
)

ecbuild_add_option( FEATURE MARS_USER_ENVIRONMENT
    DESCRIPTION "Mars user will be in $MARS_USER"
    DEFAULT OFF
)

ecbuild_add_option( FEATURE FDB
    DEFAULT ${ECMWF}
    DESCRIPTION "support for FDB"
    REQUIRED_PACKAGES "NAME fdb VERSION 4.6" )

if( HAVE_FDB )
  list( APPEND MARS_FEATURE_LIBRARIES fdb )
endif()

ecbuild_add_option( FEATURE FDB5
    DEFAULT ${ECMWF}
    DESCRIPTION "support for FDB5"
    REQUIRED_PACKAGES "NAME fdb5 VERSION 5.0" )

if( HAVE_FDB5 )
    list( APPEND MARS_FEATURE_LIBRARIES fdb5 )
endif()

### netcdf

ecbuild_add_option( FEATURE NETCDF
    DEFAULT ON
    DESCRIPTION "support for NetCDF"
    REQUIRED_PACKAGES NetCDF )

if( HAVE_NETCDF )
  list( APPEND MARS_FEATURE_LIBRARIES NetCDF::NetCDF_C )
endif()

#### Curl

ecbuild_add_option( FEATURE CURL
                    DEFAULT ON
                    DESCRIPTION "Curl library for HTTP web based api"
                    REQUIRED_PACKAGES CURL )

### others

find_package( CMath )

ecbuild_add_option( FEATURE RPC
                    DEFAULT ON
                    DESCRIPTION "support for RPC in MARS"
                    REQUIRED_PACKAGES RPC )

if( HAVE_RPC )
  # For transitive dependency
  install(
    FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindRPC.cmake
    DESTINATION ${INSTALL_CMAKE_DIR} )
endif()


set( CMAKE_THREAD_PREFER_PTHREAD 1 ) # find thread library, but prefer pthreads
find_package(Threads REQUIRED)

ecbuild_add_option( FEATURE ECMWF_DEVELOPMENT
    DEFAULT OFF
    DESCRIPTION "Special mode for development, activates ECMWF but deactivates auth, ems and schedule" )

########################################################################################################################
# sanity checks -- these are more pain than helpful to be honest

if(ECMWF)

    if( NOT HAVE_PPROC_MIR AND NOT HAVE_PPROC_EMOS  )
        message( FATAL_ERROR "At ECMWF mars-client requires PPROC (for which MIR or EMOSLIB is required)" )
    endif()

    if( NOT transi_HAVE_FFTW AND NOT atlas_HAVE_FFTW )
        message( FATAL_ERROR "At ECMWF FFTW library is required" )
    endif()

    if( NOT HAVE_ODB )
      message( FATAL_ERROR "At ECMWF mars-client requires ODB" )
    endif()

endif()

########################################################################################################################
### build source code

add_subdirectory( src )
add_subdirectory( tests )

########################################################################################################################
### finalize

ecbuild_install_project( NAME mars-client )

ecbuild_print_summary() # prints the summary of the configuration
