# Template file for 'smartmontools'
pkgname=smartmontools
version=7.5
revision=0
short_desc="S.M.A.R.T. disk health monitoring (smartctl)"
maintainer="finwo <finwo@pm.me>"
license="GPL-2.0-or-later"
homepage="https://www.smartmontools.org/"
distfiles="https://downloads.sourceforge.net/project/smartmontools/smartmontools/${version}/smartmontools-${version}.tar.gz"
checksum=690b83ca331378da9ea0d9d61008c4b22dde391387b9bbad7f29387f2595f76e
depends="glibc busybox libstdc++"
makedepends="libstdc++"

# Notes:
# - Why: a switch's boot media is usually an eMMC or a small SSD, and its wear
#   state is the thing that predicts the next outage. smartctl is how you read
#   it. The S5248F-ON captured in PROGRESS.md 2.1 boots from /dev/sda.
# - smartd (the polling daemon) is deliberately NOT shipped: it wants cron-like
#   scheduling and a mail transport, neither of which UNOS has. Health polling
#   belongs in the control plane later, not in a second supervisor.
# - The drive database is built in (--with-nvme-devicescan aside, no runtime
#   update path). update-smart-drivedb needs curl/gpg and is not shipped.

do_configure() {
	cd "${WRKSRC}"
	./configure \
		--prefix=/usr \
		--sbindir=/usr/sbin \
		--sysconfdir=/etc \
		--disable-dependency-tracking \
		--without-libcap-ng \
		--without-libsystemd \
		--without-selinux \
		--without-gnupg \
		--without-update-smart-drivedb \
		--with-nvme-devicescan
}

do_build() {
	cd "${WRKSRC}"
	make -j"$(nproc)"
}

do_install() {
	cd "${WRKSRC}"
	make install DESTDIR="${DESTDIR}"

	# smartd is not shipped (see notes): drop the daemon, its config, its
	# init/service glue and its manpages rather than leaving dead weight.
	rm -f  "${DESTDIR}"/usr/sbin/smartd
	rm -f  "${DESTDIR}"/etc/smartd.conf
	rm -f  "${DESTDIR}"/usr/share/man/man8/smartd.8 \
	       "${DESTDIR}"/usr/share/man/man5/smartd.conf.5
	# smartd_warning.sh is smartd's notification helper; without smartd it is
	# an orphan that only looks like a supported hook.
	rm -f  "${DESTDIR}"/etc/smartd_warning.sh
	rm -rf "${DESTDIR}"/etc/smartd_warning.d \
	       "${DESTDIR}"/usr/share/smartmontools \
	       "${DESTDIR}"/usr/libexec \
	       "${DESTDIR}"/lib/systemd "${DESTDIR}"/usr/lib/systemd \
	       "${DESTDIR}"/etc/init.d "${DESTDIR}"/etc/rc.d
	rm -rf "${DESTDIR}/usr/share/doc" "${DESTDIR}/usr/share/locale"

	[ -x "${DESTDIR}/usr/sbin/smartctl" ] || die "smartmontools: no smartctl"
	[ ! -e "${DESTDIR}/usr/sbin/smartd" ] || die "smartmontools: smartd should not ship"
	[ ! -e "${DESTDIR}/etc/smartd_warning.sh" ] || die "smartmontools: smartd helper left behind"
	readelf -d "${DESTDIR}/usr/sbin/smartctl" | grep -q 'libstdc++\.so\.6' \
		|| die "smartmontools: smartctl does not link libstdc++.so.6"
}
