# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=patch
pkgver=2.7.6
pkgrel=1
pkgdesc="A utility to apply patch files to original sources"
arch=('i686' 'x86_64')
url="https://www.gnu.org/software/patch/patch.html"
license=('GPL')
groups=('base-devel')
makedepends=('ed')
optdepends=('ed: for patch -e functionality')
source=(https://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.xz{,.sig}
        msys2-patch-2.7.1.patch
        msys2-patch-manifest.patch)
sha256sums=('ac610bda97abe0d9f6b7c963255a11dcb196c25e337c61f94e4778d632f1d8fd'
            'SKIP'
            'abcb73c2e0941d49335a864e28e59ba1ad6477deef981a2e89374004e658b791'
            '714da1a384d3e0acca5351980b0b3b1b09b66140fabe2e74dfd313652f964de9')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}
  [[ -f src/patch.manifest ]] && rm -f src/patch.manifest
  [[ -f src/patch.rc ]] && rm -f src/patch.rc
  patch -Np1 -i ${srcdir}/msys2-patch-2.7.1.patch
  patch -Np2 -i ${srcdir}/msys2-patch-manifest.patch

  local _arch=
  if [ "${CARCH}" == 'x86_64' ]; then
    _arch='amd64'
  elif [ "${CARCH}" == 'i686' ]; then
    _arch='x86'
  fi
  sed -e "s|@ARCH@|${_arch}|g" -i src/patch.manifest
  autoreconf -fi
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    ac_cv_func_setmode_dos=yes

  make
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make check
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make DESTDIR=$pkgdir install
}
