#! /bin/csh -f
#   DIGITAL EQUIPMENT CORPORATION  CONFIDENTIAL AND PROPRIETARY
#   Last modified on Sun Oct 18 00:11:28 PDT 1992 by mhb  
#        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=BBB

# If the first arg is "-testdir", then the second arg is the directory
# that contains the template files, and the third arg is the alg name.
# O/W, the first arg is the alg name.
if ( $1 == "-testdir" ) then
  shift
  set DIR=$1
  shift
endif
set algfile=$1
set alg=`basename $1`

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 \
          transtem.i3 ${alg}TranscriptView.i3 .i3 \
          transtem.m3 ${alg}TranscriptView.m3 .m3 \
          transtem.fv ${alg}TranscriptView.fv .fv \
          edtem.fv ${alg}EventData.fv .fv)


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

echo "m3zume 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 "m3zume creating $WORK[2]..."
  sed -e "/^IMPORT/\!d"   -e "s/#(_ALGNAME_)/$alg/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 "/#(_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:
# for 3Max:
      cat zume$$.tmp | m3pp > $WORK[2]
# for FF:
#      cat zume$$.tmp | timestamp -update -filetype $WORK[3] > $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 "m3zume 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
#(_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
