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

_realname=termcap
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.1
pkgrel=6
pkgdesc="Terminal feature database (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.gnu.org/software/termutils/"
license=("GPL" "LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip')
source=("https://ftp.gnu.org/gnu/termcap/${_realname}-${pkgver}.tar.gz"
        "0001-tparam-replace-write-with-fprintf.patch")
sha256sums=('91a0e22e5387ca4467b5bcb18edf1c51b930262fd466d5fda396dd9d26719100'
            '47d5da2df6bc7f3401b7e33eb80d40153bca60278e58b503ffb19589acd7c2da')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # fixes tparam.c: warning: implicit declaration of function 'write'
  patch -Nbp1 -i "${srcdir}/0001-tparam-replace-write-with-fprintf.patch"

  autoconf
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make
  # Build a shared library.  No need for -fPIC on Windows.
  ${MINGW_PREFIX}/bin/gcc -shared \
    -Wl,--out-implib,libtermcap.dll.a \
    -o libtermcap-0.dll \
    termcap.o tparam.o version.o
}

package() {
  cd  ${srcdir}/build-${MINGW_CHOST}
  make install prefix="${pkgdir}${MINGW_PREFIX}" exec_prefix="${pkgdir}${MINGW_PREFIX}" oldincludedir=
  rm -r "${pkgdir}"${MINGW_PREFIX}/info/
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib}
  install -m 0755 libtermcap-0.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install -m 0644 libtermcap.dll.a "${pkgdir}${MINGW_PREFIX}/lib"

  # pkg-config
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  echo -e "prefix=${MINGW_PREFIX}\n\
exec_prefix=\${prefix}\n\
libdir=\${exec_prefix}/lib\n\
includedir=\${prefix}/include\n\
\n\
Name: Termcap\n\
Description: GNU Termcap library and data base that enables programs to use display terminals in a terminal-independent manner\n\
URL: ${url}\n\
Version: ${pkgver}\n\
Libs: -L\${libdir} -ltermcap\n\
Cflags: -I\${includedir}" > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/termcap.pc"
}
