# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=flex
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.5.39
pkgrel=1
pkgdesc="A tool for generating text-scanning programs"
arch=('any')
url="https://flex.sourceforge.io/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-m4" "${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-gettext")
options=('staticlibs' 'debug' '!strip')
source=(https://downloads.sourceforge.net/sourceforge/flex/${_realname}-$pkgver.tar.bz2
        0002-dont-wait-on-child-since-there-is-no-fork.patch
        0003-define-htonl-htons-for_WIN32.patch
        0004-check-for-regex-library-for-MinGW-w64.patch
        0005-filter-c-hack-out-fork-and-pipe.patch
        0006-MinGW-w64-open-in-binary-mode.patch)
sha256sums=('add2b55f3bc38cb512b48fad7d72f43b11ef244487ff25fc00aabec1e32b617f'
            'afe5bd1828031c002d8c09da911f28853cae1e214c00400dc3b39cb7cc5ecd26'
            '8069ab03b10e1dcd582c8e83b67488754306879e6bc875da75eda358c9d90d56'
            '885aa38d6fe4689b8d6c0f47e193c21f018da9a34dfab4eab02f878871c9b8f1'
            '260df2d6685fe8e63f5162f1d7570aac3d47eedc6c96ca49b1e2c38326833fda'
            'f0e753de6178b9a6c259cf42054b8d3449f0e2ed02009f9c6e108c35f8f5265a')

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

  patch -p1 -i "${srcdir}"/0002-dont-wait-on-child-since-there-is-no-fork.patch
  patch -p1 -i "${srcdir}"/0003-define-htonl-htons-for_WIN32.patch
  patch -p1 -i "${srcdir}"/0004-check-for-regex-library-for-MinGW-w64.patch
  patch -p1 -i "${srcdir}"/0005-filter-c-hack-out-fork-and-pipe.patch
  patch -p1 -i "${srcdir}"/0006-MinGW-w64-open-in-binary-mode.patch

  # Setting this to 1 will make debugging mutliple concurrent parse traces
  # difficult, so when get to that stage may want comment this out.
  if check_option "debug" "y"; then
    sed -i "s,int yydebug;,int yydebug = 1;,g" parse.c
  fi

  # these tests used features removed in bison-2.6
  sed -i -e '/test-bison-yylloc/d' -e '/test-bison-yylval/d' tests/Makefile.in

  ./autogen.sh
}

build() {
  mkdir -p "${srcdir}"/build-${CARCH}
  cd "${srcdir}"/build-${CARCH}
  if check_option "debug" "y"; then
    CPPFLAGS="$CPPFLAGS -DYYDEBUG=1"
  fi
  ../${_realname}-$pkgver/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --without-libiconv-prefix \
    --without-libintl-prefix \
    --enable-static \
    --disable-shared
  make
}

check() {
  cd "${srcdir}"/build-${CARCH}

  make check
}

package() {
  cd "${srcdir}"/build-${CARCH}

  make DESTDIR=$pkgdir install
  cp -f ${pkgdir}${MINGW_PREFIX}/bin/flex.exe ${pkgdir}${MINGW_PREFIX}/bin/flex++.exe
  cp -f ${pkgdir}${MINGW_PREFIX}/bin/flex.exe ${pkgdir}${MINGW_PREFIX}/bin/lex.exe

  install -Dm644 COPYING \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/license.txt
}

# Previous flex ports:
# https://downloads.sourceforge.net/project/flex/flex/2.5.4.a/flex-2.5.4a.tar.bz2
# https://downloads.sourceforge.net/project/gnuwin32/flex/2.5.4a-1/flex-2.5.4a-1-src.zip
