#! /bin/bash

set -e

mkdir build || true
cd build

declare -a CMAKE_PLATFORM_FLAGS
if [[ ${target_platform} =~ .*linux.* ]]; then
  # To locate ICONV_INCLUDE_DIR and ICONV_LIBRARIES
  CMAKE_PLATFORM_FLAGS+=(-DCMAKE_FIND_ROOT_PATH="${PREFIX};${BUILD_PREFIX}/${HOST}/sysroot")
  CMAKE_PLATFORM_FLAGS+=(-DCMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES:PATH="${BUILD_PREFIX}/${HOST}/sysroot/usr/include")
fi

# We must avoid very long shebangs here.
echo '#!/usr/bin/env bash' > g-ir-scanner.sh
echo "${PYTHON} ${PREFIX}/bin/g-ir-scanner \$*" >> g-ir-scanner.sh
chmod +x ./g-ir-scanner.sh

cmake -G"${CMAKE_GENERATOR}" \
      -DCMAKE_PREFIX_PATH=${PREFIX} \
      -DCMAKE_INSTALL_PREFIX=${PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DENABLE_XPDF_HEADERS=True \
      -DENABLE_LIBCURL=True \
      -DENABLE_LIBOPENJPEG=openjpeg2 \
      -DENABLE_QT5=False \
      -DINTROSPECTION_SCANNER=${PWD}/g-ir-scanner.sh \
      "${CMAKE_PLATFORM_FLAGS[@]}" \
      ..

make -j${CPU_COUNT} ${VERBOSE_CM}
# make check requires a big data download
make install

pushd ${PREFIX}
  rm -rf lib/libpoppler*.la lib/libpoppler*.a share/gtk-doc
popd
