#!/bin/sh
# Our great Sather preprocessor
# It is used to simplify the writing of 
# classes that are thread safe in pSather and can be used in
# sather too.
# (C) by ICSI 1996, the usual Sather License (as described in
# the file cs.sa applies.
#
# Version 1 by Claudio Fleiner
################################################################################################

make_nr=0
make_sather=1

if [ $# -eq 0 ]
then
	echo "USAGE: $0 file.pp ..."
	exit 1
fi

while [ "$1" = "-nr" -o "$1" = "-nrs" ]
do
	if [ "$1" = "-nr" ] 
	then
	 	make_sather=0
		make_nr=1
	elif [ "$1" = "-nrs" ]
	then	
		make_nr=1
	fi
	shift
done

dir=$1
shift
if [ ! -d "$dir" ] 
then
	echo "$0: directory $dir does not exist"
	echo "usage $0 [-nr] -[nrs] dir file [files ...]"
	exit 1
fi

for i do

	if [ -r "$i" ] 
	then
		d=`dirname $i`
		f=`basename $i`

		if [ $make_nr = 1 ]
		then
			sed -e '1,/^[ 	]*$/s/^[ 	]*$/--!!! THIS FILE HAS BEEN CREATED FROM '$i', DO NOT EDIT IT !!!/' \
			    -e 's/^\([ 	]*\)[^ 	].*--NR: */\1/' 	\
			    -e 's/^\([ 	]*\)--NR:/\1      /' 	\
			    -e 's/^[ 	]*lock visitor then.*//'		\
			    -e 's/^[ 	]*lock mutator then.*//'		\
			    -e 's/end;*[ 	]*-- lock visitor.*//'		\
			    -e 's/end;*[ 	]*-- lock mutator.*//'		\
			    -e 's/include VISITOR_MUTATOR;*//'		\
			    -e 's/[^ 	]*init_visitor_mutator;*//'	\
				< $i > $d/nr_$f
		fi
		if [ $make_sather = 1 ]
		then
			sed -e '1,/^[ 	]*$/s/^[ 	]*$/--!!! THIS FILE HAS BEEN CREATED FROM '$i', DO NOT EDIT IT !!!/' \
			    -e 's/^\([ 	]*\)[^ 	].*--S: */\1/' 	\
			    -e 's/^\([ 	]*\)--S:/\1      /' 	\
			    -e 's/^[ 	]*lock.*then//'		\
			    -e 's/end;*[ 	]*-- lock.*//'		\
			    -e 's/include VISITOR_MUTATOR;*//'		\
			    -e 's/[^ 	]*init_visitor_mutator;*//'	\
				< $i > $dir/$f
		 	if [ $make_nr = 1 ]
			then
				sed -e 's/^\([ 	]*\)[^ 	].*--S: */\1/' 	\
				    -e 's/^\([ 	]*\)--S:/\1      /' 	\
				    -e 's/^[ 	]*lock.*then//'		\
				    -e 's/end;*[ 	]*-- lock//'	\
					< $d/nr_$f > $dir/nr_$f
			fi
		fi
	else
		echo "$0: file $i is missing or read protected"
		echo "usage $0 [-nr] -[nrs] dir file [files ...]"
	fi
done


