#!/bin/sh

# Slackware build script for libcap

# Written by Menno Duursma

# This program is free software. It comes without any warranty.
# Granted WTFPL, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.

PRGNAM=libcap
VERSION=2.14
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP 
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION || exit 1
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Set the CFLAGS
sed -i.orig "s/^\(DEBUG =\).*/\1$SLKCFLAGS/" Make.Rules

make DYNAMIC=yes
make install FAKEROOT=$PKG man_prefix=/usr

( 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
)

# Add included scripts
( cd contrib || exit 1
  for file in pcaps4convenience pcaps4server pcaps4suid0 ; do
    install -m 0755 -D $file $PKG/usr/sbin/$file
  done
)

# glibc already has the capget/capset manpage
rm -rf $PKG/usr/man/man2

# Compress the man pages
( 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
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  CHANGELOG License README License pgp.keys.asc doc/capability.notes \
  progs/quicktest.sh $CWD/capfaq-0.2.txt $CWD/README.SLACKWARE \
  $PKG/usr/doc/$PRGNAM-$VERSION
chown -R root:root $PKG/usr/doc
find $PKG/usr/doc -type f -exec chmod 644 {} \;

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $TMP/$PRGNAM-$VERSION-$ARCH-$BUILD.tgz