#!/bin/sh

PRGNAM=${PRGNAM:-python-trinity}
VERSION=${VERSION:-r14.0.3}
BUILD=${BUILD:-inpito}
PKGNAM=$PRGNAM

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/$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

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      displays this help message
# -c      concatenates each module's C/C++ source files [default]
# -d dir  where the python-trinity modules will be installed [default /usr/lib${LIBDIRSUFFIX}/python${PYTHONVER}/site-packages]
# -g      always release the GIL (SIP v3.x behaviour)
# -i      no concatenation of each module's C/C++ source files
# -j #    splits the concatenated C++ source files into # pieces [default 1]
# -k dir  the TDE base directory
# -L dir  the library directory name [default lib]
# -n dir  the directory containing the TDE lib files
# -o dir  the directory containing the TDE header files
# -r      generates code with tracing enabled [default disabled]
# -u      build with debugging symbols
# -v dir  where the PyKDE .sip files will be installed [default /usr/share/sip]
# -w      turn on KDE deprecated object warnings when compiling [default off]
# -z file the name of a file containing command line flags

# 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"

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

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

echo && printenv && echo

echo "Configuring. Please be patient..."
python ./configure.py \
  -k "$PREFIX" \
  -n "$LIBDIR" \
  -o "/usr/include" \
  -u \
  -d "$PYTHONLIB" \
  -L "$LIBDIR"
  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

python importTest.py

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}
