#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/gcstd                       */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Tue Jun 25 16:59:41 2002                          */
#*    Last change :  Wed May  7 11:39:28 2003 (serrano)                */
#*    Copyright   :  2002-03 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    This configure script checks that a standard GC installation     */
#*    is correct.                                                      */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cc=gcc
cflags=
tmp=/tmp
user=bigloo
gcdir=/usr/include/gc
gclib=gc

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --user=*)
      user="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cc=*|-cc=*)
      cc="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --tmp=*|-tmp=*)
      tmp="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --gcdir=*)
      gcdir="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --gclib=*)
      gclib="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

file=$tmp/actest$user
aout=$tmp/Xactest$user

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$cc $cflags $file.c -o $aout -I$gcdir -l$gclib >/dev/null 2> /dev/null"

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if( test -f $file.c ); then
   \rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <gc.h>
#include <gc_inl.h>

static double *
alloc_make_real( double d ) {
   double *real;

   real = (double *)GC_generic_malloc_words_small(64, PTRFREE);
   *real = d;
   
   return real;
}

double *
make_real( double d ) {
   double *real;
   ptr_t op;
   ptr_t *opp;
   DCL_LOCK_STATE;
   
   opp =  &(GC_aobjfreelist[ 64 ]);

   FASTLOCK();

   if( !FASTLOCK_SUCCEEDED() || (op = *opp) == 0 )
   {
      FASTUNLOCK();
      return alloc_make_real( d );
   }
   else
   {
      *opp = obj_link(op);
      FASTUNLOCK();

      real = (double *)op;
      *real = d;
      return real;
   }
}

int main( int argc, char *argv[] ) {
    double *d1, *d2;
    int i;
    d1 = make_real( (double)1. );
    d2 = make_real( (double)3.14 );
    for( i = 0; i < 10000; i++ ) { 
       *d2 += *make_real( *d2 );
    }
    
    return (*d1 != 1.);
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval $compile 2> /dev/null; then
   sh -c "rm -f $file.*" 2> /dev/null
   eval $aout
   \rm -f $aout
   echo "$gclib"
   exit 0
else
   sh -c "rm -f $file.*" 2> /dev/null
   \rm -f $aout
   exit 1
fi
