#!/bin/human
#
# Build LIGO GDS from tarball(s).
# 
# This worked for me in Debian Sarge after I installed newer versions
# of the necessary build tools in my $HOME, including libbz2.
# See http://www.spy-hill.net/~myers/help/ligo/GDS-build.html 
# for details.  Now I'm trying to make it work on Ubuntu 8.04
#
# Don't expect this to work out of the box for you on some other Unix.
# Take it as a starting point and adjust as needed.
#
# This version of the script is meant to be run from the top level 
# source directory, whereas earlier versions were run one level above.
#
# Building GDS will go out to the network to get a copy of the frameCPP
# library, if one does not already exist.  To avoid that you can get
# frameCPP separately and build it once and save the result. The source
# code is at http://www.ldas-sw.ligo.caltech.edu/packages/
#
# Eric Myers <myers@spy-hill.net> - 8 August 2007
# @(#) $Id: build_gds,v 1.7 2009/03/10 15:53:54 myers Exp $
######################################################################

PACKAGE=gds
PVER=2.12.4
framecpp=framecpp-1.10.1

# Where to install

GDS_Prefix=$HOME/opt/lscsoft/gds


# Setup build configuration:

GDSBUILD=online
ROOTSYS=${ROOTSYS-"$HOME/opt/root"}


# These are needed if libbz2 or other stuff are installed
# other than system default.  Modify as needed.
#
CXXFLAGS=" -I$HOME/local/include "
LDFLAGS=" -L$HOME/local/lib "

export GDSBUILD ROOTSYS CXXFLAGS LDFLAGS


##
# Begin

ARGS="$*"

echo "Preparing to build LIGO $PACKAGE $PVER ...."
echo "  "

echo "   "
echo "Tool Versions: "

show_tool_version() {
  [ -z "$1" ] && exit ;
  FULL=$1
  C=`echo $FULL | awk '{ print $1 }'`
  [ -z "$C" ] && exit ;
  P=`which $C | egrep -v "^no"`
  if [ -z "$P" ]; then
    echo "    $C could not be found!" 
    return
  fi
  $FULL 2>&1 | head -1 | pr -o 4 -t
}


echo -n "    pkg-config ";
show_tool_version "pkg-config --version"
show_tool_version "libtool --version"
show_tool_version "autoconf --version"
show_tool_version "automake --version"
show_tool_version "curl --version"
show_tool_version "wget --version"
show_tool_version "bunzip2 --version"
show_tool_version "m4 --version"


if [ -d $ROOTSYS ]; then
  echo "ROOTSYS is $ROOTSYS " 
else
  echo " Building GDS requires ROOT, but $ROOTSYS does not exist.  "
  exit 1
fi


echo "Start: `date` "

## 
# Are we in the source directory or above it?  Adjust accordingly.

thisdir=`basename $PWD`
if [ "$thisdir" != "$srcdir" -a -d $srcdir ]; then
  Above=$PWD
  cd $srcdir
else
  Above="$PWD/../"
fi


srcdir=${PACKAGE}-${PVER}
FramePrefix=${srcdir}/External

LD_LIBRARY_PATH=$srcdir/External/lib:$LD_LIBRARY_PATH 
export LD_LIBRARY_PATH 


echo " "
pwd
echo "* Setting things up... "

if [ -f configure -a ! -f configure.ORIG ]; then
  echo "* Move the original configure script out of the way.  "
  mv configure configure.ORIG
fi

if [ -x ./libtool -a ! -f libtool.ORIG ]; then
  echo "* Move original libtool script out of the way.  "
  mv libtool libtool.ORIG
fi 

echo "* Bootstrap the configure script... "
./bootstrap

echo "* Run the configure script ... "
./configure  $ARGS  --prefix=$GDS_Prefix \
	    --with-extra-cxxflags=" -I$HOME/local/include "  \
	    --with-extra-ldflags=" -L$HOME/local/lib " 

##
# Monitors: turn off the ones which don't work

if [ 0 -eq 1 ]; then
  echo " "
  echo "* Turn off problem Monitors... "

  cd Monitors
  cp Makefile Makefile.ORIG
  sed  -e 's/ Cumulus / /'  -e 's/ BicoMon / /' -e 's/ DuoTone / /'  \
       -e 's/ NoiseFloorMonitor / /'  -e 's/ PDnMon/ /'  \
       Makefile.ORIG > Makefile
  cd ..
fi


##
# Debian Sarge does not have curl, so use wget instead.

CURL=`which curl| egrep -v "^no "`
WGET=`which wget| egrep -v "^no "`

if [ "x$CURL" = "x" -a "x$WGET" != "x" ]; then 
  echo "  "
  echo "* Changing curl into wget ... "
  cp Makefile Makefile.distr
  sed  -e 's/curl/wget/g'  Makefile.distr > Makefile
elif [ "x$CURL" = "x" -a "x$WGET" = "x" ]; then 
  echo "  "
  echo "* WARNING: no curl and no wget, so we can't suck stuff over."
  echo "*          (Just so you know...) "
  echo "  "
fi


##
# Frame library (do it ourselves, not ldas-build script)

if [ -d External/${framecpp} ]; then
  echo "* FrameCPP is already unpacked "
else
  if [ ! -f External/${framecpp}.tar.gz  -a -f $Above/${framecpp}.tar.gz ]; then 
     echo "* Copying  FrameCPP tarball from above... "  
     cp $Above/${framecpp}.tar.gz External 
  fi
  if [ -f External/${framecpp}.tar.gz  ]; then 
    cd External
    echo "* Unpacking $framecpp from tarball... "
    gzip -dc ${framecpp}.tar.gz | tar xf - 
    cd ..
  fi 
fi

# Build frame library, unless it's already built

if [ -f External/lib/libframecpp.so ]; then
  echo "FrameCPP library has already been built.  Good.  "
else
  # The extra flags are to find local versions of build tools & libraries
  echo "  "
  cd External/${framecpp}
  pwd
  echo "* configuring FrameCPP... "

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

  echo "* building FrameCPP... "
  time make  

  echo "* installing FrameCPP... "
  make install
  RC=$?
  echo "  DONE with FrameCPP.  "
  if [ $RC -ne 0 ]; then 
    echo "FrameCPP build ended with RC=$RC so stopping here.  Urp.  "
    exit $RC
  fi
  cd ../../ 
fi


##
# Now the Big Build...

echo "  "
pwd
echo "* Building GDS.... "

time make 
RC=$?
echo "Finish: `date` "

if [ $RC -ne 0 ]; then
  echo "* "
  echo "* The build had problems, please fix them and try again. " 
  echo "*  (Perhaps you just need to remove some flaky Monitors?) "
  echo "* "
else
  echo "* "
  echo "* Build succeeded. "
  echo "* Now you just have to say \`make install\` "
  echo "* "
fi

exit $RC

##
