cmake_minimum_required( VERSION 3.12.0 FATAL_ERROR )

include( ${INCLUDE_CONFIG}-local-config.cmake OPTIONAL )

#set( CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_CURRENT_SOURCE_DIR}/../ecbuild/cmake" )
find_package( ecbuild 3.7 REQUIRED HINTS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../ecbuild)

project( MetviewBundle VERSION 2026.02.0 LANGUAGES C CXX )


# list all the packages we incorporate into the bundle
# note: we don't care about some packages, e.g. ODB, for this purpose
set(MV_BUNDLE_PACKAGES METVIEW MAGICS MARS_CLIENT MIR ATLAS ECKIT METKIT ECCODES)

#if(METVIEW_SITE STREQUAL ecmwf )
#    list(APPEND MV_BUNDLE_PACKAGES FDB)
#endif()

# create a variabe for each package we bundle
foreach (bunpack ${MV_BUNDLE_PACKAGES})
    if(NOT BUNDLE_SKIP_${bunpack})
        set(MV_BUNDLE_CONTAINS_${bunpack} 1)
    endif()
endforeach()

# -------------------------------------------------------------------------------------
# settings which are required across the packages to get a working Metview installation

#general
set(ENABLE_ODB           OFF CACHE BOOL "ODB support")
#set(ENABLE_ODB1          OFF CACHE BOOL "ODB-1 support")
set(ENABLE_EXPERIMENTAL  OFF CACHE BOOL "Experimental features")
set(ENABLE_FORTRAN       NOT_SET CACHE BOOL "Fortran features")


#eccodes
set(ENABLE_ECCODES_THREADS  ON CACHE BOOL "enable thread safetly in eccodes")



#mars
set(ECMWF             OFF CACHE BOOL "build the client as for ECMWF")
#set(CONFIG_FDB        OFF CACHE BOOL "database configuration to access FDB")
set(ENABLE_UDP_STATS  OFF CACHE BOOL "")
set(ENABLE_MPI        OFF CACHE BOOL "")

#GRIB_API
set(ENABLE_PNG      ON  CACHE BOOL "Enable PNG") # required on Mac OSX



#magics
set(ENABLE_PYTHON   OFF CACHE BOOL "Enable Python")
set(ENABLE_CAIRO    ON  CACHE BOOL "Enable Cairo")
set(ENABLE_PNG      ON  CACHE BOOL "Enable PNG")  # needed for Mac OS X
set(ENABLE_BUFR     ON  CACHE BOOL "Enable BUFR")
set(ENABLE_NETCDF   ON  CACHE BOOL "Enable NetCDF")

# if ENABLE_UI is OFF, then we need to tell Magics
if (ENABLE_UI STREQUAL "OFF")
    set(ENABLE_METVIEW_NO_QT  ON  CACHE BOOL "Enable Metview but no Qt")
else()
    set(ENABLE_METVIEW        ON  CACHE BOOL "Enable Metview")
endif()



# if the user did bot supply a value for ENABLE_FORTRAN, then unset it
# (we want to then use the default behaviour, per package)
if(ENABLE_FORTRAN STREQUAL "NOT_SET")
    unset(ENABLE_FORTRAN)
    unset(ENABLE_FORTRAN CACHE)
endif()



# -------------------------------------------------------------------------------------


include( ecbuild_bundle )

ecbuild_bundle_initialize()

    ecbuild_add_option( FEATURE EXPOSE_SUBPACKAGES
                        DEFAULT OFF
                        DESCRIPTION "Allow separate use of the sub-packages in the bundle")



    # if the installer does not want to expose the sub-packages then we need to:
    # a) change $PREFIX so that everything is installed into a sub-directory
    # b) change the location of the Metview startup script so it's installed
    #    where intended (Metview takes care of this)



    if (NOT ENABLE_EXPOSE_SUBPACKAGES)
        set(ORIGINAL_CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
        set(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/lib/metview-bundle")
    endif()

    if(MV_BUNDLE_CONTAINS_ECCODES)
        ecbuild_bundle( PROJECT eccodes                GIT "ssh://git@git.ecmwf.int/eccodes/eccodes"             TAG     2.46.0 UPDATE )
    endif()

#    if(MV_BUNDLE_CONTAINS_FDB)
#        set( FDB_CONF_FILE /usr/local/apps/fdb/etc/Config )
#        ecbuild_bundle( PROJECT fdb                GIT "ssh://git@git.ecmwf.int/mars/fdb_vintage"            TAG     4.11.0  UPDATE )
#    else()
#        set(FDB_DIR "/dummy/path/for/bundle")
#    endif()

    ecbuild_bundle( PROJECT eckit                  GIT "ssh://git@git.ecmwf.int/ecsdk/eckit"                 TAG  2.0.1  UPDATE )
    ecbuild_bundle( PROJECT atlas                  GIT "https://github.com/ecmwf/atlas.git"                TAG  0.45.1  UPDATE )
    ecbuild_bundle( PROJECT mir                    GIT "https://github.com/ecmwf/mir.git"                  TAG  1.28.0  UPDATE )

    if(ENABLE_ODB)
        set(ODB_ECKIT ON CACHE BOOL "ODB_API is built on ecKit")
        ecbuild_bundle( PROJECT odc                GIT "ssh://git@git.ecmwf.int/odb/odc"                     TAG  1.6.3  UPDATE )
        ecbuild_bundle( PROJECT metkit             GIT "ssh://git@git.ecmwf.int/ecsdk/metkit"                TAG  1.16.2  UPDATE )
    #if(ENABLE_ODB1)
    #    ecbuild_bundle( PROJECT odb-tools          STASH "odb/odb-tools"               BRANCH  release/0.17.1  UPDATE )
    #endif()
    endif()

    ecbuild_bundle( PROJECT mars-client            GIT "ssh://git@git.ecmwf.int/mars/mars-client"            TAG  6.33.25  UPDATE )
    ecbuild_bundle( PROJECT magics                 GIT "ssh://git@git.ecmwf.int/mag/magics"                  TAG  4.16.0   UPDATE )
    ecbuild_bundle( PROJECT metview                GIT "ssh://git@git.ecmwf.int/metv/metview"                TAG  5.26.3  UPDATE )


    ecbuild_dont_pack(FILES configure.sh;ecmwf;make-tarball.sh;test-tarball.sh;configure.clang.sh;build_solo.sh;bamboo)

ecbuild_bundle_finalize()


ecbuild_info("ENABLE_EXPOSE_SUBPACKAGES is ${ENABLE_EXPOSE_SUBPACKAGES}:")
if (ENABLE_EXPOSE_SUBPACKAGES)
    ecbuild_info("All packages in the bundle will be installed at the same level into:")
    ecbuild_info("  ${CMAKE_INSTALL_PREFIX}")
else()
    ecbuild_info("All packages in the bundle will be installed into a sub-directory:")
    ecbuild_info("  ${CMAKE_INSTALL_PREFIX}")
    ecbuild_info ("The Metview startup script will be installed into:")
    ecbuild_info("  ${ORIGINAL_CMAKE_INSTALL_PREFIX}/bin")
endif()

