#!/bin/csh -f
# Runs ASN.1 compiler, attribute compiler and GDMO compiler
# to produce C++ source for OSIMIS.
#
# Saleem
# November 1994

#
# asnComp, gdmo
#
set asnComp = "$OSIMISETCPATH/../compilers/asncompiler/asn-cmpl"
set gdmo    = "$OSIMISETCPATH/../compilers/mocompiler/gdmo/gdmo"

set msgHdr  = "-*- gdmoasn:"
set errHdr  = "*** gdmoasn - ERROR:"

if (!($?OSIMISETCPATH)) then
    echo "$msgHdr set environment variable OSIMISETCPATH then try again"
    exit 1
endif

# Set the value of GDMODIR 
setenv GDMODIR $OSIMISETCPATH/gdmodir

if ($#argv == 0 || $#argv == 3 || $#argv == 5 || $#argv >= 7 || \
    ($#argv == 4 && !("$3" == "-s" || "$3" == "-m")) || \
    ($#argv == 6 && !("$3" == "-s" && "$5" == "-m") \
		 && !("$3" == "-m" && "$5" == "-s"))) then
    echo "usage: gdmoasn-cmpl <ASN-file> <GDMO-file> \\
       [-s <GDMO.syntax.dat-file>] [-m <GDMO.mo.dat-file>]"
    exit 1
endif

# Just say which etc directory is begin used ...
echo "$msgHdr etc directory - $OSIMISETCPATH"
echo ""

set asnFile	= $1
set gdmoFile	= $2
set attrFile	= "attr.tmp"
set errorFile	= "gdmo-errors.log"

# set optional command line files

if ($#argv == 4) then
    if ("$3" == "-s") then
        set sntxdatFile = $3
    else	# "$3" == "-m"
        set modatFile = $3
    endif
else \
if ($#argv == 6) then
    if ("$3" == "-s") then
        set sntxdatFile = $4
        set modatFile = $6
    else	# "$3" == "-m"
        set modatFile = $4
        set sntxdatFile = $6
    endif
endif

echo "$msgHdr generating table of required attribute syntaxes from GDMO ..."
$gdmo -x dumpsyntax.gen $gdmoFile >& $errorFile

if ($status != 0) then
    echo "$errHdr gdmo failed - check $gdmoFile"
    echo "$msgHdr errors are logged in ${errorFile}"
    exit 1
endif

# sort and uniq the syntaxes and make the control file for the asn compiler

sort < gdmo.log > syntax.sort
uniq < syntax.sort > attr.syntaxes
rm syntax.sort

# we want to avoid generating code for syntaxes in <GDMO.syntax.dat-file>,
# so we have to remove them from attr.syntaxes

if ($#argv == 3) then
    gawk '{ if (! index($1, "#")) print $1 }' $sntxdatFile > sntx.names.unsorted
    sort < sntx.names.unsorted > sntx.names
    uniq < sntx.names > sntxdat.syntaxes # should not be needed, for safety

    comm -23 attr.syntaxes sntxdat.syntaxes > attr.syntaxes.tmp
    mv attr.syntaxes.tmp attr.syntaxes
    rm sntx.names sntx.names.unsorted sntxdat.syntaxes
endif

awk '/::=/ { print $1 }' $asnFile > asn.names.unsorted
sort < asn.names.unsorted > asn.names
comm -12 asn.names attr.syntaxes > $attrFile
rm attr.syntaxes asn.names.unsorted asn.names

echo "$msgHdr ... ok"
echo ""
echo "$msgHdr generating required attribute classes ..."
if (! { $asnComp $asnFile $attrFile }) then
    echo "$errHdr asn compiler failed"
    exit 1
endif
echo "$msgHdr ... ok"

rm $attrFile

echo ""
echo "$msgHdr generating C++ for OSIMIS from GDMO ..."
if (! { $gdmo $2 $3 $4 $5 $6 }) then
    echo ""
    echo "$errHdr error trying to generate GDMO"
    exit 1
endif
echo "$msgHdr ... ok"
echo ""
echo "$msgHdr generation of code completed."

exit 0
