#!/bin/sh
# tqt3.Slackbuild - Script for compiling the tqt3-3.5.13-inpito.tgz package
#
# tqt3-3.5.13.2 written by the Trinity Development Team
# tqt3-3.5.13.2-inpito.tgz build script by Jerry Nettrouer II <trinity@inpito.org>

# This build script is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

PRGNAM=${PRGNAM:-tqt3}
VERSION=${VERSION:-r14.0.3}
BUILD=${BUILD:-inpito}

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

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
fi

# Some Trinity programs need single thread compiling
export NUMJOBS="-j1"

# Use this as CFLAGS and CXXFLAGS:
if [ "$ARCH" = "i386" ]; then
  CPUOPT="-O2 -march=i386 -mcpu=i686"
  TARGET=i486
  LIBDIRSUFFIX=
elif [ "$ARCH" = "i486" ]; then
  CPUOPT="-O2 -march=i486 -mtune=i686"
  TARGET=i486
  LIBDIRSUFFIX=
elif [ "$ARCH" = "i686" ]; then
  CPUOPT="-O2 -march=i686"
  TARGET=i686
  LIBDIRSUFFIX=
elif [ "$ARCH" = "s390" ]; then
  CPUOPT="-O2"
  TARGET=s390
  LIBDIRSUFFIX=
elif [ "$ARCH" = "x86_64" ]; then
  CPUOPT="-O2 -fPIC"
  TARGET=x86_64
  LIBDIRSUFFIX=64
  APPEND=-64

fi

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

cd $TMP
tar xvf $CWD/$PRGNAM-$VERSION.tar.xz
cd $TMP/$PRGNAM-$VERSION

echo "Updating build environment file and directory permissions..."
chown -R root:root .
find . -type d -exec chmod 755 {} \;
find . -type f ! -perm -o=r -exec chmod o+r {} \;

# Build variables for this script
SOURCE_DIR=`pwd`

if [ "$LIBDIRSUFFIX" = "64" ]; then
  PLATFORM="linux-g++-${LIBDIRSUFFIX}"
else
  PLATFORM="linux-g++"
fi

# Return to our regular scheduled build script ...
cd $SOURCE_DIR
set -e
/sbin/ldconfig

# Set Export Variables Globally
export PREFIX="/usr/lib${LIBDIRSUFFIX}/tqt3"
export SYSCONFDIR="/etc/trinity"
export LIBDIR="${PREFIX}/lib${LIBDIRSUFFIX}"
export MANDIR="/usr/man"

export QTDIR="$TMP/$PRGNAM-$VERSION"
export QT_INCLUDE_DIR="${QTDIR}/include"
export QT_LIB_DIR="${QTDIR}/lib"

export PATH="${PATH}"
export LD_LIBRARY_PATH="/usr/lib${LIBDIRSUFFIX}"
export PKG_CONFIG_PATH="/usr/lib${LIBDIRSUFFIX}/pkgconfig"

# Enable only one of the following:
#DEBUG_AUTOTOOL_OPT="--enable-debug=full"
export DEBUG_AUTOTOOL_OPT="--disable-debug"

INSTALL_PREFIX=${PREFIX}

echo && printenv && echo

# "yes" agrees to the GPL.
echo "yes" | \
CFLAGS=$CPUOPT \
CXXFLAGS=$CPUOPT \
./configure \
  -v \
  -prefix $PREFIX \
  -libdir $LIBDIR \
  -release \
  -I/usr/include/mysql \
  -I/usr/include/freetype2/freetype \
  -L/usr/lib${LIBDIRSUFFIX} \
  -R${SOURCE_DIR}/lib \
  -qt-imgfmt-png \
  -qt-imgfmt-mng \
  -qt-imgfmt-jpeg \
  -qt-gif \
  -qt-style-motif \
  -system-zlib \
  -system-libpng \
  -system-libmng \
  -system-libjpeg \
  -shared \
  -no-pch \
  -thread \
  -stl \
  -no-g++-exceptions \
  -platform ${PLATFORM} \
  -cups \
  -ipv6 \
  -nis \
  -sm \
  -xshape \
  -xinerama \
  -xcursor \
  -xrandr \
  -xrender \
  -tablet \
  -xkb \
  -xft \
  -plugin-imgfmt-mng \
  -plugin-sql-mysql \
  -plugin-sql-sqlite \
  -plugin-style-cde \
  -plugin-style-compact \
  -plugin-style-motifplus \
  -plugin-style-platinum \
  -plugin-style-sgi \
  -plugin-style-windows \
  -lfontconfig \
  -inputmethod \
  -enable-opengl \
  -dlopen-opengl \
  || exit 1

# Build
gmake

make install INSTALL_ROOT=$PKG
make $NUMJOBS -i symlinks sub-src sub-tools
make install INSTALL_ROOT=$PKG

export QTDIR="/usr/lib${LIBDIRSUFFIX}/tqt3"
export QT_INCLUDE_DIR="${QTDIR}/include"
export QT_LIB_DIR="${QTDIR}/lib${LIBDIRSUFFIX}"

DESKTOP=trinity

# Create the lib${LIBDIRSUFFIX} directory and populate it
mkdir -p $PKG${QTDIR}/lib${LIBDIRSUFFIX}/
cp -a ${SOURCE_DIR}/lib/* $PKG${QTDIR}/lib${LIBDIRSUFFIX}/

# Tdevelop cant find the QTDIR libraries in /usr/lib64/tqt3/lib64
# so we have to create simlinks for them to the the /usr/lib64 directory
if [ "$PREFIX" = "/usr/lib${LIBDIRSUFFIX}/tqt3" ]; then

	cd "$PKG/usr/lib${LIBDIRSUFFIX}" # Enter into package /usr/lib(64)

	for LIBFILE in ${PKG}${QT_LIB_DIR}/*; # Make a list of /usr/libr(64)/tqt3/lib(64)
		do
			if [ -f $LIBFILE ] && [ $LIBFILE != "${PKG}${QT_LIB_DIR}/README" ]; then  # Test for files and README
				NAME=$( echo $LIBFILE | sed -e "s:${PKG}${QT_LIB_DIR}/::g" ) # get the name
				ln -s "$QT_LIB_DIR/$NAME" "$NAME" # create the link
			fi
	done
fi

cd $PKG${QTDIR}

# Create symbolic links to bin files
mkdir -p $PKG/usr/bin
cd $PKG/usr/bin

# Once again link a bunch of binaries to whithin the bin directory
for BINARY in $PKG${QTDIR}/bin/*;
	do
	NAME=$( echo $BINARY | sed -e "s:$PKG${QTDIR}/bin/::g" )
	echo "linking ${QTDIR}/bin/${NAME} to $NAME;"
	ln -s "${QTDIR}/bin/${NAME}" $NAME;
done

cd $PKG

# Placing the tqt-mt.pc in the pkgconfig directory
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
mkdir -p $PKG${QTDIR}/lib${LIBDIRSUFFIX}/pkgconfig
mv $PKG${QTDIR}/lib${LIBDIRSUFFIX}/tqt-mt.pc $PKG${QTDIR}/lib${LIBDIRSUFFIX}/pkgconfig/tqt-mt.pc

# Creating the link for tqt-mt.pc
cd $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
ln -sf $QTDIR/lib${LIBDIRSUFFIX}/pkgconfig/tqt-mt.pc tqt-mt.pc

cd ${SOURCE_DIR}

# Setting up the man pages
mkdir -p $PKG/${MANDIR}
cp -a doc/man/* $PKG/${MANDIR}
find $PKG/${MANDIR} -type f -exec gzip -9 "{}" \;

# Removing extras gzipped files
cd $PKG/${MANDIR}
rm -f *.gz

cd ${PKG}

# Prep profile.d directory
mkdir -p $PKG/etc/profile.d

cat $CWD/profile.d/tqt3-${DESKTOP}.sh > $PKG/etc/profile.d/tqt3.sh
cat $CWD/profile.d/tqt3-${DESKTOP}.csh > $PKG/etc/profile.d/tqt3.csh

# Put profile.d scripts in package with correct lib-path.
sed -i "s:determined_by_build_script:${QTDIR}:" $PKG/etc/profile.d/tqt3.sh
sed -i "s:determined_by_build_script:${QTDIR}:" $PKG/etc/profile.d/tqt3.csh

# Set the PKG_CONFIG_PATH to i486 or x86_64
sed -i "s:library_by_script:lib${LIBDIRSUFFIX}:" $PKG/etc/profile.d/tqt3.csh

chmod 755 $PKG/etc/profile.d/*

# Creating Symbolic Link between lib64 and lib
if [ "$ARCH" = "x86_64" ]; then
	cd ${PKG}${QTDIR}
	ln -s lib${LIBDIRSUFFIX} lib
	# I may change this to the lib-32 compatable directory in the future
fi

# 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

# Create and populate the documentation directory:
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cd ${SOURCE_DIR}
cp -a FAQ INSTALL LICENSE.* MANIFEST PLATFORMS README* changes* $PKG/usr/doc/$PRGNAM-$VERSION

# Create install directory and populate it
mkdir -p $PKG/install
cat $CWD/doinst.sh > $PKG/install/doinst.sh
sed -i "s:determined_by_build_script:${QTDIR}/lib${LIBDIRSUFFIX}:g" $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD${TAG}.${PKGTYPE:-tgz}
