name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  schedule:
    - cron: '12 2 * * 0'

jobs:
  build:
    runs-on: ubuntu-latest

    env:
      # Let's try opting into use of Nodejs v24; see:
      #  https://github.blog/changelog/2025-09-19-deprecation-of-node-20-on-github-actions-runners/
      # GitHub runners still causing problems with NodeJS v20; see:
      FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

    strategy:
      matrix:
        compiler:
          - clang
          - gcc
        container:
          - almalinux:8
          - alpine:3.18
          - ubuntu:22.04

    container: ${{ matrix.container }}

    steps:
      - name: Checkout ProFTPD
        uses: actions/checkout@v6
        with:
          repository: proftpd/proftpd
          path: proftpd

      - name: Checkout module source code
        uses: actions/checkout@v6
        with:
          path: proftpd-mod_procfs

      - name: Whitespace check
        if: ${{ matrix.container == 'ubuntu:22.04' }}
        run: |
          apt-get update -qq
          apt-get install -y git
          cd proftpd-mod_procfs
          if [[ -n $(git diff --check HEAD^) ]]; then
            echo "You must remove whitespace before submitting a pull request"
            echo ""
            git diff --check HEAD^
            exit 1
          fi

      - name: Prepare module source code
        run: |
          cp proftpd-mod_procfs/mod_procfs.c proftpd/contrib/

      - name: Install Alpine packages
        if: ${{ matrix.container == 'alpine:3.18' }}
        run: |
          apk update
          # for builds
          apk add bash build-base clang compiler-rt gcc make zlib-dev

          # for debugging
          clang --version
          gcc --version

      - name: Install RPM packages
        if: ${{ matrix.container == 'almalinux:8' }}
        run: |
          # Need to add other repos for e.g. libsodium
          yum install -y dnf-plugins-core epel-release yum-utils clang gcc make zlib-devel
          dnf config-manager --enable epel
          dnf config-manager --set-enabled powertools

          # for debugging
          clang --version
          gcc --version

      - name: Install Ubuntu packages
        if: ${{ matrix.container == 'ubuntu:22.04' }}
        run: |
          apt-get update -qq
          # for builds
          apt-get install -y clang gcc make

          # for test code coverage
          apt-get install -y lcov ruby
          gem install coveralls-lcov

          # for HTML validation
          apt-get install -y tidy

          # for debugging
          clang --version
          gcc --version

      - name: Prepare code coverage
        if: ${{ matrix.container == 'ubuntu:22.04' }}
        run: |
          lcov --directory proftpd --zerocounters

      - name: Build as static module
        env:
          CC: ${{ matrix.compiler }}
        run: |
          cd proftpd
          ./configure --with-modules=mod_procfs
          make

      - name: Install as static module
        run: |
          cd proftpd
          make install

      - name: Build as shared module
        env:
          CC: ${{ matrix.compiler }}
        run: |
          cd proftpd
          make clean
          ./configure --enable-dso --with-shared=mod_procfs
          make

      - name: Install as shared module
        run: |
          cd proftpd
          make install

      - name: Check HTML docs
        run: |
          cd proftpd-mod_procfs
          echo "Processing mod_procfs.html"
          tidy -errors -omit -q mod_procfs.html | exit 0
