# Template file for 'shadow'
pkgname=shadow
version=4.6
revision=1
build_style=gnu-configure
configure_args="--bindir=/usr/bin --sbindir=/usr/bin
 --libdir=/usr/lib --enable-shared --disable-static
 --with-libpam --without-selinux --with-acl --with-attr
 --disable-nls --enable-subordinate-ids"
hostmakedepends="automake gettext-devel libtool"
makedepends="acl-devel pam-devel"
depends="pam"
short_desc="Shadow password file utilities"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
license="BSD-3-Clause"
homepage="https://github.com/shadow-maint/shadow"
distfiles="${homepage}/releases/download/${version}/shadow-${version}.tar.xz"
checksum=0998c8d84242a231ab0acb7f8613927ff5bcff095f8aa6b79478893a03f05583
conf_files="/etc/pam.d/* /etc/default/* /etc/login.defs"

pre_configure() {
	autoreconf -fi

	case "$XBPS_TARGET_MACHINE" in
		# Completely disable unportable ruserok().
		*-musl) sed '/RUSEROK/d' -i configure;;
	esac
}

do_build() {
	# Don't install groups(1), we use the one from coreutils.
	sed -i 's/groups$(EXEEXT) //' src/Makefile
	for f in $(find man -name Makefile); do
		sed -i 's/groups\.1 / /' $f
	done
	make ${makejobs}
}

post_install() {
	mv ${DESTDIR}/usr/sbin/* ${DESTDIR}/usr/bin

	# Install our pam files not the ones supplied with shadow.
	rm -f ${DESTDIR}/etc/pam.d/*
	for f in chage passwd; do
		install -m644 ${FILESDIR}/${f}.pam ${DESTDIR}/etc/pam.d/${f}
	done
	for f in chpasswd chgpasswd groupadd groupdel groupmems \
		 groupmod newusers useradd userdel usermod; do
		install -m644 $DESTDIR/etc/pam.d/chage $DESTDIR/etc/pam.d/${f}
	done
	install -m644 ${FILESDIR}/login.defs ${DESTDIR}/etc

	# Disable creating mailbox files by default.
	sed -i -e 's/yes/no/' $DESTDIR/etc/default/useradd
	# Change default group to the users gid (100).
	sed -i -e 's/^\(GROUP\)=\(.*\)$/\1=100/' ${DESTDIR}/etc/default/useradd

	chmod 644 ${DESTDIR}/etc/default/useradd

	# Install the cron daily job.
	install -Dm744 ${FILESDIR}/shadow.cron-daily \
		${DESTDIR}/etc/cron.daily/shadow

	# Remove groups.1 manpage provided by coreutils.
	rm -f ${DESTDIR}/usr/share/man/man1/groups.1

	# Remove utilities provided by util-linux and logoutd.
	mv ${DESTDIR}/usr/bin/{newgrp,sg}

	rm \
		$DESTDIR/usr/bin/{login,su,chsh,chfn} \
		$DESTDIR/usr/bin/{nologin,logoutd,vipw,vigr}

	# ...and their many man pages
	find $DESTDIR/usr/share/man \
		'(' -name 'chsh.1'  -o \
		-name 'chfn.1'  -o \
		-name 'su.1'    -o \
		-name 'login.1' -o \
		-name 'vipw.8'  -o \
		-name 'vigr.8'  -o \
		-name 'logoutd.8' -o \
		-name 'nologin.8' -o \
		-name 'newgrp.1' ')' \
		-delete

	vlicense $FILESDIR/LICENSE
}
