# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=mono
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.12.0.199
pkgrel=2
pkgdesc='Free implementation of the .NET platform including runtime and compiler (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://www.mono-project.com/'
msys2_repository_url="https://github.com/mono/mono"
msys2_references=(
  'archlinux: mono'
  'cpe: cpe:/a:mono:mono'
  "cpe: cpe:/a:microsoft:mono"
  "cpe: cpe:/a:mono-project:mono"
)
license=('spdx:GPL OR LGPL-2.1 OR MPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-winpthreads"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-ca-certificates")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "perl-XML-Parser")
provides=("${MINGW_PACKAGE_PREFIX}-monodoc")
conflicts=("${MINGW_PACKAGE_PREFIX}-monodoc")
source=(https://download.mono-project.com/sources/mono/${_realname}-${pkgver}.tar.xz
        0001-mono-fix-build.patch
        0002-mono-avoid-appending-prefix.patch
        0003-mono-fix-runtime.patch
        0004-mono-configure-fixes.patch
        0005-mono-omit-missing-files-refs.patch)
sha256sums=('c0850d545353a6ba2238d45f0914490c6a14a0017f151d3905b558f033478ef5'
            '1537af8ef99b7835c4576020d628c7649cea9e2740439b1bce0c9957d182bf0e'
            '5d3e0923a79321d815fc0ac9845f742bca9fc0d0684eee8bfb0047586d1e4611'
            '961b86fc875ef4570d2df4500a5dca5878e5e9276c89a26ffd870a97b2ee8ffd'
            'bfa386d0c34251b726f95781a2029615bf747445c255c58f1f366723c8d2f565'
            '1656ba3332bd7813b9b6041a642062b5abe6a8f1dedc6639eb76ceb4cbba570c')

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-mono-fix-build.patch \
    0002-mono-avoid-appending-prefix.patch \
    0003-mono-fix-runtime.patch \
    0004-mono-configure-fixes.patch \
    0005-mono-omit-missing-files-refs.patch

  # Remove obsolete file that causes build failure
  rm -f mcs/class/System/win32_net_4_x_System_xtest.dll.exclude.sources

  autoreconf-2.69 -fi
}

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

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-boehm \
    --with-mcs-docs=no

  make get-monolite-latest
  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths in bin directory
  for f in "${pkgdir}"${MINGW_PREFIX}/bin/{al,al2,aprofutil,caspol,cccheck,ccrewrite,cert2spc,certmgr,cert-sync,chktrust,crlupdate,csc,csharp,csi,disco,dmcs,dtd2rng,dtd2xsd,gacutil,gacutil2,genxs,httpcfg,ikdasm,ilasm,illinkanalyzer,installvst,lc,macpack,makecert,mconfig,mcs,mdassembler,mdbrebase,mdoc,mdoc-assemble,mdoc-export-html,mdoc-export-msxdoc,mdoc-update,mdoc-validate,mdvalidater,mkbundle,mod,mono-api-html,mono-api-info,mono-cil-strip,mono-configuration-crypto,monodocer,monodocs2html,monodocs2slashdoc,mono-find-provides,mono-find-requires,monolinker,monop,monop2,mono-package-runtime,mono-service,mono-service2,mono-shlib-cop,mono-symbolicate,mono-test-install,mono-xmltool,mozroots,pdb2mdb,permview,peverify,resgen,resgen2,secutil,setreg,sgen,signcode,sn,soapsuds,sqlmetal,sqlsharp,svcutil,vbc,wsdl,wsdl2,xbuild,xsd}; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${f}
  done

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
