# Maintainer: Martell Malone <martell malone at g mail dot com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=winpty
pkgname="${_realname}"
pkgver=0.4.3
pkgrel=3
pkgdesc="A Windows software package providing an interface similar to a Unix pty-master for communicating with Windows console programs"
arch=('i686' 'x86_64')
url="https://github.com/rprichard/winpty"
license=('MIT')
makedepends=(tar 'autotools' xz gcc)
if [[ "${CARCH}" == 'i686' ]]; then
  makedepends+=('mingw-w64-cross-mingw32-gcc')
else
  makedepends+=('mingw-w64-cross-mingw64-gcc')
fi
#depends=( "ncurses-devel" )
replaces=("winpty-git")
options=('staticlibs' 'strip')

# TODO: Replace path_conv with arg_heuristic from MSYS2 runtime
#       See https://github.com/rprichard/winpty/issues/56
source=("${_realname}-${pkgver}.tar.gz::https://github.com/rprichard/${_realname}/archive/${pkgver}.tar.gz"
        path_conv.cc::'https://github.com/Alexpux/path_convert/raw/2aa1874a513e919fcf3e10d65fce4bb7336ef219/src/path_conv.cpp'
        path_conv.h::'https://github.com/Alexpux/path_convert/raw/2aa1874a513e919fcf3e10d65fce4bb7336ef219/src/path_conv.h'
        0001-Apply-POSIX-to-Win-conversion-on-all-arguments-on-MSYS.patch
        0002-fix-path-conversion.patch
        0003-restore-old-macro.patch)

sha256sums=('093037c39f9c899d79b74d5e15ff74fb59a98c492c5ed621e97e1090c3442865'
            '7181d260592dc193f235965f202a5509b113d8b478efae3f06176b8625b683cc'
            'c84e4edc5a1e387dc1ea06445db76a7bfe43e816f0c32558ce8d8562378d5782'
            'c9705d86feba11f1155e4259d28131afb14db3c30c17efe96bdd655afa72ee04'
            '47568cc4ee701c936f49bce4958c05b19260bb85a9cdea0ab8b4d9bf8a1133bd'
            'cf4ecc9ea3f26ab96bcfd21917c8f56a3eb7785d4e719955f45925d79cf38e95')

consolidate() {
  cp ../path_conv.cc src/unix-adapter/path_conv.cc
  cp ../path_conv.h  src/unix-adapter/path_conv.h
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  consolidate
  patch -p1 -i ${srcdir}/0001-Apply-POSIX-to-Win-conversion-on-all-arguments-on-MSYS.patch
  patch -p1 -i ${srcdir}/0002-fix-path-conversion.patch
  # Removed in https://github.com/cygwin/cygwin/commit/c41f0e700a4865ed1bf23621e9c76343ae5f8587
  patch -p1 -i ${srcdir}/0003-restore-old-macro.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  ./configure --build=${CHOST} \
    --prefix=/usr

  make UNIX_ADAPTER_EXE=winpty.exe
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make PREFIX=${pkgdir}/usr UNIX_ADAPTER_EXE=winpty.exe install
}
