cmake_minimum_required( VERSION 3.6.0 FATAL_ERROR )

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

set( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_CURRENT_SOURCE_DIR}/../ecbuild/cmake;${CMAKE_MODULE_PATH}" )

include( ecbuild_bundle )

project( MetviewMiniBundle C CXX Fortran )
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)
    list(APPEND MV_BUNDLE_PACKAGES ECMF_CONFIGS)
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")

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

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

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_ECMWF_CONFIGS )
        ecbuild_bundle( PROJECT metview-ecmwf-configs  STASH "metv/metview-ecmwf-configs"  TAG  master  UPDATE )
    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                STASH "mars/fdb_vintage"            TAG     4.10.0   UPDATE )
    else()
        set(FDB_DIR "/dummy/path/for/bundle")
    endif()

    my_bundle_pkg( PROJECT eckit        STASH "ecsdk/eckit"       TAG  1.4.0 )
    my_bundle_pkg( PROJECT atlas        STASH "atlas/atlas"       TAG  0.19.0 )
    my_bundle_pkg( PROJECT mir          STASH "mir/mir"           TAG  1.3.0 )
    my_bundle_pkg( PROJECT mars-client  STASH "mars/mars-client"  TAG  6.25.9 )
    my_bundle_pkg( PROJECT metview      STASH "metv/metview"      TAG  5.7.2.1 )

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

