#!/bin/sh
# A Unix shell script!
###########################################################
# This is ESPP, a preprocessor for ESPRETTY!
# EPPP processes Eiffel source files via 'espretty'!
# It passes options and filenames to 'espretty', and keeps
# a copy of the original sources as '*.e~' files, when used
# with the acsii output formater.
###########################################################
# $Author: vkyr $ (c) 1995/96
# $Date: 1996/07/03 22:57:33 $
# $Revision: 1.1 $
###########################################################

#
# SET HERE SPRETTY's ACCESS PATH! 
#
# NOTE: THIS IS THE ONLY NEEDED CUSTOMIZATION SETTING
#

ESPRETTY=./espretty

#
# Variables.
#

SCRIPTNAME="$0"
CNT=$#
FORMAT=""
#######################

#
# Process options and filenames and pass them to the 'espretty' call.
#

while [ $CNT != 0 ]
do
    case ".$1" in
        .-l)   # transforms identifiers (not Types) into lower chars
            OPTIONS="$OPTIONS $1"
            ;;

        .-n)       # print line numbers and statement level
            OPTIONS="$OPTIONS $1"
            ;;

        .-s)       # removes feature body - (.. short ..)
            OPTIONS="$OPTIONS $1"
            ;;

		  .-u)       # transforms (most) types into upper chars
            OPTIONS="$OPTIONS $1"
            ;;

        .-x)       # starts new line after redefine, until ...
            OPTIONS="$OPTIONS $1"
            ;;
				
        .-t|.-2|.-3|.-4)   # use TAB or (n) blanks for indent
            OPTIONS="$OPTIONS $1"
            ;;
				
		  .-ascii)   # use named formatter       
            OPTIONS="$OPTIONS $1"
				FORMAT=ascii
            ;;

		  .-ansi)   # use named formatter       
            OPTIONS="$OPTIONS $1"
				FORMAT=ansi
            ;;
				
		  .-mif)   # use named formatter       
            OPTIONS="$OPTIONS $1"
				FORMAT=mif
            ;;
				
		  .-mime)   # use named formatter       
            OPTIONS="$OPTIONS $1"
				FORMAT=mime
            ;;
				
		  .-rtf)   # use named formatter       
            OPTIONS="$OPTIONS $1"
				FORMAT=rtf
            ;;

	     .*)        # pass through to espretty
            FILES="$FILES $1"
            ;;
    esac
    CNT=`expr $CNT - 1`
    shift
done

#######################

#
# Print usage message, if empty filelist.
#

if [ "$FILES" = "" ]
then
	echo "Usage: $SCRIPTNAME [options] <file1.e file2.e file3.e...>"
	echo
	echo "Where:   [options] are these options supported by ESPRETTY"
	echo
	echo "          -l   transforms identifiers (not Types) into lower chars."
   echo "          -n   print line numbers and statement level."
   echo "          -s   removes feature body - (.. short ..)."
   echo "          -u   transforms (most) types into upper chars."
   echo "          -x   starts new line after redefine, until ..."
   echo "               removes empty lines in routine bodies."
   echo "          -t|-2|-3|-4   use TAB or (n) blanks for indent (default -3)."
	echo "          -ascii|   use plain ASCII for output format (default)."
	echo "          -ansi |   use keyword boldfacing for terminal output."
	echo "          -mif  |   use Frame MIFFile output format."
	echo "          -mime |   use Mime output format."
	echo "          -rtf  |   use RTF output format."
	echo
	echo " '$SCRIPTNAME' uses 'ESPRETTY' to format Eiffel source files."
	echo "  It keeps a copy of the original source files as '*.e~' backup"
	echo "  files (the formated ones are stored as '*.e' files), if used"
	echo "  with the -ascii option. -"
	echo
	echo "  For the other formaters -ansi, -mif, -mime and -rtf it uses"
	echo "  extensions of the form '*.e.formatter' for formated files."
	echo
	exit 1
fi

#######################

#
# Check read access of the files in the filelist.
#

QUIT=""
for i in $FILES
do
	if [ ! -r $i ]
	then
		echo
		echo "Can't find or access <$i>"
		echo
		QUIT=1
	fi
done

#######################

#
# Check for ESPRETTY access.
#

if [ ! -r "$ESPRETTY" ]
	then
		echo
		echo "Can't find or access <espretty>, make shure the loadpath"
		echo "settings inside this script are setup correct."
		echo "Loadpath: '$ESPRETTY'"
		echo
		QUIT=1
	fi

#######################

#
# The main ESPRETTY calling stuff.
#

if [ "$QUIT" = "1" ]
then
	echo "Exit, no file(s) written"
	echo
	exit 1
else
	for i in $FILES
	do
		j=$i
		echo "formating: <$j>"
		if [ "$FORMAT" = "ascii" -o "$FORMAT" = "" ]
		then
			mv $i $i~
	   	$ESPRETTY $i~ $OPTIONS >$j
		else
			$ESPRETTY $j $OPTIONS >$j.$FORMAT
		fi
	done
	echo
	echo "OK, done!"
	echo
fi
