#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/gc/install-gc-7.1                    */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Dec 19 07:52:02 2007                          */
#*    Last change :  Sat Mar  5 07:39:23 2011 (serrano)                */
#*    Copyright   :  2007-11 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    This shell script installs a new GC.                             */
#*    Run the script with:                                             */
#*      GC=/usr/local/src/gc-7.1.tar.gz ./install-gc                   */
#*=====================================================================*/

src=$GC.tar.gz
gc=`basename $src .tar.gz`
gentoo=gc-6.5-gentoo
gentooulibc=gc-6.6-gentoo-ulibc
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 version of the GC
$tar xfz $src -C ../gc || (echo "$tar xfz $src failed"; exit 1)
mv ../gc/gc-7.1 "../gc/$gc"_fth || (echo "mv gc-7.1 failed"; exit 1)

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

# Patch the two versions
(cd "../gc/$gc"_fth && $patch -p1 < ../$gentoo.patch > /dev/null) || exit 1
(cd "../gc/$gc"_fth && $patch -p1 < ../$gentooulibc.patch > /dev/null) || exit 1
(cd "../gc/$gc"_fth && $patch -p1 < ../$gc.patch > /dev/null) || exit 1
(cd ../gc/$gc && $patch -p1 < ../$gentoo.patch > /dev/null) || exit 1
(cd ../gc/$gc && $patch -p1 < ../$gentooulibc.patch > /dev/null) || exit 1
(cd ../gc/$gc && $patch -p1 < ../$gc.patch > /dev/null) || exit 1

# Remove parasite IA64 object files
(rm -f "../gc/$gc"_fth/libatomic_ops-1.2/src/*.a) || exit 1
(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

# Install the Makefiles.
mv $gc/Makefile.direct $gc/Makefile
mv "$gc"_fth/Makefile.direct "$gc"_fth/Makefile

# 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

# Copy the sparc_linux_mach_dep.c (resurrected from gc6.8) specific file
cp sparc_linux_mach_dep.c $gc
cp sparc_linux_mach_dep.c "$gc"_fth

# Patch for MacOS X 10.6 and higher
if [ "$hostos " = "darwin " ]; then
  if [ "`echo $hostosversion | sed 's/[a-z].*$//' | sed 's/[.]//g'`" -ge "1000" ]; then
    sed -ie "s/<ucontext\.h>/<sys\/ucontext\.h>/g" $gc/mach_dep.c
    sed -ie "s/<ucontext\.h>/<sys\/ucontext\.h>/g" "$gc"_fth/mach_dep.c
  fi
fi

