usage()
{
	echo usage: $0 \[pm100\|pm200\] \<install\> \<clobberfirst\> \<-O\>
	exit 1
}
while :; do
	case $1 in
		"")
			break;;
		pm100|pm200)
			CPU=$1
			shift;;
		install)
			INSTALL=install
			shift;;
		clobberfirst)
			CLOBBER=Y
			shift;;
		-O)
			OPT="DASHO=-O"
			shift;;
		*)
			usage;;
	esac
done
if [ "$CPU" = "" ]; then
	echo CPU type required
	usage
fi

CC_SIZE=`ls -l /bin/cc 2>/dev/null | awk '{ print $5 }'`
PCC_SIZE=`ls -l /bin/cc.pcc 2>/dev/null | awk '{ print $5 }'`
CTP_SIZE=`ls -l /bin/cc.ctp 2>/dev/null | awk '{ print $5 }'`
if [ "$CC_SIZE" = "$CTP_SIZE" ]; then
	echo "\n\007The ctp compiler is installed as /bin/cc." > /dev/tty
	echo "The pcc compiler should be installed.\n" > /dev/tty
	exit 1
elif [ "$CC_SIZE" != "$PCC_SIZE" ]; then
	echo "\n\007Check the file /bin/cc." > /dev/tty
	echo "It doesn't match /bin/cc.pcc or /bin/cc.ctp." > /dev/tty
	exit 1
fi

while [ "$CLOBBER" = Y ]; do
	#echo "\nWould you like to clobber the pm100 and pm200 directories" > /dev/tty
	#echo "in addition to the regular clobber? [y/n]: \c" > /dev/tty
	#read ANS
	#case $ANS in
		#y)
			#rm -f pm100/*.o pm100/[1-9]?lib.* pm100/unix
			#rm -f pm200/*.o pm200/[1-9]?lib.* pm200/unix;;
		#n)
			#:;;
		#*)
			#echo "\n\007Invalid response." > /dev/tty
			#continue;;
	#esac
	#echo
	make -f unix.mk CPU=$CPU clobber
	break
done

make -f unix.mk INCRT=`pwd` CPU=$CPU $OPT
if [ $? -ne 0 ]; then
	exit 1
fi
if [ "$INSTALL" = install ]; then
	cd cf
	if [ $CPU = "pm100" ]
	then
		rm -f pm100/100lib.bsd
	fi
	make -f cf.mk fast CPU=$CPU $INSTALL
	cd ..
	mcs -d ${CPU}/unix
else
	cd cf
	if [ $CPU = "pm100" ]
	then
		rm -f pm100/100lib.bsd
	fi
	make -f cf.mk fast CPU=$CPU
	cd ..
#	mcs -d ${CPU}/unix
fi
INCRT=`pwd`
echo "'unix' built into the directory ${INCRT}/${CPU}"
sync
