#!/bin/csh -f
# This is a program to cat files as separate words or as all upper-case
#  or all lower case.
# Flags can be in any form but the "f" flag must be last in group.
# Flags:
#       -s separate lines
#       -l lower case
#       -u upper case
#       -f next argument is output file.
onintr catch
@ u = 0
@ l = 0
@ s = 0
@ x = 0
while ( $#argv > 0 )
	if ( "$1" !~ -* ) break # break out of while loop
	set a=$1
	shift
	set a = `echo $a | colrm 1 1`
	while ( $a != "" )
		set f = `echo $a | colrm 2`
		set a=`echo $a | colrm 1 1`
		switch ( $f )
			case 'u':   # u = upper case
				@ u = 1
			breaksw
			case 'l':   # l = upper case
				@ l = 1
			breaksw
			case 's':   # s = separate lines
				@ s = 1
			breaksw
			case 'f':	# file name follows
				if ( $#argv > 0 ) then
					set fil = $1
					shift
				endif
				break
			breaksw
			case 'x':   # x = extra spaces 
				@ x = 1
			breaksw
			default:
				echo  "$f is illegal flag (ignored)"
			breaksw
		endsw
	end
end
rm -f /tmp/gw.$$
while ( $#argv > 0 )
	set a=$1
	shift
	if ( $u && $l ) then
		echo Illegal flag combination
		exit 1
	    else if ( $u ) then
		    tr 'a-z' 'A-Z' < $a >> /tmp/gw.$$
	        else if ( $l ) then
		    tr 'A-Z' 'a-z' < $a >> /tmp/gw.$$
	        else cat $a >> /tmp/gw.$$
                endif
            endif
        endif
end
if ( $s ) then
	tr  -s ' ' '\012' < /tmp/gw.$$ > /tmp/gw2.$$
else
	mv /tmp/gw.$$ /tmp/gw2.$$
endif
if ( $x ) then
	sed 's/./& /g' < /tmp/gw2.$$ > /tmp/gw3.$$
else
	mv /tmp/gw2.$$ /tmp/gw3.$$
endif
if ($?fil) then
	mv /tmp/gw3.$$ $fil
else
	cat /tmp/gw3.$$
endif
rm -f /tmp/gw.$$ /tmp/gw2.$$ /tmp/gw3.$$
exit 0
catch:
	rm -f /tmp/gw.$$ /tmp/gw2.$$ /tmp/gw3.$$
	exit 1
