#!/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, "STONE", Haid-und-Neu-Strasse 10-14,
# D-76131 Karlsruhe, Germany.
# --------------------------------------------------------------------------
# 'obst-cmp - 27:02:91 - Dietmar Theobald'
#
# obst-cmp [-v] [-E] [-I] [-L <language>] <file_prefix> [<schema>]
#
# Interface to the OBST compiler and host language generators. A compilation is
# carried out in two phases: In the first phase a schema file is processed and
# an schema object as an internal representation is generated which is placed in
# the root container. The second phase operates on this schema object and 
# produces a host language interface to the classes and types defined in the 
# schema.
#
# The "-v" option causes a report on the start of each compilation phase.
#
# The "-E" flag takes effect on the first phase by enabling an echo mode. This
# echo is generated by the lexical analyzer module of the OBST compiler and
# consists of the text of an token before this token is handed over to the parser
# module. The echo mode is intended to be used in debugging the OBST compiler.
# 
# The "-L" option specifies the host language interface to be generated. Up to
# now there exists only a C++ binding, which is also the default.
#
# <file_prefix> may be:
#    "^"	   : The first phase is not carried out.
#    "-"	   : Input is read from stdin.
#    another string: Input is read from "<file_prefix>.obst", or from
#		     "<file_prefix>.sos" if a file with suffix .obst does not
#		     exist.
# <schema> may be:
#    "^"	   : The second phase is not carried out.
#    another string: Output is generated for the named <schema>.
#		     The output is written to the current working directory.
# If <schema> is not given then <file_prefix> is also used as schema name. This
# is not possible if <file_prefix> is "^" or "-".
#
self='obst-cmp'

[ -n "$OBSTdir" ] || { OBSTdir="`cd \`dirname $0\`;pwd`/"; export OBSTdir; }

if [ -z "$OBSTCONTAINER" -a -z "$SOSCONTAINER" ] ; then
  echo >&2 "*** $self: environment variable OBSTCONTAINER not defined"
  exit 1
fi
    usage='[-E] [-I] [-L <language>] [-v] <file-prefix> [<schema-name>]'
  tmpfile=/tmp/$self.$$
generator='genCC'
  verbose=
   cfeopt=
   genopt=

while [ $# -gt 0 ]
do
   case "$1" in
      -E)    cfeopt='-E'                ;;
      -I)    genopt='-I'                ;;
      -L) generator="gen$2"; shift      ;;
      -v)   verbose='+'                 ;;
       -) break				;;
      -*) echo >&2 "*** usage: $self $usage"; exit 1;;
       *) break                         ;;  
   esac
   shift
done

if [ $# -eq 1 ] ; then
   [ "$1" = '^' -o "$1" = '-' ] && {
      echo >&2 "*** $self: no implicit schema name" ; exit 1; }

   if [ -r "$1".obst ]; then
      infile="$1".obst
   else
      infile="$1".sos
   fi
   schema="`basename $1`"

elif [ $# -eq 2 ] ; then
   if [ "$1" = '-'  -o "$1" = '^' ]; then
      infile="$1"
   elif [ -r "$1".obst ]; then
      infile="$1".obst
   else
      infile="$1".sos
   fi
   schema="$2"
else
   echo >&2 "*** usage: $self $usage"; exit 1
fi

[ "$infile" = '^' ] || {
   [ "$verbose" ] && echo "$self: starting compilation for $infile ..."

   if [ "$infile" = '-' ] ; then
      cat - > $tmpfile
			   
      ${OBSTdir}cfe $cfeopt $tmpfile || { rm $tmpfile; exit 1; }
      rm $tmpfile
   else
      ${OBSTdir}cfe $cfeopt $infile || exit 1
   fi
}
[ "$schema" = '^' ] || {
   [ "$verbose" ] && echo "$self: starting generation for $schema ..."

   rm -f ${schema}_sos.[Cch] ${schema}_obst.[Cch] ${schema}_use.h
   
   ${OBSTdir}$generator $genopt $schema || exit 1
   }

exit 0
