language: cpp

compiler:
    - gcc
    - clang

env:
    global:
        - MONGO_REPO="http://repo.mongodb.com/apt/ubuntu"
        - REPO_TYPE="precise/mongodb-enterprise/3.2 multiverse"
        - SOURCES_LOC="/etc/apt/sources.list.d/mongodb-enterprise.list"
        - KEY_SERVER="hkp://keyserver.ubuntu.com:80"
        - CMAKE_VERSION="cmake-3.2.3-Linux-x86_64"
        - CMAKE_BINARY="${CMAKE_VERSION}/bin/cmake"

    matrix:
        - CONFIG=Release
        - CONFIG=Debug

# Setup required repositories before installation
before_install:
    # Add the modern toolchain repositories (llvm repo depends on ubunut-toolchain-r + gcc)
    - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
    - sudo add-apt-repository -y 'deb http://llvm.org/apt/precise/ llvm-toolchain-precise-3.6 main'
    - wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add -

    # Get the latest CMake
    - curl -O https://cmake.org/files/v3.2/${CMAKE_VERSION}.tar.gz

    # MongoDB Enterprise Edition, latest stable version
    - sudo apt-key adv --keyserver ${KEY_SERVER} --recv 7F0CEB10
    - sudo apt-key adv --keyserver ${KEY_SERVER} --recv D68FA50FEA312927
    - echo "deb ${MONGO_REPO} ${REPO_TYPE}" | sudo tee ${SOURCES_LOC}

    # CMake
    - sudo add-apt-repository --yes ppa:kalakris/cmake

    # Update all the repositories
    - sudo apt-get update -qq

    # Mongo C Driver
    - git clone https://github.com/mongodb/mongo-c-driver.git

install:
    # GCC
    - if [ "$CXX" = "g++" ]; then sudo apt-get install -qq g++-4.9; export CXX="g++-4.9"; export CC="gcc-4.9"; fi

    # Clang
    - if [ "$CXX" == "clang++" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.6; export CXX="clang++-3.6"; export CC="clang-3.6"; fi

    # CMake
    - tar -zxvf ${CMAKE_VERSION}.tar.gz -C build

    # Install MongoDB Enterprise
    - sudo apt-get install mongodb-enterprise-server

    # Install CMake (Versions lower than 2.8.8 do not support object libraries)
    - sudo apt-get install cmake

    # Install Mongo C Driver
    - pushd mongo-c-driver

    # TODO: Update this to our real minimum for the C++11 driver 3.1 release, once known.
    - git checkout 1.5.0

    - ./autogen.sh --enable-tests=no --enable-examples=no --with-libbson=bundled; make; sudo make install

    - popd

before_script:
    - $CC --version
    - $CXX --version

    # Build the driver and the tests
    - cd build
    - ${CMAKE_BINARY} -DCMAKE_BUILD_TYPE=$CONFIG -DCMAKE_C_FLAGS="-Wall -Wextra -Wno-attributes -Werror -Wno-error=missing-field-initializers" -DCMAKE_CXX_FLAGS="-Wall -Wextra -Wno-attributes -Werror -Wno-error=missing-field-initializers" ..

script:
    - make format-lint

    - make all

    # Run bsoncxx tests with catch
    - ./src/bsoncxx/test/test_bson

    # Run mongocxx tests with catch
    - ./src/mongocxx/test/test_driver

    # Run mongocxx instance tests with catch
    - ./src/mongocxx/test/test_instance

    # Install headers and libs for the examples
    - make install

    # Make the examples
    - make examples

    # Run the examples
    - make run-examples
