name: "Code coverage"

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  schedule:
    - cron: '0 7 * * 2'

jobs:
  analyze:
    runs-on: ubuntu-latest

    env:
      COVERALLS_REPO_TOKEN: "${{ secrets.COVERALLS_REPO_TOKEN }}"

    steps:
    - name: Repository checkout
      uses: actions/checkout@v3

    - name: Before_install
      run: |
        lsb_release -a
        uname -a
        whoami
        tty || echo
        pwd

    - name: Install dependencies via apt-get
      run: |
        sudo add-apt-repository ppa:ubuntu-toolchain-r/test
        sudo apt-get update
        sudo apt-get -y install \
          ca-certificates \
          lcov \
          gcc \
          g++ \
          gpm \
          libgpm-dev \
          libcppunit-dev \
          autoconf-archive

    - name: Install coveralls-lcov
      run: sudo gem install coveralls-lcov

    - name: Build with with gcov
      run: script -e -c "./build.sh coverage"

    - name: Generate lcov.info and publish to coveralls.io
      run: |
        lcov --compat-libtool --directory . --capture --output-file lcov-all.info
        lcov --remove lcov-all.info '/usr/include/*' './include/*' --output-file lcov.info
        lcov --list lcov.info
        rm -f lcov-all.info
        coveralls-lcov --branch=${GITHUB_REF##*/} --repo-token "${{ secrets.COVERALLS_REPO_TOKEN }}" lcov.info

    - name: Codecov
      run: |
        ./.github/scripts/get_code_cov.sh
        curl https://keybase.io/codecovsecurity/pgp_keys.asc \
        | gpg --no-default-keyring --keyring trustedkeys.gpg --import # One-time step
        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

