#! /bin/sh
# Runs cb on specified files, making sure the file is identical.
# Compiles the files and cmp's the object files produced.
CC=${CC-cc}
CFLAGS="$CDEBUGFLAGS $DEFINES $INCLUDES"
trap 'if test -f docb$$.sv; then rm -f $oname; mv docb$$.sv $oname; fi' \
	1 2 3 15
for i
do
	case "$i" in
	*.c)
		oname=`basename $i .c`.o
		if test -f $oname; then
			mv $oname docb$$.sv
			restore=y
		else
			restore=n
		fi
		cb -sj $i > docb$$.c
		if cmp -s $i docb$$.c; then
			echo $0: $i unchanged by cb. >&2
			rm -f docb$$.c
		else
			$CC -c $CFLAGS -o $oname $i
			$CC -c $CFLAGS -o docb$$.o docb$$.c
			if cmp -s $oname docb$$.o; then
				mv $i $i~ && mv docb$$.c $i
			else
				echo $0: Ugh! $i object changed after cb >&2
				echo $0: cb output in $i\#
				mv docb$$.c $i\#
			fi
			rm -f $oname docb$$.o
		fi
		case "$restore" in
		y)	mv docb$$.sv $oname;;
		esac
		;;
	*)
		echo $0: $i does not end in .c; skipping it. >&2
		;;
	esac
done
