# This file based on https://gist.github.com/mwouts/9842452d020c08faf9e84a3bba38a66f
# See: https://help.github.com/en/actions/reference/software-installed-on-github-hosted-runners
# 2020-06-22 - slg - customized
# 2020-06-27 - slg - expanded to G++ for MacOS
#
name: CI (c++)
on: [push, pull_request]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: ['ubuntu-latest', 'macos-latest']
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Install MacOS dependencies
        if: startsWith(matrix.os, 'macos')
        run: |
          brew install autoconf automake libtool

      - name: Install Ubuntu dependencies
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          sudo apt install -y libtool autoconf automake libssl-dev pkg-config

      - name: Update autoconf
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          wget https://ftpmirror.gnu.org/autoconf/autoconf-2.71.tar.gz
          tar xfz autoconf-2.71.tar.gz && cd autoconf-2.71 && ./configure && make && sudo make install

      - name: Run C++ checks
        if: startsWith(matrix.os, 'macos')
        run: |
          cd src
          bash bootstrap.sh && ./configure && make check

      - name: Run C++ checks with codecov
        if: startsWith(matrix.os, 'ubuntu')
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
        run: |
          cd src
          bash bootstrap.sh
          ./configure -q --with-no_optimize --enable-silent-rules \
                 CFLAGS='-g -O0 -fprofile-arcs -ftest-coverage' \
                 CXXFLAGS='-g -O0 -fprofile-arcs -ftest-coverage' \
                 LIBS='-lgcov'
          make check
          gcov-9 -abcfu *cpp *h > /dev/null

          #
          # set up and run codecov
          #
          sudo apt install gpg
          curl https://keybase.io/codecovsecurity/pgp_keys.asc | \
          gpg --no-default-keyring --keyring trustedkeys.gpg --import
          curl -Os https://uploader.codecov.io/latest/linux/codecov
          curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
          curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
          gpgv codecov.SHA256SUM.sig codecov.SHA256SUM
          shasum -a 256 -c codecov.SHA256SUM
          chmod +x codecov && ./codecov -t ${CODECOV_TOKEN}
          #
          # check cleanup
          #
          git status


      - name: GCC Problem Matcher
        uses: ammaraskar/gcc-problem-matcher@0.1
