language: cpp

sudo: required
dist: xenial
services:
        - xvfb

branches:
    only:
        - master
        - development
        - /^v\d+\.\d+(\.\d+)?(-\S*)?$/

matrix:
    include:
        - os: linux
          env: QT_BASE=48 COMPILER=g++-5.4 CMAKE_BUILD_TYPE=Debug

        - os: linux
          env: QT_BASE=48 COMPILER=clang++ CMAKE_BUILD_TYPE=Debug

        - os: linux
          env: QT_BASE=5123 COMPILER=g++-5.4 CMAKE_BUILD_TYPE=RelWithDebInfo

        - os: linux
          env: QT_BASE=5123 COMPILER=clang++ CMAKE_BUILD_TYPE=Debug

        - os: osx
          env: CMAKE_BUILD_TYPE=RelWithDebInfo
          compiler: clang

before_install:
    - |
      export CHANGED_FILES=($(git diff --name-only ${TRAVIS_COMMIT_RANGE})) &&
      echo "Changed files: ${CHANGED_FILES}" &&
      export DETECTED_CHANGES_AFFECTING_BUILD=False &&
      for CHANGED_FILE in ${CHANGED_FILES}; do
        echo "Checking file ${CHANGED_FILE}" &&
        if ! [[ ${CHANGED_FILE} =~ "*.md" ]] && [[ "${CHANGED_FILE}" != "LICENSE" ]]; then
          echo "Detected changes affecting build: file ${CHANGED_FILE}" &&
          DETECTED_CHANGES_AFFECTING_BUILD=True &&
          break
        fi
      done
    - |
      if [[ ${DETECTED_CHANGES_AFFECTING_BUILD} == False ]]; then
        echo "No changes affecting build detected, exiting" &&
        travis_terminate 0 &&
        exit 1
      fi

install:
    - |
      if [ "${TRAVIS_OS_NAME}" = "linux" ]; then
        wget http://www.cmake.org/files/v3.2/cmake-3.2.0-Linux-x86_64.tar.gz &&
        tar -xzf cmake-3.2.0-Linux-x86_64.tar.gz &&
        sudo cp -fR cmake-3.2.0-Linux-x86_64/* /usr &&
        sudo apt-get -qq install p7zip-full coreutils curl &&
        sudo apt-add-repository -y ppa:ubuntu-toolchain-r/test &&
        if [ "${QT_BASE}" = "5123" ]; then
          sudo apt-add-repository -y ppa:beineri/opt-qt-5.12.3-xenial &&
          sudo apt-get -qq update &&
          sudo apt-get -qq install qt512tools qt512base qt512webchannel qt512webengine qt512websockets mesa-common-dev libgl1-mesa-dev &&
          if [ "${COMPILER}" = "clang++" ]; then
            export CXX="clang++" &&
            export CC="clang"
          fi
        else
          export DISPLAY=:99.0 &&
          sudo apt-get -qq update &&
          sudo apt-get -qq install libqt4-dev libqt4-dev-bin libqt4-network libqtwebkit-dev &&
          if [ "${COMPILER}" = "clang++" ]; then
            export CXX="clang++" &&
            export CC="clang"
          fi
        fi
      else
        brew update &&
        brew unlink cmake &&
        brew install cmake &&
        brew link cmake &&
        brew install p7zip &&
        brew install qt5 &&
        chmod -R 755 /usr/local/opt/qt5/*
      fi

before_script:
    - export QEVERCLOUD_DIR=$(pwd)
    - mkdir build
    - cd build
    - cmake --version
    - |
      if [ "${QT_BASE}" = "5123" ]; then
        source /opt/qt512/bin/qt512-env.sh &&
        cmake -DCMAKE_INSTALL_PREFIX=$(pwd)/installdir ..
      elif [ "${TRAVIS_OS_NAME}" = "osx" ]; then
        cmake -DCMAKE_PREFIX_PATH=/usr/local/opt/qt5/lib/cmake -DCMAKE_INSTALL_PREFIX=$(pwd)/installdir ..
      else
        cmake -DCMAKE_INSTALL_PREFIX=$(pwd)/installdir -DBUILD_WITH_QT4=1 ..
      fi

script:
    - make
    - make check
    - make install
    - |
      if [ "${TRAVIS_OS_NAME}" = "linux" ] && [ "${QT_BASE}" = "592" ] && [ "${COMPILER}" = "g++-4.8" ] && [ "${TRAVIS_BRANCH}" = "development" ]; then
        echo "Triggering build at OpenSUSE build service for development branch" &&
        curl -X POST -H "Authorization: Token $OSC_TOKEN" https://api.opensuse.org/trigger/runservice?project=home%3Ad1vanov%3Aquentier-development&package=qevercloud-development
      fi

after_success:
    - |
      if [ "${TRAVIS_OS_NAME}" = "linux" ] && [ "${QT_BASE}" = "5123" ] && [ "${COMPILER}" = "g++-5.4" ]; then
        if [ "${TRAVIS_BRANCH}" = "master" ] || [ "${TRAVIS_BRANCH}" = "development" ]; then
          cd $QEVERCLOUD_DIR/build/installdir &&
          7z a qevercloud_linux_qt_${QT_BASE}_x86_64.zip * &&
          wget https://github.com/d1vanov/ciuploadtool/releases/download/continuous-master/ciuploadtool_linux.zip &&
          unzip ciuploadtool_linux.zip &&
          chmod 755 ciuploadtool &&
          ./ciuploadtool -suffix="$TRAVIS_BRANCH" qevercloud_linux_qt_${QT_BASE}_x86_64.zip
        fi
      elif [ "${TRAVIS_OS_NAME}" = "osx" ]; then
        if [ "${TRAVIS_BRANCH}" = "master" ] || [ "${TRAVIS_BRANCH}" = "development" ]; then
          cd $QEVERCLOUD_DIR/build/installdir &&
          7z a qevercloud_mac_x86_64.zip * &&
          wget https://github.com/d1vanov/ciuploadtool/releases/download/continuous-master/ciuploadtool_mac.zip &&
          unzip ciuploadtool_mac.zip &&
          chmod 755 ciuploadtool &&
          ./ciuploadtool -suffix="$TRAVIS_BRANCH" qevercloud_mac_x86_64.zip
        fi
      fi
