#!/bin/sh
#
# Copyright 2005 Paul Blazejowski and Zach Borgerding <zborgerd@gmail.com>
# Available under the terms of the GNU GPL.
# Special thanks to saxa for his help with this package.
#

NAME="shadow"
VERSION="4.1.4.2"
REVISION="1"

PKG_SOURCEURL='ftp://pkg-shadow.alioth.debian.org/pub/pkg-shadow/$NAME-$VERSION.tar.bz2'

PKG_PATCH[0]='shadow-4.1.2.2-login.defs.diff'
PKG_PATCH[1]='shadow-4.1.2.2-slackwareizeuseradd.diff'

# Cribbed from Slackware 13.0 sources 
PKG_PATCH[2]="adduser"

# Still need this tho, for certain.
PKG_PATCH[3]="pam.login"
PKG_PATCH[4]="pam.passwd"

. $DLG_ROOT/config
. $DLG_ROOT/SCRIPTS/dropline-functions.sh

# First round: PAM-less binaries
prepare
applypatch 0 1
applypatch 1 1

# See http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Optimize-Options.html
# for more information about what this new flag does.
cflags -O2 -fstack-protector-all
configure --prefix=$PKG_PREFIX \
	--disable-shared \
	--disable-desrpc \
	--without-selinux \
	--without-audit \
	--without-cracklib \
	--without-libpam

make
makeinstall

#configfile $PKG_SYSCONFDIR/login.defs
# Kludge!
mv $PKG_STAGEROOT$PKG_SYSCONFDIR/login.defs $PKG_STAGEROOT$PKG_SYSCONFDIR/login.defs.new
configfile $PKG_SYSCONFDIR/login.access
configfile $PKG_SYSCONFDIR/limits

createdir $PKG_STAGEROOT/var/log
install -m 640 /dev/null $PKG_STAGEROOT/var/log/faillog
configfile /var/log/faillog

# This will *always* be called 'adduser' so no $PKG_PATCH ref needed.
findpatch filename adduser
install -m 755 $filename $PKG_STAGEROOT$PKG_PREFIX/sbin/adduser

# Slackware puts these in a different location than the default
for filename in faillog lastlog; do
	mv $PKG_STAGEROOT$PKG_PREFIX/bin/$filename $PKG_STAGEROOT$PKG_PREFIX/sbin/$filename
done
chmod 4711 $PKG_STAGEROOT/bin/su
chmod go-r $PKG_STAGEROOT$PKG_PREFIX/bin/*

# Remove these static libs because Slackware doesn't ship them
removedir $PKG_STAGEROOT/lib$LIBSUFFIX

# End of round one.  *DING!*

# Round two: PAM-ized login binary.

# This LDFLAGS setting basically eliminates a spurious linkage to libcrypt,
# which isn't apparently necessary since obviously we can still login.
export LDFLAGS="-Wl,--as-needed -fPIC -lpam -lpam_misc"
# Already set above, but there's no sense in leaving this loosely defined.
cflags -O2 -fstack-protector-all
configure --prefix=$PKG_PREFIX \
	--disable-shared \
	--disable-desrpc \
	--without-selinux \
	--without-audit \
	--with-cracklib \
	--with-libpam 
make

install -m 755 $PKG_SRCROOT/src/login $PKG_STAGEROOT/bin/login
install -m 4711 $PKG_SRCROOT/src/passwd $PKG_STAGEROOT$PKG_PREFIX/bin/passwd

# And now we copy the two needed pam auth file
createdir $PKG_STAGEROOT$PKG_SYSCONFDIR/pam.d
findpatch filename pam.login
install -m 644 $filename $PKG_STAGEROOT$PKG_SYSCONFDIR/pam.d/login

findpatch filename pam.passwd
install -m 644 $filename $PKG_STAGEROOT$PKG_SYSCONFDIR/pam.d/passwd

PKG_DOCFILES="$PKG_DOCFILES doc/*"

tidydocumentation
analyzebinaries
finalizepackage
packagize
cleanup
