#!/bin/sh
# Copyright 2006, 2007  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PKGNAM=qt32
VERSION=${VERSION:-3.3.8}
ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-4}
QTPLATFORM=linux-g++
DISTRO=${DISTRO:-slamd64}

if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
else
	PKGARCH=${ARCH}_${DISTRO}
fi

CWD=$(pwd)
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-${PKGNAM}
rm -rf $PKG
mkdir -p $PKG

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 -fPIC"
fi

cd $TMP
rm -rf qt-x11-free-${VERSION}
tar xjvf $CWD/qt-x11-free-$VERSION.tar.bz2 || exit 1
cd qt-x11-free-$VERSION
sed -i 's#/X11R6##g' mkspecs/*/qmake.conf

#zcat $CWD/qt.x86.cflags.diff.gz | patch -p1 || exit 1

zcat $CWD/qt-x11.diff.gz | patch -p1 || exit 1
zcat $CWD/qt.mysql.h.diff.gz | patch -p1 || exit 1
zcat $CWD/qt-font-default-subst.diff.gz | patch -p0 || exit 1
zcat $CWD/utf8-bug-qt3.diff.gz | patch -p0 || exit 1
# Format string bugs:
zcat $CWD/170529.diff.gz | patch -p4 || exit 1

# Make sure ownerships and permissions are sane:
chown -R root:root .
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 {} \;


# And here's what we are currently using.  The "yes" agrees to the GPL, so don't run this script if you
# don't agree.  :-)
(
	. /etc/profile.d/32dev.sh
	echo "yes" | linux32 ./configure \
	 -prefix /usr/lib$LIBSUFFIX/qt-${VERSION} \
	 -platform $QTPLATFORM \
	 -release \
	 -system-zlib \
	 -system-libpng \
	 -qt-imgfmt-png \
	 -system-libmng \
	 -qt-imgfmt-mng \
	 -system-libjpeg \
	 -qt-imgfmt-jpeg \
	 -qt-gif \
	 -thread \
	 -stl \
	 -no-g++-exceptions \
	 -xft \
	 -plugin-sql-mysql \
	 -plugin-style-cde \
	 -plugin-style-compact \
	 -qt-style-motif \
	 -plugin-style-motifplus \
	 -plugin-style-platinum \
	 -plugin-style-sgi \
	 -plugin-style-windows \
	 -enable-opengl
	
	# We're using '-i' to work around a bug where the build tries to strip html files.
	# Build the important stuff:
	export LD_LIBRARY_PATH=/tmp/qt-x11-free-3.3.8/lib
	make -j10 -i symlinks sub-src sub-tools || exit 1
	make install INSTALL_ROOT=$PKG || exit 1
)

# The infamous qt -> qt-${VERSION} link that lets us cheat on the QTDIR ;-)
( cd $PKG/usr/lib$LIBSUFFIX ; ln -sf qt-${VERSION} qt)

# Remove the last vestiges of the build in /tmp:
rm -f $PKG/usr/lib$LIBSUFFIX/qt-3.3.8/mkspecs/linux-g++/linux-g++

# Put this back as shipped:
( cd $PKG/usr/lib/qt
  zcat $CWD/qt.x86.cflags.diff.gz | patch -p1 -R )

# Strip binaries:
( 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
)

# Put a ton of links to more "normal" places.  I'd just use a prefix of /usr, but it
# creates a ton of new (and ambiguously named) /usr directories...

mkdir -p $PKG/usr/bin/32
( cd $PKG/usr/bin/32
  for file in assistant designer linguist lrelease lupdate moc qm2ts qmake qtconfig uic ; do
    ln -sf /usr/lib$LIBSUFFIX/qt/bin/$file .
  done
)

mkdir -p $PKG/usr/lib$LIBSUFFIX/pkgconfig
( cd $PKG/usr/lib$LIBSUFFIX/pkgconfig
  ln -sf /usr/lib$LIBSUFFIX/qt-${VERSION}/lib/pkgconfig/qt-mt.pc .
)

if [ "x$LIBSUFFIX" != "x" ]; then
	( cd $PKG/usr/lib$LIBSUFFIX/qt-${VERSION}; ln -s lib lib$LIBSUFFIX )
fi


cd $PKG
mkdir install
zcat $CWD/doinst.sh.gz > install/doinst.sh
cat $CWD/slack-desc > install/slack-desc
makepkg -l y -c n ../${PKGNAM}-$VERSION-$PKGARCH-$BUILD.tgz

