# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=itstool
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.6
pkgrel=4
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="XML to PO and back again (mingw-w64)"
url="http://itstool.org/"
license=("GPL3")
depends=("${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-libxml2")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=(http://files.itstool.org/itstool/${_realname}-${pkgver}.tar.bz2
        'reloc.patch'
        'pyscript2exe.py')
sha256sums=('6233cc22726a9a5a83664bf67d1af79549a298c23185d926c3677afa917b92a9'
            'f292bcd116da4ea36ee9c42e206dbd9151d147da1ef5a078e743fc9ed0a4ab47'
            'e57174517b08cf8f9fb4f43a381d342d23d2db3cad661107f35ca21c39b5734d')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i ${srcdir}/reloc.patch

  autoreconf -fvi
}

build() {
  mkdir -p "${srcdir}/build-${CHOST}"
  cd "${srcdir}/build-${CHOST}"

  PYTHON="${MINGW_PREFIX}/bin/python3" \
    ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX}

  make
}

check() {
  cd build-${CHOST}

  make check
}

package() {
  cd "${srcdir}/build-${CHOST}"

  make DESTDIR="${pkgdir}" install

  ${MINGW_PREFIX}/bin/python3 "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/itstool"
  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"
  sed -e "s|${_PRE_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/bin/itstool-script.py
}
