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

pkgname=emacs
pkgver=30.2
pkgrel=1
pkgdesc="The extensible, customizable, self-documenting, real-time display editor (msys2)"
url="https://www.gnu.org/software/${pkgname}/"
msys2_references=(
  "cpe: cpe:/a:gnu:emacs"
)
license=('GPL3')
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=('b3f36f18a6dd2715713370166257de2fae01f9d38cfe878ced9b1e6ded5befd9'
            'SKIP'
            '4a82e7f1c51ab4084dcd2625bba2c04b4a99524532e02b641ee91d2748ffddc3')
validpgpkeys=('17E90D521672C04631B1183EE78DAE0F3115E06B' # Eli Zaretskii (eliz) <eliz@gnu.org>
              'CEA1DE21AB108493CC9C65742E82323B8F4353EE')

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
  mv "${pkgdir}/usr/bin/ctags.exe" \
    "${pkgdir}/usr/bin/ctags-emacs.exe"
  mv "${pkgdir}/usr/share/man/man1/ctags.1" \
     "${pkgdir}/usr/share/man/man1/ctags-emacs.1"
  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'.
