#!/bin/bash
STARTDIR=/home/SMCDev/SMC/omniorb
OMNIVER=omniORB-4.1.7
OMNIFILE=$STARTDIR/$OMNIVER.tar.bz2
OMNIDIR=/home/SMCDev/SMC/omniorb/$OMNIVER
BUILDDIR=$STARTDIR/nativebuild
OUTPUT=/home/SMCDev/omniORB/nativeoutput
LOGFILE=$STARTDIR/native.log

DEBUG=0

if [ $DEBUG -ne 0 ]
then
  BUILDDIR="$BUILDDIR-DEBUG"
  OUTPUT="$OUTPUT-DEBUG"
  OMNIDIR="$OMNIDIR-DEBUG"
fi

cd $STARTDIR

if [ ! -f $OMNIFILE ]
then
  echo "Missing archive file: $OMNIFILE"
  exit 1
fi

echo "Cleaning up source files"
rm -rf $OMNIDIR

echo "Cleaning up old build"
rm -rf $BUILDDIR $OUTPUT

echo "Extracting pristine files from archive"
mkdir --parents $OMNIDIR
RETVAL="$?"
if [ "$RETVAL" -ne 0 ]
then
  echo "Failed to create output directory"
  exit 1
fi
cd $OMNIDIR
tar xvf $OMNIFILE --strip-components=1
RETVAL="$?"
if [ "$RETVAL" -ne 0 ]
then
  echo "Tarball extraction failed"
  exit 1
fi
if [ "$DEBUG" -ne 0 ]
then
  for fn in `find $OMNIDIR/src/lib/omniORB -type f -print | xargs grep -l CXXDEBUGFLAGS`
  do
    echo "Editing $fn"
    (
	echo '1,$s/[#][\ \t]*CXXDEBUGFLAGS[\ \t]*[=][\ \t]*[-]g/CXXDEBUGFLAGS = -g/1'
	echo 'wq'
    ) | ed $fn
  done
fi

cd $STARTDIR

echo "*"
echo "Starting build"
echo "*"
mkdir --parents $BUILDDIR $OUTPUT
cd $BUILDDIR
echo "*"
echo "* Performing initial native configuration"
echo "*"
$OMNIDIR/configure --prefix=$OUTPUT --disable-longdouble 2>&1 | tee $LOGFILE
echo "*"
echo "* Building native omniORB"
echo "*"
make 2>&1 | tee -a $LOGFILE
echo "*"
echo "* Installing native omniORB"
echo "*"
make install 2>&1 | tee -a $NATIVELOG

