language: cpp
env:
    global:
          # SECRET_KEY, used to push docs to github and to init coverity scans
        - secure: "i2f2UVDnyHT/9z0U3XvgTj8eDERvnc1Wk7HpseEjb75JwGzqn/2R+RKHmoSrwK3hFgij2IMxZL19XtHFwMz9t5A/huAAKD74KMMI/QpeZEJ/sjT3CTLcE9HEVDdJOjc7dfLRxb2hZtgvx8clZIMrpeUdPhci8openff30KvXVbg="
          # DOCKER_USERNAME
        - secure: "kiR372QH5Srye2beHVamOVLIPeXnDipWfzvzGJEZzbpH+aXsiD+CkbtulCR+XnKpnUAXQTmEc5ts1KjI9MGlxvP1ztxW8HMDGUMF4iFAjgZO8GyAZlH5I7pMEw7D5pn3W9y1LuCW5C9IsDcWnNTJkm32D7N34lLBCTQVw68ooDk="
          # DOCKER_PASSWORD
        - secure: "FMKQarGQJ/MFXnQQWEnlWMM+XItbDPgm5tzCn4k36AsAB1s1SiQ08wmy2Ys/+kRvnPN3Clpl8P2C8CoRTMJ8WCUYZVmf3HsqvsLdrODyusR5/N1y5eOKWxo+t1qN2Jzt6oIi/ofUZdn5mdzt8yif+ufxoez+2ncZDt5HoB/suHE="

# These linux-specific parameters could be moved into matrix.include items, but that's lots of repetition
sudo: required
dist: bionic
arch: amd64
services:
    - docker

# TODO: add Linux LLVM coverage; clang 3.5.0 on ubuntu trusty is too old.
matrix:
    fast_finish: true
    include:
        - os: linux
          compiler: gcc
          env: BUILD_TYPE=normal BUILD_WITH=cmake
        - os: linux
          compiler: gcc
          env: BUILD_TYPE=normal BUILD_WITH=autoconf
        - os: linux
          compiler: clang
          env: BUILD_TYPE=asan BUILD_WITH=cmake
        - os: linux
          compiler: clang
          env: BUILD_TYPE=tsan BUILD_WITH=cmake
          # TODO: enable
          # - os: linux
          #   compiler: clang
          #   env: BUILD_TYPE=msan BUILD_WITH=cmake
          # - os: linux
          #   compiler: clang
          #   env: BUILD_TYPE=ubsan BUILD_WITH=cmake
        - os: linux
          compiler: gcc
          env: BUILD_TYPE=normal BUILD_WITH=cmake
          arch: arm64
        - os: osx
          osx_image: xcode9.3 # macOS 10.13
          compiler: clang
          env: BUILD_TYPE=normal BUILD_WITH=cmake
        - os: osx
          osx_image: xcode9.3 # macOS 10.13
          compiler: clang
          env: BUILD_TYPE=normal BUILD_WITH=autoconf
        - os: linux
          compiler: gcc
          env: BUILD_TYPE=tarball BUILD_WITH=cmake
        - os: linux
          compiler: gcc
          env: BUILD_TYPE=tarball BUILD_WITH=autoconf
        - stage: deploy
          os: linux
          before_install:
          install:
            - if [[ "$TRAVIS_REPO_SLUG" == "znc/znc" && "$TRAVIS_PULL_REQUEST" == "false" && "$TRAVIS_BRANCH" == "master" ]]; then ATTEMPT_DEPLOY=yes; else ATTEMPT_DEPLOY=no; fi
            - if [[ "$ATTEMPT_DEPLOY" == "yes" ]]; then openssl aes-256-cbc -d -in .travis-github.enc -out ~/znc-github-key -k ${SECRET_KEY}; fi
            - export SECRET_KEY=no DOCKER_PASSWORD=no DOCKER_USERNAME=no
            - if [[ "$ATTEMPT_DEPLOY" == "yes" ]]; then sudo apt-get update; fi
            - if [[ "$ATTEMPT_DEPLOY" == "yes" ]]; then sudo apt-get install -y doxygen graphviz python3-yaml; fi
          script:
            - |
                  if [[ "$ATTEMPT_DEPLOY" == "yes" ]]; then
                      mkdir -p ~/.ssh
                      chmod 0600 ~/znc-github-key
                      cp .travis.ssh ~/.ssh/config
                      git config --global user.email "travis-ci@znc.in"
                      git config --global user.name "znc-travis"
                  fi
            - if [[ "$ATTEMPT_DEPLOY" == "yes" ]]; then ./.travis-generate-docs.sh; fi
            - if [[ "$ATTEMPT_DEPLOY" == "yes" ]]; then ./.travis-coverity-scan.py; fi
          after_success:
        - stage: deploy
          os: linux
          before_install:
          install:
            - if [[ "$TRAVIS_REPO_SLUG" == "znc/znc" && "$TRAVIS_PULL_REQUEST" == "false" ]]; then ATTEMPT_DEPLOY=yes; else ATTEMPT_DEPLOY=no; fi
            - if [[ "$ATTEMPT_DEPLOY" == "yes" ]]; then echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin; fi
            - export SECRET_KEY=no DOCKER_PASSWORD=no DOCKER_USERNAME=no
          script:
            - echo "$TRAVIS_BRANCH-$(git describe)" > .nightly
            - docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` --build-arg VCS_REF=`git rev-parse HEAD` --build-arg VERSION_EXTRA=+docker-git- -t "zncbouncer/znc-git:$TRAVIS_BRANCH" -t "zncbouncer/znc-git:$TRAVIS_BRANCH-$(git describe)" .
            - if [[ "$ATTEMPT_DEPLOY" == "yes" && "$TRAVIS_BRANCH" == "master" ]]; then docker tag "zncbouncer/znc-git:$TRAVIS_BRANCH" zncbouncer/znc-git:latest; fi
            - if [[ "$ATTEMPT_DEPLOY" == "yes" ]]; then docker push zncbouncer/znc-git; fi
          after_success:
before_install:
    - python -c "import fcntl; fcntl.fcntl(1, fcntl.F_SETFL, 0)"  # https://github.com/travis-ci/travis-ci/issues/8920
    - "echo os: [$TRAVIS_OS_NAME] build: [$BUILD_TYPE]"
    - export SECRET_KEY=no DOCKER_PASSWORD=no DOCKER_USERNAME=no
    - export CFGFLAGS= MYCXXFLAGS= MYLDFLAGS=
    - if [[ "$BUILD_TYPE" == "tarball" ]]; then CFGFLAGS+=" --with-gtest=$TRAVIS_BUILD_DIR/third_party/googletest/googletest --with-gmock=$TRAVIS_BUILD_DIR/third_party/googletest/googlemock --disable-swig"; fi
    - if [[ "$BUILD_TYPE" == "asan" ]]; then MYCXXFLAGS+=" -fsanitize=address -O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fPIE" MYLDFLAGS+=" -fsanitize=address -pie"; fi
    - if [[ "$BUILD_TYPE" == "tsan" ]]; then MYCXXFLAGS+=" -fsanitize=thread -O1 -fPIE" MYLDFLAGS+=" -fsanitize=thread"; fi
    - if [[ "$BUILD_TYPE" == "msan" ]]; then MYCXXFLAGS+=" -fsanitize=memory -O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins" MYLDFLAGS+=" -fsanitize=memory"; fi
    - if [[ "$BUILD_TYPE" == "ubsan" ]]; then MYCXXFLAGS=" -fsanitize=undefined -O1 -fPIE -fno-sanitize-recover" MYLDFLAGS="-fsanitize=undefined -pie -fno-sanitize-recover"; fi
    - if [[ "$BUILD_WITH" == "cmake" ]]; then CFGSUFFIX=.sh UNITTEST=unittest INTTEST=inttest; else CFGSUFFIX= UNITTEST=test INTTEST=test2; fi
    - if [[ "$CC" == "gcc" ]]; then MYCXXFLAGS+=" --coverage" MYLDFLAGS+=" --coverage"; fi
    - if [[ "$CC" == "clang" ]]; then MYCXXFLAGS+=" -fprofile-instr-generate -fcoverage-mapping" MYLDFLAGS+=" -fprofile-instr-generate"; fi
install:
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then cat /proc/cpuinfo /proc/meminfo; fi
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then lsb_release -a; fi
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update; fi
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install -y libperl-dev tcl-dev libsasl2-dev libicu-dev swig qtbase5-dev libboost-locale-dev python3-pip cpanminus; fi
    - if [[ "$TRAVIS_OS_NAME" == "linux" && "$BUILD_WITH" == "cmake" ]]; then sudo apt-get install -y cmake; fi
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib); fi
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then cpanm --notest Devel::Cover::Report::Clover; fi
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export ZNC_MODPERL_COVERAGE=1; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sw_vers; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sysctl -a | grep cpu; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sysctl -a | grep mem; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew config; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew list --versions; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install swig icu4c jq qt5 gettext; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew outdated python || brew upgrade python; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" && "$BUILD_WITH" == "cmake" ]]; then brew outdated cmake || brew upgrade cmake; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew info --json=v1 --installed | jq .; fi
    - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then export PKG_CONFIG_PATH="$(brew --prefix qt5)/lib/pkgconfig:$PKG_CONFIG_PATH"; fi
    - pip3 install coverage
    - export ZNC_MODPYTHON_COVERAGE=1
    - "echo pkg-config path: [$PKG_CONFIG_PATH]"
script:
    - set -v
    - if [[ "$BUILD_TYPE" == "tarball" ]]; then ./make-tarball.sh --nightly znc-git-2015-01-16 /tmp/znc-tarball.tar.gz; fi
    - if [[ "$BUILD_TYPE" == "tarball" ]]; then cd /tmp; tar xvf znc-tarball.tar.gz; fi
    - if [[ "$BUILD_TYPE" == "tarball" ]]; then cd /tmp/znc-git-2015-01-16; fi
    - if [[ "$BUILD_TYPE" != "tarball" && "$BUILD_WITH" != "cmake" ]]; then ./bootstrap.sh; fi
    - mkdir build
    - cd build
    - ../configure$CFGSUFFIX --enable-debug --enable-perl --enable-python --enable-tcl --enable-cyrus --enable-charset $CFGFLAGS CXXFLAGS="$CXXFLAGS $MYCXXFLAGS" LDFLAGS="$LDFLAGS $MYLDFLAGS"
    - if [[ "$BUILD_WITH" == "cmake" ]]; then cmake --system-information; else cat config.log; fi
    - make VERBOSE=1
    - env LLVM_PROFILE_FILE="$PWD/unittest.profraw" make VERBOSE=1 $UNITTEST
    - sudo make install
      # TODO: use DEVEL_COVER_OPTIONS for https://metacpan.org/pod/Devel::Cover
    - env LLVM_PROFILE_FILE="$PWD/inttest.profraw" ZNC_MODPERL_COVERAGE_OPTS="-db,$PWD/cover_db" make VERBOSE=1 $INTTEST
    - /usr/local/bin/znc --version
after_success:
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ~/perl5/bin/cover --no-gcov --report=clover; fi
    - |
         if [[ "$TRAVIS_OS_NAME" == "osx" && "$CC" == "clang" ]]; then
             xcrun llvm-profdata merge unittest.profraw -o unittest.profdata
             xcrun llvm-profdata merge inttest.profraw -o inttest.profdata
             xcrun llvm-cov show -show-line-counts-or-regions -instr-profile=unittest.profdata test/unittest_bin > unittest-cmake-coverage.txt
             xcrun llvm-cov show -show-line-counts-or-regions -instr-profile=unittest.profdata unittest > unittest-autoconf-coverage.txt
             xcrun llvm-cov show -show-line-counts-or-regions -instr-profile=inttest.profdata /usr/local/bin/znc > inttest-znc-coverage.txt
             find /usr/local/lib/znc -name '*.so' -or -name '*.bundle' | while read f; do xcrun llvm-cov show -show-line-counts-or-regions -instr-profile=inttest.profdata $f > inttest-$(basename $f)-coverage.txt; done
         fi
    - bash <(curl -s https://codecov.io/bash)
notifications:
    irc:
        channels:
            # irc.freenode.net #znc-dev (via ZNC-Linker)
            - secure: "h06+4KSb2hM1BJWQqsrurqIU3F6fK55ATCRO7/t3FK6tbSHduywfbfcJmkPUD08lATCWaKm07afbVQPoU3dc6B+0rLvlvGmOoQlB6OxiROq+fc+gXXBwpnBJ1HKwmBrEActqkyZ8GWTVLeIhhR5xYVVZZv7AZ6jzCgpfyiXPink="
        on_success: always
        on_failure: always
        skip_join: true
        nick:
            secure: "UGh4tbpviOD438ZOi+I3W6eh+R68tbpNIJc0uf5FK6xhHLLo3FRIozkL7CRVA3TxYHuahwerZpBR4TbAyc+gVUn5YRHjdsMIPHPCW/kh8ZkEJKNYXkHerbH0Np6PRLI58l6lmjMQbiWn+lqlgsHdVsyknqxDwoj7JDieE+5X53c="
        password:
            secure: "B2F1coXddY1H0/v3OkiPnUlbnSN/wapRay1PUbN8tvm7IDYOjpivhsRk+31/4pauKOoF4AZhzP6F9mhGxkGYBQ23Td5Y6+uZeaDnTYnXjs8fbV0QY1Mx+2EkHDSkLQoYlYr06pK9q55X3uLK1zTz5Ye0Po3KzbeydxOyyG+xBGg="
