name: "SonarCloud analysis"

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

jobs:
  build:
    runs-on: ubuntu-latest

    env:
      GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
      SONAR_TOKEN: "${{ secrets.SONAR_TOKEN }}"

    steps:
    - name: Repository checkout
      uses: actions/checkout@v3
      with:
        fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis

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

    - name: Build with SonarCloud
      run: |
        wget -q "https://sonarcloud.io/static/cpp/build-wrapper-linux-x86.zip"
        unzip -q "build-wrapper-linux-x86.zip" -d "/tmp" || echo
        LATEST_VERSION=$(curl -H "Accept: application/vnd.github.v3+json" \
                         https://api.github.com/repos/SonarSource/sonar-scanner-cli/releases/latest 2>/dev/null \
                         | grep "tag_name" \
                         | cut -d\" -f4)
        wget -q "https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${LATEST_VERSION}-linux.zip"
        unzip -q "sonar-scanner-cli-${LATEST_VERSION}-linux.zip" -d "/tmp" || echo
        echo "sonar.host.url=https://sonarcloud.io" >> /tmp/sonar-scanner-*/conf/sonar-scanner.properties
        mkdir /tmp/sonar-cache
        autoreconf -v --install --force
        ./configure --prefix=/usr
        /tmp/build-wrapper-linux-x86/build-wrapper-linux-x86-64 --out-dir bw-output make all
        /tmp/sonar-scanner-${LATEST_VERSION}-linux/bin/sonar-scanner

