#!/bin/sh
# Heavily based on the Slackware 13.1 SlackBuild
# http://web.mit.edu/kerberos/dist/index.html
# Packager Andrea Sciucca  ( gohanz at infinito.it)
# http://www.slacky.eu
#
# Thanks to Tom Canich, State College, Pennsylvania, USA



CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=krb5
PKG=$TMP/package/$NAME
VERSION=1.7.1
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}

SOURCE=http://web.mit.edu/kerberos/dist/krb5/1.7/$NAME-$VERSION-signed.tar

if [ ! -e $NAME-$VERSION-signed.tar ]; then
wget -c $SOURCE
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i486"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i486"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"; CHOST="x86_64"
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP
tar xvf  $CWD/$NAME-$VERSION-signed.tar || exit 1
tar xvzf $NAME-$VERSION.tar.gz || exit 1

echo -e "\E[0;32m+---------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild Kerberos |\E[0;0m"
echo -e "\E[0;32m+---------------------------+\E[0;0m"

cd $NAME-$VERSION

find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

chown -R root:root .

( cd src
LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure 	--prefix=/usr/kerberos \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--mandir=/usr/kerberos/man \
		--libdir=/usr/lib${LIBDIRSUFFIX} \
		--without-krb4 \
		--enable-dns-for-realm \
		--with-ldap \
		--program-prefix= \
		--program-suffix= \
		--build=$CHOST-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1
)

mkdir -p $PKG/etc/profile.d/
cat $CWD/kerberos.sh > $PKG/etc/profile.d/kerberos.sh
cat $CWD/kerberos.csh > $PKG/etc/profile.d/kerberos.csh
chmod 0755 $PKG/etc/profile.d/*


( 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
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)



mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cp -a  README \
$PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/$NAME.SlackBuild



( cd $PKG/usr/kerberos/man
  find . -type f -exec gzip -9 {} \;
  for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)


cd $PKG
chown -R root:root $PKG
if [ -x "$(which depfinder 2>/dev/null)" ];then
  depfinder -f -s -a -p $PKG
  mv slack-required $CWD/
elif [ -x "$(which requiredbuilder 2>/dev/null)" ];then
  requiredbuilder -y -v -s $CWD $PKG
fi

/sbin/makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP
fi
