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

pkgbase=readline
pkgname=('libreadline' 'libreadline-devel')
_basever=8.3
_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"
msys2_repository_url="https://git.savannah.gnu.org/cgit/readline.git"
msys2_references=(
  "anitya: 4173"
  "cpe: cpe:/a:gnu:readline"
)
license=('GPL')
makedepends=('ncurses-devel' 'autotools' 'gcc')
options=('!emptydirs')
source=(https://ftp.gnu.org/gnu/readline/readline-${_basever}.tar.gz{,.sig}
        inputrc
        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=('fe5383204467828cd495ee8d1d3c037a7eba1389c22bc6a041f627976f9061cc'
            'SKIP'
            '6ff0b86e0b98f453b3ce06270c649ef336e2b881be0bc7dd8f5d470afbdac75e'
            'ba2bdb5530f60c04012e8ed4635123ec815a7711cd96ca1211e71a7fe897e6ea'
            'cdb4f97d641acde59ada6f22141e6a6743f756fc183f5cb43d6c9f8ae563da29'
            '96d002c8815b29ed5dbe89d9b251182c6e60111acc3d236acb8d1bbb2f896457'
            '8ce80150b6da67c551ffebb18650cfceef8d5e0eddbc848bcef20b8ec176af05'
            '21f0a03106dbe697337cd25c70eb0edbaa2bdb6d595b45f83285cdd35bac84de'
            '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() {
  CFLAGS+=" -Wno-error=incompatible-pointer-types"

  mkdir -p ${srcdir}/build-static-${MSYSTEM} && cd ${srcdir}/build-static-${MSYSTEM}

  ../${pkgbase}-${_basever}/configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --with-curses \
    --disable-shared \
    --enable-static \
    bash_cv_termcap_lib=libncurses \
    CFLAGS="${CFLAGS} -DNCURSES_STATIC"

  make

  make DESTDIR=${srcdir}/dest install

  mkdir -p ${srcdir}/build-shared-${MSYSTEM} && cd ${srcdir}/build-shared-${MSYSTEM}

  ../${pkgbase}-${_basever}/configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --with-curses \
    --disable-static \
    --enable-shared \
    bash_cv_termcap_lib=libncurses

  make

  make DESTDIR=${srcdir}/dest install
}

package_libreadline() {
  depends=('ncurses')
  groups=('libraries')
  backup=('etc/inputrc')

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

  install -Dm644 inputrc "${pkgdir}"/etc/inputrc
}

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