name: RPM

on:
  push:
    branches:
      - master
      - 1.3.7
  pull_request:
    branches:
      - master
      - 1.3.7

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        container:
          - centos:7
          - centos:8

    container: ${{ matrix.container }}

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

      - name: Configure Centos 7 repos
        if: ${{ matrix.container == 'centos:7' }}
        run: |
          # Need to add other repos for e.g. libsodium
          yum install -y dnf-plugins-core epel-release
          # for mod_wrap
          yum install -y libnsl2-devel tcp_wrappers-devel

      - name: Configure Centos 8 repos
        if: ${{ matrix.container == 'centos:8' }}
        run: |
          # Need to add other repos for e.g. libsodium
          yum install -y dnf-plugins-core epel-release
          yum config-manager --set-enabled powertools
          # for mod_wrap
          yum install -y libnsl2-devel https://pkgs.dyn.su/el8/extras/x86_64/tcp_wrappers-libs-7.6-77.el8.x86_64.rpm https://pkgs.dyn.su/el8/extras/x86_64/tcp_wrappers-devel-7.6-77.el8.x86_64.rpm

      - 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"
