#!/bin/sh

PRGNAM=${PRGNAM:-python-tqt}
VERSION=${VERSION:-r14.0.3}
PKGNAM=$PRGNAM

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

# 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/$SRCNAM*
mkdir -p $PKG

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

# Build variables for this script
SOURCE_DIR=`pwd`

sleep 1

# Do not hard-code these options because this package replaces the stock Slackware package. Instead hard-code below.

chown -R root:root .
find . -type d -exec chmod 755 {} \;
find . -type f ! -perm -o=r -exec chmod o+r {} \;

PYTHONVER=$(python -V 2>&1 | cut -f 2 -d' ' | cut -f 1-2 -d.)
PYTHONLIB=$( python -c 'from distutils.sysconfig import get_python_lib; print get_python_lib()' )

# -h      display this help message
# -a tag  explicitly enable the qtpe module
# -b dir  where pyuic and pylupdate will be installed [default %s]\n % opt_pyqtbindir
# -c      concatenate each module's C/C++ source files
# -d dir  where the PyTQt modules will be installed [default %\ns] % opt_pyqtmoddir
# -f      keep any existing features file (when cross-compiling) [default remove]
# -g dir  where the TQt (n)tqconfig.h file can be found [default TQt include directory]
# -i      enable checking of signed interpreters using the VendorID package [default disabled]
# -j #    split the concatenated C++ source files into # pieces [default 1]
# -k      build the PyTQt modules as static libraries
# -l dir  the directory containing the VendorID header file [default %s]\n % opt_vendincdir)
# -m dir  the directory containing the VendorID library [default %s]\n % opt_vendlibdir
# -n dir  the directory containing the TQScintilla header files [default TQt include directory]
# -o dir  the directory containing the TQScintilla library [default TQt lib directory]
# -q dir  the root directory of the TQt installation [default %s]\n % def_qt_dir
# -r      generate code with tracing enabled [default disabled]
# -s      TQScintilla is a static library and not a DLL (Windows only)
# -u      build with debugging symbols (requires a debug build of Python on Windows
# -v dir  where the PyTQt .sip files will be installed [default %s]\n % opt_pyqtsipdir
# -w      don't suppress compiler output during configuration
# -y lib  explicitly specify the type of TQt library, either qt, qt-mt, qte, qte-mt, qtmt, tqt, tqt-mt, tqte, tqte-mt or tqtmt

# Ignore build messages about TQScintilla as the package is not required.

# python configure.py -h
# exit 1

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

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

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

echo "Configuring. Please be patient..."

if [ "$PREFIX" = "/usr" ]; then
  FLAGS="$CPUOPT -L${PREFIX}/lib${LIBDIRSUFFIX} -I/usr/include/tqt -I${QTDIR}/include -I${PREFIX}/include"
else
  FLAGS="$CPUOPT -L${PREFIX}/lib${LIBDIRSUFFIX} -I/usr/include/tqt -I${QTDIR}/include -I${PREFIX}/include -I/usr/include"
fi

echo && printenv && echo

echo "yes" | python configure.py \
  -b "/usr/bin" \
  -d "$PYTHONLIB" \
  -q "$QTDIR" \
  -g "$QT_INCLUDE_DIR" \
  -l "/usr/include/python$PYTHONVER" \
  CFLAGS="$FLAGS" \
  CXXFLAGS="$FLAGS"

if [ "$TEE_OUTPUT" = "false" ]; then
  make $NUMJOBS --silent || exit 1
else
  make $NUMJOBS VERBOSE=1 || exit 1
fi

make install DESTDIR=$PKG || exit 1

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cd ${SOURCE_DIR}
cp -a LICENSE NEWS README* THANKS doc/* $PKG/usr/doc/$PRGNAM-$VERSION

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

# 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

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