cmake_minimum_required( VERSION 3.12.0 FATAL_ERROR )

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

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


project( MetviewMiniBundle VERSION 5.17.0 LANGUAGES C CXX )
set(CPACK_PACKAGE_NAME Metview)  # so that the source tarball will have the name 'Metview'


# list all the packages we incorporate into the bundle
set(MV_BUNDLE_PACKAGES METVIEW MARS_CLIENT MIR ATLAS ECKIT)

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})
    set(MV_BUNDLE_CONTAINS_${bunpack} 1)
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_EXPERIMENTAL  OFF CACHE BOOL "Experimental features")
set(ENABLE_FORTRAN       NOT_SET CACHE BOOL "Fortran features")

#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 "")
set(ENABLE_BUILD_TOOLS  OFF   CACHE BOOL   "")


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


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

macro(my_bundle_pkg)
    ecbuild_bundle( ${ARGV} )
endmacro()

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_FDB)
        set( FDB_CONF_FILE /usr/local/apps/fdb/etc/Config )
        list(APPEND MV_BUNDLE_PACKAGES FDB)
        ecbuild_bundle( PROJECT fdb                GIT "ssh://git@git.ecmwf.int/mars/fdb_vintage"    TAG  4.11.0   )
    else()
        set(FDB_DIR "/dummy/path/for/bundle")
    endif()

    my_bundle_pkg( PROJECT eckit        GIT "ssh://git@git.ecmwf.int/ecsdk/eckit"       TAG  1.20.2                )
    my_bundle_pkg( PROJECT atlas        GIT "ssh://git@git.ecmwf.int/atlas/atlas"       TAG  0.30.0                )
    my_bundle_pkg( PROJECT mir          GIT "ssh://git@git.ecmwf.int/mir/mir"           TAG  1.13.0                  )
    my_bundle_pkg( PROJECT mars-client  GIT "ssh://git@git.ecmwf.int/mars/mars-client"  TAG  6.33.10  )
    my_bundle_pkg( PROJECT metview      GIT "ssh://git@git.ecmwf.int/metv/metview"      TAG  5.17.0              )

    ecbuild_dont_pack(FILES configure.sh;ecmwf;make-tarball.sh;test-tarball.sh;configure.clang.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()

