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

pkgname=emacs
pkgver=27.2
pkgrel=1
pkgdesc="The extensible, customizable, self-documenting, real-time display editor (msys2)"
url="https://www.gnu.org/software/${pkgname}/"
license=('GPL3')
arch=('i686' 'x86_64')
depends=('ncurses' 'zlib' 'libxml2' 'libiconv' 'libcrypt' 'libgnutls' 'glib2' 'libhogweed')
groups=('editors')
makedepends=('gawk' 'libiconv-devel' 'libxml2-devel' 'libiconv-devel' 'ncurses-devel' 'libcrypt-devel' 'libgnutls-devel' 'libunistring-devel')
options=('strip')
source=("https://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.xz"{,.sig}
        'configure.ac.diff')
sha256sums=('b4a7cc4e78e63f378624e0919215b910af5bb2a0afc819fad298272e9f40c1b9'
            'SKIP'
            '0cb204f2cab4740d27a47a4adc7e4168d6034e86cd522605a85bbd03fb1db59d')
validpgpkeys=('E6C9029C363AD41D787A8EBB91C1262F01EB8D39' # Eli Zaretskii (eliz) <eliz@gnu.org>
              '28D3BED851FDF3AB57FEF93C233587A47C207910') # Nicolas Petton <nicolas@petton.fr>

prepare() {
  cd "${pkgname}-${pkgver}"
  patch --binary --forward -p0 < "${srcdir}/configure.ac.diff"
  ./autogen.sh
}

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

  CPPFLAGS="-DNDEBUG"
  CFLAGS="-pipe -O3 -fomit-frame-pointer -funroll-loops"
  LDFLAGS="-s -Wl,-s"
  ./configure \
    --prefix=/usr \
    --build="${CHOST}" \
    --with-x-toolkit=no \
    --with-sound=yes \
    --with-modules \
    --without-compress-install

  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'.
