_realname=spice
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.15.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgdesc="SPICE: Simple Protocol for Independent Computing Environments (mingw-w64)"
license=("LGPLv2.1")
url="https://www.spice-space.org/"
options=('!emptydirs')
depends=(
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-gstreamer"
             "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
             "${MINGW_PACKAGE_PREFIX}-gst-plugins-good"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-lz4"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-opus"
             "${MINGW_PACKAGE_PREFIX}-pixman"
             "${MINGW_PACKAGE_PREFIX}-spice-protocol"
             "${MINGW_PACKAGE_PREFIX}-zlib"
)
optdepends=(
             "${MINGW_PACKAGE_PREFIX}-gst-plugins-ugly: for the x264enc GStreamer element"
             "${MINGW_PACKAGE_PREFIX}-gst-libav:        for the avenc_mjpeg GStreamer element"
)
makedepends=(
             "${MINGW_PACKAGE_PREFIX}-asciidoc"
             "${MINGW_PACKAGE_PREFIX}-gcc"
)
source=(https://www.spice-space.org/download/releases/spice-server/${_realname}-${pkgver}.tar.bz2{,.sig})
validpgpkeys=('206D3B352F566F3B0E6572E997D9123DE37A484F')
sha256sums=('b320cf8f4bd2852750acb703c15b72856027e5a8554f8217dfbb3cc09deba0f5' 'SKIP')

prepare() {
  # Fix test to recognize CRLF lineending
  find "${srcdir}" -name test-logging.c -exec \
    sed -i "s/\\\\n/\\\\r\\\\n/g" {} \;
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}

  mkdir -p "${srcdir}"/build-${MINGW_CHOST} && cd "${srcdir}"/build-${MINGW_CHOST}

  # --disable-silent-rules: verbose build
  # --enable-extra-checks:  adds (failing) test test-video-encoders
  # --enable-manual=no:     because manual is already provided in tarball
  # --with-sasl=no:         because compilation fails
  # --enable-lz4=${lz4}:    because linking fails on i686
  [ "i686" == "${CARCH}" ] && lz4=no || lz4=yes
  CFLAGS="-g -O2 -fstack-protector" \
  CXXFLAGS="-g -O2 -fno-exceptions -fno-check-new -fstack-protector" \
  LDFLAGS="-g" \
  "${srcdir}"/${_realname}-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --enable-lz4=${lz4} \
    --enable-gstreamer=1.0 \
    --enable-manual=no \
    --with-sasl=no

  make
}

check() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  # CI doesn't check, locally do not fail, but show results
  make check || true
}

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

  cd "${srcdir}"/${_realname}-${pkgver}
  local DOCDIR="${pkgdir}"/${MINGW_PREFIX}/share/doc/${_realname}
  local LICENSEDIR="${pkgdir}"/${MINGW_PREFIX}/share/licenses/${_realname}
  mkdir -pv "$DOCDIR" "$LICENSEDIR"
  cp -pv COPYING "$LICENSEDIR"/
  cp -av README AUTHORS CHANGELOG.md docs/images docs/*.html \
    docs/manual/images docs/manual/*.html \
    docs/manual/manual.chunked \
    "$DOCDIR"/
}
