#---------------------------------#
#      general configuration      #
#---------------------------------#

sudo: false

branches:
  only:
    - develop
    - master
    - /^\d+\.\d+\.\d+$/

language: cpp

matrix:
  include:
    - os: linux
      dist: xenial
      addons:
        apt:
          packages:
            - libpango1.0-dev
      env:
        MINICONDA_FILE="Miniconda3-latest-Linux-x86_64.sh"
        PROJ4_PATH=~/miniconda3
        CAIRO=""

    - os: osx
      osx_image: xcode12.5
      env:
        MINICONDA_FILE="Miniconda3-latest-MacOSX-x86_64.sh"
        CAIRO="-D ENABLE_CAIRO=0"

env:
  global:
    - MAGICS_SRC=${TRAVIS_BUILD_DIR}
    - MAGICS_PYTHON_SRC=${MAGICS_SRC}/../magics-python
    - MAGICS_TEST_SRC=${MAGICS_SRC}/../magics-test
    - MAGICS_PYTHON_TESTS=${MAGICS_TEST_SRC}/test/gallery
    - ECBUILD_SRC=${MAGICS_SRC}/../ecbuild
    - BUILD_DIR=${MAGICS_SRC}/build
    - INSTALL_DIR=/tmp/install
    - MAGPLUS_HOME=${INSTALL_DIR}
    - PYTHONPATH=${PYTHONPATH}:${MAGICS_PYTHON_SRC}
    - secure: "zBBdYGeZ66YTOeWJGZx+3Y6RSgAGvlkcfK/TsI0vmDZEldN/B4HB/hNURZgoopuHA+uuz9nh1Ex2KvG0MUtY9sR1NEPrA5zw2+PQQx8canuZTZYO0nwtz+pkKVZLRU64SQuKMFxi5QX9cWzDpcTB2zp7fhG2ofTmNEmkokr+r982YtoQ7bxxDHVKjtsPwK1efZK1aROiIDSzQ56uMLLazfzwhJLHlGcIUMdpoBlPgQS8oKYoh8YNZS6rqEN+VVFm84epI3Jnb4S2nSBCqnSeICn1CElSP1soml5Xn188B9JYH7sc6KgAxyBEKQuzoFkUIpIDBxYuAxCUlE7X/8mAUekJtu+1KDP2X/c2C0gcCace/5fX2T7i8AFQE1Ik3RPIhm+nisrU6B76ybQVCqtUdDffjNCdize6vsSqvuIW6t36uQqdl6NmI9Uq21US+WiE64RqqY64iPG52TiE18d/4mdQBUKc1oyZYe4jkTSSp/QUm0pHnjdR0xnViUeCah1xbygkTmLMU+DDdfwBnmIJQOFhQsL+baz/sf/3MIikpZblcds41PMacGBXw9Q4uSoJwbG/j6ABD1/IEg/K6wJ9PhtVKzLtLLfWcA9JFfifgdb9TTV9BGfTroXCjx+/n6osokTfQZFnIUD95NziuGP9eiXrv6yaP9vTAdK4XFddxkI="
    - secure: "Wdpxunz4iJmoiZ2WDwL5sSuN4+DIFuxz0Osi+GjeIIPuBM5GcsrtiRnWTR7KSVEKddnkwzrz2W7q/8f+bmQC/+Vo98Nxec/5PhABxlXEU/R31BnH+vV4O4zEWoBlS77JulMGeIYdIXTM1vKN9p1j3iqZfmYTvF9ibmKyH9nPZ2ywzSEAZrmnO3eXvUzg1c+DuFAQQuXYiFeRNFvgwuXKpJmN2is0gdDZOvoNLMFo9hrleDshe0nc5xCYw7d3w+UTMOhQPzB7O7bGQ4tRKHa/eu8ABNpGbczD6WZWl2qf8VjV20iXEQgZJN1pXvXLgFjf3gwYa1minzGuAy2hnLWj5mtI1lsMxOKCOAa2Qe6WDnX8ZKfgjRoHC1CXU/f7O35Vj/U49QLcZssLWOn5pgtBkBq227ZuMvPjIUN1GlkXZUp0pMeiAFYmc1ptWKkvsLUToItcY6gRgIGNYJ2AlWJRCM+bV2XNcXXqmzKbIb98m2gBDPboFIQnmDHL8PxLRkbLTgV88fR5mce28/Zym20FkvE9c6Kg7SK7HAD6q6VwMJhMxPmnuLQySHWFPHRnr2xYdiI9M1p9mOX8SONOsTkwjeE8h3Nkr/ZzT7M8Wiwt5mj0EiGypoZEeyd+nB3rV4naEmm/fbPfou6vXYtTwoGiJTfveoKURxKXLFrVmP1B/o8="
git:
  depth: 1

before_install:
  # check formatting only on linux
  - |
    if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
      echo $(clang-format --version)
      echo $(bash --version)
      echo "No check for now"
      #bash check_formatting.sh
    fi
  # prepare install dir
  - mkdir ${INSTALL_DIR}
  # install conda
  - |
    MINICONDA_URL="https://repo.continuum.io/miniconda"
    curl -L -O "${MINICONDA_URL}/${MINICONDA_FILE}"
    bash ${MINICONDA_FILE} -b
  # activate conda
  - source ~/miniconda3/bin/activate
  # auto-yes for conda
  - conda config --set always_yes yes
  # update conda
  - conda update -n base -c conda-forge conda
  # add extra conda dirs to path so cmake can find packages
  - export PATH=~/miniconda3/lib:~/miniconda3/include:${PATH}

install:
  # install ecbuild
  - git clone --depth 1 https://github.com/ecmwf/ecbuild.git ${ECBUILD_SRC} -b ${TRAVIS_BRANCH}
  # install magics-python source
  - git clone -b master --depth 1 https://github.com/ecmwf/magics-python.git ${MAGICS_PYTHON_SRC}
  # install magics-test
  - git clone -b master --depth 1 https://github.com/ecmwf/magics-test.git ${MAGICS_TEST_SRC}

  # install conda deps
  - conda install libnetcdf expat xarray scipy cftime
  - conda install -c conda-forge proj4=6 eccodes pytest cairo pango pip
  # search for conda-installed libraries
  - export LDFLAGS="$LDFLAGS -L${CONDA_PREFIX}/lib -Wl,-rpath,${CONDA_PREFIX}/lib"
  - pip install findlibs


#---------------------------------#
#       build configuration       #
#---------------------------------#

script:
  # build
  - mkdir -p ${BUILD_DIR} && cd ${BUILD_DIR}
  - echo "In build dir--> ${MAGICS_SRC}"
  - cmake -D CMAKE_INSTALL_PREFIX=${INSTALL_DIR}
          ${MAGICS_SRC} ${CAIRO}
  - make -j2
  - ctest -j2 --output-on-failure
  - make install > /dev/null
  # test only on linux for now
  - |
    if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
      echo "In build dir--> ${MAGICS_PYTHON_SRC}"
      cd ${MAGICS_PYTHON_SRC}
      pytest
    fi

after_success:
  # Run script to generate PNGs and push result to magics-tests
  # Only run on tagged linux builds
  - |
    if [[ "$TRAVIS_OS_NAME" == "linux" && ! -z "$TRAVIS_TAG" ]]; then
      git config --global user.email "magics-travis@ecmwf.int"
      git config --global user.name "Magics Travis CI"
      cd ${MAGICS_TEST_SRC}
      python3 build_regression.py
      git add *
      git commit --message "Magics Travis CI: $TRAVIS_TAG"
      git remote add travis-push https://${GH_USER}:${GH_TOKEN}@github.com/ecmwf/magics-test.git > /dev/null 2>&1
      git tag $TRAVIS_TAG
      git push --quiet --set-upstream travis-push master > /dev/null 2>&1
      git push --tags > /dev/null 2>&1
    fi
