# $Id$
# Maintainer (MSYS2): Ray Donnelly <mingw.android@gmail.com>
# Contributer: Tim Stahlhut <stahta01@gmail.com>

_realname=indent
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.12
pkgrel=3
pkgdesc="C language source code formatting program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.gnu.org/software/indent/"
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-gettext")
makedepends=('tar' 'texinfo-tex')
source=(https://ftp.gnu.org/gnu/indent/${_realname}-${pkgver}.tar.gz{,.sig}
        indent-2.2.11-segfault.patch
        indent-2.2.11-disable-documentation.patch
        indent-2.2.11-win32-wildexp-automake-support.patch
        indent-2.2.12-disable-texinfo2man.patch
        indent-2.2.12-fix-file-read-loop.patch)
sha256sums=('e77d68c0211515459b8812118d606812e300097cfac0b4e9fb3472664263bb8b'
            'SKIP'
            '63c66b49831a90c5191cd3295d17a4e1888d8fa2a0b02589448de3b78d4e0293'
            '624ae1c52b01e52dfd4417bb160ef606342f25548fbd90c58ef4962367179750'
            'd0ecfbc7cbf694b4222545bbd19d1b81f320ee9f5a2d5bfc86f27fe365f63145'
            'b8489a9dd7e4983f63ba6960bc3b1d8f51dd6ae08c29c6e8a9878491d0bf5e11'
            'c96f9a9a80be3fc9d53ea44369865e8ddc10187cc875a0dffcfa423a25be679a')
validpgpkeys=('782130B4C9944247977B82FD6EA4D2311A2D268D')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# ======================================= #

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

  del_file_exists src/wildexp.h

  apply_patch_with_msg \
    indent-2.2.11-segfault.patch \
    indent-2.2.11-disable-documentation.patch \
    indent-2.2.11-win32-wildexp-automake-support.patch \
    indent-2.2.12-disable-texinfo2man.patch \
    indent-2.2.12-fix-file-read-loop.patch

  autoreconf -vfi
}

build() {
  CFLAGS+=" -Wno-error=nested-externs"
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf ${_realname}-${pkgver} "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --{host,build}=${MINGW_CHOST}

  make
}

check() {
  cd "${srcdir}"/${_realname}-${pkgver}/regression
  make test
}

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