#!/bin/sh
#
#  csp2sr - convert CSP notation to SR code

NAME=csp2sr
OPTIONS="[-ti] [-te]"
PATTERN="-t[ie]"
EXTENSION=.csp
#
#  Created mechanically;  DO NOT EDIT THIS FILE.
#
VERSION="SR version 2.3.3"
SRSRC=/tmp/pkgbuild/lang/sr/work.sparc64
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 

T_IM=1
T_EX=2

case $OPTION in
    -ti) terminationdisc=$T_IM;;
    -te) terminationdisc=$T_EX;;
    *)   terminationdisc=$T_IM;;
esac
   
$CCPATH -DTERMINATIONDISC=$terminationdisc -DT_IM=$T_IM -DT_EX=$T_EX \
  -E $TMPFILE | sed "$SCRIPT" >> $SRFILE
