# 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.13
pkgrel=1
pkgdesc="C language source code formatting program (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.gnu.org/software/indent/"
msys2_references=(
  "cpe: cpe:/a:gnu:indent"
)
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-gettext-runtime")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-gperf")
source=(https://ftp.gnu.org/gnu/indent/${_realname}-${pkgver}.tar.xz{,.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
        006-disable-warnings.patch)
sha256sums=('1b81ba4e9a006ca8e6eb5cbbe4cf4f75dfc1fc9301b459aa0d40393e85590a0b'
            'SKIP'
            '63c66b49831a90c5191cd3295d17a4e1888d8fa2a0b02589448de3b78d4e0293'
            '8164941347f579d806e7fcbeb3f19aba3042bd47c5041be2e7ee57451e432c58'
            '36a202c81fbc60aa6416aacb0210ef527977102e10423289652d4658074bdb3f'
            'b8489a9dd7e4983f63ba6960bc3b1d8f51dd6ae08c29c6e8a9878491d0bf5e11'
            'c96f9a9a80be3fc9d53ea44369865e8ddc10187cc875a0dffcfa423a25be679a'
            '8ee008159b365bd8f7944ee7c57d4f03d337452742ab84fef0728ffebba64451')
validpgpkeys=('83DCD17F44B22CC83656EDA1E8446B4AC8C77261')

# 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 \
    006-disable-warnings.patch

  autoreconf -vfi
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -rf ${_realname}-${pkgver} "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  ./configure \
    --prefix=${MINGW_PREFIX}

  make
}

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

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