---
name: Build & Test (CentOS 7)

on:
  push:
    branches:
      - master
      - distros/centos-7

env:
  build-dependencies: >-
    boost169-devel
    cairo-devel
    freetype-devel
    gdal-devel
    glib2-devel
    harfbuzz-devel
    httpd-devel
    iniparser-devel
    libcurl-devel
    libicu-devel
    libjpeg-turbo-devel
    libmemcached-devel
    libpng-devel
    librados2-devel
    libtiff-devel
    libwebp-devel
    libxml2-devel
    postgresql-devel
    proj-devel
    sqlite-devel
    zlib-devel
  mapnik-version: 3.0.24

jobs:
  build-and-test:
    name: Build & Test
    runs-on: ubuntu-latest
    container:
      image: centos:7
    env:
      LD_LIBRARY_PATH: /usr/local/lib
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Install `EPEL` yum repository
        uses: ./.github/actions/yum/install
        with:
          packages: epel-release
      - name: Provision environment
        uses: ./.github/actions/yum/provision
        with:
          packages: ${{ env.build-dependencies }}
      - name: Cache "Download `Mapnik`" & "Build & Install `Mapnik`"
        id: cache-mapnik
        uses: actions/cache@v2
        with:
          path: /usr/local/src/mapnik-${{ env.mapnik-version }}
          key: centos-7-mapnik-${{ env.mapnik-version }}
      - name: Download `Mapnik`
        if: steps.cache-mapnik.outputs.cache-hit != 'true'
        run: |
          mkdir /usr/local/src/mapnik-${{ env.mapnik-version }}
          cd /usr/local/src/mapnik-${{ env.mapnik-version }}
          curl --silent --location \
            https://github.com/mapnik/mapnik/releases/download/v${{ env.mapnik-version }}/mapnik-v${{ env.mapnik-version }}.tar.bz2 \
            | tar --verbose --extract --bzip2 --strip-components=1 --file=-
      - name: Build & Install `Mapnik`
        run: |
          # Export `GDAL_DATA` & `PROJ_LIB` variables and create directories (if needed)
          export GDAL_DATA=$(gdal-config --datadir)
          export PROJ_LIB=/usr/share/proj
          mkdir -p ${GDAL_DATA} ${PROJ_LIB}
          cd /usr/local/src/mapnik-${{ env.mapnik-version }}
          ./configure BOOST_INCLUDES=/usr/include/boost169 BOOST_LIBS=/usr/lib64/boost169
          JOBS=$(nproc) make
          make install
      - name: Link `iniparser.h` to `iniparser/iniparser.h`
        run: |
          mkdir /usr/include/iniparser
          ln -s /usr/include/iniparser.h /usr/include/iniparser/iniparser.h
      - name: Run `./autogen.sh`
        run: ./autogen.sh
      - name: Run `./configure`
        run: ./configure
      - name: Run `make`
        run: make
      - name: Run `make test`
        run: make test
      - name: Configure Apache HTTP Server
        run: |
          mkdir --parents /usr/share/javascript/leaflet
          curl --silent \
            "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js" \
            > /usr/share/javascript/leaflet/leaflet.min.js
          curl --silent \
            "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css" \
            > /usr/share/javascript/leaflet/leaflet.css
          mkdir --parents /run/renderd /var/cache/renderd/tiles
          ln --symbolic \
            "${PWD}/utils/example-map" \
            /var/www/
          ln --symbolic \
            /usr/share/javascript/leaflet \
            /var/www/example-map/leaflet
          ln --symbolic \
            "${PWD}/etc/renderd/renderd.conf.examples" \
            /etc/renderd.conf
          ln --symbolic \
            "${PWD}/etc/apache2/renderd.conf" \
            /etc/httpd/conf.d/renderd.conf
          ln --symbolic \
            "${PWD}/etc/apache2/renderd-example-map.conf" \
            /etc/httpd/conf.d/renderd-example-map.conf
          echo "LoadModule tile_module /usr/lib64/httpd/modules/mod_tile.so" \
            | tee --append /etc/httpd/conf.modules.d/11-mod_tile.conf
          sed --in-place \
            "s#/usr/lib/mapnik/3.0/input#/usr/local/lib/mapnik/input#g" \
            /etc/renderd.conf
          sed --in-place \
            "s#/usr/share/fonts/truetype#/usr/share/fonts#g" \
            /etc/renderd.conf
          rm --force /etc/httpd/conf.d/welcome.conf
      - name: Run `make install`
        run: make install
      - name: Run `make install-mod_tile`
        run: make install-mod_tile
      - name: Start `renderd`
        run: renderd
      - name: Start Apache HTTP Server
        run: httpd
      - name: Test Apache HTTP Server `mod_tile` module
        run: |
          curl --silent http://localhost/renderd-example/tiles/9/297/191.png \
            | sha224sum - \
            | grep 9cd82e5af9d9002a1c75126ebdb7bf054ec0b7ed0db228dfb0a09bae
