#!/bin/sh

case $1 in
  "") cc=cc;;
   *) cc=$1;;
esac
export cc

cd auto-aux
rm -f s.h m.h

# Are chars signed?

sh runtest schar.c
case $? in
  0) echo "The char type is signed. Good!";;
  1) echo "The char type is not signed. Let's see if 'signed char' works."
     sh runtest schar2.c
     case $? in
      0) echo "Yes, it works. Good!"
         echo "#define SIGNED_CHAR_WORKS" >> s.h;;
      *) echo "No, it does not work. Let's try some compiler options."
         goodopt=""
         for option in -signed -fsigned-char; do
           sh runtest $option schar.c
           case $? in
             0) goodopt=$option; break;;
           esac
         done
         case "$goodopt" in
           "") echo "Sorry, I can't find the right option."
               echo "Please figure it out yourself, and"
               echo "add it to OTHEROPTS in src/runtime/Makefile.";;
            *) echo "Yippie! Option $goodopt works. Good!"
               echo "Add \"$goodopt\" to OTHEROPTS in src/runtime/Makefile.";;
         esac;;
     esac;;
esac

# Determine endianness

sh runtest endian.c
case $? in
  0) echo "This is a big-endian architecture."
     echo "#define BIG_ENDIAN" >> m.h;;
  1) echo "This is a little-endian architecture."
     echo "#undef BIG_ENDIAN" >> m.h;;
  2) echo "This architecture seems to be neither big endian nor little endian."
     echo "Caml Light won't run on this architecture without a major rework."
     exit 2;;
  *) echo "Something went wrong during endianness determination."
     echo "You'll have to figure out endianness yourself"
     echo "(option BIG_ENDIAN in m.h).";;
esac

# Determine alignment constraints

volatile=""
while true; do
  sh runtest $volatile align.c
  case $? in
  100) echo "Your compiler chokes on the \"volatile\" modifier."
       echo "Never mind, we'll do without it."
       volatile="-Dvolatile=";;
    0) echo "This architecture has no alignment constraints."
       echo "#undef ALIGNMENT" >> m.h
       break;;
    1) echo "This architecture has alignment constraints."
       echo "#define ALIGNMENT" >> m.h
       break;;
    *) echo "Something went wrong during alignment determination."
       echo "I'm going to assume this architecture has alignment constraints."
       echo "That's a safe bet: Caml Light will work even if it turns out that"
       echo "this architecture actually has no alignment constraints."
       echo "#define ALIGNMENT" >> m.h
       break;;
  esac
done

sh runtest dblalign.c
case $? in
  0) echo "Doubles can be word-aligned.";;
  1) echo "Doubles must be doubleword-aligned."
     echo "#define ALIGN_DOUBLE" >> m.h;;
  *) echo "Something went wrong during alignment determination for doubles."
     echo "I'm going to assume this architecture has alignment constraints over doubles."
     echo "That's a safe bet: Caml Light will work even if it turns out that"
     echo "this architecture actually has no alignment constraints."
     echo "#define ALIGN_DOUBLE" >> m.h;;
esac

# To find a good byte copy function

if sh runtest -Dcopy=memmove -Dreverse bytecopy.c; then
  echo "Function \"memmove\" is provided and handles overlapping moves correctly."
  echo "#define HAS_MEMMOVE" >> s.h
fi
if sh runtest -Dcopy=bcopy bytecopy.c; then
  echo "Function \"bcopy\" is provided and handles overlapping moves correctly."
  echo "#define HAS_BCOPY" >> s.h
fi
if sh runtest -Dcopy=memcpy -Dreverse bytecopy.c; then
  echo "Function \"memcpy\" is provided and handles overlapping moves correctly."
  echo "#define HAS_MEMCPY" >> s.h
fi

# Check for _longjmp and _setjmp

sh runtest setjmp.c
case $? in
  0) echo "_setjmp and _longjmp appear to work. Good!"
     echo "#define HAS__SETJMP" >> s.h;;
  *) echo "No _setjmp, _longjmp. We'll use setjmp and longjmp instead."
esac

# Try to find the type of signal handlers

h=""

for ty in void int; do
  rm -f /tmp/output$$
  if $cc -c -DSIGRETURN=$ty sighandler.c 2>/tmp/output$$; then
    if grep -s -i warning /tmp/output$$; then
      :
    else
      h=$ty
      break
    fi
  fi
done
rm -f sighandler.o

case "$h" in
  "") echo "Sorry, I can't determine the return type for signal handlers."
      echo "I'm assuming \"void\". If this seems to cause errors,"
      echo "try to change \"sighandler_return_type\" in s.h"
      echo "#define sighandler_return_type void" >> s.h;;
   *) echo "The return type for signal handlers appears to be \"$h\"."
      echo "#define sighandler_return_type $h" >> s.h;;
esac

# For the sys library

if sh hasgot rename; then
  echo "rename() found."
  echo "#define HAS_RENAME" >> s.h
fi

# For the Unix library

if sh hasgot socket socketpair bind listen accept connect; then
  echo "You have BSD sockets."
  echo "#define HAS_SOCKETS" >> s.h
fi

if test -f /usr/include/unistd.h; then
  echo "unistd.h found."
  echo "#define HAS_UNISTD" >> s.h
fi

if test -f /usr/include/dirent.h; then
  echo "dirent.h found."
  echo "#define HAS_DIRENT" >> s.h
fi

if sh hasgot lockf; then
  echo "lockf() found."
  echo "#define HAS_LOCKF" >> s.h
fi

if sh hasgot mkfifo; then
  echo "mkfifo() found."
  echo "#define HAS_MKFIFO" >> s.h
fi

if sh hasgot getpriority setpriority; then
  echo "getpriority() found."
  echo "#define HAS_GETPRIORITY" >> s.h
fi

if test -f /usr/include/utime.h && sh hasgot utime; then
  echo "utime() found."
  echo "#define HAS_UTIME" >> s.h
fi

if sh hasgot utimes; then
  echo "utimes() found."
  echo "#define HAS_UTIMES" >> s.h
fi

if sh hasgot dup2; then
  echo "dup2() found."
  echo "#define HAS_DUP2" >> s.h
fi

if sh hasgot fchmod fchown; then
  echo "fchmod() found."
  echo "#define HAS_FCHMOD" >> s.h
fi

if sh hasgot truncate ftruncate; then
  echo "truncate() found."
  echo "#define HAS_TRUNCATE" >> s.h
fi

if sh hasgot select; then
  echo "select() found."
  echo "#define HAS_SELECT" >> s.h
fi

if sh hasgot symlink readlink lstat;  then
  echo "symlink() found."
  echo "#define HAS_SYMLINK" >> s.h
fi

if sh hasgot wait3;  then
  echo "wait3() found."
  echo "#define HAS_WAIT3" >> s.h
fi

if sh hasgot waitpid;  then
  echo "waitpid() found."
  echo "#define HAS_WAITPID" >> s.h
fi

if sh hasgot getgroups && grep -s NGROUPS /usr/include/sys/param.h >/dev/null;
then
  echo "getgroups() found."
  echo "#define HAS_GETGROUPS" >> s.h
fi

rm -f tst
rm -f ../m.h ../s.h
mv m.h s.h ..
