name: Check style

on:
  push:
    branches-ignore:
      - gh-readonly-queue/**
      - master
  pull_request:
  merge_group:

jobs:
  check-style:
    runs-on: ubuntu-latest
    env:
      CARGO_HTTP_MULTIPLEXING: false
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Prepare Linux
      run: |
        sudo apt-get update -y
        sudo apt-get install ddnet-tools shellcheck tidy pkg-config cmake doxygen graphviz ninja-build libfreetype6-dev libnotify-dev libsdl2-dev libsqlite3-dev libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev libx264-dev python3-clang libvulkan-dev glslang-tools spirv-tools libglew-dev -y
        rustup default stable
        pip3 install pylint
        wget https://github.com/muttleyxd/clang-tools-static-binaries/releases/download/master-796e77c/clang-format-20_linux-amd64
        echo "bbb30a777d075dfbe91aa146a1ea1fa0c1d4745efa9c5490ebf9f5a7a7dec679c22f1c6015d5c85cb5b5b22964d86ae65002f1d4145bfdd7f27022d981b58979  clang-format-20_linux-amd64" | sha512sum -c
        mv clang-format-20_linux-amd64 ~/.local/bin/clang-format
        chmod +x ~/.local/bin/clang-format
        wget -O ~/.local/bin/shfmt https://github.com/mvdan/sh/releases/download/v3.8.0/shfmt_v3.8.0_linux_amd64
        chmod +x ~/.local/bin/shfmt

    - name: Cache Rust dependencies
      uses: Swatinem/rust-cache@v2

    - name: Check code style (ClangFormat)
      run: |
        clang-format -version
        scripts/fix_style.py --dry-run

    - name: Check documentation style (Doxygen)
      run: |
        doxygen --version
        doxygen -q
        # TODO: Files included for macro-based lists like config variables and editor actions cannot be found by Doxygen version 1.9.8 used in the CI.
        #       Remove this ignored warning when the version is upgraded to one where this is fixed.
        sed -i -E '/warning: include file .* not found, perhaps you forgot to add its directory to INCLUDE_PATH/d' docs/warn.log
        if [ -s docs/warn.log ]; then
          echo "--------------------------------------------------"
          echo "Doxygen warnings:"
          cat docs/warn.log
          exit 1
        fi

    - name: Shellcheck
      run: find . -type f -name '*.sh' -print0 | xargs -0 shellcheck

    - name: Shell format (shfmt)
      run: find . -type f -name '*.sh' -print0 | xargs -0 shfmt -d

    - name: Pylint
      run: |
        pylint --version
        find . -type f -name "*.py" -not -path './ddnet-libs/*' -not -path './googletest-src/*' -print0 | xargs -0 pylint --disable=no-else-raise,no-else-return,redefined-builtin,redefined-outer-name

    - name: Check header guards
      run: scripts/check_header_guards.py

    - name: Unused header files
      run: scripts/check_unused_header_files.py

    - name: Check standard header includes
      run: scripts/check_standard_headers.sh

    - name: Check absolute includes
      run: "! grep --exclude-dir rust-bridge -rE '^#include \"(antibot|base|engine|game|steam|test)/' src/"

    - name: Check config variables
      run: scripts/check_config_variables.py

    - name: Check commands & settings export
      run: |
        echo "<!DOCTYPE html><html><head><title></title></head><body>" > settingscommands.html
        scripts/export_settings_commands_table.py >> settingscommands.html
        echo "</body></html>" >> settingscommands.html
        tidy -errors --drop-empty-elements false settingscommands.html

    - name: Check log error format
      run: |
        if grep -Eqr '(msg|Print).*\(.*"[Ee]rror:' src/;
        then
          echo "Expected log errors to be in this format 'ERROR: error message'"
          echo "Found these non uppercased log errors:"
          grep -Er '(msg|Print).*\(.*"[Ee]rror:' src/
          exit 1
        fi

    - name: Validate languages
      run: scripts/languages/validate.py

    - name: Check languages
      run: scripts/languages/update_all.py

    - name: Build dilate tool
      run: |
        mkdir release
        cd release
        cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DDOWNLOAD_GTEST=OFF ..
        cmake --build . --config Release --target dilate

    - name: Check dilated images
      run: scripts/check_dilate.py release data

    - name: Build twmap tool
      run: |
        cargo install twmap-tools --git https://gitlab.com/Patiga/twmap.git/ --rev 1f192def57429a1f64ddb3ac8580744476cfdf1c --locked

    - name: Check maps
      run: |
        findings=$(find data -type f -name '*.map' ! -wholename 'data/maps/coverage.map' -print0 | xargs -0 ~/.cargo/bin/twmap-check-ddnet 2>&1 | \
          grep -v -E "\"data/maps/(ctf|dm).*\.map\": Time cps are missing on the map" | \
          grep -v -E "\"data/themes/.*\.map\": Time cps are missing on the map" | \
          grep -v -E "\"data/maps/ctf4\.map\": Map - In Image at index 3 -> Image 'jungle_doodads_old' is not a valid external image for DDNet06" | \
          grep -v -E "\"data/themes/autumn_night\.map\": Map - In Image at index 3 -> Image 'generic_lamps' is not a valid external image for DDNet06" | \
          grep -v -E "\"data/maps7?/Tutorial\.map\": Map setting - Unusual: 'sv_show_others_default'" || true)
        if [ -n "$findings" ]; then
          echo "$findings"
          exit 1
        fi
