#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/gc/install-gc-7.2alpha6              */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano, Florian Loitsch                   */
#*    Copyright   :  2007-11 Manuel Serrano, Florian Loitsch           */
#*    -------------------------------------------------------------    */
#*    This shell script installs a new GC.                             */
#*    Run the script with:                                             */
#*      GC=/usr/local/src/gc-7.2alpha6.tar.gz ./install-gc             */
#*=====================================================================*/

src=$GC.tar.gz

gc=`basename $src .tar.gz`
bglversion=`grep release ../configure | head -n 1 | awk -F= '{ print $2 }'`

tar=$TAR
patch=$PATCH

if [ "$tar " = " " ]; then
  tar=tar
fi

if [ "$patch " = " " ]; then
  patch=patch
fi

# untar the two versions of the GC
$tar xfz $src -C ../gc || (echo "$tar xfz $src failed"; exit 1)
/bin/rm -rf "../gc/$gc"_fth
mv ../gc/$gc "../gc/$gc"_fth || (echo "mv gc-7.2 failed"; exit 1)

$tar xfz $src  -C ../gc || (echo "$tar xfz $src failed"; exit 1)

# general Bigloo patch
(cd "../gc/$gc"_fth && $patch -p1 < ../$gc.patch > /dev/null)
if [ $? != "0" ]; then echo "bigloo patch failed"; exit 1; fi
(cd ../gc/$gc && $patch -p1 < ../$gc.patch > /dev/null)
if [ $? != "0" ]; then echo "bigloo patch failed"; exit 1; fi

# Remove parasite IA64 object files
(/bin/rm -f "../gc/$gc"_fth/libatomic_ops-1.2/src/*.a) || exit 1
(/bin/rm -f ../gc/$gc/libatomic_ops-1.2/src/*.a) || exit 1

# Patch the Makefiles for generating a good lib name
for p in configure configure.ac Makefile.direct Makefile.am Makefile.in Makefile.DLLs; do

  sed 's|gc\.a|$(BOOTLIBDIR)/libbigloogc-$(RELEASE).a|g' $gc/$p > $gc/$p.new
  mv $gc/$p.new $gc/$p

  sed 's|gc\.a|$(BOOTLIBDIR)/libbigloogc_fth-$(RELEASE).a|g' "$gc"_fth/$p > "$gc"_fth/$p.new
  mv "$gc"_fth/$p.new "$gc"_fth/$p

done

# Set the correct mode for configure (configure is not currently used by Bigloo)
chmod a+rx $gc/configure
chmod a+rx "$gc"_fth/configure

# Copy the inline_alloc.c file
cp ../runtime/Clib/inline_alloc.c $gc
cp ../runtime/Clib/inline_alloc.c "$gc"_fth
