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

pkgbase=readline
pkgname=('libreadline' 'libreadline-devel')
_basever=8.1
_patchlevel=001 #prepare for some patches
pkgver=${_basever}.${_patchlevel}
pkgrel=1
pkgdesc="GNU readline library"
arch=('i686' 'x86_64')
url="https://tiswww.case.edu/php/chet/readline/rltop.html"
license=('GPL')
makedepends=('ncurses-devel')
#backup=('etc/inputrc')
options=('!emptydirs')
source=(https://ftp.gnu.org/gnu/readline/readline-${_basever}.tar.gz{,.sig}
        readline-7.0.3-3.clipboard.patch
        readline-7.0.3-3.src.patch
        readline-6.3-msys2.patch
        readline-6.3-paste-utf8.patch)
if [ $_patchlevel -gt 0 ]; then
    for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
        source=(${source[@]} https://ftp.gnu.org/gnu/readline/readline-${_basever}-patches/readline${_basever//./}-$(printf "%03d" $p){,.sig})
    done
fi
sha256sums=('f8ceb4ee131e3232226a17f51b164afc46cd0b9e6cef344be87c65962cb82b02'
            'SKIP'
            '2fd3b112ad7cb16258efe960e59fdb1252d7a18397d45273c05ad2d216a63cf7'
            'cdb4f97d641acde59ada6f22141e6a6743f756fc183f5cb43d6c9f8ae563da29'
            '05c08602b5aacd937a2237720f5974836a0c40e77da8451866bff2885d70637f'
            '8ce80150b6da67c551ffebb18650cfceef8d5e0eddbc848bcef20b8ec176af05'
            '682a465a68633650565c43d59f0b8cdf149c13a874682d3c20cb4af6709b9144'
            'SKIP')
validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB') # Chet Ramey

prepare() {
  cd ${srcdir}/${pkgbase}-${_basever}
  for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
    msg "applying patch readline${_basever//./}-$(printf "%03d" $p)"
    patch -p0 -i $srcdir/readline${_basever//./}-$(printf "%03d" $p)
  done
  patch -p2 -i ${srcdir}/readline-7.0.3-3.src.patch
  patch -p1 -i ${srcdir}/readline-6.3-msys2.patch
  patch -p1 -i ${srcdir}/readline-6.3-paste-utf8.patch
  patch -p1 -i ${srcdir}/readline-7.0.3-3.clipboard.patch
}

build() {
  cd ${srcdir}/${pkgbase}-${_basever}

  # Remove RPATH from shared objects (FS#14366)
  sed -i 's|-Wl,-rpath,$(libdir) ||g' support/shobj-conf

  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --with-curses \
    bash_cv_termcap_lib=libncurses
  make VERBOSE=1

  make DESTDIR=${srcdir}/dest install
}

package_libreadline() {
  depends=('ncurses')
  groups=('libraries')
  install=readline.install

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

package_libreadline-devel() {
  groups=('development')
  options=('staticlibs')
  depends=("libreadline=${pkgver}" 'ncurses-devel')
  pkgdesc="readline headers and libraries"

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