#!/bin/sh  
# --------------------------------------------------------------------------
# Copyright 1991 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 Projekt", Haid-und-Neu-Strasse 10-14,
# D-76131 Karlsruhe, Germany.
# --------------------------------------------------------------------------
# 'obst-CC - 03/09/91 - Dietmar Theobald'
#
# obst-CC <command> [-o <object_file>] [-schema <schema>]
#		    [-no_scp] [-keep_scp] [-no_sil]
#		    <args>...
#
# 'obst-CC' performs C++ compilations of OBST method implementations and schema
# interface modules.
# The actual compilation is done by executing the arguments with the
# modifications listed below, i.e. the first argument is expected to be the
# name of the C++ compiler.
# After a successful compilation the generated object file is associated with
# a schema using 'obst-sil'. To this end an object file must be named on the
# command line by "-o <object_file>".
# The call of obst-sil can be suppressed by using the option '-no_sil'.
#
# Two kinds of source files may be compiled with 'obst-CC' (the suffixes '.C',
# '.cc' can be used in place of '.c'):
#  - schema interface modules ("*_obst.c", "*_obst.c"), and
#  - method implementations ("*.c", "*.cc", "*.C").
#
# The corresponding schema is given either explicitly using the '-schema'
# option, or implicitly by looking for a single schema file in the directory
# containing "*_sos.c", "*_obst.c", or "*.c", respectively.
#
# In case of compiling method implementations contained in '<p>.c' an
# intermediary source file '<p>_scp.c' is maintained in the same directory as
# '<p>.c'. This intermediary file is regenerated by the OBST preprocessor
# 'obst-scp' each time '<p>.c' changes, or if '<p>_scp.c' does not exist.
# (The same holds analogously for '<p>.C', '<p>.cc'.)
#
# The intermediary file is used as input for the compilation.
# That preprocessing can be suppressed by the option '-no_scp'.
# If the option '-keep_scp' is enabled, the file '<p>_scp.c' is kept after
# the compilation, removed otherwise.
#
self='obst-CC'

if [ -z "$OBSTCONTAINER" -a -z "$SOSCONTAINER" ] ; then
   echo >&2 "*** $self: environment variable OBSTCONTAINER not defined"
   exit 1
fi

 tmp_scp='+'
  no_scp=
  no_sil="$SOSC_BOOTING"
  infile=
 outfile=
  schema=
del_file="___$$___" 	# set to non-existing name

# internal remark: no -v option since this would discard any such parameters
#		   which are intended to be passed to the compiler.

while [ $# -gt 0 ] ; do
   case "$1" in
     -no_scp)       no_scp='+' ;;
     -no_sil)       no_sil='+' ;;
     -keep_scp)     tmp_scp='' ;;

     -o)	    [ "$outfile" ] && {
                       echo >&2 "*** $self: multiple -o options"; exit 1
                       }
                    shift ; outfile="$1"
		    ;;
     -schema)	    [ "$schema"  ] && {
                       echo >&2 "*** $self: multiple -schema options"; exit 1
                       }
      	            shift ; schema="$1"
		    ;;
     *_obst.[Cc]|\
     *_obst.cc  |\
     *_sos.cc   |\
     *_sos.[Cc]   ) [ "$infile"  ] && {
		       echo >&2 "*** $self: multiple source files"; exit 1
		       }
		    infile="$1"
		    ;;
     *.[Cc] |\
     *.cc     )	    [ "$infile"  ] && {
                       echo >&2 "*** $self: multiple source files"; exit 1
                       }
      	              infile="$1"
		      suffix=`echo "$1" | sed 's|.*\.\([^.]*\)|\1|'`
      	            call_scp='+'
		    ;;
     *)		    args="$args '$1'";_args="$_args $1";
		    ;;
   esac
   shift
done
[ "$no_scp"    ] && { call_scp=''; tmp_scp='' ;}
[ "$infile"    ] || { echo >&2 "*** $self: source file missing"; exit 1 ;}
[ "$outfile"   ] || { echo >&2 "*** $self: object file missing"; exit 1 ;}
[ -r "$infile" ] || { echo >&2 "*** $self: source file $infile not found"
		      exit 1 ;}

in_dir=`dirname "$infile"`
[ -n "$schema" -o \( -n "$no_scp" -a -n "$no_sil" \) ] || {
   for f in $in_dir/*.obst $in_dir/*.sos
   do
      [ -f "$f" ] 2> /dev/null && {
	 [ "$schema" ] && {
	    echo >&2 "*** $self: several OBST schema files in $in_dir"; exit 1
	 }
	 schema=`echo "/$f" | sed 's|.*/\(.*\)\..*|\1|'`
      }
   done
   [ "$schema" ] || {
      echo >&2 "*** $self: no OBST schema file in $in_dir"; exit 1; }
}
   
[ "$call_scp" ] && {
   im_file=`echo "$infile" | sed "s|\(.*\)\.$suffix|\1|"`_scp.$suffix
     
   if [ "$tmp_scp" ] ; then
      del_file="$im_file"
   else
      set `ls -L -t $infile $im_file 2> /dev/null` ""
      [ "$1" = "$infile" ] || call_scp=''
   fi
   [ "$call_scp" ] && { echo "+ obst-scp $schema -o $im_file $infile"
   			obst-scp $schema -o $im_file $infile || exit 1; }
   infile="$im_file"
}

if [ "$OBSTHOSTS" ] ; then
   obst-host -v $_args -o $outfile $infile
else
   eval `echo "echo + $args -o $outfile $infile"`
   eval `echo $args -o $outfile $infile`
fi

status=$?
[ $status -eq 0  -a  -z "$no_sil" ] && {
   echo "+ obst-sil $schema -a $outfile"
   obst-sil $schema -a $outfile || { rm -f $outfile $del_file; exit 1 ;}
   }
rm -f $del_file

exit $status
