# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=binwalk
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.3.1
pkgrel=2
pkgdesc="A tool for searching a given binary image for embedded files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/ReFirmLabs/binwalk/"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/ReFirmLabs/${_realname}/archive/refs/tags/v${pkgver}.tar.gz)
sha256sums=('7ec9d8fcb8686f4060d37e1096669e3ed8ce1194c91ad80199622448bcc01b19')

prepare() {
  cd "${srcdir}"
  rm -rf "python-build-${CARCH}" | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-purelib=;--install-data=" \
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-purelib=;--install-data=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  install -m 644 -D "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i "${pkgdir}${MINGW_PREFIX}"/bin/binwalk
}
