#!/bin/sh
# A Unix shell script!
#
# $Date: 1996/07/22 14:40:26 $
# $Revision: 1.1 $
# (C) by Valentino Kyriakides
#
# Unformat 'espretty' ANSI, HTML, LaTeX, MIME, MIF and RTF class format files
# back into an pure ASCII class representation!
#
# NOTE: The regular espression substitutions herein used, are based *highly*
#       on 'espretty's' output formaters codings. So if you make any changes 
#       to 'espretty's' format classes string outputs, you must also do the 
#       appropriate changes to the below 'SED' substitutions!!!


#
# Initial settings and Variables.
#
SED=/bin/sed
SCRIPTNAME="$0"
CNT=$#
FORMAT=""

####################### procedures


Help () 
{
	echo "Usage: $SCRIPTNAME -f [ansi | html | latex | mime | mif | rtf] <file...>"
	echo
	echo "unformat 'espretty' formated <files...> back into '*.e' class files!"
	echo
	echo
	exit 1
}


#
# format processing subroutines start here!
#
# NOTE: These regular espression substitutions are based *highly* on
#       'espretty's' output formaters coding. So if you make changes 
#       to 'espretty's' format classes string outputs, you must also  
#       do the appropriate changes to the below 'perl' substitutions!
#


#
# Remove espretty's 'latex' tags via 'sed' Regular expression substitutions!
#

latex_file()
{
	$SED -e '/^% Produced by espretty for SmallEiffel/d' \
	"-e" '/^\\documentstyle\{article\}/d' \
	"-e" '/\\oddsidemargin1.5cm/d' \
	"-e" '/\\evensidemargin1.5cm/d' \
	"-e" '/\\textwidth16cm/d' \
	"-e" '/\\begin\{document\}/d' \
	"-e" '/\\frenchspacing/d' \
	"-e" '/\\def\\~\{\\hspace\*\{1ex\}\}/d' \
	"-e" '/\\def\\\|\{\$\|\$\}/d' \
	"-e" '/\\def\\<\{\$<\$\}/d' \
	"-e" '/\\def\\>\{\$>\$\}/d' \
	"-e" '/\\hspace\{-4ex\}/d' \
	"-e" '/^\\end{document}/d' \
	"-e" 's/\\~/ /g' \
	"-e" 's/-\\,/-/g' \
	"-e" 's/\\\{/{/g' \
	"-e" 's/\\\}/}/g' \
	"-e" 's/\\\$/$/g' \
	"-e" 's/\\\&/\&/g' \
	"-e" 's/\\#/#/g' \
	"-e" 's/\\%/%/g' \
	"-e" 's/\\_/_/g' \
	"-e" 's/\\^/^/g' \
	"-e" 's/\\\\//g' \
	"-e" 's/\\\|/|/g' \
	"-e" 's/\\</</g' \
	"-e" 's/\\>/>/g' \
	"-e" 's/\{\\bf //g' \
	"-e" 's/\{\\em //g' \
	"-e" 's/\}/~/g' \
	"-e" 's/(\{.*)~/$+/g' \
	"-e" 's/~//g' <$ffile >$fname.e
}

#
# Remove espretty's 'html' tags via 'sed' Regular expression substitutions!
#
html_file()
{ 
	$SED -e '/<HTML>/d' \
	"-e" '/<HEAD>/d' \
	"-e" '/<TITLE>Produced by espretty for SmallEiffel<\/TITLE>/d' \
	"-e" '/<\/HEAD>/d' \
	"-e" '/<BODY>/d' \
	"-e" '/<CODE>/d' \
	"-e" '/<\/CODE>/d' \
	"-e" '/<\/BODY>/d' \
	"-e" '/<\/HTML>/d' \
	"-e" 's/&nbsp;/ /g' \
	"-e" 's/<B>//g' \
	"-e" 's/<\/B>//g' \
	"-e" 's/<I>//g' \
	"-e" 's/<\/I>//g' \
	"-e" 's/<BR>//g' <$ffile >$fname.e
}

#
# Remove espretty's 'mime' tags via 'sed' Regular expression substitutions!
#
mime_file()
{
	$SED -e 's/<nl>//g' \
	"-e" 's/<bold>//g' \
	"-e" 's/<\/bold>//g' \
	"-e" 's/<italic>//g' \
	"-e" 's/<\/italic>//g' \
	"-e" 's/<lt>/</g' <$ffile >$fname.e
}

#
# Remove espretty's 'ansi' tags via 'sed' Regular expression substitutions!
#
ansi_file()
{
	$SED -e 's/.\[1m//g' \
	"-e" 's/.\[0m//g' <$ffile >$fname.e
}

#
# Remove espretty's 'rtf' tags via 'sed' Regular expression substitutions!
#
rtf_file()
{
	$SED -e '/\{\\rtf0\\ansi\\deff0\{\\fonttbl\\f0\\froman Times-Roman;\}/d' \
	"-e" '/\{\\colortbl \\red0\\green0\\blue0;\}/d' \
	"-e" '/\{\\stylesheet\{\\fs24 \\snext0 Normal;\}\}/d' \
	"-e" '/\\paperw.*\\gutter0\\widowctrl$/d' \
	"-e" '/\\sectd\\f0/d' \
	"-e" '/\\sect/d' \
	"-e" 's/\}rtf//g' \
	"-e" 's/\\~/ /g' \
	"-e" 's/\\_/-/g' \
	"-e" 's/\\par //g' \
	"-e" 's/\\b //g' \
	"-e" 's/\\b0 //g' \
	"-e" 's/\\i //g' \
	"-e" 's/\\i0 //g' \
	"-e" 's/\\\\//g' \
	"-e" 's/(\\\{)/{/g' \
	"-e" 's/(\\\})/}/g' <$ffile >$fname.e
}

#
# Remove espretty's 'mif' tags via 'sed' Regular expression substitutions!
#
mif_file ()
{
   $SED -e '/<MIFFile 3.00> # Generated by spretty v1.1/d' \
   "-e" '/<Para/d' \
   "-e" '/  <PgfTag `EProgList'"'"'>/d' \
   "-e" '/> # end of Para/d' \
   "-e" '/# End of MIFFile/d' \
   "-e" '/    <Font /d' \
	"-e" '/      <FTag `Bold'"'"'>/d' \
	"-e" '/      <FFamily `Times'"'"'>/d' \
	"-e" '/      <FVar `Regular'"'"'>/d' \
	"-e" '/      <FWeight `Bold'"'"'>/d' \
	"-e" '/      <FAngle `Regular'"'"'>/d' \
	"-e" '/      <FSize  12.0 pt>/d' \
	"-e" '/      <FPairKern Yes >/d' \
	"-e" '/      <FTag `Emphasis'"'"'>/d' \
	"-e" '/      <FTag `'"'"'>/d' \
	"-e" '/      <FAngle `Regular'"'"'>/d' \
	"-e" '/      <FWeight `Regular'"'"'>/d' \
	"-e" '/      <FAngle `Italic'"'"'>/d' \
	"-e" '/    > # end of Font/d' \
	"-e" 's/    <String `//g' \
	"-e" 's/'"'"'># end$//g' \
   "-e" 's/    <Char HardSpace >$/ /g' \
	"-e" 's/    <Char HardReturn >//g' \
	"-e" '/  <ParaLine/d' \
	"-e" '/  > # end of ParaLine/d' \
	"-e" 's/\\\\/\\/g' \
	"-e" 's/\\>/>/g' \
	"-e" 's/\\xd5/'"'"'/g' \
	"-e" 's/\\xd4/`/g' <$ffile >$fname.e	
}


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

while [ $CNT != 0 ]
do
    case ".$1" in
        .-f)   # transforms identifiers (not Types) into lower chars
            OPTION="$OPTION $1"
            ;;
				
		  .ansi)   # use named formatter       
            OPTION="$OPTION $1"
				FORMAT=ansi
            ;;
				
		  .html)   # use named formatter       
            OPTION="$OPTION $1"
				FORMAT=html
            ;;
				
		  .latex)   # use named formatter       
            OPTION="$OPTION $1"
				FORMAT=latex
            ;;

		  .mif)   # use named formatter       
            OPTION="$OPTION $1"
				FORMAT=mif
            ;;
				
		  .mime)   # use named formatter       
            OPTION="$OPTION $1"
				FORMAT=mime
            ;;
				
		  .rtf)   # use named formatter       
            OPTION="$OPTION $1"
				FORMAT=rtf
            ;;

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

#######################
#
# Print usage message, if wrong option.
#

CHECK="-f $FORMAT"
echo "$CHECK"
echo
if [ "$OPTION" = "-f $FORMAT" ]
then
	echo "In Option Help = $OPTION mit dem Format = $FORMAT"
	Help
fi

#
# Print usage message, if empty filelist.
#

if [ "$FILES" = "" ]
then
   echo "In Files Help = $FILES"
	Help
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


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

#
# The main UNFORMAT calling stuff.
#

if [ "$QUIT" = "1" ]
then
	echo "Exit, no file(s) written"
	echo
	exit 1
else
	for i in $FILES
	do
		ffile=$i
		fname=`basename $i .$FORMAT`
		echo "unformating: <$ffile>"
		if [ "$FORMAT" = "ansi" ]
		then
			ansi_file
		elif [ "$FORMAT" = "html" ]
		then
			html_file
		elif [ "$FORMAT" = "latex" ]
		then
			fname=`basename $i .tex`
			latex_file
		elif [ "$FORMAT" = "mif" ]
		then
			mif_file
		elif [ "$FORMAT" = "mime" ]
		then
			mime_file
		else
			rtf_file
		fi
	done
	echo
	echo "OK, done!"
	echo
fi
exit 0

