#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/configure                            */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Tue Jan 25 16:05:10 1994                          */
#*    Last change :  Sun Mar 22 07:38:20 1998 (serrano)                */
#*    -------------------------------------------------------------    */
#*    The Bigloo's configuration file                                  */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    User variables                                                   */
#*---------------------------------------------------------------------*/
#*--- paths -----------------------------------------------------------*/
bindir=/usr/local/bin
libdir=/usr/local/lib/bigloo
mandir=/usr/local/man/man1
docdir=/usr/doc
tmp=/tmp

#*--- compilers -------------------------------------------------------*/
cc=gcc
cflags=-O2
as=as
ar=ar
arflags=qcv
shell=/bin/sh

#*--- indent ----------------------------------------------------------*/
indent="/usr/local/bin/indent -npro -bap -bad -nbc -bl -ncdb -nce -nfc1 -ip0 -nlp -npcs -nsc -nsob -cli0.5 -di0 -l80 -d1 -c0 -ts2 -st"

#*--- mask of Bigloo intalled files -----------------------------------*/
bmask=755

#*--- ld shared library option ----------------------------------------*/
ldopt=

#*---------------------------------------------------------------------*/
#*    Hacker variables                                                 */
#*    -------------------------------------------------------------    */
#*    Don't modifiy these variables unless you know what you are       */
#*    doing.                                                           */
#*---------------------------------------------------------------------*/
#*--- The basename of the compiler ------------------------------------*/
bigloo=bigloo

#*--- libraries -------------------------------------------------------*/
libraryname=$bigloo

#--- additional user library used to link bigloo applications ---------*/
extralibs="-lm"

#*---------------------------------------------------------------------*/
#*               !!! DON'T EDIT AFTER THIS COMMENT !!!                 */
#*---------------------------------------------------------------------*/
action=all
bigloo_cfg=runtime/Include/bigloo_cfg.h
makefile_cfg=Dmakefile.cfg
autoconfdir=`dirname $0 2> /dev/null`/autoconf
if [ $? != "0" ]; then
   autoconfdir="autoconf"
fi

# Argument parsing
while : ; do
  case $1 in
    "")
      break;;

    --bigloo_cfg.h=*)
      action="bigloo_cfg";
      bigloo_cfg="`echo $1 | sed 's/^[-a-z_.]*=//'`";;

    --Dmakefile.cfg=*)
      action="Dmakefile.cfg";
      makefile_cfg="`echo $1 | sed 's/^[-Da-z.]*=//'`";;

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

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

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

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

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

    -*)
      echo "Usage: configure [options]" >&2;
      echo "   --bigloo_cfg.h=file" >&2;
      echo "   --Dmakefile.cfg=file" >&2;
      echo "   --bindir=file" >&2;
      echo "   --libdir=file" >&2;
      echo "   --mandir=file" >&2;
      echo "   --docdir=file" >&2;
      echo "   --indent=file" >&2;
      exit -1;
  esac
  shift
done

#*---------------------------------------------------------------------*/
#*    all action                                                       */
#*---------------------------------------------------------------------*/
if [ $action = "all" ]; then
  if [ -d bin ]; then
     rm -rf bin
  fi
  mkdir bin

  if [ -d lib ]; then
     rm -rf lib
  fi
  mkdir lib
fi

#*---------------------------------------------------------------------*/
#*    bigloo_cfg.h                                                     */
#*    -------------------------------------------------------------    */
#*    This part of the configure script produces the file              */
#*    runtime/Include/bigloo_cfg.h. This file contains all the         */
#*    machine dependant information used by Bigloo.                    */
#*---------------------------------------------------------------------*/
if [ $action = "all" -o $action = "bigloo_cfg" ]; then

  # We first cleanup the file
  rm -f $bigloo_cfg 2> /dev/null
  echo "/* Automatically generated file (don't edit) */" > $bigloo_cfg
  echo "/* `date` */" >> $bigloo_cfg

  # The stack direction
  stackgrows=`$autoconfdir/stackdown --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:stack. Aborting";
     exit 1;
  fi

  echo "#undef STACK_GROWS_DOWN" >> $bigloo_cfg
  if( test $stackgrows = "1" ); then
    echo "   stack grows: down"
    echo "#define STACK_GROWS_DOWN 1" >> $bigloo_cfg
  else
    echo "   stack grows: up"
    echo "#define STACK_GROWS_DOWN 0" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # The pointer alignment
  alignment=`$autoconfdir/alignment --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:alignment. Aborting";
     exit 1;
  fi

  echo "   ptr alignment: " $alignment
  echo "#undef PTR_ALIGNMENT" >> $bigloo_cfg
  echo "#define PTR_ALIGNMENT $alignment" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # setjmp/longjmp
  prefix=`$autoconfdir/setjmp --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:setjmp. Aborting";
     exit 1;
  fi

  setjmp="$prefix"setjmp
  longjmp="$prefix"longjmp

  echo "   setjmp: $setjmp longjmp: $longjmp"
  echo "#undef SETJMP" >> $bigloo_cfg
  echo "#define SETJMP $setjmp" >> $bigloo_cfg
  echo "#undef LONGJMP" >> $bigloo_cfg
  echo "#define LONGJMP $longjmp" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # Does the system have bcopy?
  bcopy=`$autoconfdir/bcopy --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:bcopy. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_BCOPY" >> $bigloo_cfg
  if( test $bcopy = "1" ); then
    echo "   bcopy: yes"
    echo "#define HAVE_BCOPY 1" >> $bigloo_cfg
  else
    echo "   bcopy: no"
    echo "#define HAVE_BCOPY 0" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # The number of window register
  window=`$autoconfdir/winreg --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:winreg. Aborting";
     exit 1;
  fi

  echo "   nb window register: " $window
  echo "#undef NB_WINDOW_REGISTER" >> $bigloo_cfg
  echo "#define NB_WINDOW_REGISTER $window" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # Does the system have sigsetmask?
  sigsetmask=`$autoconfdir/sigsetmask --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:sigsetmask. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_SIGSETMASK" >> $bigloo_cfg
  if( test $sigsetmask = "0" ); then
    echo "   sigsetmask not found"
    echo "#define HAVE_SIGSETMASK 0" >> $bigloo_cfg
   else
    echo "   sigsetmask found"
    echo "#define HAVE_SIGSETMASK 1" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # Does the system have sigprocmask?
  sigprocmask=`$autoconfdir/sigprocmask --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:sigprocmask. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_SIGPROCMASK" >> $bigloo_cfg
  if( test $sigprocmask = "0" ); then
    echo "   sigprocmask not found"
    echo "#define HAVE_SIGPROCMASK 0" >> $bigloo_cfg
  else
    echo "   sigprocmask found"
    echo "#define HAVE_SIGPROCMASK 1" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # A warning if neither sigsetmask nor sigprocmask is defined
  if( test $sigsetmask = "0" -a $sigprocmask = "0"); then
     echo "*** WARNING:sigsetmask nor sigprocmask found"
     echo "(^C disabled in the interpreter)."
  fi
  echo "" >> $bigloo_cfg

  # Does the system have alloca?
  alloca=`$autoconfdir/alloca --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:alloca. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_ALLOCA" >> $bigloo_cfg
  if( test $alloca = "1" ); then
    echo "   alloca: yes"
    echo "#define HAVE_ALLOCA 1" >> $bigloo_cfg
  else
    echo "   alloca: no"
    echo "#define HAVE_ALLOCA 0" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # Does the system have getcwd?
  getcwd=`$autoconfdir/getcwd --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:getcwd. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_GETCWD" >> $bigloo_cfg
  if( test $getcwd = "0" ); then
    echo "   getcwd not found"
    echo "#define HAVE_GETCWD 0" >> $bigloo_cfg
  else
    echo "   getcwd found"
    echo "#define HAVE_GETCWD 1" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # Does the system have getwd?
  getwd=`$autoconfdir/getwd --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:getwd. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_GETWD" >> $bigloo_cfg
  if( test $getwd = "0" ); then
    echo "   getwd not found"
    echo "#define HAVE_GETWD 0" >> $bigloo_cfg
  else
    echo "   getwd found"
    echo "#define HAVE_GETWD 1" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # Are the static constants aligned?
  cnstalign=`$autoconfdir/cnstalign --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:cnstalign. Aborting";
     exit 1;
  fi

  enforcedcnstalign=`$autoconfdir/enforcedcnstalign --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:enforcedcnstalign. Aborting";
     exit 1;
  fi

  echo "#undef CONSTANT_ALIGNED" >> $bigloo_cfg
  if( test $cnstalign = "1" ); then
    echo "   constant alignment: ok"
    echo "#define CONSTANT_ALIGNED 1" >> $bigloo_cfg
  elif( test $cnstalign = "0" ); then
    if( test $enforcedcnstalign = "1" ); then
       echo "   constant alignment: enforced"
       echo "#define CONSTANT_ALIGNED 0" >> $bigloo_cfg
    else
       echo "***ERROR:configure:illegal alignment."
       echo "It is then impossible install Bigloo on this architecture, sorry."
       exit 1
    fi
  fi
  echo "" >> $bigloo_cfg

  # Does this system implements pipe?
  pipe=`$autoconfdir/pipe --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:pipe. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_PIPE" >> $bigloo_cfg
  if( test $pipe = "0" ); then
    echo "   popen/pclose not found"
    echo "#define HAVE_PIPE 0" >> $bigloo_cfg
  else
    echo "   popen/pclose found"
    echo "#define HAVE_PIPE 1" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # Does this compiler implements longlong?
  longlong=`$autoconfdir/longlong --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:longlong. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_LONGLONG" >> $bigloo_cfg
  if( test $longlong = "1" ); then
    echo "   long long: yes"
    echo "#define HAVE_LONGLONG 1" >> $bigloo_cfg
  else
    echo "   long long: no"
    echo "#define HAVE_LONGLONG 0" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # Is sigchld available for us
  sigchld=`$autoconfdir/sigchld --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:sigchld. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_SIGCHLD" >> $bigloo_cfg
  if( test $sigchld = "1" ); then
    echo "   sigchld: yes"
    echo "#define HAVE_SIGCHLD 1" >> $bigloo_cfg
  else
    echo "   sigchld: no"
    echo "#define HAVE_SIGCHLD 0" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg
  
  # Is sigaction available
  sigaction=`$autoconfdir/sigaction --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:sigaction. Aborting";
     exit 1;
  fi

  echo "#undef HAVE_SIGACTION" >> $bigloo_cfg
  if( test $sigaction = "1" ); then
    echo "   sigaction: yes"
    echo "#define HAVE_SIGACTION 1" >> $bigloo_cfg
  else
    echo "   sigaction: no"
    echo "#define HAVE_SIGACTION 0" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg
  
  # for now only the port is Unix
  echo "   operating system: unix"
  $autoconfdir/unix >> $bigloo_cfg
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:os. Aborting";
     exit 1;
  fi
  echo "" >> $bigloo_cfg

  # which shell to use
  `$shell -c "ls -l > /dev/null 2> /dev/null"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:$shell. Aborting";
     exit 1;
  fi

  echo "   shell: $shell"
  echo "#undef SHELL" >> $bigloo_cfg
  echo "#define SHELL \"$shell\"" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # which C compiler to use
  echo "   c compiler: $cc"
  echo "#undef C_COMPILER" >> $bigloo_cfg
  echo "#define C_COMPILER \"$cc\"" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # the library directory
  echo "   library directory: $libdir"
  echo "#undef LIBRARY_DIRECTORY" >> $bigloo_cfg
  echo "#define LIBRARY_DIRECTORY \"$libdir\"" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # additionals non bigloo library
  echo "   additional libraries: $extralibs"
  echo "#undef USER_LIBRARIES" >> $bigloo_cfg
  echo "#define USER_LIBRARIES \"$extralibs\"" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # the C beautifier
  echo "#undef C_BEAUTIFIER" >> $bigloo_cfg
  if( test `$autoconfdir/indent --cc="$cc" --cflags="$cflags" --indent="$indent"` != "1" ); then
    echo "   c beautifier: no"
    echo "#define C_BEAUTIFIER \"\"" >> $bigloo_cfg
  else
    echo "   c beautifier: $indent"
    echo "#define C_BEAUTIFIER \"$indent\"" >> $bigloo_cfg
  fi
  echo "" >> $bigloo_cfg

  # library basename
  echo "   library basename: $libraryname"
  echo "#undef LIBRARY_BASE_NAME" >> $bigloo_cfg
  echo "#define LIBRARY_BASE_NAME \"$libraryname\"" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # double precision
  prec=`$autoconfdir/doubleprec --cc="$cc"`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:doubleprec. Aborting";
     exit 1;
  fi

  echo "   double precision: $prec"
  echo "#undef DOUBLE_PRECISION" >> $bigloo_cfg
  echo "#define DOUBLE_PRECISION $prec" >> $bigloo_cfg
  echo "" >> $bigloo_cfg

  # The shared libraries
  ldopt=`$autoconfdir/ldshare --cc="$cc" -ldopt=$ldopt`
  echo "#undef HAVE_SHARED_LIBRARY" >> $bigloo_cfg

  if( test "$ldopt" = "" ); then
    echo "   shared library: no (because I don't know ld option)"
    echo "#define HAVE_SHARED_LIBRARY 0" >> $bigloo_cfg
  else
    echo "   shared library: yes"
    echo "#define HAVE_SHARED_LIBRARY 1" >> $bigloo_cfg
  fi
fi
  
#*---------------------------------------------------------------------*/
#*    Dmakefile.cfg                                                    */
#*    -------------------------------------------------------------    */
#*    This part of the configure script produces the file              */
#*    Dmakefile.cfg. This file contains machine dependant informations */
#*    and location where Bigloo is to be installed.                    */
#*---------------------------------------------------------------------*/
if [ $action = "all" -o $action = "Dmakefile.cfg" ]; then

  # We create an unexisting temporary file name
  name=$tmp/foo
  while( test -f "$name.s" -o -f "$name.o" ); do 
    name="$name"x;
  done

  # We first cleanup the file
  rm -f $makefile_cfg 2> /dev/null
  touch $makefile_cfg

  # The basename
  echo "LIBRARYNAME=\"$libraryname\"" >> $makefile_cfg

  # The C compiler
  echo "CC=\"$cc\"" >> $makefile_cfg
  echo "CFLAGS=\"$cflags\"" >> $makefile_cfg

  # The assembler
  echo "   checking $as"
  as=`$autoconfdir/ascpp -as="$as" -cc="$cc" -tmp=$tmp --name=$name`
  if [ $? != "0" ]; then
     echo "*** ERROR:configure:as. Aborting";
     exit 1;
  fi

  if [ "$as " = " " ]; then
    echo "***ERROR:configure:$as:can't find as that invokes cpp,";
    echo "set $as variable."
    rm -f $name.s $name.o
    exit 1;
  else
     echo "AS=\"$as\"" >> $makefile_cfg
  fi 

  # Ar checking
  echo "   checking $ar $arflags"

  (cd $tmp; 
   compile="$ar $arflags $name.a $name.o > /dev/null 2> /dev/null";
   if eval $compile; then
     /bin/rm -f $name.s $name.o $name.a
   else
      arflags="-q -c";
      compile="$ar $arflags $name.a $name.o > /dev/null 2> /dev/null";
      if eval $compile; then
        /bin/rm -f $name.s $name.o $name.a
      else
        echo "***ERROR:configure:ar:Can't automatically setup ar flags"; 
        echo "Please, set ar flags up inside ./configure";
        /bin/rm -f $name.s $name.o $name.a; 
        exit 1
      fi
   fi) || exit $*
  echo "AR=\"$ar\"" >> $makefile_cfg
  echo "ARFLAGS=\"$arflags\"" >> $makefile_cfg
  
  # Does the system require ranlib?
  if( test `$autoconfdir/ranlib --cc="$cc"` = "0" ); then
     echo "   ranlib: no"
     echo "RANLIB=true" >> $makefile_cfg
  else
     echo "   ranlib: yes"
     echo "RANLIB=\"ranlib\"" >> $makefile_cfg
  fi

  # The shared libraries
  if( test "$ldopt" = "" ); then
    echo "   shared library: no (because I don't know ld option)"
    echo "LD=true" >> $makefile_cfg
    echo "CPSHARED=true" >> $makefile_cfg
  else
    echo "   shared library: yes"
    echo "LD=\"ld $ldopt\"" >> $makefile_cfg
    echo "CPSHARED=cp" >> $makefile_cfg
  fi

  # bmask
  echo "BMASK=$bmask" >> $makefile_cfg

  # The release number
  echo "BVERSION=" >> $makefile_cfg

  # the common Makefile entry
  echo "" >> $makefile_cfg
  echo "boot:" >> $makefile_cfg
  echo "	\$(MAKE) Dboot \\" >> $makefile_cfg
  echo "                 LIBRARYNAME=\$(LIBRARYNAME) \\" >> $makefile_cfg
  echo "                 CC=\$(CC) \\" >> $makefile_cfg
  echo "                 CFLAGS=\$(CFLAGS) \\" >> $makefile_cfg
  echo "                 AS=\$(AS) \\" >> $makefile_cfg
  echo "                 AR=\$(AR) \\" >> $makefile_cfg
  echo "                 ARFLAGS=\$(ARFLAGS) \\" >> $makefile_cfg
  echo "                 RANLIB=\$(RANLIB) \\" >> $makefile_cfg
  echo "                 LD=\$(LD) \\" >> $makefile_cfg
  echo "                 CPSHARED=\$(CPSHARED) \\" >> $makefile_cfg
  echo "                 BMASK=\$(BMASK)" >> $makefile_cfg
  echo "" >> $makefile_cfg
  echo "install:" >> $makefile_cfg
  echo "	\$(MAKE) Dinstall \\" >> $makefile_cfg
  echo "                 BVERSION=\$(BVERSION) \\" >> $makefile_cfg
  echo "                 BINDIR=$bindir \\" >> $makefile_cfg
  echo "                 LIBDIR=$libdir \\" >> $makefile_cfg
  echo "                 MANDIR=$mandir \\" >> $makefile_cfg
  echo "                 DOCDIR=$docdir \\" >> $makefile_cfg
  echo "                 BMASK=\$(BMASK) \\" >> $makefile_cfg
  echo "                 RANLIB=\$(RANLIB) \\" >> $makefile_cfg
  echo "                 CPSHARED=\$(CPSHARED)" >> $makefile_cfg
  echo "" >> $makefile_cfg
  echo "uninstall:" >> $makefile_cfg
  echo "	\$(MAKE) Duninstall \\" >> $makefile_cfg
  echo "                 BINDIR=$bindir \\" >> $makefile_cfg
  echo "                 LIBDIR=$libdir \\" >> $makefile_cfg
  echo "                 MANDIR=$mandir \\" >> $makefile_cfg
  echo "                 DOCDIR=$docdir \\" >> $makefile_cfg
  echo "" >> $makefile_cfg
  echo "clean:" >> $makefile_cfg
  echo "	\$(MAKE) Dclean \\" >> $makefile_cfg
  echo "                 BINDIR=$bindir \\" >> $makefile_cfg
  echo "                 LIBDIR=$libdir \\" >> $makefile_cfg
  echo "                 MANDIR=$mandir \\" >> $makefile_cfg
  echo "                 DOCDIR=$docdir \\" >> $makefile_cfg
fi

#*---------------------------------------------------------------------*/
#*    Ok, we are done now                                              */
#*---------------------------------------------------------------------*/
echo "configuration done."
echo "-------------------"
