#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# pam.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2010  Vincent Batts, vbatts@hashbangbash.com
# Copyright 2010-2026  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Set some variables using values from the Slackware upstream scripts:
pushd $CWD
XSB=$CWD/$PKGNAM.SlackBuild
[ ! -x $XSB ] && { echo "ERROR: Cannot find x86 SlackBuild for ${PKGNAM}!" ; exit 1; }
for upstreamvar in \
  SRCNAM \
  PAMRHVER \
  ; do
  eval $( grep -E "^${upstreamvar}=" $XSB  )
  echo "Upstream variable: ${upstreamvar} version $( eval $( echo "echo \$${upstreamvar}" ) )"
done
popd

# Extract source:
tar xf $CWD/$SRCNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $SRCNAM-*/ || failextract
slackhousekeeping

# Better take the Red Hat added modules and patches, because that's very
# likely to be the most standard as far as PAM goes:
tar xvf $CWD/pam-redhat-$PAMRHVER.tar.?z || exit 1
for file in CHANGELOG COPYING README ; do
  mv pam-redhat-$PAMRHVER/${file}* ./${file}.pam-redhat
done
# Add additional PAM modules from Red Hat:
for file in pam-redhat-$PAMRHVER/* ; do
  if [ ! -d modules/$(basename $file) ]; then
    echo "Moving module directory $(basename $file)."
    mv -fv $file modules
  else
    echo "$(basename $file) already exists in modules/, not moving!"
  fi
done

# Apply patches:
cat $CWD/fedora-patches/pam-1.5.3-unix-nomsg.patch | patch -p1 --verbose || exit 1
cat $CWD/fedora-patches/pam-1.7.0-redhat-modules.patch | patch -p1 --verbose || exit 1

# Improve the comments in /etc/environment:
cat $CWD/patches/pam.etc.environment.better.comments.diff | patch -p1 --verbose || exit 1

# Configure:
mkdir meson-build
pushd meson-build
export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
meson setup \
   --prefix=/ \
   --libdir=/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --includedir=/usr/include \
   --localstatedir=/var \
   --localedir=/usr/share/locale \
   --mandir=/usr/man \
   --buildtype=release \
   -Dsecuredir=/lib${LIBDIRSUFFIX}/security \
   -Dpam_lastlog=enabled \
   -Dvendordir=/etc \
   .. || failconfig

# Build:
"${NINJA:=ninja}" $NUMJOBS || ${NINJA} || failmake

# Install into package:
DESTDIR=$PKG $NINJA install || failinstall
popd

# Relocate some documentation:
mkdir -p $PKG/usr/doc/pam-$VERSION
mv $PKG/share/doc/Linux-PAM/* $PKG/usr/doc/pam-$VERSION
rm -r $PKG/share

# Thanks to BLFS for the manpages:
( cd $PKG
  tar xf $CWD/${SRCNAM}-*-manpages.tar.?z || exit 1
) || failextract

# Add extra symlinks added by pam.spec:
( cd $PKG/lib${LIBDIRSUFFIX}/security
  for type in acct auth passwd session ; do
    ln -vsf pam_unix.so pam_unix_${type}.so
  done )

# This package can own the /etc/pam.d/ directory
mkdir -p $PKG/etc/pam.d

# Relocate pkgconfig files:
if [ -d $PKG/lib${LIBDIRSUFFIX}/pkgconfig -a ! -d $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig ]; then
   mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}
   mv -fv $PKG/lib${LIBDIRSUFFIX}/pkgconfig $PKG/usr/lib${LIBDIRSUFFIX}
fi

# Don't clobber config files:
find $PKG/etc -type f -exec mv {} {}.new \;

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  AUTHORS COPYING* Copyright NEWS README* \
  $PKG/usr/doc/$PKGNAM-$VERSION
rm -f $PKG/usr/doc/$PKGNAM-$VERSION/index.html
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file
changelogliposuction CHANGELOG $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh

# Append config statements to the install/doinst.sh:
( cd $PKG
  for i in $(find etc -type f -name "*.new") ; do
    echo "config $i" >> $PKG/install/doinst.sh ;
  done )

# This is a pam helper that can only be called from pam
chown root:root $PKG/sbin/unix_chkpwd
chmod 6755 $PKG/sbin/unix_chkpwd

slackmp -p       # run makepkg -p -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
