#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://trolltech.com
# Packagers Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
# Required: Postgresql, Unixodbc, Sqlite

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/qt-x11-opensource
NAME=qt-x11-opensource
VERSION=4.3.4
CHOST=i486
ARCH=${ARCH:-i686}
BUILD=2as
SOURCE=ftp://ftp.trolltech.com/qt/source/$NAME-src-$VERSION.tar.gz

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

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
fi

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

cd $TMP
tar xvzf $CWD/$NAME-src-$VERSION.tar.gz

echo -e "\E[0;32m+------------------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+------------------------------------------+\E[0;0m"

cd $NAME-src-$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 .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure -prefix /opt/qt4 -qt-gif -no-exceptions -fast -system-sqlite \
-qdbus -pch -nomake examples -nomake demos -confirm-license
make -j4 sub-src sub-tools
make install

(mkdir -p $PKG/opt/qt4
 cd /opt/qt4 
 cp -a -r * $PKG/opt/qt4
)

(mkdir -p $PKG/usr/lib/pkgconfig
 cd /opt/qt4/lib/pkgconfig
 cp -a * $PKG/usr/lib/pkgconfig
)

mkdir -p $PKG/usr/share/pixmaps
cp -a $CWD/*.png $PKG/usr/share/pixmaps

mkdir -p $PKG/usr/share/applications
cp -a $CWD/*.desktop $PKG/usr/share/applications

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a INSTALL LICENSE* OPENSOURCE-NOTICE.TXT README $PKG/usr/doc/$NAME-$VERSION

( 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/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/qt4.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/qt4.SlackBuild

cd $PKG
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/qt4-$VERSION-$ARCH-$BUILD.tgz

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