#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/distribution/configure ...           */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Tue Jan 25 16:05:10 1994                          */
#*    Last change :  Wed Jan 25 08:23:14 1995 (serrano)                */
#*    -------------------------------------------------------------    */
#*    The Bigloo's configuration file                                  */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    Some user variable                                               */
#*---------------------------------------------------------------------*/
cc="gcc"
indent="/usr/local/bin/indent"
indent_opt="-npro -nbap -nbc -bl -ncdb -nce -nfc1 -ip0 -nlp -npcs -nsc -nsob -cli0.5 -di0 -l80 -d1 -c0 -st"
cuflags="-O"

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

#*---------------------------------------------------------------------*/
#*    Some system variable                                             */
#*---------------------------------------------------------------------*/
site="`pwd`"
version="1.7"

tools_dir="$site/tools"
comptime_dir="$site/runtime$version"
runtime_dir="$site/comptime$version"
recette_dir=$site/recette
lib_dir="$site/lib/$version"
bin_dir="$site/bin"

cflags="-O -I$lib_dir" 

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;
    --cc=*|-cc=*)
      cc="`echo $1 | sed 's/^[-a-z]*=//'`";;
    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

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

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 $comptime_dir"
  exit 1;
fi

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

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

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

echo ". compiling tools"

$cc $cflags beep.c -o beep || exit $?
$cc $cflags dirname.c -o dirname || exist $?
$cc $cflags arch.c -o arch || exist $?

cd "$site"

#*---------------------------------------------------------------------*/
#*    We check the architecture                                        */
#*---------------------------------------------------------------------*/
arch=`$tools_dir/arch`

echo ". checking architecture"

if ( test $arch = "unknown" ); then
  echo "Bigloo have not been compiled on such architecture, sorry"
  exit 1;
fi

#*---------------------------------------------------------------------*/
#*    We generate the good `bigloo.h' file.                            */
#*---------------------------------------------------------------------*/
echo ". generating bigloo.h file"

\rm -f $lib_dir/bigloo.h

cat distribution/bigloo.h.head > $lib_dir/bigloo.h

echo \#define LIBRARY_DIR"()" \"$lib_dir\" >>  $lib_dir/bigloo.h
echo \#define INCLUDE_DIR"()" \"$lib_dir\" >>  $lib_dir/bigloo.h
echo \#define CC"()"  \"$cc\" >> $lib_dir/bigloo.h

cat distribution/bigloo.h.tail >> $lib_dir/bigloo.h

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

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/boot $site/m
  cat $site/m | sed -e s,"cuflags=.*","cuflags=\"$cuflags\"", > $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
fi

#*---------------------------------------------------------------------*/
#*    We modifiy the comptime1.7/Engine/param.{c,scm} files if the     */
#*    system is solaris to add the ucb lib.                            */
#*---------------------------------------------------------------------*/
ucblib=/usr/ucblib/libucb.a
if( test $arch = "sun4-solaris" ); then

  echo ". checking libucb"
  if test ! -f $ucblib; then
    echo "Can't find $ucblib library"
   exit 1;
  fi

  echo ". updating comptime1.7/Engine/param.{c,scm} files for solaris"
  ( cd $site/comptime1.7/Engine;

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

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

#*---------------------------------------------------------------------*/
#*    Checking indent                                                  */
#*---------------------------------------------------------------------*/
echo ". checking $indent"
do_indent=1

if( test ! -f $indent ); then
  do_indent=0
else
  ( cd /tmp; 
    echo "main( int argc, char *argv[] ) { return 1; }" > bigloo-foo.c;
    $indent $indent_opt bigloo-foo.c > bigloo-bar.c || exit $? ) ||
    do_indent=0
fi

if( test ! -f /tmp/bigloo-bar.c ); then
  do_indent=0
fi

\rm -f /tmp/bigloo-bar.c; 
\rm -f /tmp/bigloo-foo.c;

if( test "$do_indent" = "0" ); then
  echo "   ***ERROR: $indent (ignored)"

  ( cd $site/comptime1.7/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 $site/comptime1.7/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

#*---------------------------------------------------------------------*/
#*    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 "-------------------"
