# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=pegtl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.2.8
pkgrel=1
pkgdesc="Parsing Expression Grammar (PEG) Template Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/taocpp/PEGTL/'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/taocpp/PEGTL/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('319e8238daebc3a163f60c88c78922a8012772076fdd64a8dafaf5619cd64773')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # Remove Werror flag
  sed -i 's/-Werror//g' src/test/pegtl/CMakeLists.txt
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DPEGTL_INSTALL_DOC_DIR=share/doc/${_realname} \
      -DPEGTL_INSTALL_CMAKE_DIR=lib/cmake/${_realname} \
      -DPEGTL_BUILD_TESTS=OFF \
      -DPEGTL_BUILD_EXAMPLES=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
