#!/bin/sh
# --------------------------------------------------------------------------
# Copyright 1992 by Forschungszentrum Informatik (FZI)
#
# You can use and distribute this software under the terms of the license
# version 1 you should have received along with this software.
# If not or if you want additional information, write to
# Forschungszentrum Informatik, "OBST", Haid-und-Neu-Strasse 10-14,
# D-76131 Karlsruhe, Germany.
# --------------------------------------------------------------------------
# 'obst-boot - 05:06:91 - Dietmar Theobald'
#              18:10:93 - changed to autoconf by Oliver Spatscheck
#
# obst-boot
#
# The OBST version is booted, i.e. its compiler is bootstrapped and the version
# is (re-)generated using this compiler.
#
# The compiler bootstrap is done in several phases, each phase consists of
# three steps:
#  - initializing the container directory,
#  - compiling all OBST schemas which are classified as boot schemas,
#  - generating a boot version of the version's compiler using the previously
#    generated schema files.
# The last step is not performed if the generated schema files did not change
# with respect to the previous phase. This signals also that the bootstrap of
# a boot compiler is completed.
#
# The final phase consists of the complete generation of the version using the
# previously generated boot compiler.

set -e

# It is currently not necessary to run scp and compare results (bs) 4.5.94
do_scp=''

      phase='0'
[ -n "$OBSTrootdir" ] || { OBSTrootdir="`cd \`dirname $0\`/..;pwd`/"; export OBSTrootdir; }
bootschemas="knl agg dir mta prot cci sync trans ext_sync"

OBSTCONTAINER=$OBSTrootdir/cnt ; export OBSTCONTAINER
SOSC_BOOTING='+'	      ; export SOSC_BOOTING	# old stuff
PATH="$OBSTrootdir/bin:$PATH"  ; export PATH	

cd $OBSTCONTAINER
rm -f *.obst *.sos
for s in $bootschemas
do
   ln -s $OBSTrootdir/src/$s/${s}.obst .
done

cd $OBSTrootdir

# remove the following line when porting to a new platform
# where you have to restart obst-boot several times !!!!!!!!!
rm -f lib/*.o

echo "--- PHASE $phase: make boot_compiler"
make -r boot_compiler CPP_BOOT=-DBOOT

while true
do
   phase=`expr $phase + 1`
   cd $OBSTCONTAINER

   echo "--- PHASE $phase: initialize" ; obst-init

   for s in $bootschemas
   do
      echo "--- PHASE $phase: compile $s" ; obst-cmp $s
   done

   differs=''
   for s in $bootschemas
   do
      s_differs=''
      for f in ${s}*.[Cch]
      do
	 cmp -s $f $OBSTrootdir/src/$s/$f || { s_differs='+'
				               mv -f $f $OBSTrootdir/src/$s/$f
					     }
      done
      [ "$s_differs" ] && { echo "--- PHASE $phase: schema $s differs"
                            differs="$differs $s"
                          }
   done

   if [ "$do_scp" ] ; then
      cd $OBSTrootdir/src
      for s in $differs
      do
         for f in $s/*.[Cc]
         do
            case "$f" in
                  *_sos.[Cc] | *_scp.[Cc]  | *_obst.[cC] \
                             | *_err.[cC]  | *_main.[cC]) continue;;
            esac
   
            echo "--- PHASE $phase: obst-scp $f"
            obst-scp $s $OBSTrootdir/src/$f
         done
      done
   fi
 
   [ "$differs" ] || break

   echo "--- PHASE $phase: make boot_compiler"
   cd $OBSTrootdir
   make -r boot_compiler CPP_BOOT=-DBOOT
done

cd $OBSTCONTAINER; rm -f *.sos *.obst *.[Cch] *.out

echo "--- PHASE 999: remove auxiliary boot files"
cd $OBSTrootdir
rm -f `egrep -l BOOT src/*/*.[Cc] \
	  | sed -e 's|src/[^/]*/\([^ ]*\.\)[Cc]|lib/\1o|'` \
      BOOT.necessary
rm -f bin/init
