#!/bin/sh
#set -xv
#trap "" 1
#set -x

if test "$1" = "all"
then
	if test -r /bin/sun && /bin/sun
	then
		if /bin/sun3 || /bin/sun3x
		then
			eval $0 -f68881
			eval $0 -ffpa
			eval $0 -fswitch
		fi
	fi
	exec $0
fi

hoopslib=${2-"hoops${1}.a"}

EXTRA=

if test -r /bin/sun && /bin/sun
then
	if /bin/sparc
	then
		OPT="-PIC -DSUNVIEW -Dsun -fsingle -sun4 $1"
	elif /bin/i386
	then
		OPT="-pic -DSUNVIEW -Dsun -fsingle $1"
	elif /bin/sun3 || /bin/sun3x
	then
		OPT="-pic -DSUNVIEW -Dsun -fsingle $1"
	else
		echo "This does not appear to a supported Sun workstation." 1>&2
		exit 2
	fi
else
	echo "This does not appear to a Sun workstation." 1>&2
	exit 2
fi

if test -r update${1}.time -a -r $hoopslib
then
	NEWER="-newer update${1}.time"
else
	NEWER=
	date >update${1}.time
fi

date >update${1}.newtime

OPT="-DPRINTF -DPOSTSCRIPT -DHPGL -I. $OPT"

EXIT=0

rm -f *.o >/dev/null 2>/dev/null

NOOP_FILES=""

find * -name "????*.[cs]" $NEWER -print | 
while read name
do
	echo $NOOP_FILES | grep -s $name
	if test $? -eq 0
	then
		echo /bin/cc -c -g $OPT $name >>/tmp/up$$
		echo /bin/cc -c -g $OPT $name 
		/bin/cc -c -g $OPT $EXTRA $name 2>&1
	else 
		echo /bin/cc -c $OPT $name >>/tmp/up$$
		echo /bin/cc -c $OPT $name 
		/bin/cc -c $OPT $EXTRA $name 2>&1
	fi
done |
fgrep -v "loses precision" |
tee /tmp/uperr$$ 

fgrep -v "/bin/cc" /tmp/uperr$$ > /tmp/uperrB$$
if test -s /tmp/uperrB$$
then
	EXIT=2
fi
rm -f /tmp/up$$ /tmp/uperr$$ /tmp/uperrB$$ >/dev/null 2>&1

mv update${1}.time update${1}.oldtime 
mv update${1}.newtime update${1}.time

# if there is at least one object file... 
if test "`echo ????*.o`"
then
	if test -r /bin/sun && /bin/sun
	then

		# throw away empty objects
		nm ????*.o 2>&1 1>/dev/null | 
		sed -e 's/nm: /rm -f /' -e 's/: no name list//' |
		sh -vx
		
		if test "`echo ????*.o`"
		then
			echo "ar rlv $hoopslib ..."
			ar rlv $hoopslib ????*.o

			if test -f /usr/bin/ranlib -o -f /bin/ranlib
			then
				rm -f ????*.o >/dev/null 2>&1
				echo "ranlib $hoopslib"
				ranlib $hoopslib
			fi
		fi		
	fi
fi

exit $EXIT
