# Copyright 2025-2026 illogician <illogician@tutamail.com>
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit autotools flag-o-matic

DESCRIPTION="MicroGnuEmacs clone featuring added modes, Unicode support, and more"
HOMEPAGE="https://github.com/paaguti/mg3a"
SRC_URI=" https://github.com/paaguti/${PN}/archive/refs/heads/master.zip -> ${P}.zip"

LICENSE="custom"
SLOT="0"
KEYWORDS="amd64 arm arm64 riscv x86"
IUSE="+dired +pipein +prefixregion slow mouse +pythonmode makemode cmode +clikemode +usermodes +usermacros ucsnames +searchall searchsimple searchenter noobjs backups"
S=${WORKDIR}/${PN}-master

DEPEND="sys-libs/ncurses[tinfo]"
RDEPEND="${DEPEND}"
BDEPEND="virtual/pkgconfig"

src_prepare() {
	default
	eapply "${FILESDIR}"/bootstrap.patch
	head -n 17 Makefile.am >> Makefile.am.new && rm Makefile.am && mv Makefile.am.new Makefile.am
	./bootstrap.sh
}

src_configure() {
	eapply "${FILESDIR}"/fileio-c.patch
	eapply "${FILESDIR}"/modestring-name.patch

	append-libs -ltinfo
	append-cflags -std=gnu17
	econf \
		$(use_enable dired) \
		$(use_enable pipein) \
		$(use_enable prefixregion) \
		$(use_enable slow) \
		$(use_enable mouse) \
		$(use_enable pythonmode) \
		$(use_enable makemode) \
		$(use_enable cmode) \
		$(use_enable clikemode) \
		$(use_enable usermodes) \
		$(use_enable usermacros) \
		$(use_enable ucsnames) \
		$(use_enable searchall) \
		$(use_enable searchsimple) \
		$(use_enable searchenter) \
		$(use_enable noobjs) \
		$(use_enable backups)
}

src_compile() {
	emake
}

src_install() {
	mv mg mg3
	dobin mg3

	docompress -x /usr/share/doc/${P}
	insinto "/usr/share/doc/${P}"
	doins ${WORKDIR}/${PN}-master/README.*
	doins -r ${WORKDIR}/${PN}-master/orig
	doins -r ${WORKDIR}/${PN}-master/bl
}