# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Paul Moore <p.f.moore@gmail.com>

_realname=diffutils
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.12
pkgrel=1
pkgdesc="GNU Diffutils is a package of several programs related to finding differences between files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.gnu.org/software/diffutils/'
msys2_repository_url='https://git.savannah.gnu.org/cgit/diffutils.git'
msys2_references=(
  "cpe: cpe:2.3:a:gnu:diffutils"
)
license=('GPL3')
depends=($([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-libsigsegv")
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-gettext")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"
        "001-msvc_invalid_parameter_handler.patch")
sha256sums=('7c8b7f9fc8609141fdea9cece85249d308624391ff61dedaf528fcb337727dfd'
            '8842a1acaf127087ae2c52ed1c7da9275c9ba8aae02ce0df7dd59dbac89b16d9')


prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p0 -i "${srcdir}"/001-msvc_invalid_parameter_handler.patch

  #make test cases work
  sed -i "s|case \$perms in drwx--\[-S\]---\*|case \$perms in drwx\*|" tests/init.sh
}

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

  CFLAGS+=" -Wno-incompatible-pointer-types"

  # it enables C23 while strptime.c uses old style functions still an fails
  export ac_cv_prog_cc_c23=no

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make WINDOWS_STAT_INODES=1
}

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

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/README" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/README"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
