#!/bin/sh
CWD=`pwd`
TMP=${TMP:-/tmp}
PKG=$TMP/package-cyrus-sasl

VERSION=2.1.22
ARCH=${ARCH:-i486}
BUILD=1

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

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf cyrus-sasl-$VERSION
tar xjvf $CWD/cyrus-sasl-$VERSION.tar.bz2
cd cyrus-sasl-$VERSION
chown -R root:root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --disable-static \
  --enable-login \
  --disable-anon \
  --without-ldap \
  --with-saslauthd \
  --with-gdbm \
  --with-dblib=gdbm
make
make install DESTDIR=$PKG
( 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
)
mkdir -p $PKG/var/state/saslauthd
mkdir -p $PKG/etc/rc.d
zcat $CWD/rc.saslauthd.gz > $PKG/etc/rc.d/rc.saslauthd.new
# Fix sloppy man page installation:
rm -r $PKG/usr/man/cat8
cat saslauthd/saslauthd.mdoc > $PKG/usr/man/man8/saslauthd.8
gzip -9 $PKG/usr/man/man?/*.?
mkdir -p $PKG/usr/doc/cyrus-sasl-$VERSION
cp -a \
  AUTHORS COPYING INSTALL NEWS README README.andrew \
  doc \
  $PKG/usr/doc/cyrus-sasl-$VERSION
find $PKG/usr/doc/cyrus-sasl-$VERSION -type f -exec chmod 644 {} \;
rm -f $PKG/usr/doc/cyrus-sasl-$VERSION/doc/*Makefile*
mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

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