# Shell script to build the bcc 16-bit and 32-bit libraries for Minix

# The usage is

#	./makelib (86|386) [(clean|compare|<target>)]

# If the second # option is "compare", the freshly-built temporary copy of
# the library is compared with the working copy, and identical files are
# discarded from the temporary copy. Otherwise second option is passed to
# "make" as the thing to be made; it defaults to "install".

# The library is built in $tmplibdir and must be copied to $libdir by hand.
# The compare option will compare the 2 versions.
# It is essential to clean out the previous objects when switching between
# the two libraries.

# Nonstandard library directories to be built. Comment out the lines not used.
# earlstdiodir=../e20		# Earl Chew's stdio
# localdir=../local

bad=0
cpu=$1
here=`pwd`
option=$2

case $cpu in
86)	asmstringdir=../string		# fastest 16-bit string functions
	bccsupportdir=86		# 16-bit compiler support
	cc="bcc -0"			# 16-bit compiler
	as="as -0 -a"			# 16-bit assembler (for asmstringdir)
	cpp="/usr/lib/cpp -P -Di8088"	# C preprocessor (for asmstringdir)
	libdir=/usr/local/lib/i86	# 16-bit library
	tmplibdir=$here/i86 ;;		# build 16-bit library here
386)	bccstringdir=string		# fastest 32-bit string functions
	bccsupportdir=386		# 32-bit compiler support
	cc="bcc -3"			# 32-bit compiler
	libdir=/usr/local/lib/i386	# 32-bit libraries
	tmplibdir=$here/i386 ;;		# build 32-bit library here
*)	bad=1 ;;
esac

if test $bad -eq 1 -o $# -ge 3
then
	echo "usage: $0 (86|386) [(clean|compare|<target>)]"
	exit 1
fi

case $option in
install|"")	:
	option=install ;;
compare)	:
	cd $tmplibdir
	rm -rf tmpold tmpnew
	mkdir tmpold tmpnew
	cd tmpold
	ar x $libdir/libc.a
	cd ../tmpnew
	ar x ../libc.a
	for blech in '[a-e]' '[f-j]' '[k-o]' '[p-t]' '[u-z]' '*'
	do
		echo "$blech"
		for i in `ls $blech*.o 2>/dev/null`
		do
			if cmp -s $i ../tmpold/$i
			then
				rm $i ../tmpold/$i
			fi
		done
	done
	cd ..
	for i in `ls *.o 2>/dev/null`
	do
		if cmp -s $i $libdir/$i
		then
			rm $i
		fi
	done
	ls tmpold tmpnew
	exit 0 ;;
esac

case $cpu in
86)	if test -f stamp.386
	then
		echo -n "You must clean out the 32-bit objects before "
		echo "working with the 16-bit ones"
		exit 1
	else
		touch stamp.86
	fi ;;
386)	if test -f stamp.86
	then
		echo -n "You must clean out the 16-bit objects before "
		echo "working with the 32-bit ones"
		exit 1
	else
		touch stamp.386
	fi ;;
esac

# Filter out various unwanted and duplicated files using sed.
exclude=$tmplibdir/sed.in

# Miscellaneous junk. Some use floating point, which does not work.
echo	'
	s/^difftime.c$//
	s/^example.c$//
	s/^sincos.c$//
	' > $exclude

# These from $asmstringdir or $bccstringdir, not lib/ansi.
echo	'
	s/^memcmp.c$//
	s/^memcpy.c$//
	s/^memset.c$//
	s/^strcmp.c$//
	s/^strcpy.c$//
	s/^strcspn.c$//
	s/^strlen.c$//
	s/^strncat.c$//
	s/^strncmp.c$//
	s/^strncpy.c$//
	s/^strpbrk.c$//
	' >>$exclude

case "$asmstringdir" in
"")	: ;;
*)	# These from $asmstringdir, not lib/ansi.
   echo	'
	s/^memchr.c$//
	s/^memmove.c$//
	s/^strcat.c$//
	s/^strchr.c$//
	s/^strcoll.c$//
	s/^strrchr.c$//
	s/^strspn.c$//
	s/^strstr.c$//
	s/^strtok.c$//
	s/^strxfrm.c$//
	' >>$exclude ;;
esac

case "$earlstdiodir" in
"")	: ;;
*)	# These from $earlstdiodir, not the standard directories.
   echo	'
	s/^exit.c$//
	s/^fclose.c$//
	s/^fflush.c$//
	s/^fgetc.c$//
	s/^fgets.c$//
	s/^fopen.c$//
	s/^fprintf.c$//
	s/^fputc.c$//
	s/^fputs.c$//
	s/^fread.c$//
	s/^freopen.c$//
	s/^fscanf.c$//
	s/^fseek.c$//
	s/^ftell.c$//
	s/^fwrite.c$//
	s/^gets.c$//
	s/^printf.c$//
	s/^puts.c$//
	s/^scanf.c$//
	s/^setbuf.c$//
	s/^sprintf.c$//
	s/^sscanf.c$//
	s/^tmpnam.c$//
	s/^ungetc.c$//
	s/^vsprintf.c$//
	s/^cleanup.c$//
	s/^doprintf.c$//
	s/^printdat.c$//
	s/^ctermid.c$//
	s/^cuserid.c$//
	s/^fdopen.c$//
	' >>$exclude ;;
esac

# Finish off exclusion file with pattern to convert to object names.
echo 's/c$/o/' >>$exclude

for dir in ../ansi ../ibm ../other ../posix $earlstdiodir $asmstringdir \
	$localdir \
	$bccstringdir $bccsupportdir
do (
	echo $dir
	cd $dir
	case $dir in
	$asmstringdir)	:
		objs=`ls *.x 2>/dev/null | sed -e 's/strerror.x//; s/x$/o/'`
		make -f $here/makefile.proto $option \
			CC="cc$" LIBDIR="$tmplibdir" OBJS="$objs" \
				AS="$as" CPP="$cpp" ;;
	$bccstringdir|$bccsupportdir)	:
		make $option CC="$cc" LIBDIR="$tmplibdir" ;;
	$earlstdiodir)	:
		case $option in
		install)	:
			make stdio.a CC="$cc -c"
			case $cpu in
			386)	for specialclassfile in _z_cvt.o _z_tvc.o \
					fprintf.o fscanf.o printf.o scanf.o \
					sprintf.o sscanf.o vfprintf.o \
					vprintf.o vsprintf.o
				do
					zap1 $specialclassfile 15 1
				done ;;
			esac
			ar r $tmplibdir/libc.a \
				`cat Liborder | sed -e 's/perror.o//'` ;;
		*)	make $option ;;
		esac ;;
	*)	objs=`ls *.c 2>/dev/null | sed -f $exclude`
		make -f $here/makefile.proto $option \
			CC="$cc" LIBDIR="$tmplibdir" OBJS="$objs" ;;
	esac
) done

rm -f $exclude

case $option in
clean)	rm -rf stamp.$cpu $tmplibdir/tmpold $tmplibdir/tmpnew ;;
esac
