#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/gcversion                   */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Aug  9 13:27:23 1995                          */
#*    Last change :  Sat Sep 30 19:20:06 2017 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Check the GC version                                             */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=
gcdir=gc-boehm

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

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

    --gcincdir=*|-gcincdir=*)
      gcdir="`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 >/dev/null"

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

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

int main( int argc, char *argv[] ) {
   printf( "%d%d%d\n", GC_VERSION_MAJOR, GC_VERSION_MINOR, GC_VERSION_MICRO );
   return 0;
}
EOF

if eval "$BUILDSH $compile"; then
   eval "$HOSTSH $aout"
   ret_code=$?
   rm -f $aout
   rm -f $file.*
   exit $ret_code;
fi

cat > $file.c <<EOF
#include <gc.h>
#include <stdio.h>

#if( GC_ALPHA_VERSION == 255 ) 
#undef GC_ALPHA_VERSION
#define GC_ALPHA_VERSION 0
#endif

int main( int argc, char *argv[] ) {
   printf( "%d%d%d\n", GC_VERSION_MAJOR, GC_VERSION_MINOR, GC_ALPHA_VERSION );
   return 0;
}
EOF

if eval "$BUILDSH $compile"; then
   eval "$HOSTSH $aout"
   ret_code=$?
   rm -f $aout
   rm -f $file.*
   exit $ret_code;
fi

cat > $file.c <<EOF
#include <gc.h>
#include <../version.h>
#include <stdio.h>

#if( GC_ALPHA_VERSION == 255 ) 
#undef GC_ALPHA_VERSION
#define GC_ALPHA_VERSION 0
#endif

int main( int argc, char *argv[] ) {
   printf( "%d%d%d\n", GC_VERSION_MAJOR, GC_VERSION_MINOR, GC_ALPHA_VERSION );
   return 0;
}
EOF

if eval "$BUILDSH $compile"; then
   eval "$HOSTSH $aout"
   ret_code=$?
   rm -f $aout
   rm -f $file.*
   exit $ret_code;
fi

cat > $file.c <<EOF
#include <gc.h>
#include <../version.h>
#include <stdio.h>

int main( int argc, char *argv[] ) {
   printf( "%d%02d\n", GC_VERSION_MAJOR, GC_VERSION_MINOR );
   return 0;
}
EOF

if eval "$BUILDSH $compile"; then
   eval "$HOSTSH $aout"
   ret_code=$?
   rm -f $aout
   rm -f $file.*
   exit $ret_code;
fi

rm -f $aout
rm -f $file.*
echo "000"
exit 0
