#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/signbit                     */
#*    -------------------------------------------------------------    */
#*    Author      :  Jose Romildo                                      */
#*    Creation    :  Sat Nov 11 13:27:23 1995                          */
#*    Last change :  Tue Mar 25 17:18:26 2008 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Detect the mask for the sign bit of an integer type.             */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cc=gcc
cflags=
tmp=/tmp
user=bigloo
type=long
alignment=0

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

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

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

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

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

#*---------------------------------------------------------------------*/
#*    Default setting                                                  */
#*---------------------------------------------------------------------*/
if [ "$type " = " " ]; then
  type="int"
fi

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$cc $cflags $file.c -o $aout >/dev/null 2> /dev/null"
run="$aout 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 <stdio.h>
#include <limits.h>
main( argc, argv )
int   argc;
char *argv[];
{
   int n;
   unsigned long long x = 1;
   for (n = 8 * sizeof($type) - 1 - ($alignment); n > 0; n--)
      x <<= 1;
   printf("%#llx\n", x);
   return 0;
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval $compile; then
   \rm -f $file.*
   result=`$run`
   rm -f $aout
   rm -f $aout*
   echo "$result"
else
   \rm -f $file.*
   echo ""
fi
