# Maintainer: Andrew Chadwick <a.t.chadwick@gmail.com>

_realname=mypaint
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.1
pkgrel=8
pkgdesc="Simple drawing & painting program that works well with Wacom-style graphics tablets (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://mypaint.org"
msys2_repository_url="https://github.com/mypaint/mypaint"
license=('spdx:GPL-2.0-or-later AND ISC')
depends=("${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gsettings-desktop-schemas"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libmypaint"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-mypaint-brushes2"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-python-cairo"
         "${MINGW_PACKAGE_PREFIX}-python-gobject"
         "${MINGW_PACKAGE_PREFIX}-python-numpy")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-swig")
install="${_realname}-${MSYSTEM}.install"
source=(https://github.com/mypaint/mypaint/releases/download/v${pkgver}/mypaint-${pkgver}.tar.xz
        "0001-universal-newline.patch"::https://github.com/mypaint/mypaint/commit/032a155b72f2b021f66a994050d83f07342d04af.patch)
sha256sums=('f3e437d7cdd5fd28ef6532e8ab6b4b05d842bcdd644f16a0162dad3d8e57bb16'
            'b893d4b94dea23f1c71e363893bba2439a824bec05575b1514d6821397fc3d53')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

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

  apply_patch_with_msg \
    0001-universal-newline.patch
}

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

    ${MINGW_PREFIX}/bin/python setup.py build
}

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

    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
        ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

    # Fix so the correct interpretter is used
    local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
    for name in mypaint mypaint-ora-thumbnailer; do
        sed -e "s|${PREFIX_WIN}/bin/python.exe|${MINGW_PREFIX}/bin/python.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/$name
    done
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/mypaint/lib/config.py

    install -Dm644 COPYING \
        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.mypaint"
    install -Dm644 Licenses.md \
        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/Licenses.md"
    install -Dm644 Licenses.dep5 \
        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/Licenses.dep5"
}
