language: cpp
dist: trusty
osx_image: xcode10.2
cache: ccache

os:
  - linux
  - osx

branches:
  only:
    - main
    - release
    - testing
    - /^feature.+$/

env:
  global:
    # $TAGPERM environment variable
    # generate a token at https://github.com/settings/tokens and encrypt it
    # with `travis encrypt TAGPERM=<yoursecrettoken> --add env.global`
    # also see: https://docs.travis-ci.com/user/encryption-keys/#Usage
    # and: https://docs.travis-ci.com/user/best-practices-security/
    - secure: AERYyBcRb6amrkLUzmbUWMOv80evhJDQd9tFNyRIUztooDCLv1/ncllhoXYglBVQUqqk7seKHFS5LZ3mvi62NnEtLkc7140HRWXYKmVb65r1zyoleA6sBZYKwunQzT3sPL/HbUphrDVIsvn/xzBWNlMS6DHSefg0LnEW8J6ArKE=
    # KEY_PASSWORD
    - secure: "Mte2zY0rgIa4M4bIeSKijFAFOtFy+DahRYCUgOB60cuS4b/+HMHnfmVGMqnBKitC9KVzb5fctMt2pGPK2hdlirvfkOd9ctEaEd1qSzADJjfQUKqLReOfi1N6Ln2dYqlZoDjnYpGWwTUl/uX+KtWomYW2TCZZG/uJEeh3QYkxlJQ="
    # DEVELOPER_NAME
    - secure: "CrB159u8KEwtsiCxnwvnCumiRkGgL+9aFgI2dosUvj3PtfebcVdhzRKBt+17jLapLCQEYoml8RVszLNuYpZu8y/3DhCk4ftWMumnvKOp8CIFb/4BFKMZxyuw+HlMg5oKwY4GOopnHo4mTEM51pDHMP8rfIxCjLimVeDc0uZaaj8="
    # CCACHE
    - USE_CCACHE=1
    - CCACHE_COMPRESS=1
    - CCACHE_MAXSIZE=1G
    - CCACHE_CPP2=1
  matrix:
    - CONFIG=release
    #- CONFIG=debug

install:
  - |
    if [[ "$TRAVIS_OS_NAME" == "linux" ]]
    then
      lsb_release -a
      sudo apt-add-repository -y ppa:ubuntu-toolchain-r/test
      sudo apt-add-repository -y ppa:beineri/opt-qt562-trusty
      sudo apt-get -qq update
      sudo apt-get -qq install g++-4.8 libc6-i386 qt56tools qt56svg qt56script qt56websockets qt56x11extras
      export CXX="g++-4.8"
      export CC="gcc-4.8"
    fi
  # OS X Qt http://braumeister.org/search/qt5
  # we need to use macdeployqtfix after using macdeployqt to fix the library paths!
  - |
    if [[ "$TRAVIS_OS_NAME" == "osx" ]]
    then
      brew update > /dev/null
      brew install qt5 ccache
      export PATH="/usr/local/opt/ccache/bin:$PATH"
      export QTDIR="/usr/local/opt/qt5"
    fi

script:
  - |
    if [[ "$TRAVIS_OS_NAME" == "linux" ]]
    then
      QTDIR="/opt/qt56"
      PATH="$QTDIR/bin:$PATH"
      qt56-env.sh
    fi
  - |
    if [[ "$TRAVIS_OS_NAME" == "osx" ]]
    then
      PATH="$QTDIR/bin:$PATH"
      LDFLAGS=-L$QTDIR/lib
      CPPFLAGS=-I$QTDIR/include
    fi
  - cd ./tests
  - qmake DEFINES+=INTEGRATION_TESTS CONFIG+=ccache && make -s -j 2
  - if [ "${TRAVIS_OS_NAME}" = "linux" ]; then
    if [ "${TRAVIS_BRANCH}" != "release" ]; then
    ./../bin/tests/tests -platform minimal -txt
    && ../travis/linux/coveralls.sh
    ;
    fi
    ;
    else
    ./../bin/tests/tests.app/Contents/MacOS/tests -platform minimal -txt
    ;
    fi
  - cd ./../src
  - echo "#define BUILD ""$TRAVIS_BUILD_NUMBER" > build_number.h
  - echo "#define RELEASE \"Travis CI\"" > release.h
  - qmake QOwnNotes.pro CONFIG+=$CONFIG CONFIG+=ccache
  - lrelease QOwnNotes.pro
  - make

before_deploy:
  - export VERSION_NUMBER=`cat version.h | sed "s/[^0-9,.]//g"`
  # create debian package for linux and a disk image for OSX
  - |
    if [[ "$TRAVIS_OS_NAME" == "linux" ]]
    then
      cd ../travis
      dpkg-deb -x QOwnNotes.deb build
      install ../src/QOwnNotes build/opt/QOwnNotes
      mkdir -p build/usr/bin
      ln -s /opt/QOwnNotes/QOwnNotes build/usr/bin
      mv ../src/languages/*.qm build/opt/QOwnNotes/
      mv ../icons/* build/opt/QOwnNotes/
      sed -i 's/VERSION_NUMBER/'"$VERSION_NUMBER"'/g' build/DEBIAN/control
      dpkg -b build QOwnNotes-$VERSION_NUMBER.deb
      cp QOwnNotes-$VERSION_NUMBER.deb QOwnNotesDev-$TRAVIS_COMMIT.deb
    fi
  - |
    if [[ "$TRAVIS_OS_NAME" == "osx" ]]
    then
      ../travis/create-dmg.sh
      cp QOwnNotes-$VERSION_NUMBER.dmg QOwnNotesDev-$TRAVIS_COMMIT.dmg
    fi
  #
  # add a tag
  #
  - TIME_STRING=`date +%H%M%S`
  - git config --global user.email "builds@travis-ci.com"
  - git config --global user.name "Travis CI"
  - if [ "${TRAVIS_OS_NAME}" == "linux" ]; then
    if [ "${TRAVIS_BRANCH}" == "release" ]; then
    export GIT_TAG=linux-b$TRAVIS_BUILD_NUMBER-$TIME_STRING
    ;
    else
    export GIT_TAG=dev-linux-b$TRAVIS_BUILD_NUMBER-$TIME_STRING
    ;
    fi
    ;
    else
    if [ "${TRAVIS_BRANCH}" == "release" ]; then
    export GIT_TAG=macosx-b$TRAVIS_BUILD_NUMBER-$TIME_STRING
    ;
    else
    export GIT_TAG=dev-macosx-b$TRAVIS_BUILD_NUMBER-$TIME_STRING
    ;
    fi
    ;
    fi
  - git tag $GIT_TAG -a -m "Generated tag from TravisCI for build $TRAVIS_BUILD_NUMBER"
  - git push -q https://$TAGPERM@github.com/pbek/QOwnNotes --tags >/dev/null 2>&1

deploy:
  # Linux release release
  - provider: releases
    api_key:
      secure: DaUepv9X3aPy91r8FBCyA0yUEQ0CNYQyA5EzlXsvLWsTi7XjscWZXCkv2BoFxk+MXvsCweaS6/qRbIn8/cW1TFLo6ZAbHjucnmjtSZn9wr9j3SEV7fM6jpAc6XBMMHtgjCaKZMpvH1R2Ed6O3DNr7L0//zB/PDhQx+rDeDre1kA=
    file: QOwnNotes-$VERSION_NUMBER.deb
    skip_cleanup: true
    on:
      repo: pbek/QOwnNotes
      branch: release
      tags: false
      condition: $CONFIG = release && ${TRAVIS_OS_NAME} = linux
    # macOS release release
  - provider: releases
    api_key:
      secure: DaUepv9X3aPy91r8FBCyA0yUEQ0CNYQyA5EzlXsvLWsTi7XjscWZXCkv2BoFxk+MXvsCweaS6/qRbIn8/cW1TFLo6ZAbHjucnmjtSZn9wr9j3SEV7fM6jpAc6XBMMHtgjCaKZMpvH1R2Ed6O3DNr7L0//zB/PDhQx+rDeDre1kA=
    file: QOwnNotes-$VERSION_NUMBER.dmg
    skip_cleanup: true
    on:
      repo: pbek/QOwnNotes
      branch: release
      tags: false
      condition: $CONFIG = release && ${TRAVIS_OS_NAME} = osx
    # Linux development release
#  - provider: releases
#    api_key:
#        secure: DaUepv9X3aPy91r8FBCyA0yUEQ0CNYQyA5EzlXsvLWsTi7XjscWZXCkv2BoFxk+MXvsCweaS6/qRbIn8/cW1TFLo6ZAbHjucnmjtSZn9wr9j3SEV7fM6jpAc6XBMMHtgjCaKZMpvH1R2Ed6O3DNr7L0//zB/PDhQx+rDeDre1kA=
#    file: QOwnNotesDev-$TRAVIS_COMMIT.deb
#    skip_cleanup: true
#    on:
#        repo: pbek/QOwnNotes
#        branch: main
#        tags: false
#        condition: ${TRAVIS_OS_NAME} = linux
# macOS development release
#  - provider: releases
#    prerelease: true
#    api_key:
#        secure: DaUepv9X3aPy91r8FBCyA0yUEQ0CNYQyA5EzlXsvLWsTi7XjscWZXCkv2BoFxk+MXvsCweaS6/qRbIn8/cW1TFLo6ZAbHjucnmjtSZn9wr9j3SEV7fM6jpAc6XBMMHtgjCaKZMpvH1R2Ed6O3DNr7L0//zB/PDhQx+rDeDre1kA=
#    file: QOwnNotesDev-$TRAVIS_COMMIT.dmg
#    skip_cleanup: true
#    on:
#        repo: pbek/QOwnNotes
#        branch: main
#        tags: false
#        condition: ${TRAVIS_OS_NAME} = osx

notifications:
  email:
    recipients:
      - developer@bekerle.com
    on_success: change
    on_failure: change
  #  irc:
  #    # https://docs.travis-ci.com/user/notifications/#IRC-notification
  #    channels:
  #      - "chat.freenode.net#qownnotes"
  #    template:
  #      - "[%{commit}] %{repository} (%{branch}): %{message} | Commit message: %{commit_message} | Changes: %{compare_url} | Build details: %{build_url}"
  #    on_success: never
  #    on_failure: change
  #    use_notice: true
  #    skip_join: true
  webhooks:
    urls:
      - https://webhooks.gitter.im/e/7c80d5baa6681f02dca5
      #- https://fathomless-fjord-24024.herokuapp.com/notify
      #- "https://scalar.vector.im/api/neb/services/hooks/dHJhdmlzLWNpLyU0MHBiZWslM0FtYXRyaXgub3JnLyUyMXJVenJSdnJuck9zTGFzRGRicCUzQW1hdHJpeC5vcmc"
    on_success: change # options: [always|never|change] default: always
    on_failure: always # options: [always|never|change] default: always
    on_start: never # options: [always|never|change] default: always
