# Maintainer: Frode Solheim <frode@fs-uae.net>
# Contributor: Duong Pham <dthpham@gmail.com>
# Contributor: Lara Maia <dev@lara.click>

_realname=cx_Freeze
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-cx_Freeze")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=6.1
pkgrel=2
pkgdesc="Python package for freezing scripts into executables (mingw-w64)"
arch=('any')
license=('PSF')
url="https://cx-freeze.sourceforge.io/"
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-six")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('staticlibs' 'strip' '!debug')
source=(cx_Freeze-${pkgver}.tar.gz::https://github.com/anthony-tuininga/cx_Freeze/archive/${pkgver}.tar.gz
        cx_freeze.patch)
sha256sums=('97ebaa6226737cba0a6a5d2a715c50a871181ce01b23800358d159c15ebcc96a'
            '3445bde0dfddfea64c9513d53105a3c1099ed9f9a2861fcc4d12a018f48dcbab')

prepare() {
  cd ${_realname}-${pkgver}
  patch -Np1 -i ${srcdir}/cx_freeze.patch
  cd ..
  rm -Rf python-cx_Freeze-${CARCH}
  cp -a cx_Freeze-${pkgver} python-cx_Freeze-${CARCH}
}

build() {
  echo "Building for Python"
  cd python-cx_Freeze-${CARCH}
  #change version of hard-coded DLL:
  sed -i -e 's/libffi-7.dll/libffi-6.dll/g' ./cx_Freeze/hooks.py
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  cd python-cx_Freeze-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  # fix python command in files
  pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
  for filename in cxfreeze cxfreeze-quickstart; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${filename}
  done
  popd > /dev/null
}
