name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
  schedule:
    - cron: '0 0 * * 0'

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        compiler:
          - gcc
          - clang

    steps:
      - name: Checkout ProFTPD
        run: |
          git clone --depth 1 https://github.com/proftpd/proftpd.git proftpd

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

      - name: Whitespace check
        run: |
          cd proftpd-mod_proxy_protocol
          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_proxy_protocol/mod_proxy_protocol.c proftpd/contrib/

      - name: Install packages
        run: |
          # Need to add other repos for e.g. libsodium
          sudo apt-get update -qq
          # for unit tests
          sudo apt-get install -y check
          sudo apt-get install -y libsubunit-dev

          # for integration/regression test
          # for test code coverage
          sudo apt-get install -y lcov
          sudo gem install coveralls-lcov
          # for HTML validation
          sudo apt-get install -y tidy
          # for clang
          sudo apt-get install -y clang
          # for debugging
          clang --version
          gcc --version

      - name: Prepare code coverage
        run: |
          lcov --directory proftpd --zerocounters

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

      - name: Install with static modules
        run: |
          cd proftpd
          sudo make install

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

      - name: Install with shared modules
        run: |
          cd proftpd
          sudo make install

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