# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Simon Sobisch <simonsobisch@gnu.org>

_realname=pdcurses
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
provides=("${MINGW_PACKAGE_PREFIX}-curses")
#replaces=("${MINGW_PACKAGE_PREFIX}-ncurses" "${MINGW_PACKAGE_PREFIX}-termcap")
#conflicts=("${MINGW_PACKAGE_PREFIX}-ncurses" "${MINGW_PACKAGE_PREFIX}-termcap")
pkgver=4.1.0
pkgrel=5
pkgdesc="Curses library on the Win32 API (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.projectpluto.com/win32a.htm"
license=('Public Domain')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/Bill-Gray/PDCursesMod/archive/v${pkgver}.tar.gz"
        001-mingw-pdcurses-4.1.0-build.patch
        002-fix-exports.patch
        003-fix-_bool-was-not-declared.patch
        004-link-implib.patch)
sha256sums=('c6e036c0cb24f7909dbb8fa5011564727cd64a91efd3b7bb3e81c7509d7f5fde'
            '913b5aff09d0ab1a2197f66a98657927d85a0dc3577c2b5e69179148fb2b0242'
            '246f93facdd2703f8b9d0bcd57e89688fd861d34a30facc60a48892b330b08bc'
            '6f6875068a13988988b8b04300636db4c4f9f543632597232cb2b64a2fd1218b'
            '39c6488570cd7b492f636b47972d40ec6e809744daeed85ef415df9c1ae6f3ff')

prepare() {
  cd PDCursesMod-${pkgver}
  patch -p1 -i ${srcdir}/001-mingw-pdcurses-4.1.0-build.patch
  patch -p1 -i ${srcdir}/002-fix-exports.patch
  patch -p1 -i ${srcdir}/003-fix-_bool-was-not-declared.patch
  patch -p1 -i ${srcdir}/004-link-implib.patch
}

build() {
  cd "${srcdir}/PDCursesMod-${pkgver}"

# NOte that you should use something like -${CARCH}
# to prevent building i686 compiled binaries from
# being compiled with x86_64 compiled binaries and
# vice-versa.  That causes build failures - no surpise.
  cp -rf wingui wingui-shared-${CARCH}
  pushd wingui-shared-${CARCH}
    make -f Makefile.mng \
      CC=${MINGW_PREFIX}/bin/gcc \
      STRIP=${MINGW_PREFIX}/bin/strip \
      AR=${MINGW_PREFIX}/bin/ar \
      WIDE=Y \
      UTF8=Y \
      DLL=Y
  popd

  cp -rf wingui wingui-static-${CARCH}
  pushd wingui-static-${CARCH}
    make -f Makefile.mng \
      CC=${MINGW_PREFIX}/bin/gcc \
      STRIP=${MINGW_PREFIX}/bin/strip \
      AR=${MINGW_PREFIX}/bin/ar \
      WIDE=Y \
      UTF8=Y
  popd
  
}

package() {
  cd "${srcdir}/PDCursesMod-${pkgver}"

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,include,lib}
  mkdir ${pkgdir}${MINGW_PREFIX}/include/pdcurses
 
  install wingui-shared-${CARCH}/*.exe ${pkgdir}${MINGW_PREFIX}/bin/
  install wingui-shared-${CARCH}/libpdcurses.dll ${pkgdir}${MINGW_PREFIX}/bin/
  install wingui-shared-${CARCH}/libpdcurses.dll.a ${pkgdir}${MINGW_PREFIX}/lib/libpdcurses.dll.a
  install wingui-static-${CARCH}/libpdcurses.a ${pkgdir}${MINGW_PREFIX}/lib/libpdcurses.a

  echo '#include "pdcurses/curses.h"' > pdcurses.h
  install -m 0644 curses.h panel.h term.h acs_defs.h ${pkgdir}${MINGW_PREFIX}/include/pdcurses/
  install -m 0644 pdcurses.h ${pkgdir}${MINGW_PREFIX}/include/pdcurses.h
}
