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

pkgbase=libedit
pkgname=('libedit' 'libedit-devel')
_base_ver=3.1
_snap_date=20240808
pkgver=${_snap_date}_${_base_ver}
pkgrel=1
pkgdesc="Libedit is an autotool- and libtoolized port of the NetBSD Editline library."
groups=('libraries')
arch=('i686' 'x86_64')
license=('BSD')
url="https://www.thrysoee.dk/editline/"
depends=('ncurses' 'sh')
makedepends=('ncurses-devel' 'autotools' 'gcc')
options=('staticlibs')
source=(https://www.thrysoee.dk/editline/${pkgname}-${_snap_date}-${_base_ver}.tar.gz
        libedit-20191231-3.1.patch
        cygwin-build.patch)
sha256sums=('5f0573349d77c4a48967191cdd6634dd7aa5f6398c6a57fe037cc02696d6099f'
            '858f123a0844bb16903014a9b3e7dd4668654931a07ca8a9a4d1a8a3572863b0'
            'b04f81f22bd5601c3df54dcdc7f79c5a25a7a8563336238129ac7bba123ed2a3')

prepare() {
  cd "${srcdir}/${pkgname}-${_snap_date}-${_base_ver}"

  patch -p1 -i ${srcdir}/cygwin-build.patch

  # https://github.com/msys2/MSYS2-packages/pull/2241
  patch -p1 -i ${srcdir}/libedit-20191231-3.1.patch

  autoreconf -fi
}

build() {
  cd "${srcdir}/${pkgname}-${_snap_date}-${_base_ver}"

  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --enable-widec \
    CPPFLAGS="$CPPFLAGS -I/usr/include/ncursesw" \
    LDFLAGS="$LDFLAGS -L/usr/lib/ncursesw"

  make

  make DESTDIR=${srcdir}/dest install
  install -Dm644 COPYING ${srcdir}/dest//usr/share/licenses/libedit/LICENSE
}

package_libedit() {
  groups=('libraries')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
}

package_libedit-devel() {
  pkgdesc="libedit headers and libraries"
  groups=('development')
  depends=("libedit=${pkgver}" 'ncurses-devel')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
