#!/bin/sh
#
# config 1.1 - Choose among the K&R and Ansi compilers

case $1 in
kr|KR)
	if test -d ansi
	then
		echo "config: Already set up for the K&R compiler"
		exit 1
	fi

	echo mkdir ansi
	mkdir ansi || exit

	echo mv Makefile bootblock.s boothead.s masterboot.s ansi
	mv Makefile bootblock.s boothead.s masterboot.s ansi || exit

	echo "sed ... < ansi/Makefile > Makefile"
	sed	-e '/^# ACK A/,/^# end/s/^/#/' \
		-e '/^## ACK K/,/^## end/s/^#//' < ansi/Makefile > Makefile \
								|| exit

	for s in ansi/*.s
	do
		x=`basename $s .s`.x
		# The sed script is based on the xenix_to_asld script
		# found among the 1.5 to 1.6.16 patches.
		echo "sed ... < $s > $x"
		sed -e '
			s/^[ 	][ 	]*//
			s/[!].*//
			s/[ 	]*$//
			h
			s/^[_a-zA-Z][_a-zA-Z0-9]*:[ 	]*//
			s/\.data1/.byte /g
			s/\.data2/.word /g
			s/\.data4/.long /g
			s/(b\([xp]\)),(\([sd]\)i)/(b\1_\2i)/g
			s/^\([ecsd]\)seg$/seg \1s/g
			s/^inb\([ 	]*\)/in\1/g
			s/^outb\([ 	]*\)/out\1/g
			s/^movsb$/movb/g
			s/^movs$/movw/g
			s/^stosb$/stob/g
			s/^stos$/stow/g
			s/^lodsb$/lodb/g
			s/^lods$/lodw/g
			s/^scasb$/scab/g
			s/^scas$/scaw/g
			s/^callf[ 	][ 	]*/calli /g
			s/^jmpf[ 	][ 	]*/jmpi /g
			s/^retf$/reti/
			s/^jmp[ 	][ 	]*\([_a-zA-Z]\)/j \1/g
			s/^br[ 	][ 	]*/jmp /g
			H
			g
			/^[_a-zA-Z][_a-zA-Z0-9]*:/{
			s/:.*\n/: /
			s/^/\
			/
			}
			s/^.*\n//
			s/^[ 	]*//
			/^$/d' < $s > $x || exit
	done
	;;
ansi|ANSI)
	if test ! -d ansi
	then
		echo "config: Already set up for the ANSI compiler"
		exit 1
	fi

	echo rm bootblock.x boothead.x masterboot.x
	rm bootblock.x boothead.x masterboot.x || exit
	echo "mv ansi/* ."
	mv ansi/* . || exit
	echo rmdir ansi
	rmdir ansi || exit
	;;
*)
	echo "Usage: config ansi|kr" >&2; exit 1
esac
