# 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.6
pkgrel=2
pkgdesc='System V Release 4.0 curses emulation library'
arch=(x86_64 aarch64)
url='https://invisible-island.net/ncurses/ncurses.html'
license=(MIT-open-group)
depends=(
  glibc
  libgcc
  libstdc++
)
makedepends=(
  autoconf-archive
  git
)
optdepends=('bash: for ncursesw6-config')
provides=(
  libncurses++w.so
  libformw.so
  libmenuw.so
  libpanelw.so
  libncursesw.so
)
replaces=(
  alacritty-terminfo
  rio-terminfo
  wezterm-terminfo
)
source=(
  $pkgname::git+https://github.com/ThomasDickey/ncurses-snapshots.git?signed#tag=v${pkgver/./_}
  $pkgname-6.6-libs.patch
  $pkgname-6.6-pkgconfig.patch
)
sha512sums=('cad17bf83ef3ccd71fb7c33933ddbbbef2e8bd050d5e4e4ebb344b5df8292b1cd3c9e1787e88087d73cc96f625ba0c7cd6714d7720af7f8bd50b314e9838d2a7'
            'aa1d2cdc5bce7a99b277f2ecd4a109a337ba39f3ec522c3daf52dba0c1be05c9f0bf695d8bdfdbda1c1f6f3f7214ef0eb4b5b9e1b7e9f05db14419b3bc6ab271'
            'c74c9393659e1d744995285f270531108320975da8ca39b0e4cdcaa07a51cdd977bcfbf8eb131c6a81ba2e63e92bb185947ce6a1231c8bb8a2e5e6035c385cfa')
b2sums=('af7f3dd1fd2d471d6b1b144acfbf4f44bdbf040ff8746a48aa9fdcc86f74a004939a31204d22966ef857096ccd7da6ee74769996e082a5a2735b1e6e934319a3'
        '29f2c29e229687b8539be5147ce35f08b8a8eeead880a8a34189b3a02400fd752ee96314eed3e175503281dc05bf3668f26edeca4540668592d46d4cddc9aef4'
        'd8c079262332253c758f1c42ff642db0b14a86695dcaa735a73615ab594f65e0d1de3e31732ff7c413078b43f8e71e86b6ce41236e54e049ecf65d8cf7587e0a')
validpgpkeys=('19882D92DDA4C400C22C0D56CC2AF4472167BE03')  # Thomas Dickey <dickey@invisible-island.net>

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

build() {
  local configure_options=(
    --prefix=/usr
    --disable-root-access
    --disable-root-environ
    --disable-setuid-environ
    --enable-widec
    --enable-pc-files
    --mandir=/usr/share/man
    --with-cxx-binding
    --with-cxx-shared
    --with-manpage-format=normal
    --with-pkg-config-libdir=/usr/lib/pkgconfig
    --with-shared
    --with-versioned-syms
    --with-xterm-kbs=del
    --without-ada
  )

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

package() {
  local _pkgver=${pkgver/_*/}

  make DESTDIR="$pkgdir" install -C $pkgname
  install -vDm 644 $pkgname/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/usr/lib/lib${lib}.so"
    ln -sv ${lib}w.pc "$pkgdir/usr/lib/pkgconfig/${lib}.pc"
  done

  # some packages look for -lcurses during build
  printf 'INPUT(-lncursesw)\n' > "$pkgdir/usr/lib/libcursesw.so"
  ln -sv libncurses.so "$pkgdir/usr/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/usr/lib/lib${lib}.so"
    ln -sv libncursesw.so.${pkgver:0:1} "$pkgdir/usr/lib/lib${lib}.so.${_pkgver:0:1}"
    ln -sv ncursesw.pc "$pkgdir/usr/lib/pkgconfig/${lib}.pc"
  done
}
