#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/gcstd                       */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Tue Jun 25 16:59:41 2002                          */
#*    Last change :  Mon Jun 11 10:08:34 2007 (serrano)                */
#*    Copyright   :  2002-07 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    This configure script checks that a standard GC installation     */
#*    is correct.                                                      */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cc=gcc
cflags=
tmp=/tmp
user=bigloo
gcincdir=/usr/include/gc
gclibdir=/usr/lib
gclib=gc
dlopenopt=

#*---------------------------------------------------------------------*/
#*    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]*=//'`";;

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

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

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

    --dlopen=*)
      dlopen="`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$gcincdir -I$gcincdir/gc -L $gclibdir -l$gclib >/dev/null 2>> configure.log"
compile2="$cc $cflags $file.c -o $aout -I$gcincdir -I$gcincdir/gc -L $gclibdir -l$gclib $dlopen >/dev/null 2>> configure.log"

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

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

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

#if( defined( GC_THREADS ) && defined( THREAD_LOCAL_ALLOC ) )
   real = (double *)GC_THREAD_MALLOC_ATOMIC( sizeof( double ) );
#else
   real = (double *)GC_MALLOC_ATOMIC( sizeof( double ) );
#endif
   *real = d;
   
   return real;
}

int main( int argc, char *argv[] ) {
    double *d1, *d2;
    int i;

    GC_INIT();

    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; then
   sh -c "rm -f $file.*" 2> /dev/null
   eval $aout
   \rm -f $aout
   echo "$gclib"
   exit 0
else
echo "$compile" > /dev/stderr
exit 1;
   if eval $compile2; 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
      echo "Cannot compile gc checker. Something broken with your GC settings." >&2
      echo "Bigloo cannot be configured with the --customgc=no option." >&2
      echo "" >&2
      exit 1
   fi
fi
