#! /bin/csh -f
#   DIGITAL EQUIPMENT CORPORATION  CONFIDENTIAL AND PROPRIETARY
#   Last modified on Sun Jun  5 16:17:39 PDT 1994 by mhb    
#        modified on Fri Jun  3 10:13:04 PDT 1994 by heydon 
#        modified on Mon Jan 24 13:16:06 PST 1994 by najork 
#        modified on Wed Jul 15 16:32:45 PDT 1992 by johnh
#        modified on Fri May 22 17:00:20 PDT 1992 by muller
#        modified on Fri Dec 30 10:52:31 PST 1988 by brooks


#set DIR="."			# for testing
#set DIR="/-/com/dec/src/udir/johnh/m3packages/m3zume/src"   # for testing
set DIR="/udir/mhb/m3/m3zume/src"
set DIR=BBB

# The -GEF flag is no longer supported.
if ( xxx$1 == xxx-GEF ) then 
  echo 'The -GEF option is no longer supported; try "man m3zume"'
  exit 1
endif

# If the first arg is "-testdir", then the second arg is the directory
# that contains the template files.
if ( xxx$1 == xxx-testdir ) then
  shift
  set DIR=$1
  shift
endif

set me="m3zume"
set mode = ""
set base = ""
if ( xxx$1 == xxx-3D ) then 
  set mode=`echo $1 | sed s/-//`
  set me=m3zume:${mode}
  shift
else if ( xxx$1 == xxx-Juno ) then
  set mode=`echo $1 | sed s/-//`
  set me=m3zume:${mode}
  shift
  set base=$1
  shift
else if ( ( xxx$1 == xxx-Obliq ) || ( xxx$1 == xxx-Obliq3D ) ) then 
  set mode=`echo $1 | sed s/-//`
  set me=m3zume:${mode}
  shift
  set file=$1
  set base=`basename $1`
  shift
endif

# Now the first arg is the name of the algorithm file.
set algfile=$1
set alg=`basename $1`

if ($mode == "") then
set WORK=(algtem.i3 ${alg}AlgClass.i3 .i3 \
          algtem.m3 ${alg}AlgClass.m3 .m3 \
          viewtem.i3 ${alg}ViewClass.i3 .i3 \
          viewtem.m3 ${alg}ViewClass.m3 .m3 \
          ietem.i3 ${alg}IE.i3 .i3 \
          ietem.m3 ${alg}IE.m3 .m3 \
          datatem.i3 ${alg}DataView.i3 .i3 \
          datatem.m3 ${alg}DataView.m3 .m3 \
          transtem.i3 ${alg}TranscriptView.i3 .i3 \
          transtem.m3 ${alg}TranscriptView.m3 .m3 \
          transtem.fv ${alg}TranscriptView.fv .fv \
          edtem.fv ${alg}EventData.fv .fv) 
else if ($mode == "3D") then
set WORK=(algtem.i3 ${alg}AlgClass.i3 .i3 \
          algtem.m3 ${alg}AlgClass.m3 .m3 \
          viewtem.i3 ${alg}ViewClass.i3 .i3 \
          viewtem.m3 ${alg}ViewClass.m3 .m3 \
          view3Dtem.i3 ${alg}3DViewClass.i3 .i3 \
          view3Dtem.m3 ${alg}3DViewClass.m3 .m3 \
          ie3Dtem.i3 ${alg}IE.i3 .i3 \
          ie3Dtem.m3 ${alg}IE.m3 .m3 \
          datatem.i3 ${alg}DataView.i3 .i3 \
          datatem.m3 ${alg}DataView.m3 .m3 \
          transtem.i3 ${alg}TranscriptView.i3 .i3 \
          transtem.m3 ${alg}TranscriptView.m3 .m3 \
          transtem.fv ${alg}TranscriptView.fv .fv \
          edtem.fv ${alg}EventData.fv .fv) 
else
echo "mode is ${mode}"
set WORK=(${mode}tem.i3 `basename ${alg} .evt`${base}${mode}View.i3 .i3 \
          ${mode}tem.m3 `basename ${alg} .evt`${base}${mode}View.m3 .m3)
endif 

if (! -f $algfile.evt) then
  echo "${me} cannot find file $algfile.evt."
  exit 1
endif

echo "${me} processing file $algfile.evt..."
    # strip off the imports, put one per line:
awk -f $DIR/algimport.awk $algfile.evt \
        | awk -f $DIR/uncomm.awk \
        | sed -f $DIR/fixspaces.sed  \
        | awk -f $DIR/importlist.awk \
        | sort -u  > zume$$.evtimport
    # strip off the algorithm data:
awk -f $DIR/algdata.awk $algfile.evt > zume$$.algdata
    # uncomment, regularize spacing, put one proc per line
awk -f $DIR/uncomm.awk $algfile.evt \
        | sed -f $DIR/fixspaces.sed  \
	| awk -f $DIR/fmtlines.awk > zume$$.events

while ($#WORK > 0)
  echo "${me} creating $WORK[2]..."
  sed -e "/^IMPORT/\!d" \
      -e "s/#(_ALGNAME_)/$alg/g" \
      -e "s/#(_VIEWNAME_)/$base/g" \
      $DIR/$WORK[1] \
        | sed -f $DIR/fixspaces.sed  \
        | awk -f $DIR/importlist.awk \
        | sort -u \
        | comm -23 zume$$.evtimport - > zume$$.import
  echo '#(_TRANSFORM_)' \
        | cat zume$$.events - $DIR/$WORK[1] \
        | sed -e "s/#(_ALGNAME_)/$alg/g" \
              -e "s/#(_VIEWNAME_)/$base/g" \
              -e "/#(_IMPORTS_)/r zume$$.import" \
	      -e "/#(_IMPORTS_)/d" \
              -e "/#(_ALGDATA_)/r zume$$.algdata" \
	      -e "/#(_ALGDATA_)/d" \
	| awk -f $DIR/transform.awk > zume$$.tmp
  switch ($WORK[3])
    case .i3:
    case .m3:
      cat zume$$.tmp > $WORK[2]
      breaksw
    case .fv:
      cat zume$$.tmp > $WORK[2]
      breaksw
    default:
      echo "Unknown file type $WORK[3]"; exit 1
      breaksw
    endsw
  shift WORK; shift WORK; shift WORK
end
echo "${me} finished."
 
/bin/rm zume$$.events zume$$.evtimport zume$$.import zume$$.algdata zume$$.tmp

exit 0

#{ 			start repeated block; must be alone at head of line
#{_OUTPUT		start repeated block of output events only
#{_FEEDBACK		start repeated block of feedback events only
#}			end repeated block; must be alone at head of line
#|                      introduces between-args material; must be alone
                        # at head of line and inside a per-arg block
#(_ALGNAME_)		algorithm name
#(_VIEWNAME_)		view name (for -Obliq or -Obliq3D)
#(_IMPORTS_)		import statements go here
#(_ALGDATA_)		data fields for algorithm object
#(_TRANSFORM_)		boundary between parsing and transforming
#(_EVENT_)		name of event
#(_EVENTSTYLE_)		"OutputEvent", "FeedbackEvent", or "UpdateEvent",
			# as apropos
#(_EVENTPRIO_)		Integer priority, in 1..9
#(_ARGSTR_)		unprocessed arg string, with no trailing semis
#(_NONULL_ARGSTR_)	... w/ a dummy integer declaration if null
#(_SEMI_ARGSTR_)	... w/ leading semi if non-null
#(_ARGSTR_SEMI_)        ... w/ trailing semi if non-null
#(_ARGTYPES_)		types of args, no names
#(_COMMA_ARGTYPES_)	... w/ leading comma
#(_SPACED_ARGTYPES_)    types of args, with spaces, not commas, between
#(_ARGNAMES_)		names of args, no types, as for a call
#(_COMMA_ARGNAMES_)	... w/ leading comma
#(_TYPE_FORMATS_)	fmt string to pass to PRINTF to print args
#(_PRINTF_ARGS_)	argument string to pass to PRINTF to print args
#(_ARGNAME_)		single argument name
#(_ARGTYPE_)		single argument type
#(_ARGFMT_)		a function that gives a printable text when
                        # applied to the argument
