#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/scribe/configure                            */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Tue Jan 25 16:05:10 1994                          */
#*    Last change :  Sun Jan 13 07:29:09 2002 (serrano)                */
#*    Copyright   :  1994-2002 Manuel Serrano, see LICENSE file        */
#*    -------------------------------------------------------------    */
#*    The scribe configuration file                                    */
#*=====================================================================*/

# the name of the current bigloo compiler
bigloo=bigloo

# bigloo compilation flags
boptflags="-no-hello -unsafe -O3 -L $""(SCRIBEDIR)/lib -ld-relative"
bsafeflags="-no-hello -L $""(SCRIBEDIR)/lib -ld-relative"
bflags=$boptflags
bheapflags="-unsafe -q -mkaddheap -mkaddlib -L $""(SCRIBEDIR)/lib"
bjvmflags=

# the afile, jfile and btags binaries
afile=afile
jfile=
btags=btags

# C compilation (left blank for automatic configuration (from Bigloo setup))
cc=
cflags=
ldopt=

# path (left blank for automatic configuration (from Bigloo setup))
libdir=
bindir=
scribelibdir=
scribescrdir=
docdir=
mandir=

# mask of Scribe intalled files
smask=755

#*---------------------------------------------------------------------*/
#*               !!! DON'T EDIT AFTER THIS COMMENT !!!                 */
#*---------------------------------------------------------------------*/
release=0.7
requiredbigloo=2.4b

action=all
makefile_config=Makefile.scrconfig
scribe_config=scribeapi/configure.scm
summary=yes

http="www-sop.inria.fr/mimosa/fp"
scribehttphost="$http/Scribe"
scribeurl="http://$scribehttphost"
autoconfdir=`dirname $0 2> /dev/null`/autoconf

if [ $? != "0" ]; then
   autoconfdir="autoconf"
fi

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

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

    --makefile.scrconfig=*)
      action="Makefile.scrconfig";
      makefile_config="`echo $1 | sed 's/^[-Da-z.]*=//'`";;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    --no-summary)
      summary=no;;

    --debug)
      bflags="-g -cg $bsafeflags";;

    --optimize)
      bflags=$boptflags;;

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

    --prefix=*)
      prefix="`echo $1 | sed 's/^[^=]*=//'`";
      bindir=$prefix/bin;
      libdir=$prefix/lib/scribe/$release;
      scribelibdir=$prefix/lib/scribe/$release;
      scribescrdir=$prefix/lib/scribe/$release/scr;
      mandir=$prefix/man/man1;
      infodir=$prefix/info;
      docdir=$prefix/doc/scribe-$release;;

    -*)
      echo "Usage: configure [options]" >&2;
      echo "   --scribe_config=file" >&2;
      echo "   --Makefile.scrconfig=file" >&2;
      echo "   --prefix=dir" >&2;
      echo "   --libdir=file" >&2;
      echo "   --scribelibdir=file" >&2;
      echo "   --scribescrdir=file" >&2;
      echo "   --docdir=file" >&2;
      echo "   --mandir=file" >&2;
      echo "   --bigloo=comp" >&2;
      echo "   --afile=afile" >&2;
      echo "   --jfile=jfile" >&2;
      echo "   --btags=btags" >&2;
      echo "   --cc=comp" >&2;
      echo "   --cflags=args" >&2;
      echo "   --ldopt=args" >&2;
      echo "   --smask=mask" >&2;
      echo "   --no-summary" >&2;
      echo "   --debug" >&2;
      echo "   --optimize" >&2;
      exit -1;
  esac
  shift
done

#*---------------------------------------------------------------------*/
#*    First check if bigloo exists and if it is recent enough          */
#*---------------------------------------------------------------------*/
if [ ! -f $bigloo ]; then
   which $bigloo > /dev/null 2> /dev/null
   if [ "$?" != "0" ]; then
      echo "*** ERROR:configure:bigloo. Aborting"
      echo "Can't find bigloo."
      exit 1;
   fi
fi

installedbigloo=`$autoconfdir/bversion --bigloo=$bigloo --version=$requiredbigloo`

if [ $? != "0" ]; then
  echo "*** ERROR:configure:bigloo. Aborting"
  echo "Your version ($installedbigloo) of Bigloo is too old."
  echo "Release $requiredbigloo or more recent is required."
  echo "Bigloo may be downloaded from $http"
  exit 1;
fi

#*---------------------------------------------------------------------*/
#*    The binary directory                                             */
#*---------------------------------------------------------------------*/
if [ "$bindir " = " " ]; then
   if [ "$bigloo " = " " ]; then
      bgl=`which bigloo`;
   else
      bgl=`which $bigloo`;
   fi
   bindir=`dirname $bgl`
fi

#*---------------------------------------------------------------------*/
#*    The library directory                                            */
#*---------------------------------------------------------------------*/
if [ "$libdir " = " " ]; then
   libdir=`$autoconfdir/blibdir --bigloo="$bigloo"`
fi

#*---------------------------------------------------------------------*/
#*    We check the installed Bigloo Makefile.config file               */
#*---------------------------------------------------------------------*/
if [ ! -f $libdir/Makefile.config ]; then
   echo "*** ERROR:configure:Can't find Makefile.config file"
   echo "Should be $libdir/Makefile.config."
   exit 1;
fi

#*---------------------------------------------------------------------*/
#*    jfile                                                            */
#*---------------------------------------------------------------------*/
if [ "$jfile " = " " ]; then
   if [ ! -f $bigloo ]; then
      which jfile > /dev/null 2> /dev/null
      if [ "$?" != "0" ]; then
         jfile=true;
      else
         jfile=jfile;
      fi
   fi
fi

#*---------------------------------------------------------------------*/
#*    We are now able to set the correct value for cc since we know    */
#*    what Bigloo is.                                                  */
#*---------------------------------------------------------------------*/
if [ "$cc " = " " ]; then
  cc=`$bigloo -eval '(begin (print *cc*) (exit 0))'`
fi

if [ "$cflags " = " " ]; then
  cflags=`grep '^CFLAGS=' $libdir/Makefile.config | sed 's/^[A-Z]*=//'`
fi

ldflags=`grep '^EXTRALIBS=' $libdir/Makefile.config | sed 's/^[A-Z]*=//'`
cpicflags=`grep '^CPICFLAGS=' $libdir/Makefile.config | sed 's/^[A-Z]*=//'`

if [ "$ldopt " = " " ]; then
  ldopt=`grep '^LD=' $libdir/Makefile.config | sed 's/^[A-Z]*=//'`
fi

#*---------------------------------------------------------------------*/
#*    Shared library support                                           */
#*---------------------------------------------------------------------*/
bglshared=`grep 'define HAVE_SHARED_LIBRARY' $libdir/bigloo_config.h`

if [ "$bglshared" != "#define HAVE_SHARED_LIBRARY 1" ]; then
  echo "*** ERROR:configure:bigloo. Aborting"
  echo "Your version of Bigloo does not support shared libraries."
  echo "This feature is required for running Scribe."
  exit 1;
fi

#*---------------------------------------------------------------------*/
#*    Completing dirs                                                  */
#*---------------------------------------------------------------------*/
if [ "$scribelibdir " = " " ]; then
   scribelibdir=$libdir;
fi
if [ "$scribescrdir " = " " ]; then
   scribescrdir=$scribelibdir/scr;
fi
if [ "$docdir " = " " ]; then
   docdir=`grep '^DOCDIR=' $libdir/Makefile.config | sed 's/^[A-Z]*=//'`
   docdir=`dirname $docdir`/scribe-$release
fi
if [ "$mandir " = " " ]; then
   mandir=`grep '^MANDIR=' $libdir/Makefile.config | sed 's/^[A-Z]*=//'`
fi

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

  # We first cleanup the file
  rm -f $scribe_config 2> /dev/null
  echo ";; Automatically generated file (don't edit)" > $scribe_config
  echo ";; `date`" >> $scribe_config
  echo "" >> $scribe_config
  echo "(module __scribeapi_configure" >> $scribe_config
  echo "   (export (scribe-version::bstring)" >> $scribe_config
  echo "           (scribe-style-directory::bstring)" >> $scribe_config
  echo "           (scribe-library-directory::bstring)" >> $scribe_config
  echo "           (scribe-url::bstring))" >> $scribe_config
  echo "   (eval   (export-all)))" >> $scribe_config
  echo "" >> $scribe_config
  echo "(define (scribe-version) \"$release\")" >> $scribe_config
  echo "" >> $scribe_config
  echo "(define (scribe-style-directory) \"$scribescrdir\")" >> $scribe_config
  echo "" >> $scribe_config
  echo "(define (scribe-library-directory) \"$scribelibdir\")" >> $scribe_config
  echo "" >> $scribe_config
  echo "(define (scribe-url) \"$scribeurl\")" >> $scribe_config
  echo "" >> $scribe_config
fi

#*---------------------------------------------------------------------*/
#*    Makefile.scrconfig                                               */
#*    -------------------------------------------------------------    */
#*    This part of the configure script produces the file              */
#*    Makefile.scrconfig. This file contains machine dependant         */
#*    informations and location where Bigloo is to be installed.       */
#*---------------------------------------------------------------------*/
if [ $action = "all" -o $action = "Makefile.scrconfig" ]; then

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

  # We check the C compiler
  cat > $name.c <<EOF
     int foobar( int x ) {
        return x;
  }
EOF

  if $cc $cflags -c $name.c >/dev/null 2>&1
  then
     true 
  else
     echo "***ERROR:configure:$cc:Can't compile c file -- $cc $cflags -c $name.c";
     /bin/rm -f $name.c $name.o $name.a; 
     exit 1
  fi
  /bin/rm -f $name.c $name.o $name.a; 

  # We first cleanup the file
  rm -f $makefile_config 2> /dev/null
  touch $makefile_config
  echo "## Scribe ($release) configure" >> $makefile_config
  echo >> $makefile_config

  # The release number
  echo "SCRIBERELEASE=$release" >> $makefile_config
  echo >> $makefile_config

  # The bigloo compiler
  echo "BIGLOO=$bigloo" >> $makefile_config
  echo "BFLAGS=$bflags" >> $makefile_config
  echo "BSAFEFLAGS=$bsafeflags" >> $makefile_config
  echo "BHEAPFLAGS=$bheapflags" >> $makefile_config
  echo "BJVMFLAGS=$bjvmflags" >> $makefile_config
  echo "AFILE=$afile" >> $makefile_config
  echo "JFILE=$jfile" >> $makefile_config
  echo "BTAGS=$btags" >> $makefile_config
  echo >> $makefile_config

  # The boot directory
  echo "SCRIBEDIR=`pwd`" >> $makefile_config
  echo >> $makefile_config

  # The bigloo bin directory
  echo "SCRIBEBINDIR=$bindir" >> $makefile_config
  echo >> $makefile_config
 
  # The bigloo lib directory
  echo "SCRIBELIBDIR=$scribelibdir" >> $makefile_config
  echo >> $makefile_config
 
  # The name of the library
  echo "LIBRARYSCRIBENAME=scribe" >> $makefile_config
  echo >> $makefile_config

  # The bigloo configuration
  echo "include $libdir/Makefile.config" >> $makefile_config
  echo >> $makefile_config

  # The doc directory
  if [ ! "$docdir " = " " ]; then
    echo "SCRIBEDOCDIR=$docdir" >> $makefile_config;
    echo >> $makefile_config
  fi

  # The man directory
  if [ ! "$mandir " = " " ]; then
    echo "SCRIBEMANDIR=$mandir" >> $makefile_config;
    echo >> $makefile_config
  fi
  
  # Http
  echo "HOSTHTTP=$scribehttphost" >> $makefile_config
  echo >> $makefile_config

  # mask
  echo "SMASK=$smask" >> $makefile_config
  echo >> $makefile_config
fi

#*---------------------------------------------------------------------*/
#*    Ok, we are done now                                              */
#*---------------------------------------------------------------------*/
if [ "$summary" = "yes" ]; then
  echo 
  echo 
  echo "** Configuration summary **"
  echo 
  echo "Release number:"
  echo "  Biglook release number................ $release"
  echo "  Minimum Bigloo version required....... $requiredbigloo"
  echo "  Installed version .................... $installedbigloo"
  echo
  echo "Compilers:"
  echo "  Bigloo................................ $bigloo"
  echo "  Bigloo compilation flags.............. $bflags"
  echo "  Bigloo safe link flags................ $bsafeflags"
  echo "  Bigloo heap flags..................... $bheapflags"
  echo "  afile................................. $afile"
  echo "  jfile................................. $jfile"
  echo "  btags................................. $btags"
  echo "  cc.................................... $cc"
  echo "  cc compilation flags.................. $cflags"
  echo "  link options.......................... $ldopt"
  echo 
  echo "Path:"
  echo "  Binary directory...................... $bindir"
  echo "  Library directory..................... $libdir"
  echo "  Scribe library directory.............. $scribelibdir"
  echo "  Scribe src directory.................. $scribescrdir"
  echo "  Documentation directory............... $docdir"
  echo "  Man pages directory................... $mandir"
  echo "  Home page............................. $scribeurl"
  echo 
  echo "Misc configuration:"
  echo "  mask for installed files.............. $smask"
  echo
fi
