#! /bin/sh
#
#	fmcolor 2.0 5/8/89
#
#   Copyright (c) 1986-1989 by Frame Technology Corp.
#
# Modification History:
#
# v 1.13 (6/21/88)	Eddie Kessler
#	process id in tmpfile
#	update for 1.3's bin layout
#
# v 1.12 (1/8/88):	David Fuchs
#   Work with 1.12's new bin layout
#   Fix greps to only match with 3rd dimension (i.e., "x1 ", not "x1")
#
# v 1.01 (9/21/87):	Suzann Larsen
#	No longer append .rf on end of input file names.
#	Add more checks for type of file before conversion.
#	Add conversion from encoded to standard if necessary.
#	Saves original file as "filename.original".
#
# v 1.0  (6/6/87):	Steve Kirsch
#	Original.
#
# Global variables
version=2.0
tmpfile=/tmp/fmcolor.$$
arglist=$*

#### Subroutines
# convert filtername filename
convert () {
	$1 <$2 >$2.tmp
	if [ ! -s $2.tmp ]; then
		echo "$1: Error converting $2."
		rm -f $2.tmp
		return 1
	fi
	mv $2.tmp $2
	return 0
}

#### Main Body
if [ $# -eq 0 ]; then
	echo "Format is:   fmcolor <filenames>"
	echo "This program converts bitmaps to encoded monochrome bitmaps."
	echo "Each argument should be a Sun rasterfile (bitmap)."
	echo "You must have write permission to the bitmap's directory."
	echo "The original bitmap will be saved in 'filename.original'."
	exit 1
fi
echo "fmcolor $version: Converts all bitmap arguments to encoded monochrome bitmaps."
for fname in $arglist
do

# check if file exists.  Do file command to see what kind of file.
	echo -n "$fname:  "
	if [ ! -f $fname ]; then
		echo "Argument is not a file.  Not converted."
		continue
	fi
	file $fname >$tmpfile

# set color = 1 for color, 0 for bw
	color=0
	grep "x8 " $tmpfile >/dev/null
	if [ $? -eq 0 ]; then
		color=1
	else
		grep "x1 " $tmpfile >/dev/null
		if [ $? -ne 0 ]; then
			echo "File is not a x1 or x8 bitmap. Not converted."
			continue
		fi
	fi

# set encoded = 1 for encoded bitmaps, 0 for standard
	encoded=0
	grep encoded $tmpfile >/dev/null
	if [ $? -eq 0 ]; then
		encoded=1
	fi

# if fname.original exists, don't overwrite it
	if [ -f $fname.original -o -d $fname.original ]; then
		echo "$fname.original already exists.  Not converted."
		continue
	fi

# save original bitmap and make copy to work with
	mv $fname $fname.original 
	if [ $? -ne 0 ]; then
		echo "Cannot write to its directory.  Not converted."
		continue
	fi
	cp $fname.original $fname

# convert color bitmaps
	if [ $color -eq 1 ]; then

# If encoded, must first convert to standard format for rasfilter8to1 to work.
		if [ $encoded -eq 1 ]; then 
			echo -n "Converting to standard... "
			convert /usr/lib/rasfilters/convert.2 $fname
			if [ $? -eq 1 ]; then
				continue
			fi
			encoded=0
		fi

# convert standard color to standard black & white
# Note that Sun had bugs in old OS 3.x versions that we patch over...
		echo -n "Converting to monochrome... "
		if [ -x $FMHOME/bin/bin.`arch`/rasfilter8to1 ]; then
			convert $FMHOME/bin/bin.`arch`/rasfilter8to1 $fname
		else
			convert rasfilter8to1 $fname
		fi
		if [ $? -eq 1 ]; then
			continue
		fi
    	fi

# convert all standard to encoded
	if [ $encoded -eq 0 ]; then 
		echo -n "Converting to encoded... "
		convert /usr/lib/rasfilters/convert.2 $fname
		if [ $? -eq 1 ]; then
			continue
		fi
	fi

	echo "Done."
done
rm -f $tmpfile
echo "fmcolor completed."
exit 0
