#!/bin/bash
set -eo pipefail
sudo apt-get update
sudo apt-get -y install \
   build-essential cmake \
   zlib1g-dev libjpeg-dev libgnutls28-dev libssl-dev \
   libzopfli-dev

cmake -S . -B build \
    -DCI_MODE=1 -DBUILD_STATIC_LIBS=0 -DCMAKE_BUILD_TYPE=Release \
    -DREQUIRE_CRYPTO_OPENSSL=1 -DREQUIRE_CRYPTO_GNUTLS=1 \
    -DENABLE_QTC=1 -DZOPFLI=1
cmake --build build --verbose -j$(nproc) -- -k

# Make sure we are using zopfli
export QPDF_ZOPFLI=force
zopfli="$(./build/zlib-flate/zlib-flate --_zopfli)"
if [ "$zopfli" != "11" ]; then
    echo "zopfli is not working"
    exit 2
fi

# If this fails, please see ZLIB COMPATIBILITY in README-maintainer.md.
# The tests are very slow with this option. Just run essential tests.
# If zlib-flate and qpdf tests all pass, we can be pretty sure it works.
(cd build; ctest --verbose -R zlib-flate)
(cd build; ctest --verbose -R qpdf)
