# Maintainer: András Svraka <svraka.andras@gmail.com>

_realname=emacs-pdf-tools-server
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
# Issues may arise if not used with the same release of the pdf-tools emacs package
pkgver=1.1.0
pkgrel=1
pkgdesc="Emacs support library for PDF files (mingw-w64)"
arch=("any")
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/vedang/pdf-tools"
msys2_references=(
  'aur: emacs-pdf-tools-git'
)
license=("spdx:GPL-3.0")
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-emacs")
optdepends=("${MINGW_PACKAGE_PREFIX}-imagemagick")
source=("https://github.com/vedang/pdf-tools/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
noextract=(${_realname}-${pkgver}.tar.gz)
sha256sums=("bb5badef03e27411e62290d71c9e4657952230b6bd557c8523699d42c30a3866")

prepare() {
  # Workaround for symlinks in tarball and the different package and git repo
  # names.
  mkdir -p "${srcdir}/${_realname}-${pkgver}"
  tar xzf "${srcdir}/${_realname}-${pkgver}.tar.gz" \
    -C "${srcdir}/${_realname}-${pkgver}" \
    --strip-components=1 \
    --exclude "README"

  cd "${srcdir}/${_realname}-${pkgver}/server"

  autoreconf -fvi
}

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

  # For vasprintf
  CFLAGS+=" -D_GNU_SOURCE" \
  CXXFLAGS+=" -std=c++17" \
  ../"${_realname}-${pkgver}"/server/configure \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}"

  make
}

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

  cd "${srcdir}/${_realname}-${pkgver}"
  install --target-directory="${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname} \
          -D --mode=644 \
          README.org NEWS
  install --target-directory="${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname} \
          -D --mode=644 \
          COPYING COPYING.SYNCTEX
}
