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

pkgbase=libedit
pkgname=('libedit' 'libedit-devel')
_base_ver=3.1
_snap_date=20210910
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')
options=('staticlibs')
source=(https://www.thrysoee.dk/editline/${pkgname}-${_snap_date}-${_base_ver}.tar.gz
        libedit-20191231-3.1.patch
        libedit-20130712-1.src.patch
        libedit-3.0-20120311-msys2.patch)
sha256sums=('6792a6a992050762edcca28ff3318cdb7de37dccf7bc30db59fcd7017eed13c5'
            '858f123a0844bb16903014a9b3e7dd4668654931a07ca8a9a4d1a8a3572863b0'
            'e973c43b6576fbafef69f05c3c95ee256bfb972d3622e8c87658e737415cc258'
            'd7060b7449241d87612ce1a2fd1fdb91fa8ad5ff40f4e039b2d2cd7a64565d83')

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

  patch -p1 -i ${srcdir}/libedit-20191231-3.1.patch
  patch -p2 -i ${srcdir}/libedit-20130712-1.src.patch
  patch -p1 -i ${srcdir}/libedit-3.0-20120311-msys2.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/
  rm "${pkgdir}"/usr/share/man/man3/history.3 # conflicts with readline
  cp "${pkgdir}"/usr/share/man/man3/editline.3 "${pkgdir}"/usr/share/man/man3/el.3
}

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