# Maintainer: Carsten Haitzler <raster@rasterman.com>
# Contributor: Ryan Gammon <ryan@gamnation.net>

_realname=efl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.28.1
pkgrel=2
pkgdesc="Enlightenment Foundation Libraries - Development version (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://www.enlightenment.org"
msys2_repository_url="https://git.enlightenment.org/enlightenment/efl"
license=('spdx:Apache-2.0 AND BSD-2-Clause AND MIT AND FTL AND GPL-2.0-or-later AND LGPL-2.1-only')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-luajit"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-dbus"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libavif"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libjxl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-fribidi"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-gstreamer"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
         "${MINGW_PACKAGE_PREFIX}-libraw"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-mono"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)
source=("https://download.enlightenment.org/rel/libs/${_realname}/${_realname}-${pkgver}.tar.xz"
        "fix-prefix.patch"
        "fix-stat64-mingw-gcc.patch")
sha256sums=('84cf6145f9cc82bfff690005be24392c8f3c52f8e00ff04d8eea371429c09424'
            '9ec7c0a0587a075133ef0d4cb986e9d26970a77cc42d23dfa5ed466d4b851bd5'
            '92779e6fcd4a7d4c6a1ab0f055f921f7911ee2289915082bd7f7a5d2f27b21c6')

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

  patch -Np1 -i "${srcdir}/fix-prefix.patch"
  patch -Np1 -i "${srcdir}/fix-stat64-mingw-gcc.patch"

  # Work around https://github.com/mesonbuild/meson/issues/7267 by creating mcs.bat based on
  # https://github.com/mono/release/blob/master/windows-installer/bat/wrapper.bat
  # and using that as the mono compiler since meson+python subprocess+windows can't run shebang 
  # shell scripts like /mingw64/bin/mcs. Efl supports -Ddotnet as well which doesn't have this 
  # issue, but .net core is not part of mingw and needs to be installed in windows.
  echo @`cygpath -w ${MINGW_PREFIX}/bin/mono.exe` --gc-params=nursery-size=64m %MONO_OPTIONS% `cygpath -w ${MINGW_PREFIX}/lib/mono/4.5/csc.exe` %* > ./csc.bat
}

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  # EFL_RUN_IN_TREE needs to be set manually on Windows. See:
  # https://github.com/Enlightenment/efl-ci/commit/18ce3e8066f954bd32f2ba890e308def86c6a668
  export EFL_RUN_IN_TREE=1

  # In latest gcc 15 gnu23 will be set as default, lots of new keywords are newly reserved for that.
  # See https://en.cppreference.com/w/c/keyword

  CFLAGS+=" -Wno-implicit-function-declaration -Wno-unused-parameter -Wno-deprecated-declarations -Wno-unused-function -Wno-attributes" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  CSC="${srcdir}/${_realname}-${pkgver}/csc.bat" \
    ${MINGW_PREFIX}/bin/meson setup \
      --prefix="${MINGW_PREFIX}" \
      --default-library shared \
      "${_extra_config[@]}" \
      -Dc_std=gnu17 \
      -Dcpp_std=gnu++17 \
      -Dsystemd=false \
      -Dpulseaudio=false \
      -Dv4l2=false \
      -Dlibmount=false \
      -Deeze=false \
      -Dx11=false \
      -Dxinput2=false \
      -Devas-loaders-disabler='pdf','ps','rsvg','json' \
      -Dopengl=none \
      -Dpixman=true \
      -Dembedded-lz4=false \
      -Dfribidi=true \
      -Dinput=false \
      -Dbuild-examples=false \
      -Dbuild-tests=false \
      -Dbindings=mono \
      -Dbuild-examples=false \
      -Dbuild-tests=false \
      -Dlua-interpreter=luajit \
      "../${_realname}-${pkgver}"

   ${MINGW_PREFIX}/bin/meson compile
}

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

  DESTDIR="$pkgdir" ${MINGW_PREFIX}/bin/meson install

  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/" README.md
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/" AUTHORS COMPLIANCE COPYING
}
