# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=antlr3
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.5.2
pkgrel=2
pkgdesc="ANother Tool for Language Recognition (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.antlr3.org/"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/antlr/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('a66d12fd5ca052fdd2b0a64b70d0c58db40e6dec72c3f4497934203090d92808')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  cd "${srcdir}"
  tar xf "${_realname}-${pkgver}.tar.gz" "${_realname}-${pkgver}/runtime/C"

  cd "${_realname}-${pkgver}/runtime/C"
  sed -e "s/AM_INIT_AUTOMAKE(foreign)/AM_INIT_AUTOMAKE([foreign subdir-objects])/g" \
      -i configure.ac
  sed -e "s/\(libantlr3c_la_LDFLAGS.*\)/\1 -no-undefined\nlibantlr3c_la_LIBADD = -lws2_32/g" \
      -e "s/INCLUDES = -Iinclude/AM_CPPFLAGS = -I\$(srcdir)\/include/g" \
      -i Makefile.am

  autoreconf -fvi
}

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/runtime/C/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-abiflags

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
}
