#!/bin/sh

# The package name is sip to maintain consistency with the stock
# Slackware package. This naming scheme allows updating the package
# (upgradepkg) rather than installing two packages. The Trinity sources are
# named sip4-tqt. The build script handles this difference.

PRGNAM=${PRGNAM:-sip4-tqt}
VERSION=${VERSION:-r14.0.3}
BUILD=${BUILD:-inpito}
PKGNAM=$PRGNAM

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

# If it exists remove the original sip package
RMSIP=$( ls /var/log/packages/ | grep "sip-" )

if [ -f $RMSIP ]; then
	removepkg $RMSIP
fi

# If it exists remove any previous sip4-tqt package
RMSIP4TQT=$( ls /var/log/packages/ | grep "sip4-tqt" )

if [ -f $RMSIP4TQT ]; then
	removepkg $RMSIP4TQT
fi

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

# 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

# Build variables for this script
SOURCE_DIR=`pwd`

sleep 1

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 {} \;

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, --help            show this help message and exit
# -k, --static          build the SIP module as a static library
# -p PLATFORM, --platform=PLATFORM
#                       the platform/compiler configuration [default:
#                       linux-g++]
# -u, --debug           build with debugging symbols
#
# Query:
#   --show-platforms    show the list of supported platform/compiler
#                       configurations
#   --show-build-macros
#                       show the list of supported build macros
#
# Installation:
#   -b DIR, --bindir=DIR
#                       where the SIP code generator will be installed
#                       [default: /usr/bin]
#   -d DIR, --destdir=DIR
#                       where the SIP module will be installed [default:
#                       /usr/lib${LIBDIRSUFFIX}/python2.6/site-packages]
#   -e DIR, --incdir=DIR
#                       where the SIP header file will be installed [default:
#                       /usr/include/python2.7]
#   -v DIR, --sipdir=DIR
#                       where .sip files are normally installed [default:
#                       /usr/share/sip]

# python configure.py -h
# exit 1

echo "Configuring..."

# Set Export Variables Globally
export PREFIX_OLD="/usr"
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"

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

python configure.py \
  -b "/usr/bin" \
  -d "$PYTHONLIB" \
  -e "/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

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