name: RPM

on:
  push:
    branches:
      - master
      - 1.3.9
    paths-ignore:
      - NEWS
      - RELEASE_NOTES
      - 'doc/**'
      - '**/*.html'
      - '**/*.md'
  pull_request:
    branches:
      - master
      - 1.3.9
    paths-ignore:
      - NEWS
      - RELEASE_NOTES
      - 'doc/**'
      - '**/*.html'
      - '**/*.md'

jobs:
  build:
    runs-on: ubuntu-latest

    env:
      # GitHub runners still causing problems with NodeJS v20; see:
      #  https://github.com/actions/checkout/issues/1809.
      ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true

    strategy:
      matrix:
        container:
          - almalinux:8

    container: ${{ matrix.container }}

    steps:
      - name: Checkout source code
        uses: actions/checkout@v3

      - name: Configure RPM repos
        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
          dnf config-manager --enable epel
          dnf config-manager --set-enabled powertools

      - name: Install packages
        run: |
          # for builds
          yum install -y bash gcc make imake
          # for rpm builds
          yum install -y rpm-build rpmlint redhat-rpm-config
          # for dependencies
          yum install -y GeoIP-devel
          yum install -y gettext
          yum install -y hiredis-devel
          yum install -y libacl-devel
          yum install -y libcap-devel
          yum install -y libmemcached-devel
          yum install -y libsodium-devel
          yum install -y mysql-devel
          yum install -y ncurses-devel
          yum install -y openldap-devel cyrus-sasl-devel
          yum install -y openssl-devel
          yum install -y pam-devel
          yum install -y pcre-devel
          yum install -y postgresql-devel
          yum install -y sqlite-devel
          yum install -y zlib-devel

      - name: Generate RPM spec
        run: |
          ./configure
          make
          # To properly name the tarball later with the version suffix, we need
          # to query the built `proftpd` binary for its version.
          echo "release_version=$(./proftpd -v | cut -f3 -d' ')" >> $GITHUB_ENV
          make dist

      - name: Check RPM spec
        run: |
          rpmlint proftpd.spec

      - name: Build release tarball
        run: |
          cd ..
          mv proftpd "proftpd-${{ env.release_version }}"
          tar zcf "/tmp/proftpd-${{ env.release_version }}.tar.gz" "proftpd-${{ env.release_version }}"
          mv "proftpd-${{ env.release_version }}" proftpd

      - name: Build RPM from release tarball
        run: |
          rpmbuild -tb -vvv --with everything "/tmp/proftpd-${{ env.release_version }}.tar.gz"
