#!/bin/sh
#
#  m2sr - convert monitor notation to SR code

NAME=m2sr
OPTIONS="[-sc] [-sw] [-su] [-sx]"
PATTERN="-s[cwux]"
EXTENSION=.m
#
#  Created mechanically;  DO NOT EDIT THIS FILE.
#
VERSION="SR version 2.3.3"
SRSRC=/pbulk/work/lang/sr/work
SRCMD=/usr/pkg/bin
SRLIB=/usr/pkg/lib/sr
MAN1=/usr/pkg/man/man1
MAN3=/usr/pkg/man/man3
MAN5=/usr/pkg/man/man5
MANEXT=1
CCPATH=/usr/bin/cc
RSHPATH=/usr/bin/ssh
LIBC=
LIBR="-R/usr/X11R7/lib -L/usr/X11R7/lib"
LIBM=-lm
XINCL=/usr/X11R7/include
XTANGO=
VFPATH=
VGMACS=
VMASK=0002


# process options

USAGE="usage: $0 $OPTIONS file$EXTENSION"
HDIR=$SRLIB

for i
do
    case $i in
	$PATTERN)
	    OPTION=$i
	    shift;;
	-e)
	    HDIR=$SRSRC/links
	    shift;;
	-*)
	    echo "$USAGE" 1>&2
	    exit 1;;
	*)
	    break;;
    esac
done


#  validate file argument

INFILE=$1
BASE=`echo $INFILE | sed 's/\.[^.]*$//'`
SRFILE=$BASE.sr

if [ $# != 1 -o X$BASE$EXTENSION != "X$1" ]
then
    echo "$USAGE" 1>&2
    exit 1
fi

if [ ! -r $INFILE ]
    then
        echo 1>&2 "$0: cannot read $INFILE"
        exit 1
fi


# if .sr output file exists, check that it was generated by this tool
# so that we do not mistakenly blow away a good file.

BANNER="/* This SR file was generated by $NAME */"

if [ -r $SRFILE ]
    then
	LINE1=`sed 1q $SRFILE`
	if [ "$LINE1" != "$BANNER" ]
	    then
	        echo 1>&2 \
	    "$SRFILE exists and was not created by $NAME - will not overwrite"
                exit 1
        fi
fi


# build a .c file in /tmp that will be preprocessed to make the .sr file

TMPFILE=/tmp/$NAME$$.c
trap 'x=$?; /bin/rm -f $TMPFILE; exit $x' 0 1 2 15

/bin/cat $HDIR/$NAME.h $INFILE > $TMPFILE

echo "$BANNER" >$SRFILE


# define sed script for postprocessing the cpp results

SCRIPT='
s=/ *%=/\*=g
s=% */=\*/=g
s=/  */=//=g
s=\[  *]=[]=g
s=\*  *\*=\*\*=g
s/\~  *=/\~=/g
s/ *: *= */:=/g
'


# handle tool-specific options and build the .sr file 

SC=1
SW=2
SU=3
SX=4

case $OPTION in
    -sc) signaldisc=$SC;;
    -sw) signaldisc=$SW;;
    -su) signaldisc=$SU;;
    -sx) signaldisc=$SX;;
    *)   signaldisc=$SC;;
esac

$CCPATH -DSIGNALDISC=$signaldisc -DSC=$SC -DSW=$SW -DSU=$SU -DSX=$SX \
   -E $TMPFILE | sed "$SCRIPT" >>$SRFILE
