#!/bin/human
#
# Build just the the FrameCPP library, as a part of building GDS.
# Includes extra flags to use $HOME/lib and $HOME/include
# in the build, as I needed on building from $HOME on Debian Sarge
# (or modify for your own local build tools)
#
# Includes patches taken from External/patchall
#
# While this is just an outline of what you need to do, it is quite
# possible that you can just run this script from the GDS source directory,
# the External subdirectory, or the FrameCPP source directory under External.
# (No guarantees :-)
#
# Eric Myers - <myers@spy-hill.net>  - 8 August 2007
# @(#) $Id: build_framecpp_lib,v 1.4 2009/03/10 16:00:11 myers Exp $
######################################################################

FRAMECPP_VER=1.10.1


##
# Begin

echo "Building FrameCPP $FRAMECPP_VER ...."
echo "Start: `date` "

srcdir=framecpp-${FRAMECPP_VER}

# Find the GDS/External directory, and framecpp source should be under it

thisdir=`basename $PWD`
if [ $thisdir = "External" ]; then
  External="$PWD"
  echo "External is $External "
  if [ ! -d $srcdir -a -f ${srcdir}.tar.gz ]; then 
    gzip -dc  ${srcdir}.tar.gz | tar xf -
  fi
fi
updir=`dirname $PWD`
if [ `basename $updir` = "External" ]; then
  External=$updir
  echo "PWD is $PWD"
  echo "External is one above:  $External "
fi 
if [ -d External ]; then
  External=$PWD/External
  echo "External is one below: $External "
fi
if [ -z "$External" -o ! -d "$External" ]; then
  echo "I'm lost in $PWD and cannot find External to set Prefix. "
  exit 1
fi


# Unpack source tarball, if necessary
# (The `gzip -d | tar ` works even on older Suns)

cd $External

tarball=${srcdir}.tar.gz

if [ -d $srcdir ]; then
 echo "Source directory $srcdir is already in place. "
elif [ -f $tarball ]; then
  echo "Unpacking tarball $tarball ... "
  gzip -dc  $tarball | tar xf -
fi 


# If there is already something there then don't step on it
# (We might have unpacked a pre-built tarball)

if [ -f ${External}/lib/libframecpp.so ]; then
  echo "FrameCPP library has already been built.  "
  echo "If you want to rebuild, please say `make clean` first. "
  echo "Stop. "
  exit 2
else
  echo "FrameCPP library is not yet in place. "
fi


##
# Patches for particular versions of FrameCPP.  
# Taken from External/patchall   
# (You should check to see if there are any for your version.)

case "$srcdir" in 
    "framecpp-1.8.0") {
	patch   --input patches/ldas-1.8.0-patch1 \
		$srcdir/lib/framecpp/src/Version6/FrStatData.cc ; 
    } ;;

    "framecpp-1.8.235") {
	echo "patching FrameCPP 1.8.235 for GDS..."
	mv $srcdir/build-ldas $srcdir/build-ldas.distr
	grep -v PATH=/ldcg $srcdir/build-ldas.distr > $srcdir/build-ldas
	chmod +x $srcdir/build-ldas
	if [ -d "/ldcg" -a `which gcc` != "/ldcg/bin/gcc" ]; then
	    for f in $srcdir/configure $srcdir/lib/configure $srcdir/lib/general/configure $srcdir/lib/framecpp/configure ; do
		mv $f ${f}.distr
		sed -e s/ldcg/xxxx/g ${f}.distr > $f
		chmod +x $f
	    done
	fi
    } ;;

   "framecpp-1.9.120") {
        patch   --input patches/framecpp-1.9.120-patch1 \
                $srcdir/lib/general/src/Directory.cc ; 
        patch   --input patches/framecpp-1.9.120-patch2 \
                $srcdir/configure ; 
        patch   --input patches/framecpp-1.9.120-patch3 \
                $srcdir/lib/general/configure ; 
        patch   --input patches/framecpp-1.9.120-patch4 \
                $srcdir/lib/framecpp/configure ; 
        patch   --input patches/framecpp-1.9.120-patch5 \
                $srcdir/lib/perceps/configure ; 
        patch   --input patches/framecpp-1.9.120-patch6 \
                $srcdir/lib/configure ; 
    } ;;

esac


##
# Build it:

Prefix=$External/$srcdir
cd $Prefix

rm -f lib/framecpp/src/config.h 

./configure --prefix=$Prefix \
	    --with-extra-cxxflags="-I$HOME/include"  \
	    --with-extra-ldflags="-L$HOME/lib" 

echo "  "
pwd
echo "* Building FrameCPP... "

time make 
RC=$?

if [ $RC -ne 0 ]; then
  echo "* "
  echo "* The build had problems. RC=$RC "
  echo "* Please fix and try again. " 
  echo "* "
  exit $RC
fi


##
# Install it in Prefix

echo "* Installing FrameCPP... "

make install
RC=$?

if [ $RC -ne 0 ]; then
  echo "* "
  echo "* The installation had problems. RC=$RC "
  echo "* "
  exit $RC
fi

echo "Finish: `date` "

if [ ! -f ${External}/lib/libframecpp.so ]; then
  echo "* " 
  echo "* FrameCPP library is not in place. "
  echo "* Something went wrong. "
  exit 3
else
  echo "FrameCPP library is now in place for GDS build. "
  exit 0
fi

exit 47


###########################################################

cat >/dev/null <<EOF 

# This is the proceedure used by gds/External/Makefile
# We may want to emulate it or use some of these flags

./build-ldas -install -no-stow --prefix=$HOME 
echo Not using \
	--with-optimization=high --without-odbc  --without-ospace \
	--disable-metadata-api --enable-inlining  --without-stlport \
	-warn-unsupported-compiler 

EOF

####
