#!/bin/sh
#*=====================================================================*/
#*    serrano/tmp/bigloo1.8/configure                                  */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Tue Jan 25 16:05:10 1994                          */
#*    Last change :  Thu Apr 18 10:10:16 1996 (serrano)                */
#*    -------------------------------------------------------------    */
#*    The Bigloo's configuration file                                  */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    User variables                                                   */
#*---------------------------------------------------------------------*/
cc=gcc
bindir=/usr/local/bin
libdir=/usr/local/lib
mandir=/usr/local/man/man1
indent=/usr/local/bin/indent
indent_opt="-npro -bap -bad -nbc -bl -ncdb -nce -nfc1 -ip0 -nlp -npcs -nsc -nsob -cli0.5 -di0 -l80 -d1 -c0 -ts2 -st"

#*---------------------------------------------------------------------*/
#*               !!! DON'T EDIT AFTER THIS COMMENT !!!                 */
#*---------------------------------------------------------------------*/

#*---------------------------------------------------------------------*/
#*    Some system variable                                             */
#*---------------------------------------------------------------------*/
site=`pwd`
version=1.8
cversion=0.2
default_lib_dir=/udir/serrano/prgm/project/bigloo/lib/1.8

tools_dir="$site/tools"
runtime_dir="$site/runtime$version"
comptime_dir="$site/comptime$version"
cigloo_dir="$site/cigloo$cversion"
recette_dir=$site/recette
config_dir=$site/config
lib_dir="$site/lib/$version"
bin_dir="$site/bin"
dist_dir="$site/distribution"

cflags="-O"

#*---------------------------------------------------------------------*/
#*    Checking                                                         */
#*---------------------------------------------------------------------*/
echo ". checking directories"

if test ! -d $comptime_dir; then
  echo "no such directory $comptime_dir"
  exit 1;
fi

if test ! -d $runtime_dir; then
  echo "no such directory $runtime_dir"
  exit 1;
fi

if test ! -d $lib_dir; then
  echo "no such directory $lib_dir"
  exit 1;
fi

if test ! -d $bin_dir; then
  echo "no such directory $bin_dir"
  exit 1;
fi

#*---------------------------------------------------------------------*/
#*    settings permissions ...                                         */
#*---------------------------------------------------------------------*/
chmod u+x $config_dir/autoconf/*

#*---------------------------------------------------------------------*/
#*    We check `as'                                                    */
#*---------------------------------------------------------------------*/
echo ". checking as"

( cd /tmp; 
  echo "" > foo.s; 
  as foo.s -o foo.o > /dev/null 2> /dev/null || exit $? ) || exit $? 

#*---------------------------------------------------------------------*/
#*    We check `ar'                                                    */
#*---------------------------------------------------------------------*/
echo ". checking ar"

( cd /tmp; 
  ar qcv foo.a foo.o > /dev/null 2> /dev/null || exit $? ) || exit $? 

#*---------------------------------------------------------------------*/
#*    First we have to compile the tools files                         */
#*---------------------------------------------------------------------*/
cd "$tools_dir"

echo ". compiling tools"

echo "   beep"
$cc $cflags beep.c -o beep || exit $?
echo "   dirname"
$cc $cflags dirname.c -o dirname || exist $?
echo "   if_mach"
$cc $cflags if_mach.c -o if_mach || exist $?
echo "   if_not_there"
$cc $cflags if_not_there.c -o if_not_there || exist $?

echo "   GC_if_mach"
$cc $cflags GC_if_mach.c -I$runtime_dir/Gc -o GC_if_mach || exist $?
echo "   GC_if_not_there"
$cc $cflags GC_if_not_there.c -I$runtime_dir/Gc -o GC_if_not_there || exist $?

cd "$site"

#*---------------------------------------------------------------------*/
#*    We modify the makefiles                                          */
#*---------------------------------------------------------------------*/
echo ". updating makefiles"

cp $site/distribution/boot.orig $site/distribution/boot
cp $site/distribution/install.orig $site/distribution/install
cp $runtime_dir/Makefile.orig $runtime_dir/Makefile
cp $comptime_dir/Makefile.orig $comptime_dir/Makefile
cp $recette_dir/Makefile.orig $recette_dir/Makefile
cp $site/Makefile.orig $site/Makefile
cp $site/distribution/install.orig $site/distribution/install
cp $cigloo_dir/Makefile.orig $cigloo_dir/Makefile
cp $runtime_dir/Include/bigloo.h.orig $runtime_dir/Include/bigloo.h
cp $comptime_dir/Engine/param.c.orig $comptime_dir/Engine/param.c
cp $comptime_dir/Engine/param.scm.orig $comptime_dir/Engine/param.scm

if( test ! "gcc" = "$cc" ); then
  mv $runtime_dir/Makefile $runtime_dir/m
  cat $runtime_dir/m | sed -e s,"gcc",$cc, > $runtime_dir/Makefile
  \rm -f $runtime_dir/m

  mv $comptime_dir/Makefile $comptime_dir/m
  cat $comptime_dir/m | sed -e s,"gcc",$cc, > $comptime_dir/Makefile
  \rm -f $comptime_dir/m

  mv $recette_dir/Makefile $recette_dir/m
  cat $recette_dir/m | sed -e s,"gcc",$cc, > $recette_dir/Makefile
  \rm -f $recette_dir/m

  mv $site/Makefile $site/m
  cat $site/m | sed -e s,"gcc",$cc, > $site/Makefile
  \rm -f $site/m

  mv $site/distribution/boot $site/m
  cat $site/m | sed -e s,"gcc",$cc, > $site/distribution/boot
  \rm -f $site/m

  mv $site/distribution/install $site/m
  cat $site/m | sed -e s,"gcc",$cc, > $site/distribution/install
  \rm -f $site/m

  (cd comptime$version
   mv $comptime_dir/Engine/param.c $comptime_dir/Engine/p.c
   cat $comptime_dir/Engine/p.c | sed -e s,"gcc",$cc, > $comptime_dir/Engine/param.c
   rm $comptime_dir/Engine/p.c
  
   mv $comptime_dir/Engine/param.scm $comptime_dir/Engine/p.scm
   cat $comptime_dir/Engine/p.scm | sed -e s,"gcc",$cc, > $comptime_dir/Engine/param.scm
   rm $comptime_dir/Engine/p.scm)
fi

mv $site/distribution/boot $site/m
cat $site/m | sed -e s,"cuflags=.*","cuflags=\"$cflags\"", > $site/distribution/boot
\rm -f $site/m

mv $cigloo_dir/Makefile $site/m
cat $site/m | sed -e s,"the-bin-dir",$bindir, > $cigloo_dir/Makefile
\rm -f $site/m


#*---------------------------------------------------------------------*/
#*    We modify the distribution/install file.                         */
#*---------------------------------------------------------------------*/
mv $site/distribution/install $site/i
cat $site/i | sed -e s,"the-bin-dir",$bindir, > $site/distribution/install
\rm -f $site/i

mv $site/distribution/install $site/i
cat $site/i | sed -e s,"the-lib-dir",$libdir, > $site/distribution/install
\rm -f $site/i

mv $site/distribution/install $site/i
cat $site/i | sed -e s,"the-man-dir",$mandir, > $site/distribution/install
\rm -f $site/i
 
#*---------------------------------------------------------------------*/
#*    We modify the current default path in Engine/param.{scm,c}       */
#*---------------------------------------------------------------------*/
(cd comptime$version

  mv $comptime_dir/Engine/param.c $comptime_dir/Engine/p.c
  cat $comptime_dir/Engine/p.c | sed -e s,$default_lib_dir,$libdir/bigloo/$version, > $comptime_dir/Engine/param.c
  rm $comptime_dir/Engine/p.c

  mv $comptime_dir/Engine/param.scm $comptime_dir/Engine/p.scm
  cat $comptime_dir/Engine/p.scm | sed -e s,$default_lib_dir,$libdir/bigloo/$version, > $comptime_dir/Engine/param.scm
  rm $comptime_dir/Engine/p.scm)

#*---------------------------------------------------------------------*/
#*    System configuration                                             */
#*---------------------------------------------------------------------*/
echo ". system configuration"

#*---------------------------------------------------------------------*/
#*    The ranlib check                                                 */
#*---------------------------------------------------------------------*/
if( test `$config_dir/autoconf/ranlib --cc=$cc` = "0" ); then
  echo "   ranlib: no"
  mv $runtime_dir/Makefile $runtime_dir/m
  cat $runtime_dir/m | sed -e s,"ranlib",true, > $runtime_dir/Makefile
  \rm -f $runtime_dir/m

  mv $dist_dir/boot $dist_dir/b
  cat $dist_dir/b | sed -e s,"ranlib",true, > $dist_dir/boot
  \rm -f $dist_dir/b

  mv $dist_dir/install $dist_dir/i
  cat $dist_dir/i | sed -e s,"ranlib",true, > $dist_dir/install
  \rm -f $dist_dir/i
else
  echo "   ranlib: yes"
fi

#*---------------------------------------------------------------------*/
#*    The bigloo.h configuration                                       */
#*---------------------------------------------------------------------*/
cp $runtime_dir/Include/bigloo.h $lib_dir/bigloo.h.old

#*--- stack -----------------------------------------------------------*/
stackgrows=`$config_dir/autoconf/stackdown --cc=$cc`
if( test $stackgrows = "1" ); then
  echo "   stack grows: down"
  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_STACK_GROWS_DOWN","   1", > $lib_dir/bigloo.h.new
else
  echo "   stack grows: up"
  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_STACK_GROWS_DOWN","   0", > $lib_dir/bigloo.h.new
fi
\rm -f $lib_dir/bigloo.h.old
mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old

#*--- alignment -------------------------------------------------------*/
alignment=`$config_dir/autoconf/alignment --cc=$cc`
echo "   ptr alignment: " $alignment

cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_PTR_ALIGNMENT","   $alignment", > $lib_dir/bigloo.h.new
mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old

#*--- setjmp ----------------------------------------------------------*/
prefix=`$config_dir/autoconf/setjmp --cc=$cc`
setjmp="$prefix"setjmp
longjmp="$prefix"longjmp

echo "   setjmp: $setjmp longjmp: $longjmp"
cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_SETJMP","   $setjmp", > $lib_dir/bigloo.h.new
\rm -f $lib_dir/bigloo.h.old
mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old
cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_LONGJMP","   $longjmp", > $lib_dir/bigloo.h.new
\rm -f $lib_dir/bigloo.h.old
mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old

#*--- endianness ------------------------------------------------------*/
#* endian=`$config_dir/autoconf/bigendian --cc=$cc`                    */
#* if( test $endian = "0" ); then                                      */
#*   echo "   endian: little"                                          */
#*   cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_LITTLE_ENDIAN","   1", > $lib_dir/bigloo.h.new */
#* else                                                                */
#*   echo "   endian: big"                                             */
#*   cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_LITTLE_ENDIAN","   0", > $lib_dir/bigloo.h.new */
#* fi                                                                  */
#* \rm -f $lib_dir/bigloo.h.old                                        */
#* mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old                      */

#*--- bcopy -----------------------------------------------------------*/
bcopy=`$config_dir/autoconf/bcopy --cc=$cc`
if( test $bcopy = "1" ); then
  echo "   bcopy: yes"
  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_HAVE_BCOPY","   1", > $lib_dir/bigloo.h.new
else
  echo "   bcopy: no"
  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_HAVE_BCOPY","   0", > $lib_dir/bigloo.h.new
fi
\rm -f $lib_dir/bigloo.h.old
mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old

#*--- window register -------------------------------------------------*/
window=`$config_dir/autoconf/winreg --cc=$cc`
echo "   nb window register: " $window

cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_NB_WINDOW_REGISTER","   $window", > $lib_dir/bigloo.h.new
\rm -f $lib_dir/bigloo.h.old
mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old

#*---------------------------------------------------------------------*/
#*    updating Engine/param.{c,scm}                                    */
#*---------------------------------------------------------------------*/
#*--- alloca ----------------------------------------------------------*/
alloca=`$config_dir/autoconf/alloca --cc=$cc`
if( test $alloca = "1" ); then
  echo "   alloca: yes"
  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_HAVE_ALLOCA","   1", > $lib_dir/bigloo.h.new
else
  echo "   alloca: no"
  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_HAVE_ALLOCA","   0", > $lib_dir/bigloo.h.new
  mv $comptime_dir/Engine/param.c $comptime_dir/Engine/p.c
  cat $comptime_dir/Engine/p.c | sed -e s,"ALLOCA","NONE", > $comptime_dir/Engine/param.c
  rm $comptime_dir/Engine/p.c
  mv $comptime_dir/Engine/param.scm $comptime_dir/Engine/p.scm
  cat $comptime_dir/Engine/p.scm | sed -e s,"ALLOCA","NONE", > $comptime_dir/Engine/param.scm
  rm $comptime_dir/Engine/p.scm
fi
\rm -f $lib_dir/bigloo.h.old
mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old

#*--- libucb.a --------------------------------------------------------*/
ucb=/usr/ucblib/libucb.a
sigsetmask=`$config_dir/autoconf/sigsetmask --cc=$cc --ucb=$ucb`

if( test $sigsetmask = "0" ); then
  echo "   sigsetmask not found (^C disabled in the interpreter)"
  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_HAVE_SIGSETMASK","   0", > $lib_dir/bigloo.h.new
  rm $lib_dir/bigloo.h.old
  mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old
elif( test $sigsetmask = "1" ); then
  echo "   sigsetmask found"
  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_HAVE_SIGSETMASK","   1", > $lib_dir/bigloo.h.new
  rm $lib_dir/bigloo.h.old
  mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old
else
  echo "   sigsetmask found in [$ucb]"

  cat $lib_dir/bigloo.h.old | sed -e s,"   CURRENT_HAVE_SIGSETMASK","   1", > $lib_dir/bigloo.h.new
  rm $lib_dir/bigloo.h.old
  mv $lib_dir/bigloo.h.new $lib_dir/bigloo.h.old

  ( cd $comptime_dir/Engine;

    mv param.scm param.scm.old;
    cat param.scm.old | sed -e s,"-lm","-lm $ucb", > param.scm
    \rm -f param.scm.old;

    mv param.c param.c.old;
    cat param.c.old | sed -e s,"-lm","-lm $ucb", > param.c
    \rm -f param.c.old )

  ( cd $dist_dir;

    mv boot boot.old;
    cat boot.old | sed -e s,"-lm","-lm $ucb", > boot
    \rm -f boot.old )
fi

#*---------------------------------------------------------------------*/
#*    Stop the settings of Bigloo.h                                    */
#*---------------------------------------------------------------------*/
mv $lib_dir/bigloo.h.old $lib_dir/bigloo.h

#*---------------------------------------------------------------------*/
#*    Checking indent                                                  */
#*---------------------------------------------------------------------*/
do_indent=1

if( test `$config_dir/autoconf/indent --cc=$cc --cflags=$cflags --indent=$indent --indentflags="$indent_opt"` != "1" ); then
  do_indent=0
fi

if( test "$do_indent" = "0" ); then
  echo "   $indent: no"

  ( cd $comptime_dir/Engine;

    mv param.scm param.scm.old;
    cat param.scm.old | sed -e s,"/usr/local/bin/indent","#f", > param.scm
    \rm -f param.scm.old;

    mv param.c param.c.old;
    cat param.c.old | sed -e s,"/usr/local/bin/indent","", > param.c
    \rm -f param.c.old )  
else
  echo "   $indent ok ... setting."

  ( cd $comptime_dir/Engine;

    mv param.c param.c.old;
    cat param.c.old | sed -e s,"/usr/local/bin/indent",$indent, > param.c
    \rm -f param.c.old;

    mv param.c param.c.old;
    cat param.c.old | sed -e s,"-npro -bap -bad -nbc -bl -ncdb -nce -nfc1 -ip0 -nlp -npcs -nsc -nsob -cli0.5 -di0 -l80 -d1 -c0 -ts2 -st","$indent_opt", > param.c
 
    \rm -f param.c.old )
fi

#*---------------------------------------------------------------------*/
#*    Installing runtime/Include/bigloo.h                              */
#*---------------------------------------------------------------------*/
(cd $runtime_dir/Include; rm -f bigloo.h; ln -s $lib_dir/bigloo.h .)

#*---------------------------------------------------------------------*/
#*    clean /tmp                                                       */
#*---------------------------------------------------------------------*/
(cd /tmp; \rm -f foo.a foo.o foo.s foo.c)

#*---------------------------------------------------------------------*/
#*    We change the flags of the two files distribution/{boot,install} */
#*---------------------------------------------------------------------*/
echo ". changing files modes"

chmod a+rx distribution/boot
chmod a+rx distribution/install

#*---------------------------------------------------------------------*/
#*    Ok it is finished                                                */
#*---------------------------------------------------------------------*/
echo "configuration done."
echo "-------------------"
