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

_realname=sed
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.9
pkgrel=1
pkgdesc="Sed is a stream editor (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.gnu.org/software/sed/'
msys2_references=(
  "cpe: cpe:2.3:a:gnu:sed"
)
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-gettext")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"{,.sig}
        msvc_invalid_parameter_handler.patch
        001-link-to-bcrypt.patch)
sha256sums=('6e226b732e1cd739464ad6862bd1a1aba42d7982922da7a53519631d24975181'
            'SKIP'
            'f53f59031fe9f0e37c8cf358ab16a7c6a887df84085e7c146f30282ca9bff243'
            'cef2f39f3ea9a1a4010d812da7434116bff56db69df27620c4670c3723e805e0')
validpgpkeys=('155D3FC500C834486D1EEA677FD9FCCB000BEEEE') # Jim Meyering <jim@meyering.net>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/msvc_invalid_parameter_handler.patch"
  patch -Np1 -i "${srcdir}/001-link-to-bcrypt.patch"
}

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

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

  # Workaound for build failure
  sed -e "s|lib/netinet/in.h|netinet/in.h|g" -i gnulib-tests/Makefile

  make
}

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"
}
