name: CI

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

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        compiler:
          - clang
          - gcc
        container:
          - alpine:3.14
          - centos:8
          - ubuntu:18.04

    container: ${{ matrix.container }}

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

      - name: Checkout module source code
        uses: actions/checkout@v2
        with:
          path: proftpd-mod_case

      - name: Whitespace check
        if: ${{ matrix.container == 'ubuntu:18.04' }}
        run: |
          apt-get update -qq
          apt-get install -y git
          cd proftpd-mod_case
          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_case/mod_case.c proftpd/contrib/

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

          # for OpenSSL support
          apk add openssl openssl-dev

          # for debugging
          clang --version
          gcc --version
          openssl version -a

      - name: Install Centos packages
        if: ${{ matrix.container == 'centos:8' }}
        run: |
          # Need to add other repos for e.g. libsodium
          yum install -y dnf-plugins-core epel-release clang gcc make zlib-devel
          yum config-manager --set-enabled powertools
          # for unit tests
          yum install -y check-devel https://cbs.centos.org/kojifiles/packages/subunit/1.4.0/1.el8/x86_64/subunit-1.4.0-1.el8.x86_64.rpm https://cbs.centos.org/kojifiles/packages/subunit/1.4.0/1.el8/x86_64/subunit-devel-1.4.0-1.el8.x86_64.rpm

          # for OpenSSL support
          yum install -y openssl-devel

          # for debugging
          clang --version
          gcc --version
          openssl version -a

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

          # for OpenSSL support
          apt-get install -y libssl-dev

          # for integration/regression test
          apt-get install -y \
            libdata-dumper-simple-perl \
            libdatetime-perl \
            libfile-copy-recursive-perl \
            libfile-path-tiny-perl \
            libfile-spec-native-perl \
            libnet-ssh2-perl \
            libnet-ssleay-perl \
            libnet-telnet-perl \
            libposix-2008-perl \
            libtest-unit-perl \
            libtime-hr-perl \
            libwww-perl
          PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install Net::FTPSSL'

          # 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
          openssl version -a

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

      - name: Build as static module
        env:
          CC: ${{ matrix.compiler }}
        run: |
          cd proftpd
          ./configure LIBS="-lm -lsubunit -lrt -pthread" --enable-devel=coverage --enable-tests --with-modules=mod_case:mod_copy:mod_sftp:mod_tls
          make

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

      - name: Run integration tests
        if: ${{ matrix.compiler == 'gcc' && matrix.container == 'ubuntu:18.04' }}
        env:
          PROFTPD_TEST_BIN: /usr/local/sbin/proftpd
          PROFTPD_TEST_DIR: ${{ github.workspace }}/proftpd
        run: |
          cd proftpd-mod_case
          perl tests.pl

      - name: Build as shared module
        env:
          CC: ${{ matrix.compiler }}
        run: |
          cd proftpd
          make clean
          ./configure LIBS="-lm -lsubunit -lrt -pthread" --enable-devel --enable-dso --with-shared=mod_case
          make

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

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