name: coverity-scan

on:
  schedule:
    - cron: "0 00 * * *" # Daily at 00:00 UTC
  workflow_dispatch:

env:
  TOKEN: ${{ secrets.COVERITY_SCAN_TOKEN }}
  EMAIL: ${{ secrets.COVERITY_SCAN_EMAIL }}
  SHA1: ${{ github.sha }}
  FILE: exult-ubuntu-gcc.tgz
  PROJ: exult/exult

jobs:
  coverity-linux:
    if: ${{ github.repository_owner == 'exult' }}
    runs-on: ubuntu-latest
    steps:
      - name: Activity check
        run: |
          DATEUTC=$(curl -sL https://api.github.com/repos/exult/exult/commits | jq -r '[.[]][0]' | jq -r '.commit.committer.date')
          TIMESTAMP=$(date --utc -d "$DATEUTC" +%s)
          DAYS=$(( ($(date --utc -d "0:00" +%s) - $TIMESTAMP) / 86400 ))
          if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
            echo EXULT_REPO_ALIVE=true >> $GITHUB_ENV
            echo event is workflow_dispatch. Force building snapshot not doing activity check
          else
            if [ $DAYS -gt 0 ]; then
              echo EXULT_REPO_ALIVE=false >> $GITHUB_ENV
              echo "No activity in 24 hours before midnight UTC not running Coverity. Last activity at $DATEUTC."
            else
              echo EXULT_REPO_ALIVE=true >> $GITHUB_ENV
            fi
          fi
      - name: Install dependencies
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        run: |
          if ! apt-fast -- help &> /dev/null; then
            sudo add-apt-repository -u -y ppa:apt-fast/stable
            sudo apt-get update
            echo debconf apt-fast/maxdownloads string 16 | sudo debconf-set-selections
            echo debconf apt-fast/dlflag boolean true | sudo debconf-set-selections
            echo debconf apt-fast/aptmanager string apt-get | sudo debconf-set-selections
            DEBIAN_FRONTEND=noninteractive sudo apt install -y apt-fast
          else
            sudo apt-fast update
          fi
          sudo apt-fast install -y \
            zlib1g-dev libogg-dev libvorbis-dev libasound2-dev libfluidsynth-dev libsdl2-dev libsdl2-image-dev libpng-dev libfreetype6-dev libgtk2.0-dev libgtk-3-dev \
            libgdk-pixbuf2.0-dev libxml2-dev bison flex timidity libgimp2.0-dev libicu-dev autoconf-archive
      - name: Download Coverity Build Tool
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        run: |
          wget -q https://scan.coverity.com/download/Linux --post-data "token=$TOKEN&project=$PROJ" -O /tmp/cov-analysis-linux64.tar.gz
          mkdir /tmp/cov-analysis-linux64
          tar xzf /tmp/cov-analysis-linux64.tar.gz --strip 1 -C /tmp/cov-analysis-linux64
      - name: Checkout code
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        uses: actions/checkout@v4
      - name: Run autoreconf
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        run: |
          autoreconf -v -i
      - name: Configure
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        run: |
          ./configure --with-debug=extreme --enable-exult-studio --enable-exult-studio-support --enable-compiler --enable-gimp-plugin \
            --enable-zip-support --enable-shared --enable-midi-sfx --enable-shp-thumbnailer --enable-data --enable-mods \
            --with-usecode-debugger=yes --enable-usecode-container --enable-nonreadied-objects \
            --enable-aseprite-plugin
      - name: Build with cov-build
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        run: |
          export PATH=/tmp/cov-analysis-linux64/bin:$PATH
          cov-build --dir cov-int make -j 2
      - name: Submit the result to Coverity Scan
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        run: |
          tar czvf $FILE cov-int
          curl \
            --form project=$PROJ \
            --form token=$TOKEN \
            --form email=$EMAIL \
            --form file=@$FILE \
            --form version=master \
            --form description="$SHA1" \
            https://scan.coverity.com/builds?project=$PROJ
