#! /bin/sh

# tmsnc.SlackBuild created by danix
# revision:	1
# date:		26/05/2007
# required:	OpenSSL (>=0.9.7d)
#		NCurses (>=5.4)

# Setting up initial variables:
CWD=`pwd`
TMP=${TMP:-/tmp}
APP=tmsnc
VERSION=0.3.2
ARCH=${ARCH:-i486}
BUILD=1dx
PKG=$TMP/package-$APP

# Setting up SLKCFLAGS
if [ "$ARCH" = "i386" ]; then
	SLKCFLAGS="-O2 -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
	SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "x86-64" ]; then
	SLKCFLAGS="-O2 -fPIC"
fi

# Extracting Sources
rm -rf $PKG
mkdir -p $PKG
rm -rf $TMP/$APP-$VERSION
cd $TMP || exit 1
tar -zxvf "$CWD"/$APP-$VERSION.tar.gz || exit 1
cd $APP-$VERSION || exit 1
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Configure and compile sources
CFLAGS="$SLKCFLAGS" \
	./configure \
	--prefix=/usr \
	--sysconfdir=/etc \
	--localstatedir=/var \
	--mandir=/usr/man \
	--build=$ARCH-slackware-linux \
	--host=$ARCH-slackware-linux
make || exit
make install DESTDIR=$PKG || exit

# Install documentation
mkdir -p $PKG/usr/doc/$APP-$VERSION
cp -a AUTHORS COPYING ChangeLog README INSTALL doc/manual.* /$PKG/usr/doc/$APP-$VERSION
find /$PKG/usr/doc/$APP-$VERSION -type f -exec chmod 644 {} \;
cat "$CWD"/$APP.SlackBuild > $PKG/usr/doc/$APP-$VERSION/$APP.SlackBuild

# Final tuning
mkdir -p $PKG/install
cat "$CWD"/slack-desc > $PKG/install/slack-desc
if [ -e "$CWD"/doinst.sh.gz ]; then
	zcat "$CWD"/doinst.sh.gz > $PKG/install/doinst.sh
fi
(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 objects" | grep ELF | cut -f 1 -d : | xargs strip --strip unneeded 2> /dev/null
)
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
if [ -d $PKG/usr/info ]; then
	gzip -9 $PKG/usr/info/*.info
	rm -f $PKG/usr/info/dir
fi

# Building the package
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
/sbin/makepkg -l y -c n "$CWD"/$APP-$VERSION-$ARCH-$BUILD.tgz

