# Maintainer: Frederik Carlier <frederik.carlier@keysight.com>

_realname=gnustep-back
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.32.0
pkgrel=1
pkgdesc="GNUstep GUI Backend (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gnustep.github.org/'
msys2_repository_url='https://github.com/gnustep/libs-back'
license=('spdx:LGPL-2.1-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-lld"
             "${MINGW_PACKAGE_PREFIX}-gnustep-make"
             rsync)
depends=("${MINGW_PACKAGE_PREFIX}-gnustep-gui"
         "${MINGW_PACKAGE_PREFIX}-cairo")
source=("${msys2_repository_url}/releases/download/back-${pkgver//./_}/${_realname}-${pkgver}.tar.gz")
sha256sums=('ce171095012ac5d845f6e1285a5c71e011fd00aa5981ea1d5a5183706478218d')

build() {
  rsync --recursive --times --links "${srcdir}/${_realname}-${pkgver}"/* "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  . ${MINGW_PREFIX}/share/GNUstep/Makefiles/GNUstep.sh

  case ${MSYSTEM} in
    MINGW*|UCRT*)
      export LDFLAGS="-fuse-ld=lld -lstdc++ -lgcc_s"
      ;;
    CLANG*)
      export LDFLAGS="-lc++"
      ;;
  esac

  CC="$MINGW_PREFIX/bin/clang" \
  CXX="$MINGW_PREFIX/bin/clang++" \
  ./configure \
    --enable-graphics=cairo \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}"
  
  CC="$MINGW_PREFIX/bin/clang" \
  CXX="$MINGW_PREFIX/bin/clang++" \
  make
}

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

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

  make install DESTDIR="${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB"
}
