# Copyright 2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit systemd linux-info

DESCRIPTION="A very simple kdump setup for Archlinux"
HOMEPAGE="https://github.com/adam900710/simple-kdump"
if [[ "${PV}" == *"9999" ]]; then
	inherit git-r3
	EGIT_REPO_URI="https://github.com/adam900710/simple-kdump.git"
else
	COMMIT=90376078e295b3d117f81d8ba13d21acf7af1763
	SRC_URI="https://github.com/adam900710/simple-kdump/archive/${COMMIT}.tar.gz -> ${P}.tar.gz"
	S="${WORKDIR}/simple-kdump-$COMMIT"
fi

LICENSE="MIT"
SLOT="0"
KEYWORDS=""

RDEPEND="
	sys-apps/systemd
	sys-apps/kexec-tools
	sys-apps/makedumpfile[zstd]
"

pkg_setup() {
	# https://github.com/adam900710/simple-kdump/issues/5#issuecomment-3272400656
	CONFIG_CHECK="
		~CRASH_RESERVE
		~KEXEC
		~CRASH_DUMP
	"
	# https://github.com/adam900710/simple-kdump
	CONFIG_CHECK+="
		~PROC_VMCORE
	"
	## https://wiki.archlinux.org/title/Kdump#Manual_steps
	#CONFIG_CHECK+="
	#	~DEBUG_INFO
	#	~CRASH_DUMP
	#	~PROC_VMCORE
	#	~DEBUG_INFO
	#	~DEBUG_INFO_BTF
	#"

	linux-info_pkg_setup
}

src_compile() {
	# due to QA complaint: https://wiki.gentoo.org/wiki/Project:Systemd/conf.d_files
	sed -i 's~EnvironmentFile=/etc/conf.d/~EnvironmentFile=/etc/~' simple-kdump-setup.service || die
}

src_install() {
	systemd_dounit emergency-kdump.target simple-kdump-setup.service simple-kdump-collect.service

	# due to QA complaint: https://wiki.gentoo.org/wiki/Project:Systemd/conf.d_files
	insinto /etc/
	doins simple-kdump.conf
}

info_message() {
	elog "See https://wiki.archlinux.org/title/Kdump#The_automatic_way:_simple-kdump or"
	elog "https://github.com/adam900710/simple-kdump?tab=readme-ov-file#how-to-use"
	elog "on how to configure simple-kdump. Configuration file is located in"
	elog "/etc/simple-kdump.conf instead."
	elog ""
	elog "Note: simple-kdump uses static kernel and initramfs paths."
	elog "This may be incompatible with your kernel setup without further tinkering."
	elog "You may be able to set dracut_rescue_image to \"yes\" to create a"
	elog "rescue initramfs in dracut which should give you a path to a kernel & initramfs"
	elog "without changing on every kernel release, but that has only been tested with"
	elog "systemd-boot setups."
	elog ""
	elog "This only work with systems that uses systemd as the init system."
	elog "This also uses /var/crash as the hardcoded directory where files go to."
}

pkg_postinst() {
	info_message
}