#!/bin/sh

PRGNAM=tqtinterface
CWD=`pwd`
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=$CWD

# Set the config option variables if they are not already set:
if [ -r ../KDE.options ]; then
  . ../KDE.options
fi

rm -rf $PKG
rm -fr $TMP/$PRGNAM*
mkdir -p $PKG/usr

# Avoid a version number in .la files:
if [ -d /usr/lib/qt ]; then
  QTDIR=/usr/lib/qt
fi

echo "Building $PRGNAM-$VERSION."
echo
echo "Copying svn dependencies to build environment..."
# Source files from Trinity are already unpacked. Just copy to $TMP.
echo "Copying $PRGNAM source files to $TMP..."
cp -a ../src/dependencies/$PRGNAM $TMP/
cd $TMP/$PRGNAM || exit 1
echo

echo "Building make and config files..."
echo
cp -Rp /usr/lib/build/libtool.m4 admin/libtool.m4.in
cp -Rp /usr/lib/build/ltmain.sh admin/ltmain.sh
make -f admin/Makefile.common
echo
echo "Finished building make and config files."
echo

chown -R root:root .

CFLAGS=$CPUOPT \
CXXFLAGS=$CPUOPT \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc/kde \
  --program-prefix="" \
  --program-suffix=""

make || exit 1
make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/en $PKG/usr/doc/$PRGNAM-$VERSION/

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

( 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
)
if [ -d $PKG/usr/man ]; then
  gzip -9 $PKG/usr/man/man?/*
fi

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

# cleanup
rm -rf $TMP/$PRGNAM*
rm -rf $PKG
