#!/bin/sh

# The package name is libart_lgpl 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 libart-lgpl. The build script handles this difference.

PRGNAM=${PRGNAM:-libart_lgpl}
SRCNAM=${SRCNAM:-libart-lgpl}
VERSION=${VERSION:-r14.0.3}
BUILD=${BUILD:-inpito}

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

# 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-$VERSION

mkdir -p $PKG/usr

cd $TMP
tar xvf $CWD/$SRCNAM-$VERSION.tar.xz
cd $TMP/$SRCNAM-$VERSION
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 {} \;

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

echo && printenv && echo

CFLAGS="$CPUOPT" \
CXXFLAGS=$CPUOPT \
./configure \
  --prefix=${PREFIX} \
  --libdir=${LIBDIR} \
  --mandir=${MANDIR}

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 AUTHORS COPYING NEWS README $PKG/usr/doc/$PRGNAM-$VERSION

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

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

if [ -d $PKG${MANDIR} ]; then
  ( cd $PKG${MANDIR}
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

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