# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit flag-o-matic toolchain-funcs

# Kermit only uses the third component, e.g. cku211.tar.gz for
# what we would call 8.0.211. 4-5 are for betas if used.
MY_P="cku$(ver_cut 3-5)"
MY_P=${MY_P/_/-}

# ckermit gets releases very infrequently, so don't be too afraid to
# package betas. It's better than it being unbuildable and so on.
DESCRIPTION="Combined serial and network communication software package"
HOMEPAGE="https://www.openkermit.org/"
SRC_URI="
	https://github.com/OpenKermit/${PN}/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
"

LICENSE="BSD"
SLOT="0"
KEYWORDS="~amd64 ~arm ~hppa ~ppc ~ppc64 ~x86"
IUSE="ncurses test"

RESTRICT="!test? ( test )"

DEPEND="
	ncurses? ( >=sys-libs/ncurses-5.2:= )
"
RDEPEND="
	${DEPEND}
	net-dialup/lrzsz
	test? ( dev-libs/check )
"
BDEPEND="
	ncurses? ( virtual/pkgconfig )
"

PATCHES=(
	"${FILESDIR}"/${PN}-10.0.416_beta12-build-wart.patch
	"${FILESDIR}"/${PN}-11.1.516-fix-makefile-typos.patch
	"${FILESDIR}"/${PN}-11.1.516-tests.patch
)

src_prepare() {
	default

	#tc-export_build_env BUILD_CC
}

src_compile() {
	# "ckcfn3.c:224:16: error: type of 'sndfilter' does not match original declaration"
	filter-lto

	# We don't enable any of the telnet/ftp authentication stuff
	# since there are other packages which do these things better
	# USE="kerberos pam shadow ssl zlib"
	append-cppflags -DNO_AUTHENTICATION -DNOLOGIN -DNOFTP

	if use ncurses; then
		append-cppflags "-DCK_NCURSES"
		append-cppflags "$($(tc-getPKG_CONFIG) --cflags ncurses)"
		append-libs "$($(tc-getPKG_CONFIG) --libs ncurses)"
	fi

	append-cppflags -DHAVE_PTMX # bug #202840
	append-cppflags -DHAVE_CRYPT_H -DHAVE_OPENPTY -DNO_PTY_XOPEN_SOURCE
	append-cppflags -DNOARROWKEYS # bug #669332

	local emakeargs=(
		CC="$(tc-getCC)"
		CC2="$(tc-getCC)"
		KFLAGS="${CPPFLAGS} ${CFLAGS} -std=gnu17"
		LIBS="-lcrypt -lresolv -lutil ${LIBS}"
		LNKFLAGS="${LDFLAGS}"
	)

	emake "${emakeargs[@]}" linuxa
}

src_install() {
	dodir /usr/bin /usr/share/man/man1

	emake DESTDIR="${ED}" prefix=/usr manroot=/usr/share install
	dodoc -r *.txt README.md doc/*

	# make the correct symlink
	rm "${ED}"/usr/bin/kermit-sshsub || die
	dosym kermit /usr/bin/kermit-sshsub
}
