name: "Coverity Scan"

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

jobs:
  analyze:
    runs-on: ubuntu-latest

    env:
      COVERITY_SCAN_BRANCH_PATTERN:     "${{ github.ref}}"
      COVERITY_SCAN_NOTIFICATION_EMAIL: "guru.mail@muenster.de"
      COVERITY_SCAN_PROJECT_NAME:       "${{ github.repository }}"
      # Set in repo settings -> secrets -> repository secrets
      COVERITY_SCAN_TOKEN:              "${{ secrets.COVERITY_SCAN_TOKEN }}"
      CURRENT_REF:                      "${{ github.ref }}"

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

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

    - name: Download coverity scan build tool
      run: |
        wget -q https://scan.coverity.com/download/cxx/linux64 \
          --post-data "token=$COVERITY_SCAN_TOKEN&project=gansm%2Ffinalcut" \
          -O cov-analysis-linux64.tar.gz
        mkdir cov-analysis-linux64
        tar xzf cov-analysis-linux64.tar.gz --strip 1 -C cov-analysis-linux64

    - 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 \
          gcc \
          g++ \
          gpm \
          libgpm-dev \
          libcppunit-dev \
          autoconf-archive

    - name: Build with cov-build
      run: |
        export PATH=`pwd`/cov-analysis-linux64/bin:$PATH
        cat cov-int/scm_log.txt || echo
        autoreconf -v --install --force
        ./configure --prefix=/usr \
          CPPFLAGS="-DDEBUG" \
          CXXFLAGS="-g -O0 -DDEBUG -DUNIT_TEST" \
          --with-unit-test
        cov-build --dir cov-int make V=1 -j10

    - name: Submit the result to Coverity Scan
      run: |
        tar czvf finalcut.tgz cov-int
        curl \
        --form token=$COVERITY_SCAN_TOKEN \
        --form email=$COVERITY_SCAN_NOTIFICATION_EMAIL \
        --form file=@finalcut.tgz \
        --form version="main" \
        --form description="Build submitted via github action" \
        https://scan.coverity.com/builds?project=gansm%2Ffinalcut

    - name: Make unit test
      run: |
        script -e -c "make check" && RET=$? || RET=$?
        cat test/*.log || echo
        echo "Exit status: $RET"
        exit "$RET"

