# SCCS_ID - "%W%	%E%"
#
#   Copyright (c) 1985  The Wollongong Group, Inc.  All Rights Reserved
#
ETHERDIR=/usr/ethernet
DRVLIST=/etc/lddrv/InstDrv
TMPFILE=/tmp/win$$
RCFILE=/etc/lddrv/ether.rc
DRIVER=ether
Basename="WIN/3B"
SETUPFORM=$ETHERDIR/ua/Setup.form
HLPMSG="Enter the host name and the internet address for this machine. The hostname and internet address must be unique across your network. The hostname can be at the most 14 characters and must contain only alphanumeric characters. The format of the internet address is:  \"nn.nn.nn.nn\"  where \"nn\" is any number between 0 and 255. If you are not sure what your internet address should be, please consult the Administrator's Guide"

Name=`cat ./Name`
if [ ! -s /etc/lddrv/lddrv ]		# need loadable driver stuff
then
	message "ERROR #1:You need to install UNIX PC System Software Version 3.0 before installing the $Name Package. Aborting $Name installation."
	message "Please select 'Remove Installed Software' and remove the $Name package.  IGNORE the message that installation is complete.  The installation of this package has been ABORTED!!"
	exit 1
fi
				# package shouldn't be in installed list yet
if grep "$Basename" $UA/Uninstall.menu > /dev/null
then
	message "ERROR #2:The $Basename package has been installed before. To install this version, remove the current installation.  Aborting $Name Installation.  IGNORE the message that installation is complete.  The installation of this package has been ABORTED!!"
	exit 64
else
				# Name MAY appear in CONTENTS more than once!	
				# Last entry is ours.  First field is name of
				# Remove script.
	RET=`grep "$Name" /usr/installed/CONTENTS |tail -1`
	set $RET
	Rmv=$1
				# no prev or current release installed. Whew!
				# Make sure Name appears only once in CONTENTS
		grep -v "$Name" /usr/installed/CONTENTS >/tmp/cnt$$
		mv /tmp/cnt$$ /usr/installed/CONTENTS
		echo "$RET" >>/usr/installed/CONTENTS
		Remove=/usr/installed/${Rmv}.un
		echo "\n\n\n\n\t            AT&T UNIX PC Enhanced TCP/IP\n\t          ${Name}\n\t              Copyright (c) 1986 AT&T\n\t                All Rights Reserved\n\n\n"
fi

#
# to update the master file, find four CONSECUTIVE slots for character devices
# free in the /etc/master file.
#

# Remove old entries if any
/etc/masterupd -d ether 2>/dev/null
/etc/masterupd -d dnet 2>/dev/null
/etc/masterupd -d ptc 2>/dev/null
/etc/masterupd -d pts 2>/dev/null


N=`/etc/masterupd -l | awk '/^Character D/,/^Non dev/ { if( $1 > 0 && $1 < 99 ) print $1 }'`
for i in $N
do
	L1=`expr $i + 1`
	L2=`expr $i + 2`
	L3=`expr $i + 3`
	L4=`expr $i + 4`
	for j in $N
	do
		if [ $j -eq $L1 -o $j -eq $L2 -o $j -eq $L3 -o $j -eq $L4 ]
		then
			continue 2
		fi
	done
	break
done
NetMajor=$L1
DnetMajor=$L2
PtsMajor=$L3
PtcMajor=$L4
#
/etc/masterupd -a -p Net_ -C $NetMajor char init release open close read write ioctl $DRIVER
/etc/masterupd -a -p Dnet -C $DnetMajor char open close ioctl dnet
/etc/masterupd -a -C $PtsMajor char open close read write ioctl pts
RET=`masterupd -a -C $PtcMajor char open close read write ioctl ptc 2>&1`
if [ "$?" != "0" ]
then
	if [ "$RET" = "too many character devices" ]
	then
		message "ERROR #4:The maximum number of loadable drivers has already been added.  You must remove a loadable driver package before installing this package.  Aborting $Name Installation."
	else
		message "ERROR #5:There is a problem installing the $Name package. Cannot create loadable driver entry - ${RET}. Consult the UNIX PC ETHERNET User's Guide, or inform your system administrator. Aborting $Name installation."
	fi
	message "Please select 'Remove Installed Software' and remove the $Name package.  IGNORE the message that installation is complete.  The installation of this package has been ABORTED!!"
	exit 1
fi

# Remove old device nodes if any
rm -f /dev/net /dev/tcp /dev/udp /dev/[pt]typ[0-9]
#
# Make nodes
#
/etc/mknod /dev/net c $DnetMajor 0
/etc/mknod /dev/tcp c $NetMajor 1
/etc/mknod /dev/udp c $NetMajor 2
#
# Make nodes for pseudo tty driver
#
/etc/mknod /dev/ttyp0 c $PtsMajor 0
/etc/mknod /dev/ttyp1 c $PtsMajor 1
/etc/mknod /dev/ttyp2 c $PtsMajor 2
/etc/mknod /dev/ttyp3 c $PtsMajor 3
/etc/mknod /dev/ttyp4 c $PtsMajor 4
/etc/mknod /dev/ttyp5 c $PtsMajor 5
/etc/mknod /dev/ttyp6 c $PtsMajor 6
/etc/mknod /dev/ttyp7 c $PtsMajor 7
#
#
/etc/mknod /dev/ptyp0 c $PtcMajor 0
/etc/mknod /dev/ptyp1 c $PtcMajor 1
/etc/mknod /dev/ptyp2 c $PtcMajor 2
/etc/mknod /dev/ptyp3 c $PtcMajor 3
/etc/mknod /dev/ptyp4 c $PtcMajor 4
/etc/mknod /dev/ptyp5 c $PtcMajor 5
/etc/mknod /dev/ptyp6 c $PtcMajor 6
/etc/mknod /dev/ptyp7 c $PtcMajor 7

chmod 666 /dev/?typ? /dev/tcp /dev/udp /dev/net

if [ -f $DRVLIST ]
then
	cp $DRVLIST $UA/tmp$$
	uaupd -r "Ethernet" tmp$$
	mv $UA/tmp$$ $DRVLIST 
fi

cat >>$DRVLIST <<END
Name=Ethernet
File=$DRIVER
Comment=driver for ETHERNET and accessories by AT&T
END

#
#  Install files depending on the release
#
release=`uname -v`
rel1=`expr $release : '\(.\)'`
rel2=`expr $release : '.\.\(.\)'`
relno=${rel1}${rel2}
if [ $relno -gt 30 ]
then
	# Unix System 5 Version 3.5 or newer
	( cd new ; find . -print | cpio -pdum .. 2>/dev/null )
fi

echo "Installing files"
ret=`cat ./Files | cpio -pdulm / 2>&1`
if [ $? -ne 0 ]
then
	message "ERROR #7:There is a problem installing the $Name package. Cannot install needed files - ${ret}. Consult the UNIX PC ETHERNET User's Guide, or inform your system administrator. Aborting $Name installation."
	message "Please select 'Remove Installed Software' and remove the $Name package.  IGNORE the message that installation is complete.  The installation of this package has been ABORTED!!"
	exit 1
fi
#
# ETHER ADMINISTRATION
echo "Adding ETHERNET administration"
				# add entry to install's Administration window
cat >$TMPFILE <<END
Name = Ethernet Setup
Default = Open
Open = UA Ether-Setup
Help = EXEC -d /usr/bin/uahelp -h /usr/ethernet/ua/ether.hlp -t "Enet Setup"
END

mv /u/install/Administration $UA/insAdm$$
uaupd -r "Ethernet Setup" -a $TMPFILE insAdm$$ 
mv $UA/insAdm$$ /u/install/Administration
rm -f $TMPFILE
				# add entry to Office window
cat >$TMPFILE <<END
Name=Ethernet
Default=Open
Open=UA Ethernet
Help=EXEC -d /usr/bin/uahelp -h /usr/ethernet/ua/ether.hlp -t "Enet Services"
END

uaupd -r "Ethernet" -a $TMPFILE Office 
rm -f $TMPFILE

UNAME=`uname -n`
#
# SETUP
#
export UNAME
while true
do
	RET=`shform $SETUPFORM`
	set $RET
	case $1 in
	\'Enter\')
		eval netname=$2
		eval netnumber=$3
		eval sysname=$4
		eval inaddr=$5
		eval aliases=$6
		eval AutoLoad=$7
		if [ "$sysname" = "" -o "$inaddr" = "" ]
		then
			clear
			message "Hostname and Internet address must be specified"
			continue
		fi
		if [ "$sysname" = "UNIXPC" ]
		then
			message -i "The ETHERNET package requires each machine to have a unique name. This name is used for all network activity and for all mail to your machine.\n\nPress Enter to continue."
			continue
		fi
		echo "$netname		$netnumber" >> /etc/networks
		echo "$inaddr	$sysname	$aliases" >> /etc/hosts
		echo "Name=$sysname" >> /usr/ethernet/ua/HostTable
		sed "/^HOSTNAME.*/s//HOSTNAME=$sysname/" $RCFILE > $TMPFILE
		cp $TMPFILE $RCFILE
		rm -f $TMPFILE
		break;
		;;
	\'Help\')
		message -i "$HLPMSG"
		;;
	*)
		message "Invalid key pressed"
		;;
	esac
done
#
# Do sendmail Setup
#
sed "/^CH.*/s//CH$sysname $aliases/" /usr/lib/sendmail.cf > $TMPFILE
cp $TMPFILE /usr/lib/sendmail.cf
rm -f $TMPFILE

ln /usr/lib/sendmail /usr/bin/mailq
echo > /usr/lib/aliases.dir
echo > /usr/lib/aliases.pag
chmod 644 /usr/lib/aliases*

mkdir /usr/spool/mqueue /usr/spool/rwho
chmod 777 /usr/spool/mqueue /usr/spool/rwho

#
# edit the shutdown file so it resets the board
#
ed - /etc/shutdown <<EOF
0a
ifconfig en0 down > /dev/null 2> /dev/null
.
w
q
EOF

if [ "$AutoLoad" = "YES" -o "$AutoLoad" = "yes" -o "$AutoLoad" = "Yes" ]
then
	grep -v "$DRIVER" /etc/lddrv/drivers > $TMPFILE
	cp $TMPFILE /etc/lddrv/drivers
	echo "$DRIVER" >> /etc/lddrv/drivers
	rm -f $TMPFILE

	clear
	message -iu "Loading ETHERNET driver.\n\nPlease wait....." &
	cd /etc/lddrv
	MSG=`/etc/lddrv/lddrv -av $DRIVER 2>&1`
	if [ "$?" != "0" ]		# if can't load driver, inform user
	then
		message "ERROR #8The $Name package is installed, but ETHERNET Driver can not be loaded  - $MSG.  Consult the UNIX PC ETHERNET User's Guide, or inform your system administrator."
	else				# initialize driver
		$RCFILE
		if [ "$?" != "0" ]	# if initializing fails tell user
		then
			message "ERROR #9:Cannot initialize ETHERNET.  Make sure that $RCFILE contains the correct hostname/internet-address. Ethernet driver is being unloaded"
			./lddrv -d $DRIVER
		fi
	fi

fi
exit 0
