# 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=8
pkgdesc='Portable version of the NetBSD make build tool'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://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}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-libiconv")

source=("https://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
        0006-ffs.patch
)
# checksum source: https://www.crufty.net/ftp/pub/sjg/bmake-$pkgver.tar.gz.sha1
sha256sums=('fc0948b4eb0cf21fcec4f89660c0295150c55bf070d4b7445858947f84f7e67e'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '04618b67b12da203a7bf7075c93e00fda223539db0fe94e9762f1e409f8d6540'
)

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
  patch -p1 -l -i "${srcdir}"/0006-ffs.patch
}

build() {
  cd "${srcdir}"/bmake
  export SYSROOTWINDOWSPATH=$(cygpath -w /)
  aclocal
  autoreconf
  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}
}
