#!/bin/sh

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

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

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

# Perform a quick test of /proc. If /proc is unmounted then the Java bindings will fail because
# /proc/self/exe does not exist. In a normal system that should never happen but might happen
# in a chroot environment.
if [ ! -L /proc/self/exe ]; then
 echo "Check that /proc is mounted. /proc/self/exe does not exist as a sym link."
 exit 1
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`

cp -a /usr/share/aclocal/libtool.m4 ${SOURCE_DIR}/admin/libtool.m4.in
cp -a /usr/share/libtool/build-aux/ltmain.sh ${SOURCE_DIR}/admin/ltmain.sh
make -f admin/Makefile.common || exit 1

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

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


# Available modules:
# dcopc dcopjava dcopperl dcoppython
# kalyptus kdejava kjsembed korundum
# python qtjava qtruby qtsharp smoke xparts
# export DO_NOT_COMPILE=${DO_NOT_COMPILE:-"qtsharp"}

# xparts doesn't compile with newer systems.
# if [ "$SLACK_VER" = "14.0" ]; then
#   export DO_NOT_COMPILE=${DO_NOT_COMPILE:-"xparts"}
# fi

CFLAGS="`pkg-config ruby-1.9 --cflags 2>/dev/null`"
 if [ "$?" = "0" ]; then
   echo "A ruby pkgconfig file exists. No patching required."
   RUBY_EXTRA_INCLUDE=""
   if [ "`uname -m`" = "x86_64" ]; then
     RUBY_EXTRA_INCLUDE="/usr/include/ruby-1.9.1/x86_64-linux/"
   else
     RUBY_EXTRA_INCLUDE="/usr/include/ruby-1.9.1/i486-linux/"
   fi
   if [ -d "$RUBY_EXTRA_INCLUDE" ]; then
     echo "RUBY_EXTRA_INCLUDE: $RUBY_EXTRA_INCLUDE"
   else
     echo "$RUBY_EXTRA_INCLUDE does not exist."
     exit 1
   fi
 fi

#   --enable-qscintilla=no \
#   --with-extra-includes=${RUBY_EXTRA_INCLUDE} \

echo && printenv && echo

CFLAGS="$CPUOPT" \
CXXFLAGS="$CPUOPT" \
./configure \
  --prefix=${PREFIX} \
  --sysconfdir=${SYSCONFDIR} \
  --libdir=${LIBDIR} \
  --mandir=${MANDIR} \
  --with-qt-dir=${QTDIR} \
  --with-qt-includes=${QT_INCLUDE_DIR} \
  --with-qt-libraries=${QT_LIB_DIR} \
  --disable-rpath \
  $DEBUG_AUTOTOOL_OPT \
  --enable-qscintilla=no \
  --with-extra-includes=${RUBY_EXTRA_INCLUDE} \
  --enable-closure || exit 1

# Compile perl bindings first due to the vendor_perl:
if [ -n "`echo $DO_NOT_COMPILE | grep dcopperl`" ]; then
  echo "DO_NOT_COMPILE: $DO_NOT_COMPILE."
  echo "Not building dcopperl."
else
  echo "Building dcopperl."
  cd dcopperl
    perl Makefile.PL INSTALLDIRS=vendor || exit 1
    make || exit 1
  echo
  echo "======================================"
  echo "Finished making dcopperl bindings."
  echo "======================================"
  echo
  cd ..
fi

# Make sure the application is compiled using single thread
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 COPYING.LIB INSTALL NEWS README $PKG/usr/doc/$PRGNAM-$VERSION

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

# Remove perllocal files:
( cd $PKG ; find . -name perllocal.pod -exec rm "{}" \; )

# 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

# tdebindings places one man page in /usr/man and another in /usr/share/man.
# Trinity bug report 348 was filed against this nuisance. The problem is
# caused by the perl scripts in dcopperl that creates the Makefile.
if [ -d $PKG${MANDIR} ]; then
  if [ -d $PKG/usr/share/man/man3 ]; then
    mv $PKG/usr/share/man/man3 $PKG${MANDIR}/
    rm -fr $PKG/usr/share/man
  fi
fi

if [ -d $PKG${MANDIR} ]; then
  gzip -9 $PKG${MANDIR}/man?/*
fi

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