#!/bin/bash -xv

APP="pam"
tarball=$(ls ${softname}*tar*);VERSION=${tarball%.*};VERSION=${VERSION%.*}; VERSION=${VERSION##*-}
ARCH=${ARCH:-x86_64}
BUILD='1jp'

case "$ARCH" in
  i486)      SLKCFLAGS="-O2 -march=i486 -mtune=i686"
             SLKLDFLAGS=""
             LIBDIRSUFFIX=""
             ARCHOPTS=""
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC"
             SLKLDFLAGS="-L/usr/lib64"
             LIBDIRSUFFIX="64"
             ARCHOPTS="--arch=x86_64 --enable-pic"
             ;;
esac

export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export LDFLAGS="$SLKLDFLAGS"

CWD=$(pwd)
PKG=/tmp/$APP-$VERSION-$ARCH-$BUILD
rm -rf $PKG $PKG.t*z
mkdir -p $PKG 
TARBALL="$(ls Linux-PAM*tar*)"
echo $TARBALL

TMP=$CWD/tmp
rm -rf $TMP
mkdir -p $TMP

cd $CWD
tar -xvf $TARBALL -C $TMP


SRC=$(ls -d $TMP/*/)
echo $SRC 


cd $SRC 

# Change ownership and permissions if necessary
# This may not be needed in some source tarballs, but it never hurts
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

./configure --prefix=/usr \
			--libdir=/usr/lib${LIBDIRSUFFIX} \
			--disable-selinux \
			--sysconfdir=/etc \
			--localstatedir=/var \
			--with-gnu-ld \
 			--enable-docdir=/usr/doc/${APP}-${VERSION} \
 			--mandir=/usr/man \
 			--enable-securedir=/usr/lib/security \
 			--enable-sconfigdir=/etc/security \
 			--enable-read-both-confs \
 			--includedir=/usr/include/security 

make -j6 || exit 1

make install DESTDIR=$PKG || exit 1

# Clean 1 or 2 things ...
rm -rf $PKG/var

# Source
mkdir -p $PKG/usr/src/$APP-$VERSION
cp $CWD/build-pam.sh $PKG/usr/src/$APP-$VERSION/

# Documentation
mkdir -p $PKG/usr/doc/$APP-$VERSION 

# Copy documentation to the docs directory and fix permissions
cp -a AUTHORS CHANGELOG COPYING ChangeLog Copyright NEWS README $PKG/usr/share/doc/Linux-PAM/*.txt $PKG/usr/doc/$APP-$VERSION
find $PKG/usr/doc/$APP-$VERSION -type f -exec chmod 644 {} \;
rm -rf $PKG/usr/share/doc

# Strip some libraries and binaries
( cd $PKG
   find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
   find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Compress man pages if they exist
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in `find . -type l` ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
  )
fi

# Compress info pages if they exist (and remove the dir file)
if [ -d $PKG/usr/info ]; then
  gzip -9 $PKG/usr/info/*.info
  rm -f $PKG/usr/info/dir
fi

# Fix some ownerships & permissions
chown root:shadow $PKG/sbin/unix_chkpwd
chmod 2755 $PKG/sbin/unix_chkpwd

# PAM system policy
mkdir -p $PKG/etc/pam.d
rm -f $PKG/etc/pam.d/*
cat << "EOF" > $PKG/etc/pam.d/system-auth
auth       requisite  pam_nologin.so
auth       required   pam_env.so
auth	   required	  pam_unix.so shadow nullok
account	   required	  pam_unix.so
password   required   pam_unix.so nullok obscure min=5 md5 shadow
session	   required	  pam_unix.so
session    required   pam_limits.so
session    optional   pam_lastlog.so
EOF

cat << "EOF" > $PKG/etc/pam.d/other
auth		include		system-auth
account		include		system-auth
password	include		system-auth
session		include		system-auth
EOF

cat << "EOF" > $PKG/etc/security/limits.conf
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - an user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open files
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4
@audio   -  rtprio     99
@audio   -  memlock    unlimited

# End of file
EOF

# Install dir
mkdir -p $PKG/install

cat << "EOF" > $PKG/install/slack-desc
pam: PAM - Pluggable Authentication Modules
pam:
pam: PAM is a free implementation of the sun RFC for solaris.
pam: Basically, it is a flexible mechanism for authenticating users. 
pam: PAM provides a way to develop programs that are independent of
pam: authentication scheme. These programs need "authentication modules" 
pam: to be attatched to them at run-time in order to work. 
pam: 
pam: 
pam: 
pam: 
EOF


cd $PKG
/sbin/makepkg -l y -c n $PKG.txz

rm -rf $PKG
rm -rf $TMP
