#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=shadow
PKGVERSION=4.6
PKGREVISION=1
PKGLICENSE="BSD"
PKGSUMMARY="password and account management"
PKGDOWNLOAD="https://github.com/shadow-maint/shadow/releases/download/$PKGVERSION/$PKGNAME-$PKGVERSION.tar.xz"
PKGOPTIONS="ACL ATTR NLS PAM SELINUX"
PKGPROTECT="etc/login.defs"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${ACL:=yes}
: ${ATTR:=yes}
: ${NLS:=yes}
: ${PAM:=yes}
: ${SELINUX:=no}

if [ "$ACL" = "yes" ]; then
	PKGDEPENDS="core/acl $PKGDEPENDS"
	with_acl="--with-acl"
else
	with_acl="--without-acl"
fi

if [ "$ATTR" = "yes" ]; then
	PKGDEPENDS="core/acl $PKGDEPENDS"
	with_attr="--with-attr"
else
	with_attr="--without-attr"
fi

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="core/gettext:build $PKGDEPENDS"
	with_nls="--enable-nls"
else
	with_nls="--disable-nls"
fi

if [ "$PAM" = "yes" ]; then
	PKGDEPENDS="security/pam $PKGDEPENDS"
	with_pam="--with-pam"
else
	with_pam="--without-pam"
fi

if [ "$SELINUX" = "yes" ]; then
	with_selinux="--enable-selinux"
else
	with_selinux="--disable-selinux"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvaf $PKGNAME-$PKGVERSION.tar.xz
	pushd $PKGNAME-$PKGVERSION

	# disable groups, it's provided by coreutils and documentation by man-pages.
	sed -i 's/groups$(EXEEXT) //' src/Makefile.in
	find man -name Makefile.in -exec sed -i 's/groups\.1 / /'   {} \;
	find man -name Makefile.in -exec sed -i 's/getspnam\.3 / /' {} \;
	find man -name Makefile.in -exec sed -i 's/passwd\.5 / /'   {} \;

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--bindir=/usr/bin \
		--sbindir=/usr/sbin \
		--sysconfdir=/etc \
		--without-audit \
		--without-tcb \
		--without-libcrack \
		--disable-static \
		--enable-shared \
		$with_acl \
		$with_attr \
		$with_nls \
		$with_pam \
		$with_selinux
	make
	make install DESTDIR=$DESTDIR

	# --disable-selinux does not update pam.d files
	if [ "$SELINUX" = "no" ]; then
		sed -i -e "/pam_selinux.so/d" $DESTDIR/etc/pam.d/{login,su}
	fi

	# pam_console.so isn't shipped with linux-pam.
	sed -i -e "/pam_console.so/d" $DESTDIR/etc/pam.d/login

	popd
	rm -rf $PKGNAME-$PKGVERSION
}
