# Maintainer:  David Runge <dvzrv@archlinux.org>
# Contributor:  Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: judd <jvinet@zeroflux.org>

pkgname=ncurses
pkgver=6.4_20230520
_commit=c74a1c4202f4524076da8be8554dacec391c84b2  # refs/tags/v6_4_20230520
pkgrel=2.1
pkgdesc='System V Release 4.0 curses emulation library'
url='https://invisible-island.net/ncurses/ncurses.html'
license=(MIT-open-group)
ncxfab_makedepends=(
  base-devel
  autoconf-archive
  git
)
ncxfat_makedepends=(
  binutils
  glibc
  gcc-libs
)
ncxfah_optdepends=('bash: for ncursesw6-config')
provides=(
  libncurses++w.so
  libformw.so
  libmenuw.so
  libpanelw.so
  libncursesw.so
)
replaces=(alacritty-terminfo)
source=(
  ncurses::git+https://github.com/ThomasDickey/ncurses-snapshots.git?signed#commit=$_commit
  ncurses-6.3-libs.patch
  ncurses-6.3-pkgconfig.patch
)
sha512sums=('SKIP' #'94fd092907471539fbf8f8e65e353a870fca5b35af06803b111dfca193a7b3112f1a9b659e7876d5d58f3008bae45aa0f5d43f4ab805b45ec063ed1930629c10'
            'adb02b838c40f1e58a1b31c26d5cd0f2a1c43f3b6d68e839981764c0f6c905a9eb51dd36ff018628fdeb20747cc7467727d57135408ab4848259384077a52b28'
            '2d2c0ec3c880e638ab4aa3dbff5e28e4cd233153e24816bd87e077f848aa3edd5114cd0f2a7f6e8869dd1861a2746e512886c18264ff1676927dcc320c5ef958')
b2sums=('SKIP' #'7df6d10b7807cebd5706e730b783ae2405aaba1958488edf3c9c17c6bbeac1c9d259ec27aa6ea1a57cd4e997eab7e58ea52351b19dbfeb32ee045d19e9f6ca01'
        '31bb10e82dd018a75e57252052650d9f0f5eb5e7e887118c2ea40032b11f59ec6aa4d9bae804c615cbecdf3382f3434e0c9e9e8440fdefe66a507be020b8965c'
        'fb6cf606cf3db7f6b306272696a63bce83d52cfa91e850f9a7bdb9d3d8455a26943529a9cf79731dddc7f763c27211a9afab9c4c31dbb6d12fd720eb390eb0a3')
validpgpkeys=('19882D92DDA4C400C22C0D56CC2AF4472167BE03')  # Thomas Dickey <dickey@invisible-island.net>

pkgver() {
  cd ncurses
  git describe --tags | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/v//g;s/_/./'
}

prepare() {
  # do not link against test libraries
  patch -Np1 -d ncurses -i ../ncurses-6.3-libs.patch
  # do not leak build-time LDFLAGS into the pkgconfig files:
  # https://bugs.archlinux.org/task/68523
  patch -Np1 -d ncurses -i ../ncurses-6.3-pkgconfig.patch
  # NOTE: can't run autoreconf because the autotools setup is custom and ancient
}

build() {
    #CFLAGS="$CFLAGS -I/usr/aarch64/include"
    if (( MPKG_CROSSCOMPILED )); then
        INSTALL_ARG="INSTALL=/usr/bin/install --strip-program="${MPKG_TARGETMACHINE}"-strip -c"
    fi

    local configure_options=(
    --prefix=$MPKG_USRPATH
    --build=$MPKG_BUILDMACHINE
    --host=$MPKG_TARGETMACHINE
    # This is an outcome of interactions between g++ (does not behave
    # as specified with respect to ordering of processing of -I
    # includes, some are prioritised as proven by stracing it) and a
    # wildly unfortunate legacy test in ncurses which breaks g++ by
    # passing an absolute (and unneeded) include path with defeats the
    # sysroot, can't be overridden because of the g++ mess, and then
    # as a result of the failure the test just nulls out the C++
    # compiler, without any way to override).
    --includedir=/usr/$MPKG_TARGETMACHINE/include
    --disable-root-access
    --disable-root-environ
    --disable-setuid-environ
    --enable-widec
    --enable-pc-files
    --mandir=$MPKG_USRPATH/share/man
    --with-cxx-binding
    --with-cxx-shared
    --with-manpage-format=normal
    --with-pkg-config-libdir=$MPKG_USRPATH/lib/pkgconfig
    --with-shared
    --with-versioned-syms
    --with-xterm-kbs=del
    --without-ada
    --enable-overwrite
    "$INSTALL_ARG"
  )

  cd ncurses
  ./configure "${configure_options[@]}"
  make
}

package() {
  ncxfat_depends=(glibc gcc-libs)

  local _pkgver=${pkgver/_*/}

  make DESTDIR="$pkgdir" INCLUDEDIR="$pkgdir/$MPKG_USRPATH/include" install -C ncurses
  install -vDm 644 ncurses/COPYING -t "$pkgdir$/usr/share/licenses/$pkgname/"

  # fool packages looking to link to non-wide-character ncurses libraries
  for lib in ncurses ncurses++ form panel menu; do
    printf "INPUT(-l%sw)\n" "${lib}" > "$pkgdir$MPKG_USRPATH/lib/lib${lib}.so"
    ln -sv ${lib}w.pc "$pkgdir$MPKG_USRPATH/lib/pkgconfig/${lib}.pc"
  done

  # some packages look for -lcurses during build
  printf 'INPUT(-lncursesw)\n' > "$pkgdir$MPKG_USRPATH/lib/libcursesw.so"
  ln -sv libncurses.so "$pkgdir$MPKG_USRPATH/lib/libcurses.so"

  # tic and ticinfo functionality is built in by default
  # make sure that anything linking against it links against libncursesw.so instead
  for lib in tic tinfo; do
    printf "INPUT(libncursesw.so.%s)\n" "${_pkgver:0:1}" > "$pkgdir$MPKG_USRPATH/lib/lib${lib}.so"
    ln -sv libncursesw.so.${pkgver:0:1} "$pkgdir$MPKG_USRPATH/lib/lib${lib}.so.${_pkgver:0:1}"
    ln -sv ncursesw.pc "$pkgdir$MPKG_USRPATH/lib/pkgconfig/${lib}.pc"
  done
}
