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

#*---------------------------------------------------------------------*/
#*    Some user variable                                               */
#*---------------------------------------------------------------------*/
cc="gcc"

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

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/install $site/m
  cat $site/m | sed -e s,"gcc",$cc, > $site/distribution/install
  \rm -f $site/m
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 "-------------------"
