#
#	386 WINS Install Package
#

# REWIND=/dev/rmt1
REWIND=$1
# DRIVE=/dev/rmt0
DRIVE=$2

TMPDIR=`pwd`
TMPERR=/tmp/win.ierr
ERROR1=" Errors have been written to the file $TMPERR."
NAME="`cat tcp.name`"
rm -f $TMPERR 

# Prompt for yes or no answer - returns non-zero for no
askyn() {
	while	echo "$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		     [yY]) return 0 		                  ;;
	             [nN]) return 1		                  ;;
		        *) echo "\nPlease answer y, n, or q" >&2  ;;
		esac
	done
}

preserveit() {
	if [ -s $1 ]
	then
		echo "Preserving $1 as $2"
		mv $1 $2
	fi
}

# main
#

echo "Installing `cat ./tcp.name` (C) Copyright 1990, 1991   ARIX Corporation"

# Check for AT&T TLI library package
#
grep "Networking Support Utilities" /usr/options/*name > /dev/null 2>&1
if [ $? != 0 ]
then
	echo "The Network Support Utilities package must be installed"
	echo "before the ${NAME} package can be installed."
	echo "Please install the Network Support Utilities Package"
	echo "first, and then install this package."
	exit 1
fi

if [ ! \( -f /usr/options/ln.name -a -f /usr/options/lwdb.name \) ]
then
	echo "The LANWAN device driver package must be installed"
	echo "before the ${NAME} package can be installed."
	echo "Please install the LANWAN device driver Package"
	echo "first, and then install this package."
	echo "Installation aborted."
	exit 1
fi

# Check for ARIX shared STREAMS Driver package
#
grep "STREAMS Drivers" /usr/options/*name > /dev/null 2>&1
if [ $? != 0 ]
then
	echo "The ARIX Shared STREAMS Drivers Package must be installed"
	echo "before the ${NAME} package can be installed."
	echo "Please install the ARIX Shared STREAMS Drivers Package"
	echo "first, and then install this package."
	exit 1
fi

#
# if it appears that a conflicting set of drivers is installed, then ask
# the user to double check that he wants to do this
#

if [ -f /usr/options/cmc.name ]
then
	echo "The CMC package is presently installed on this system."
	echo "It must be uninstalled before the ${NAME} package"
	echo "can be installed.   Installation aborted."
	exit 1
fi

if [ -s /usr/options/tcp.name ]
then
	echo "The ${NAME} package appears to be already installed."
	askyn "Do you wish to continue ?" || {
		echo $ERROR2
		exit 1
	}
fi

#
# Determine space requirements for WIN3B
#
ROOTneeds=50
USRneeds=4500
#
# How much space is available in the necessary filesystems?
#
USRspace=`expr "\`df /usr 2>/dev/null\`" : '.*: *\([0-9]*\)'`
ROOTspace=`expr "\`df / 2>/dev/null\`" : '.*: *\([0-9]*\)'`
fatal=0
if [ "${USRspace}" -lt "${USRneeds}" ]
then
    fatal=1
    echo "**ERROR** ${NAME} cannot be installed --"
    echo "Not enough space on the hard disk."
    echo "There are $USRspace blocks available on the /usr file system --"
    echo "$USRneeds blocks are needed."
fi    
if [ "${ROOTspace}" -lt "${ROOTneeds}" ]  
then
    if [ $fatal -eq 0 ]
    then
	    echo "**ERROR** ${NAME} cannot be installed --"
	    echo "Not enough space on the hard disk."
    fi
    echo "There are $ROOTspace blocks available on the / (root) file system --"
    echo "$ROOTneeds blocks are needed."
    exit 1
fi
if [ $fatal -eq 1 ]
then
    exit 1
fi

cp tcp* /usr/options

preserveit	/etc/hosts		/etc/hosts.SAVE
preserveit	/etc/hosts.equiv	/etc/hosts.equiv.SA
preserveit	/etc/networks		/etc/networks.SAVE
preserveit	/.rhosts		/.rhosts.SAVE
preserveit	/usr/etc/inetinit.cf	/usr/etc/inetinit.cf.SA

# At this point the install scripts should have been read in via sysadm 
# installpkg.  The tape has been rewound, so skip past the first file.
< $DRIVE

# now read in the rest
echo "Installing user programs, demons, and include files..."
cd /
cpio -iBdmuv < $REWIND 2>> $TMPERR
CPIOERRS=`grep -c Cannot $TMPERR`
cd $TMPDIR

# if there were errors while reading the tape, let the user know and quit
# here.

if [ $CPIOERRS -ne 0 ]
then
	echo  $ERROR1
	echo  $ERROR2
	exit 1
fi

#
# customize WIN/TCP network
#

UNAME=`uname -n`
OLDUNAME=$UNAME
LDOMAIN=

#
# Setup various network files
#

while true
do
    echo "The system name is $UNAME, is this what you want? (default n) [y, n]: \c"
    read ans
    if [ "$ans" = "y" ]
    then
	break
    fi
    UNAME=
    while [ "$UNAME" = "" ]
    do
	    echo 'Please enter the new system name (without domain name) > \c'
	    read UNAME
    done
done

ed -s /etc/system <<EOE 
/^NODE/s/^.*$/NODE			\"$UNAME\"		# system\'s network node name/
w
q
EOE

while
	test "$LDOMAIN" = ""
do
	echo "enter name of domain containing this host > \c"
	read LDOMAIN
done

# change node name in running system 
echo "changing name of system to $UNAME"
/local/bin/setnode -i -n $UNAME /arix
/usr/bin/hostname $UNAME
/usr/bin/hostid 0

#
# edit the rhosts and sendmail/mailx files
#

# .rhosts and hosts.equiv files

echo "me" > /.rhosts
echo "me" > /etc/hosts.equiv
echo "$UNAME" >> /.rhosts
echo "$UNAME" >> /etc/hosts.equiv

echo "Creating device nodes\n"
sh ./mknodes

echo "\n"
sh ./setup_driver "$UNAME" "$LDOMAIN"

echo "\n"
sh ./setup_demons "$UNAME" "$LDOMAIN"

echo "\n"
sh ./setup_rfs "$UNAME" "$LDOMAIN"

# SETUP SENDMAIL AND SENDMAIL.CF

sh ./setup_sendmail "$UNAME" "$LDOMAIN"

# SETUP SPECIAL NETWORK FILES
# 	/etc/hosts
#	/etc/hosts.equiv
#	/etc/networks
#	/.rhosts 
#	/usr/etc/inetinit.cf

hostssave="n"
if [ -r /etc/hosts.SAVE -o -r /etc/hosts.equiv.SA -o -r /etc/networks.SAVE \
  -o -r /.rhosts.SAVE   -o -r /usr/etc/inetinit.cf.SA ]
then
	askyn "Do you wish to use the saved NETWORK and HOST database files?" && {

	hostssave=y;
	echo "restoring: "
	if [ -r /etc/hosts.SAVE ]
	then
		echo "/etc/hosts " 
		mv /etc/hosts.SAVE /etc/hosts
	fi

	if [ -r /etc/hosts.equiv.SA ]
	then
		echo "/etc/hosts.equiv " 
		mv /etc/hosts.equiv.SA /etc/hosts.equiv
	else
		echo "me" > /etc/hosts.equiv
		echo "$lname" >> /etc/hosts.equiv
	fi

	if [ -r /etc/networks.SAVE ]
	then
		echo "/etc/networks " 
		mv /etc/networks.SAVE /etc/networks
	fi

	if [ -r /.rhosts.SAVE ]
	then
		echo "/.rhost " 
		mv /.rhosts.SAVE /.rhosts
	else
		echo "me" > /.rhosts
		echo "$lname" >> /.rhosts
	fi

	if [ -r /usr/etc/inetinit.cf.SA ]
	then
		echo "WARNING: if you are installing ARIX/TCP on a host that previously"
		echo "had an older version of ARIX/TCP, you must use the new inetinit.cf"
		echo "from WIN/TCP"
		askyn "Are you installing over an older version?" && {
			echo "using the installed version of inetinit.cf "
		} || {
			echo "restoring saved /usr/etc/inetinit.cf " 
			mv /usr/etc/inetinit.cf.SA /usr/etc/inetinit.cf
		}
	fi
}
fi


askyn "Do you wish to re-make the kernel at this time?" && {
	cd /usr/sys
	make install
	if [ $? -ne 0 ]
	then	echo "The kernel make failed."
		echo "You must correct the problem and rebuild the kernel\c"
		echo "before TCP/IP can be run."
		exit 1
	fi
}

rm -f $TMPERR
exit 0
