---
name: Linux and Tests

on:
  push:
    branches-ignore:
      - 'gh-pages'
  pull_request:
  workflow_dispatch:
    inputs:
      animview:
        description: 'AnimView?'
        type: boolean
      pr:
        description: 'Build this PR (optional)'
        type: number

jobs:
  Linux-apt-get:
    strategy:
      fail-fast: false
      matrix:
        include:
          - lua: LuaJIT
            packages: cmake libluajit2-5.1-dev # CMake 4, Openresty's luajit
            cmakejit: '-DWITH_LUAJIT=ON -DLUA_LIBRARY=/usr/lib/x86_64-linux-gnu/libluajit-5.1.so -DWITH_MOVIES=OFF -DWITH_MIDI_DEVICE=OFF'
          - lua: Lua 5.1
            packages: 'libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev librtmidi-dev'
            cmake: 1
            static_analysis: 1
            docs: 1
    runs-on: ubuntu-24.04 # noble
    name: Linux apt-get ${{matrix.lua}} ${{inputs.animview == 'true' && 'with AnimView' || ''}}
    steps:
      - uses: actions/checkout@v6  # Keeps the git OAuth token after checkout
      - name: Checkout selected PR
        env:
          GH_TOKEN: ${{github.token}}
        if: inputs.PR
        run: gh pr checkout --repo CorsixTH/CorsixTH ${{inputs.pr}}
      - name: Install CorsixTH and lua test requirements
        run: |
          sudo apt-get update
          sudo apt-get install ${{matrix.packages}} liblua5.1-dev lua5.1 luarocks libsdl2-dev libfreetype-dev \
           libsdl2-mixer-dev libcurl4-openssl-dev ${{inputs.animview == 'true' && 'libwxgtk3.0-gtk3-dev' || ''}}
          # Required for lua lint check
          sudo luarocks install luacheck
          # Required for lua unit tests
          sudo luarocks install busted
      - name: Install docs and static analysis requirements
        if: matrix.static_analysis
        run: |
          # Required for LDocGen
          sudo luarocks install lpeg
          sudo apt-get install doxygen yamllint
          sudo pip3 install -I codespell==2.2 cmakelint==1.4
          sudo luarocks install luafilesystem
      - name: Install CMake 3.14
        if: matrix.cmake
        run: |
          # Install CMake version 3.14, the oldest CorsixTH-supported version, which does not support Lua 5.4
          curl -L https://github.com/Kitware/CMake/releases/download/v3.14.6/cmake-3.14.6-Linux-x86_64.sh -o cmake.sh
          sudo bash cmake.sh --prefix=/usr/local/ --exclude-subdir --skip-license
          cmake --version
      - name: Create CorsixTH makefiles with ${{matrix.lua}}
        run: |
          cmake . -G"Unix Makefiles" -Bbuild ${{matrix.cmakejit}} --debug-output -LA \
           -DBuILD_ANIMVIEW=${{inputs.animview == 'true' && 'ON' || 'OFF'}}
      - name: Build CorsixTH with ${{matrix.lua}}
        run: |
          cmake --build build/ -- VERBOSE=1
          sudo cmake --build build/ -- install
      - name: Run lua tests
        if: matrix.static_analysis
        run: |
          # Validate lua files
          find CorsixTH -name '*.lua' -print0 | xargs -0 -I{} luac5.1 -p {}
          # Run lua lint check
          luacheck --quiet --codes --ranges CorsixTH
          # Run lua unit tests
          busted --verbose --directory=CorsixTH/Luatest
      - name: Run simple code tests
        if: matrix.static_analysis
        run: |
          # Check if there is bad whitespace.
          python3 scripts/check_whitespace.py -e scanner.cpp -e parser.cpp
          # Check for incorrectly encoded files.
          python3 scripts/check_language_files_not_BOM.py
          # Check if there are lua classes with invalid/improper declarations.
          python3 scripts/check_lua_classes.py
          # Check for tabs (should be spaces)
          ! grep -IrnP '\t' CorsixTH/Lua
          # Build and package LevelEdit
          ant -buildfile LevelEdit/build.xml dist
          # Run codespell (temporarily not a failure, Nov 2023)
          codespell --enable-colors --quiet-level 2 --skip="languages,corsix-th.6,*.dat" \
            -L sav,unexpect,persistance,defin,uint,inout,currenty,blong,falsy,manuel \
            AnimView CorsixTH CorsixTH/Lua/languages/english.lua LevelEdit libs || true
          # Cmake file linter
          cmakelint --filter=-linelength AnimView/CMakeLists.txt CMakeLists.txt CorsixTH/CMakeLists.txt \
            CorsixTH/CppTest/CMakeLists.txt CorsixTH/Src/CMakeLists.txt CorsixTH/SrcUnshared/CMakeLists.txt \
            libs/CMakeLists.txt libs/rnc/CMakeLists.txt CMake/GenerateDoc.cmake
          # Validate these build files
          yamllint --config-data "rules: {line-length: disable}" .github/workflows/*.yml
          shellcheck --shell sh scripts/macos_luarocks
          # Check the windows config file is up to date
          eval "$(luarocks --lua-version 5.1 path)"
          lua5.1 scripts/generate_windows_config.lua
          git diff --exit-code
      - name: Generate documentation
        if: matrix.docs
        run: |
          cmake --build build/ --target doc
      - name: Upload documentation
        if: github.ref == 'refs/heads/master' && github.repository == 'CorsixTH/CorsixTH' && matrix.docs
        run: |
          git config user.email "documentationbot@corsixth.com"
          git config user.name "Docs Bot"
          git fetch origin gh-pages
          git checkout --force gh-pages
          rsync --recursive build/doc/ .
          git add animview/ corsixth_engine/ corsixth_lua/ index.html leveledit/
          if ! git diff --cached --exit-code; then
            git commit --message "Documentation from $(git rev-parse --short master) [no CI]"
            git push origin gh-pages
          else
            echo "No change to documentation."
          fi

  Linux-vcpkg:
    runs-on: ubuntu-22.04
    name: Linux vcpkg Lua 5.5 ${{inputs.animview == 'true' && 'with AnimView' || ''}}
    env:
      VCPKG_BINARY_SOURCES: clear;files,${{github.workspace}}/vcpkg_cache,readwrite
    steps:
      - uses: actions/checkout@v6
      - name: Checkout selected PR
        env:
          GH_TOKEN: ${{github.token}}
        if: inputs.PR
        run: gh pr checkout --repo CorsixTH/CorsixTH ${{inputs.pr}}
      - name: Install build dependencies
        run:  |
          wget https://apt.llvm.org/llvm.sh
          chmod +x llvm.sh
          sudo ./llvm.sh 20
          sudo apt-get install libltdl-dev libx11-dev libxft-dev libxext-dev libxtst-dev \
            libxrandr-dev nasm clang-format-20 clang-tidy-20 autoconf-archive
      - name: Restore vcpkg cache
        uses: actions/cache@v5
        with:
          path: ${{ github.workspace }}/vcpkg_cache
          key: vcpkg-linux-${{hashFiles('vcpkg.json','vcpkg-configuration.json')}}
          restore-keys: vcpkg-linux-
      - name: Get CMake and Ninja
        uses: lukka/get-cmake@v4.3.3

      - name: Setup vcpkg
        uses: lukka/run-vcpkg@v11

      - name: Build CorsixTH, run tests
        uses: lukka/run-cmake@v10
        with:
          configurePreset: 'linux-dev-vcpkg'
          configurePresetAdditionalArgs: "['-DBUILD_ANIMVIEW=${{inputs.animview == 'true' && 'ON' || 'OFF'}}']"
          buildPreset: 'linux-dev-vcpkg'
          buildPresetAdditionalArgs: "['--verbose']"
          testPreset: 'linux-dev-vcpkg'

      - name: Run clang code tests
        run: |
          # Check cpp format
          clang-format-20 -i CorsixTH/Src/*.cpp CorsixTH/Src/*.h AnimView/*.cpp \
            AnimView/*.h libs/rnc/*.cpp libs/rnc/*.h CorsixTH/SrcUnshared/main.cpp
          git diff
          # Clang-tidy linter
          run-clang-tidy-20 -p build/dev-vcpkg
          git diff --quiet # exit if clang-format made any changes
