# 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")
# No replace or conflict as the installed names are different
#replaces=("${MINGW_PACKAGE_PREFIX}-ncurses" "${MINGW_PACKAGE_PREFIX}-termcap")
#conflicts=("${MINGW_PACKAGE_PREFIX}-ncurses" "${MINGW_PACKAGE_PREFIX}-termcap")
pkgver=4.5.3
pkgrel=1
pkgdesc="Curses library on the Win32 API (mingw-w64)"
# ports that are available with this package
_pdcports="wincon wingui vt"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.projectpluto.com/win32a.htm"
msys2_repository_url="https://github.com/Bill-Gray/PDCursesMod"
license=('Public Domain')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
changelog=${_realname}.changelog
source=("https://github.com/Bill-Gray/PDCursesMod/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('5be1c4a1ba42c958deb219e6fe45fd3315444bc47cfe0c89f5ac0d8c00cc5930')

#prepare() {
#  cd PDCursesMod-${pkgver}
#  patch -p1 -i "${srcdir}"/001-mingw-pdcurses-4.3.x-build.patch
#}

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

  for pdc_port in ${_pdcports}; do
    cp -rf ${pdc_port} ${pdc_port}-shared-${MSYSTEM}
    make -C ${pdc_port}-shared-${MSYSTEM} \
        CC=${MINGW_PREFIX}/bin/cc \
        STRIP=${MINGW_PREFIX}/bin/strip \
        AR=${MINGW_PREFIX}/bin/ar \
        LIBNAME=libpdcurses.dll \
        DLLNAME=libpdcurses \
        WIDE=Y \
        UTF8=Y \
        DLL=Y

    cp -rf ${pdc_port} ${pdc_port}-static-${MSYSTEM}
    make -C ${pdc_port}-static-${MSYSTEM} \
        CC=${MINGW_PREFIX}/bin/cc \
        STRIP=${MINGW_PREFIX}/bin/strip \
        AR=${MINGW_PREFIX}/bin/ar \
        LIBNAME=libpdcurses \
        WIDE=Y \
        UTF8=Y
  done
}

# we use the check target to verify that the demos would link correctly
# and could then (manually) run the tests to verify the results *look* correct
check() {
  cd "${srcdir}/PDCursesMod-${pkgver}"

  for pdc_port in ${_pdcports}; do
    cp -rf ${pdc_port} ${pdc_port}-shared-${MSYSTEM}
    make -C ${pdc_port}-shared-${MSYSTEM} \
        CC=${MINGW_PREFIX}/bin/cc \
        STRIP=${MINGW_PREFIX}/bin/strip \
        AR=${MINGW_PREFIX}/bin/ar \
        LIBNAME=libpdcurses.dll \
        DLLNAME=libpdcurses \
        WIDE=Y \
        UTF8=Y \
        DLL=Y \
        demos

    cp -rf ${pdc_port} ${pdc_port}-static-${MSYSTEM}
    make -C ${pdc_port}-static-${MSYSTEM} \
        CC=${MINGW_PREFIX}/bin/cc \
        STRIP=${MINGW_PREFIX}/bin/strip \
        AR=${MINGW_PREFIX}/bin/ar \
        LIBNAME=libpdcurses \
        WIDE=Y \
        UTF8=Y \
        demos
  done
}

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

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,include,lib}
  mkdir "${pkgdir}"${MINGW_PREFIX}/include/pdcurses

  # no exe - those are only demos and only run for check
  #install "wingui-shared-${MSYSTEM}/*.exe "${pkgdir}"${MINGW_PREFIX}/bin/

  # install dll and shared library of the "default port" (as in earlier versions of this package: wingui)
  install "wingui-shared-${MSYSTEM}/libpdcurses.dll" "${pkgdir}${MINGW_PREFIX}/bin/"
  install "wingui-static-${MSYSTEM}/libpdcurses.a"   "${pkgdir}${MINGW_PREFIX}/lib/"

  # as all share the same ABI and the binaries are intended to work only with the basename - only install one link library
  install "wingui-shared-${MSYSTEM}/libpdcurses.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/"

  # the additional static libraries and actual dll's go in with a rename
  for pdc_port in ${_pdcports}; do
    install "${pdc_port}-static-${MSYSTEM}/libpdcurses.a"   "${pkgdir}${MINGW_PREFIX}/lib/libpdcurses_${pdc_port}.a"
    install "${pdc_port}-shared-${MSYSTEM}/libpdcurses.dll" "${pkgdir}${MINGW_PREFIX}/bin/libpdcurses_${pdc_port}.dll"
  done

  install -m 0644 curses.h panel.h term.h "${pkgdir}"${MINGW_PREFIX}/include/pdcurses/
  # needs defines matching the make step, see https://github.com/Bill-Gray/PDCursesMod/issues/133
  echo '/* if you want to use the DLL one: #define PDC_DLL_BUILD 1 */' > pdcurses.h
  echo '/* if you want to use ncurses compatible mouse: #define PDC_NCMOUSE 1 */' >> pdcurses.h
  echo '#define PDC_WIDE 1'           >> pdcurses.h
  echo '#define PDC_FORCE_UTF8 1'     >> pdcurses.h
  echo '#include "pdcurses/curses.h"' >> pdcurses.h
  install -m 0644 pdcurses.h "${pkgdir}"${MINGW_PREFIX}/include/pdcurses.h
}
