#!/usr/local/bin/perl
# A Perl script!
#
###########################################################
# This is 'espp', a preprocessor for 'espretty'!
# 'espp' processes Eiffel source files via 'espretty'!
###########################################################
# $Author: vkyr $ (c) 1996
# $Date: 1996/07/22 14:36:23 $
# $Revision: 1.1 $
###########################################################

$SCRIPTNAME = $0;

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

$ESPRETTY = "./espretty";


#
# Check the 'espretty' access path.
#

if (! -r $ESPRETTY) {
		print "\nCan't find or access <espretty>, make sure the loadpath\n";
		print "settings inside this script are setup correct.\n";
		print "Current loadpath is: '$ESPRETTY'\n\n";
		exit;
}

#
# Process and check command line options.
#

$format = "ascii";
while ($_ = $ARGV[0], /^-/) {
    shift;

    if (/^-[lnsuxt1234]$/) {
		push (@fopts, $_);
    }
	 elsif (/^-ansi$/) {
	 	$format = "ans";
		push (@fopts, $_);
	 }
	 elsif (/^-ascii$/) {
	 	$format = "ascii";
		push (@fopts, $_);
	 }
	 elsif (/^-html$/) {
	 	$format = "htm";
		push (@fopts, $_);
	 }
	 elsif (/^-latex$/) {
	 	$format = "tex";
		push (@fopts, $_);
	 }
	 elsif (/^-mif$/) {
	 	$format = "mif";
		push (@fopts, $_);
	 }
	 elsif (/^-mime$/) {
	 	$format = "mim";
		push (@fopts, $_);
	 }
	 elsif (/^-rtf$/) {
	 	$format = "rtf";
		push (@fopts, $_);
	 }
	 elsif (/^-[hH]$/) {
	 	&usage;
	 }
    else {
		print "Illegal option: $_\n\n";
		die "Type '$SCRIPTNAME -h' for help!\n\n";
    }
}

#
# The main file handling and calling stuff.
#

$status = "false";
foreach $file (@ARGV) {
	unless (-r $file) {
			warn "Can't find or access file: $file\n";
			next;
	}
	$dummy = $file;
	@fpart = split(/\./,$dummy);
	$outfile = $fpart[0];
		
	print "formating: <$file>\n";
	if ($format eq "ascii") {
		$dummy = "$outfile.bak";
		rename ($file, $dummy);
	   system("$ESPRETTY $dummy @fopts > $outfile.e");
	} else {
		system("$ESPRETTY $file @fopts >$outfile.$format");
	}
	$status = "true";
}
if ($status eq "true") {
	print "OK, done!\n";
}
else {
	print "$SCRIPTNAME: no input <files...> specified!\n\n";
	print "Type '$SCRIPTNAME -h' for help.\n\n";
}


#
# espp's usage message.
#

sub usage
{
	print "Usage: $SCRIPTNAME [options] <file.e...>\n\n";
	print "Where:   [options] are these options supported by 'espretty'\n\n";
	print "         -l   transforms identifiers (not Types) into lower chars.\n";
   print "         -n   print line numbers and statement level.\n";
   print "         -s   removes feature body - (.. short ..).\n";
   print "         -u   transforms (most) types into upper chars.\n";
   print "         -x   starts new line after redefine, until ...\n";
   print "              removes empty lines in routine bodies.\n";
   print "-t|-2|-3|-4   use TAB or (n) blanks for indent (default -3).\n";
	print "    -ascii|   use plain ASCII for output format (default).\n";
	print "    -ansi |   use keyword boldfacing for terminal output.\n";
	print "    -html |   use HTML output format.\n";
	print "    -latex|   use LaTeX output format.\n";
	print "    -mif  |   use Frame MIFFile output format.\n";
	print "    -mime |   use Mime output format.\n";
	print "    -rtf  |   use RTF output format.\n\n";
	print " '$SCRIPTNAME' uses 'espretty' to format Eiffel source files.\n";
	print "  It keeps a copy of the original source files as '*.bak' backup\n";
	print "  files (the formated ones are stored as '*.e' files), if used\n";
	print "  with the (default) '-ascii' option. For the other formats:\n";
	print "         -ansi,   -html,   -latex,   -mif,   -mime,   -rtf\n";
	print "  originals are kept as '*.e' and the formated ones are stored as:\n";
	print "        '*.ans', '*.htm', '*.tex', '*.mif', '*.mim', '*.rtf'.\n\n";
	exit;
}

# End of perl script
