# Maintainer: Ivy Foster <code@escondida.tk>
# Contributor: Alex Szczuczko <alex at szc dot ca>
# Contributor: Devin Cofer <ranguvar{AT]archlinux[DOT}us>
# Contributor: Imanol Celaya <ilcra1989@gmail.com>
# Windows port: Timofey Denisov <tim4job@bmail.ru>

_realname=bmake
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=20181221
pkgrel=6
pkgdesc='Portable version of the NetBSD make build tool'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='http://www.crufty.net/help/sjg/bmake.html'
license=(BSD)

# upstream recommends using python for improved meta2deps script
depends=("${MINGW_PACKAGE_PREFIX}-binutils"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-libiconv")

source=("http://www.crufty.net/ftp/pub/sjg/bmake-$pkgver.tar.gz"
        0001-first-version-mingw-patch.patch
        0002-enable-debug-output-on-test.patch
        0003-print-pwd-on-fail-install.patch
        0004-add-windows-draft-configs.patch
        0005-fallback-open-file-through-native-windows-path.patch
)
# checksum source: http://www.crufty.net/ftp/pub/sjg/bmake-$pkgver.tar.gz.sha1
sha1sums=('ce7db05c68e2fbad41d1dd92ec110406093293bd'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
)

prepare() {
  cd "${srcdir}"/bmake
  patch -p1 -l -i "${srcdir}"/0001-first-version-mingw-patch.patch
  patch -p1 -l -i "${srcdir}"/0002-enable-debug-output-on-test.patch
  patch -p1 -l -i "${srcdir}"/0003-print-pwd-on-fail-install.patch
  patch -p1 -l -i "${srcdir}"/0004-add-windows-draft-configs.patch
  patch -p1 -l -i "${srcdir}"/0005-fallback-open-file-through-native-windows-path.patch
}

build() {
  cd "${srcdir}"/bmake
  export SYSROOTWINDOWSPATH=$(cygpath -w /)
  aclocal --verbose || die "aclocal failed"
  autoreconf --verbose || die "autoreconf failed"
  bash -x ./boot-strap --prefix=${MINGW_PREFIX} op=build
}

# op=build also runs unit tests; thus no check()

package() {
  cd "${srcdir}"/bmake

  export SYSROOTWINDOWSPATH=$(cygpath -w /)

  # Fix directory permissions on install
  # install -Dm644 "${pkgdir}${MINGW_PREFIX}/bin" "${pkgdir}${MINGW_PREFIX}/share/licenses/$pkgname"

  bash -x ./boot-strap --install-destdir="${pkgdir}" --prefix=${MINGW_PREFIX} op=install

  rm -rf "${pkgdir}${MINGW_PREFIX}/share/man/cat1"
  install -Dm644 bmake.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1/bmake.1"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  head -n70 main.c > "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # install profile script
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/etc/profile.d
  echo "export SYSROOTWINDOWSPATH=$(cygpath -w /); export MAKESYSPATH=${MINGW_PREFIX}/share/mk" > "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/bmake.sh
  cp "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/bmake.{sh,zsh}
}