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

_realname=readline
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=8.0
_patchlevel=004
pkgver=${_basever}.${_patchlevel}
pkgrel=2
pkgdesc="MinGW port of readline for editing typed command lines (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         #"${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-termcap"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
license=('GPL')
url="https://cnswww.cns.cwru.edu/php/chet/readline/rltop.html"
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${_basever}.tar.gz"{,.sig}
        0001-Add-private-history_rename-function-and-in-WIN32-use.patch)
if [ ${_patchlevel} -gt 00 ]; then
    for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
        source=(${source[@]} https://ftp.gnu.org/gnu/${_realname}/${_realname}-${_basever}-patches/readline${_basever//./}-$(printf "%03d" $p){,.sig})
    done
fi
validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB')
sha256sums=('e339f51971478d369f8a053a330a190781acb9864cf4c541060f12078948e461'
            'SKIP'
            '353dcbbc0435c60e33f02f8fb1c2d10e9b966ebe1b3857a0b2e2e268029c6f35'
            'd8e5e98933cf5756f862243c0601cb69d3667bb33f2c7b751fe4e40b2c3fd069'
            'SKIP'
            '36b0febff1e560091ae7476026921f31b6d1dd4c918dcb7b741aa2dad1aec8f7'
            'SKIP'
            '94ddb2210b71eb5389c7756865d60e343666dfb722c85892f8226b26bb3eeaef'
            'SKIP'
            'b1aa3d2a40eee2dea9708229740742e649c32bb8db13535ea78f8ac15377394c'
            'SKIP')

prepare() {
  cd "${srcdir}/readline-${_basever}"
  patch -p1 -i ${srcdir}/0001-Add-private-history_rename-function-and-in-WIN32-use.patch

  for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
    msg "applying patch readline${_basever//./}-$(printf "%03d" $p)"
    patch -Np0 -i ${srcdir}/readline${_basever//./}-$(printf "%03d" $p)
  done

  # Remove RPATH from shared objects (FS#14366)
  sed -i 's|-Wl,-rpath,$(libdir) ||g' support/shobj-conf
  # Fully spell out linker option
  sed -i 's|-Wl,--export-all |-Wl,--export-all-symbols |g' support/shobj-conf
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  ../${_realname}-${_basever}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-shared \
    --without-curses
  make
}

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