# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=wineditline
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.205
pkgrel=3
pkgdesc="port of the NetBSD Editline library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('BSD')
url="https://mingweditline.sourceforge.io/"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-cmake" "dos2unix")
options=('staticlibs')
source=("https://sourceforge.net/projects/mingweditline/files/${_realname}-${pkgver}.tar.bz2"
        '001-fix-installing.patch'
        '002-fix-exports.patch'
        '003-dont-link-with-def.patch'
        '004-add-pkgconfig.patch'
        '005-fix-missing-extern-c.patch')
sha256sums=('dc8b25cbd503dd41241b8de0146af20182b552ac31cf0fc7e103b83aec25e448'
            '821d0a36115832a76497dc6d7ea4b6e45e4ce73621030a59865f851309f8db34'
            '38e779fbdebf2f29038b598920e733a4a51638834013c3cc73fb85a7d50cffd2'
            '19077726758de7780cd68f3f47d8353516fd864ede1903d55280a1fb3dbe408d'
            '0350d2d027ae665fb39bbdf847da3c9e0bbd5f55ee0f90b377b1891e933dd28c'
            '794503a1c07872423ac3bca515a51a543769ee19fb2a94258e024bd51bef8df4')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  
  # Get rid of crlf endings
  find . -type f -exec dos2unix {} \;
  
  patch -Np1 -i ${srcdir}/001-fix-installing.patch
  patch -Np1 -i ${srcdir}/002-fix-exports.patch
  patch -Np1 -i ${srcdir}/003-dont-link-with-def.patch
  patch -Np1 -i ${srcdir}/004-add-pkgconfig.patch
  patch -Np1 -i ${srcdir}/005-fix-missing-extern-c.patch

  rm -rf bin32 bin64 lib32 lib64
}

build() {
  mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${pkgver}

  make
}

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

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  sed -i ${srcdir}/${_realname}-${pkgver}/${_realname}.pc -e "s,prefix=\${prefix},prefix=${MINGW_PREFIX},"
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/${_realname}.pc ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc
}
