# Maintainer: Ricky <rickleaf.wu@gmail.com>

pkgname=emacs
pkgver=31.1
pkgrel=1
pkgdesc="The extensible, customizable, self-documenting, real-time display editor (msys2)"
url="https://www.gnu.org/software/${pkgname}/"
msys2_repository_url='https://cgit.git.savannah.gnu.org/cgit/emacs.git'
msys2_references=(
  "cpe: cpe:/a:gnu:emacs"
)
license=('spdx:GPL-3.0-or-later')
arch=('i686' 'x86_64')
depends=('ncurses' 'zlib' 'libxml2' 'libiconv' 'libgnutls' 'glib2' 'libhogweed')
groups=('editors')
makedepends=('gawk' 'libiconv-devel' 'libxml2-devel' 'libiconv-devel' 'ncurses-devel' 'libgnutls-devel' 'libunistring-devel' 'autotools' 'gcc')
options=('strip')
source=("https://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.xz"{,.sig}
        '001-no-rebase.patch')
sha256sums=('1da5790d9580c81932b5bf700633114468da7b3412d69faa767daebf974f4586'
            'SKIP'
            '8e69312a75d4ac7e4c63c6bee8f385ef0756d9f945c685317b8750746bdd635b')
validpgpkeys=('17E90D521672C04631B1183EE78DAE0F3115E06B' # Eli Zaretskii (eliz) <eliz@gnu.org>
              'CEA1DE21AB108493CC9C65742E82323B8F4353EE'
              '8DC2487E51ABDD90B5C4753F0F56D0553B6D411B')

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

  patch -p1 -i "${srcdir}/001-no-rebase.patch"

  ./autogen.sh
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  # gl_cv_clean_version_stddef:
  # https://github.com/msys2/MSYS2-packages/pull/5503#issuecomment-3054117962

  CPPFLAGS="-DNDEBUG"
  CFLAGS="-pipe -O3 -fomit-frame-pointer -funroll-loops"
  LDFLAGS="-s -Wl,-s"
  # native compilation is not enabled due to not finding libgccjit.h, and is
  # unsupported on 32-bit (and fails configure there due to this)
  ./configure \
    --prefix=/usr \
    --build="${CHOST}" \
    --with-x-toolkit=no \
    --with-sound=yes \
    --with-modules \
    --without-compress-install \
    --without-native-compilation \
    gl_cv_clean_version_stddef=yes

  make
}

package() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  make DESTDIR="${pkgdir}" install
  rm -f "${pkgdir}/usr/share/info/info.info.gz"

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

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

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