#!/bin/sh

# Copyright 2006, 2007, 2008  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
# Copyrught 2010, 2011 Niels Horn, Rio de Janeiro, RJ, Brazil
#
# 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.

# This is based on the original SlackBuild from /extra/kde-compat in
# Slackware 13.1

# Adapted for SlackBuilds.org by Niels Horn <niels.horn@gmail.com>
# - moved building from /opt to $TMP
# - included patch for new libpng (based on work by Stuart Winter)
# Revision date 2011/03/12
# 
# Adapted for www.slacky.eu by Andrea Sciucca ( gohanz at infinito.it)
#
#
# Exit on most errors
set -e


# Set variables:
CWD=`pwd`
OUTPUT=${OUTPUT:-$CWD}
PKGNAME=qt3
TMP=${TMP:-/tmp/txz/$PKGNAME}
PKG=$TMP/package
VERSION=${VERSION:-3.3.8b}
ARCH=${ARCH:-i486}
JOBS=${JOBS:--j2}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}

# Download sorce tarball if still not present
SOURCE="ftp://ftp.qt.nokia.com/qt/source/qt-x11-free-$VERSION.tar.gz"

if [ ! -e $CWD/qt-x11-free-$VERSION.tar.gz ]; then
 wget $SOURCE
fi



# Set compiling FLAGS
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i486"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"; CHOST="x86_64"
fi

# Create building directory
rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT


# Don't build a devel package (but put a devel SYSTEM on this machine...)
NODEVEL=true


# Decompress the source tarball
cd $TMP
tar xvzf $CWD/qt-x11-free-$VERSION.tar.gz

echo -e "\E[0;32m+----------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild Qt3 |\E[0;0m"
echo -e "\E[0;32m+----------------------+\E[0;0m"

# Adjust permissions and ownerships
cd qt-x11-free-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .


# Force CFLAGS in qmake.conf
sed -i "/QMAKE_CFLAGS_RELEASE/s|-O2|$SLKCFLAGS|" mkspecs/linux-g++/qmake.conf

# Use built-in gif reader
sed -i "/#define QT_BUILTIN_GIF_READER/s|0|1|" src/kernel/qgif.h

# Fix location of mysql.h
sed -i "s|mysql\.h|mysql/mysql\.h|" src/sql/drivers/mysql/qsql_mysql.h

# Build with libpng 1.4:
patch -lp0 < $CWD/qt3-libpng14.diff


# 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.  :-)
echo "yes" | \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  -prefix /opt/kde3/lib${LIBDIRSUFFIX}/$PKGNAME-$VERSION \
  -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:
make -i

# Now set QTDIR...
QTDIR=$PKG/opt/kde3/lib${LIBDIRSUFFIX}/$PKGNAME-$VERSION

# ... & point LD_LIBRARY_PATH to the new libs and "make + install" the rest...
LD_LIBRARY_PATH=$QTDIR/lib make install INSTALL_ROOT=$PKG
make -i symlinks sub-src sub-tools
make install INSTALL_ROOT=$PKG

# Link the shared libraries into /opt/kde3/lib
cd $PKG/opt/kde3/lib${LIBDIRSUFFIX}
  for file in $PKGNAME-$VERSION/lib/*.so* ; do
    ln -sf $file .
  done
cd -

# Leave these out so as not to interfere with Qt4:
if [ -z "$NODEVEL" ]; then
  # Add man pages:
  mkdir -p $PKG/usr/man
  cp -a doc/man/* $PKG/usr/man
  find $PKG/usr/man -type f -exec gzip -9 "{}" \;
fi

# Put profile.d scripts in package w/ correct lib-path
mkdir -p $PKG/etc/profile.d
cat $CWD/profile.d/qt.sh  | sed -e "s#/lib#/lib${LIBDIRSUFFIX}#" \
  > $PKG/etc/profile.d/qt.sh
cat $CWD/profile.d/qt.csh | sed -e "s#/lib#/lib${LIBDIRSUFFIX}#" \
  > $PKG/etc/profile.d/qt.csh
chmod 755 $PKG/etc/profile.d/*


# Strip binaries and shared objects, if present
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
)


# Make links to the qt3 binaries
mkdir -p $PKG/opt/kde3/bin
cd $PKG/opt/kde3/bin
  for file in assistant designer linguist lrelease lupdate moc qm2ts qmake qtconfig uic ; do
    ln -sf ../lib${LIBDIRSUFFIX}/$PKGNAME-$VERSION/bin/$file .
  done
cd -

# Make a link to qt-mt.pc
mkdir -p $PKG/opt/kde3/lib${LIBDIRSUFFIX}/pkgconfig
cd $PKG/opt/kde3/lib${LIBDIRSUFFIX}/pkgconfig
  ln -sf ../$PKGNAME-$VERSION/lib/pkgconfig/qt-mt.pc .
cd -


# Add documentation files: readme, changelog and so on
mkdir -p $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild
cp -a FAQ INSTALL LICENSE.GPL* LICENSE.QPL MANIFEST PLATFORMS README* changes*  \
$PKG/usr/doc/$PKGNAME-$VERSION
chmod 644 $PKG/usr/doc/$PKGNAME-$VERSION/*


# Make a link to "qt3" w/o version number
cd $PKG/opt/kde3/lib${LIBDIRSUFFIX}
  ln -sf qt3-${VERSION} qt3
cd -


# Also add the slack-desk file and this slackbuild to the package
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild/slack-desc
cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild/$PKGNAME.SlackBuild


# This much documentation isn't needed for the compat qt3 package -- look in the
# source if you need it.
rm -rf $PKG/opt/kde3/lib${LIBDIRSUFFIX}/qt3/doc

# Building some post-install execution script 	
cat $CWD/doinst.sh | sed -e "s|/lib|/lib${LIBDIRSUFFIX}|" \
  > $PKG/install/doinst.sh



# Find and print dependencies into slack-required file
cd $PKG
chown -R root:root $PKG
if [ -x /usr/bin/requiredbuilder ];then
requiredbuilder -y -v -s  $CWD $PKG
fi
# Create txz package
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}


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




