#!/bin/sh
#------------------------->  Bourne shell - script  <-------------------------#
#- Copyright (C) 1996 by International Computer Science Institute            -#
#- This file is part of the GNU Sather package. It is free software; you may -#
#- redistribute  and/or modify it under the terms of the  GNU General Public -#
#- License (GPL)  as  published  by the  Free  Software  Foundation;  either -#
#- version 2 of the license, or (at your option) any later version.          -#
#- This  program  is distributed  in the  hope that it will  be  useful, but -#
#- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY -#
#- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/GPL for more details.        -#
#- The license text is also available from:  Free Software Foundation, Inc., -#
#- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     -#
#------------->  Please email comments to <bug-sather@gnu.org>  <-------------#

# 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.
#
# 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
