# 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}-git"
epoch=1
pkgver=0.4.4_dev.564.ce9239a
pkgrel=1
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")

# Explicitly reference the git version of the indirect dependency mingw-w64-cross-crt
# for avoiding problems with its default provider mingw-w64-cross-crt-clang-git
makedepends=(git tar automake xz
             mingw-w64-cross-gcc
             mingw-w64-cross-crt-git)

provides=("${_realname}")
conflicts=("${_realname}")
#depends=( "ncurses-devel" )
options=('staticlibs' 'strip')

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

sha256sums=('SKIP'
            '7181d260592dc193f235965f202a5509b113d8b478efae3f06176b8625b683cc'
            'c84e4edc5a1e387dc1ea06445db76a7bfe43e816f0c32558ce8d8562378d5782'
            'c9705d86feba11f1155e4259d28131afb14db3c30c17efe96bdd655afa72ee04'
            '4427afccf0b648d3bc1aa7442b0f611674efd91474fee48f7bf842194d6cea4f')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.%s.%s" "$(cat VERSION.txt | tr '-' '_')" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

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}"
  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
}

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

  make UNIX_ADAPTER_EXE=winpty.exe
}

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