#ident	"@(#)eisa:add-on/scsi.in/install/Install	1.1"

#	SCSI Drivers Install Package
#

TMP=/tmp/scsidrv.err
ERROR1=" Errors have been written to the file $TMP."
rm -f $TMP > /dev/null 2>&1
NAME="`cat Name`"
PKG="SCSI Utility Package"
echo "\n\n\n\nInstalling ${NAME}\n\n"
if [ -x /etc/conf/bin/idcheck -a -x /etc/conf/bin/idbuild -a -x /etc/conf/bin/idinstall ]
then 
	:	
else
	message "The Installable Driver feature has been removed.  The ${NAME} cannot be installed."
	exit 1
fi

RELEASE=`uname -v`

RETVCHK=0
RETADD=0
RETDMA=0
#### Olivetti ESC-1 is first party DMA, no ports below 0x400 used.
DMAC=-1
IOA=0
IOE=0

DRVLIST=""
# find out which drivers are to be installed
VANSWER=""
for DRVNAME in scsi sd01 st01 sc01 sw01
do
	message -d "Do you want to install '${DRVNAME}' driver?(y or n)"
	read VANSWER
	if [ "x$VANSWER" = xY -o "x$VANSWER" = xy ]
	then
		DRVLIST="${DRVLIST} ${DRVNAME}"
	fi
done

for DRIVER in ${DRVLIST}
do
	/etc/conf/bin/idcheck -p $DRIVER > /dev/null 2>&1
	RETTP=$?
	if [ $RETTP != 0 ]
	then
		break 2
	fi
done

if [ $RETTP = 0 ]
then
	################
	# find out if acceptable interrupts are available and then
	# prompt the user to select one
	VECTMSG=""
	for VECT in 5 10 11 15
	do
		/etc/conf/bin/idcheck -v $VECT >/dev/null 2>&1
		RETVCHK=$?
		if [ "$VECT" = 9 ]
		then
			VECT=2
		fi
		if [ "$RETVCHK" = 0 ]
		then
			VECTMSG="${VECTMSG}\n\tIRQ ${VECT}"
			VECTLIST="${VECTLIST} ${VECT}"
		fi
	done
	
	if [ "$VECTLIST" != "" ]
	then
		while true
		do
			VANSWER=""
			message -d "Available interrupt(s) for the ${NAME}are:\
\n ${VECTMSG}\n\nType the interrupt number(default 11) and strike the ENTER \
key or type Q to cancel installation."
			read VANSWER
			if [ "x$VANSWER" != xQ -a "x$VANSWER" != xq ]
			then
				for i in $VECTLIST
				do
					if [ "x$VANSWER" = "x$i" ]
					then
						INT="$VANSWER"
						break 2
					fi
				done
			else
				exit 1
			fi
			message -d "Invalid Answer"
		done
	fi
        #####################

fi

ERROR=""
if [ $RETTP != 0 ]
then 
	ERROR="has already been installed.  You cannot install this package \
on top of the old package.  You must remove the old ${PKG} before you can \
install the new one"

else
	if [ "$VECTLIST" = "" ]
	then
		ERROR="cannot be installed because interrupts 9, 10, 11, 12, 14\
 and 15 are in use"
		if [ $RETADD != 0 -o $RETDMA != 0 ]
		then
			ERROR="${ERROR} and"
		fi
	fi
	if [ $RETADD != 0 ]
	then 
		ERROR="${ERROR} cannot be installed because all usable addresses are in use"
			ERROR="${ERROR} and"
	fi
	if [ $RETDMA != 0 ]
	then 
		ERROR="${ERROR} cannot be installed because DMA Channel \
0, 5, 6, and 7 are in use"
	fi
fi

if [ \( "$VECTLIST" = "" \) -o $RETADD != 0 -o $RETDMA != 0 -o $RETTP != 0 ]
then
	message "The ${PKG} ${ERROR}.  \
Use Remove Installed Software to remove the existing driver before attempting \
to install the ${NAME} again."
	exit 1
fi

message -c "You are installing the ${NAME} \
using hardware interrupt number ${INT}. \
Be sure that these values are not in use by another add-on board."

if [ $? != 0 ]
then
	exit 1
fi

# Make sure to switch interrupt 2 to 9 so that idbuild will not fail.
if [ "$INT" = 2 ]
then
	INT=9
fi

sed -e "s/IOA/${IOA}/" -e "s/IOE/${IOE}/" -e "s/INT/${INT}/" \
< scsi/System > Sys.$$
cat Sys.$$ > scsi/System
rm -f Sys.$$

sed -e "s/DMA/${DMAC}/" < scsi/Master > Sys.$$
cat Sys.$$ > scsi/Master
rm -f Sys.$$

if [ $RETTP != 0 ]
then
	chmod +x Remove
	./Remove NOBUILD
fi

############
echo "\nInstalling '${DRVLIST}' drivers."
for DIR in ${DRVLIST}
do
	cd $DIR
	/etc/conf/bin/idinstall -a $DIR 2>> $TMP
	if [ $? != 0 ]
	then
        	message "The installation cannot be completed due to an error\
 in the '$DIR' driver installation. $ERROR1  Please try the installation again.\
 If the error occurs again, contact your AT&T Service Representative or \
AT&T Personal Computer Dealer."
		chmod +x ../Remove
		../Remove NOBUILD
		exit 1
	fi
	cd ..
done

echo "\nInstalling /usr/include/sys files."
cd sys
chmod 444 *
chown bin *
chgrp bin *
mv * /usr/include/sys

echo "\nInstalling /etc/scsi files."
cd ../cmd
	[ -d /etc/scsi ] || mkdir /etc/scsi
	find . -print|cpio -pdu /etc/scsi
	mv /etc/scsi/DISK /etc/scsi/format.d
cd ..

echo "\nComplete.\n\n\n"

/etc/conf/bin/idbuild 2>> $TMP
if [ $? != 0 ]
then
        message "The installation cannot be completed due to an error in the \
kernel reconfiguration. $ERROR1  Please try the installation again.  If the \
error occurs again, contact your AT&T Service Representative or AT&T Personal \
Computer Dealer."
	chmod +x Remove
	./Remove NOBUILD
        exit 1
fi

rm -f $TMP
exit 0
