#!/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
            etc/pam.d/chage
            etc/pam.d/chfn
            etc/pam.d/chgpasswd
            etc/pam.d/chpasswd
            etc/pam.d/chsh
            etc/pam.d/groupadd
            etc/pam.d/groupdel
            etc/pam.d/groupmems
            etc/pam.d/groupmod
            etc/pam.d/login
            etc/pam.d/newusers
            etc/pam.d/passwd
            etc/pam.d/su
            etc/pam.d/useradd
            etc/pam.d/userdel
            etc/pam.d/usermod
            etc/shadow"

: ${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="acl $PKGDEPENDS"
	with_acl="--with-acl"
else
	with_acl="--without-acl"
fi

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

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

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

if [ "$SELINUX" = "yes" ]; then
	with_selinux="--with-selinux"
else
	with_selinux="--without-selinux"
fi

rm -rf $PKGNAME-$PKGVERSION
tar xvf $PKGNAME-$PKGVERSION.tar.xz
cd $PKGNAME-$PKGVERSION

#
# Disable groups, it's provided by busybox/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= \
	--bindir=/bin \
	--sbindir=/bin \
	--enable-shared \
	$with_acl \
	$with_attr \
	$with_nls \
	$with_pam \
	$with_selinux
make ubindir=/bin usbindir=/bin
make DESTDIR=$DESTDIR ubindir=/bin usbindir=/bin install

if [ "$PAM" = "yes" ]; then
	for i in \
	    chage chfn chgpasswd chpasswd chsh groupadd groupdel \
	    groupmems groupmod login newusers passwd su useradd \
	    userdel usermod; do
		install -Dm0600 ../$i $DESTDIR/etc/pam.d/$i
	done
fi

cd ..
rm -rf $PKGNAME-$PKGVERSION
