: ${SHOME?} ${I?} ${QPE?}

# specialize S to a particular machine architecture and operating system
# uses files in $SHOME/machines/*

echo "Which of the following most accurately describes your computer?"
ls $SHOME/machines
read TYPE
if test "$TYPE" = "other" -o ! -d $SHOME/machines/$TYPE;
	then
	echo You got troubles, buddy
	echo 'Prepare your own versions of $I/u/mach.m and $QPE/system.h'
	exit 1
	fi
if test -f $SHOME/machines/$TYPE/mach.m
then
	rm -f $I/u/mach.m 2>/dev/null
	cp $SHOME/machines/$TYPE/mach.m $I/u
else
	echo "Sorry, no mach.m file for this machine; make one yourself"; exit 1
fi
if test -f $SHOME/machines/$TYPE/system.h
then
	rm -f $QPE/system.h 2>/dev/null
	cp $SHOME/machines/$TYPE/system.h $QPE
else
	echo "Sorry, no system.h file for this machine; make one yourself"; exit 1
fi
chmod u+w $SHOME/ENVIRONMENT $QPE/system.h $I/u/mach.m
OPDEF=
# pick up OpSys value from mach.m file if present
OPSYS=`grep OpSys $I/u/mach.m | sed -e 's/.*,//' | tr -cd 'A-Za-z0-9_.'`
echo "OpSys=$OPSYS" >>$SHOME/ENVIRONMENT
while test "$OPSYS" = ""
do
	echo "Which of the following is your Operating System?"
	echo "1 - UNIX System V"
	echo "2 - Berkeley 4.[123]BSD"
	echo "3 - Ultrix"
	echo "4 - Research (Ninth,... Edition) UNIX"
	echo "5 - Seventh Edition or earlier, 32V"
	echo "6 - Other"
	read SYSTEM
	if test "$SYSTEM" = "6" ;
		then
		echo You got troubles, buddy
		echo 'Prepare your own versions of $I/u/mach.m and $QPE/system.h'
		echo 'Pay particular attention to the Fortran-C Interface'
		exit 1
		fi
	case $SYSTEM in
	1)	OPSYS=ATT; OPDEF=ATT_UNIX;;
	[23])	OPSYS=Berkeley; OPDEF=Berkeley;;
	4)	OPSYS=Research; OPDEF=Research;;
	5)	OPSYS=Old;;
	esac
done
echo "OpSys=$OPSYS" >>$SHOME/ENVIRONMENT

if test "$OPDEF" != ""
then
	echo "#define $OPDEF 1" >>$QPE/system.h

	ed - $I/u/mach.m <<!
\$i
define(\`OpSys',$OPSYS)
.
w
!
fi

case $OPDEF in
Berkeley)
ed - $I/u/mach.m <<!
/MAIN__/s//MAIN_/
/MAX_FILE_NAME_LEN/s/14/255/
w
!
ed - $QPE/system.h <<!
/MAX_FILE_NAME_LEN/s/14/255/
w
!
esac

cd $SHOME/src/qpe
make machine.h

echo ""
nfiles=`ls $SHOME/machines/$TYPE|wc -w`
if test -r $SHOME/machines/$TYPE/README
then
	echo "Read the file $SHOME/machines/$TYPE/README"
	echo "for information about the installation process."
elif test $nfiles -gt 2
then
	echo "There are files in $SHOME/machines/$TYPE that may help"
	echo "in the installation process."
else
	echo "There may be files in $SHOME/doc with hints on the installation"
	echo "process for your machine and operating system."
fi
echo ""
echo 'RIGHT NOW execute 
. ENVIRONMENT
in the S HOME directory'
