# Maintainer: Haroogan <Haroogan@gmail.com>
# Maintainer: Oscar Fuentes <ofv@wanadoo.es>

_realname=emacs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=27.1
pkgrel=2
pkgdesc="The extensible, customizable, self-documenting, real-time display editor (mingw-w64)"
url="https://www.gnu.org/software/${_realname}/"
license=('GPL3')
arch=('any')
depends=("${MINGW_PACKAGE_PREFIX}-universal-ctags-git"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-jansson"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
optdepends=("${MINGW_PACKAGE_PREFIX}-giflib"
            "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
            "${MINGW_PACKAGE_PREFIX}-libpng"
            "${MINGW_PACKAGE_PREFIX}-librsvg"
            "${MINGW_PACKAGE_PREFIX}-libtiff"
	    # ImageMagick is considered unsafe and unstable. See
	    # INSTALL file on Emacs top source directory. If
	    # ImageMagick support is restored, check if the patch in
	    # image.c.diff is still necessary:
            # "${MINGW_PACKAGE_PREFIX}-imagemagick"
            "${MINGW_PACKAGE_PREFIX}-libxml2")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "make"
             "autoconf"
             "texinfo"
             "patch"
             "${optdepends[@]}")
# Don't zip info files because the built-in info reader uses gzip to
# decompress them. gzip is not available as a mingw binary.
options=('strip' '!zipman')
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"{,.sig})
sha256sums=('4a4c128f915fc937d61edfc273c98106711b540c9be3cd5d2e2b9b5b2f172e41'
            'SKIP')
validpgpkeys=('28D3BED851FDF3AB57FEF93C233587A47C207910')

prepare() {
  cd "${_realname}-${pkgver}"
  ./autogen.sh
}

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

  ../${_realname}-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --with-modules \
    --without-dbus \
    --without-compress-install

  # --without-compress-install is needed because we don't have gzip in
  # the mingw binaries.

  make
}

package() {
  cd "build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  rm -f "${pkgdir}${MINGW_PREFIX}/bin/ctags.exe"
  rm -f "${pkgdir}${MINGW_PREFIX}/share/man/man1/ctags.1.gz"

  local dir="${pkgdir}${MINGW_PREFIX}/share/${_realname}"
        dir="${dir}/$(ls -1 ${dir} | grep -E '([0-9]+\.[0-9]+)(\.[0-9]+)?')/src"

  mkdir -p "${dir}"
  cd "${srcdir}/${_realname}-${pkgver}/src"
  cp *.c *.h *.m "${dir}"
}

# TODO:
# Patch `shell-file-name' default in the C source code similarly to
# `source-directory'.
