#!/bin/sh
#
#	@(#)install_snc	1.9 93/05/17 Copyright (C) 1992  Sun Microsystems, Inc
#
#	Copyright (c) 1991, Sun Microsystems, Inc.  All Rights Reserved.
#	Sun considers its source code as an unpublished, proprietary
#	trade secret, and it is available only under strict license
#	provisions.  This copyright notice is placed here only to protect
#	Sun in the event the source is deemed a published work.  Dissassembly,
#	decompilation, or other means of reducing the object code to human
#	readable form is prohibited by the license agreement under which
#	this code is provided to the user or company in possession of this
#	copy.
#
#	RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
#	Government is subject to restrictions as set forth in subparagraph
#	(c)(1)(ii) of the Rights in Technical Data and Computer Software
#	clause at DFARS 52.227-7013 and in similar clauses in the FAR and
#	NASA FAR Supplement.
#
#	This script is only for installation from CD-ROM. There is no other 
#	installation media.
#
#	DESCRIPTION:
#		Installs Sun Network CoProcessor on Sun-4/600MP systems
#
#	USAGE:
#		install_snc -mMOUNT_PT -pPRODUCT_DIR -tROOT_DIR
#
#	Exit status:
#		0	ok
#		1	usage
#		2	tar failed
#		3	error in making kernel
#		4	installation stopped at user request
#		5	bad product directory parameter passed in
#		6	user is not logged in as root
#		7	not enough disk space
#		8	directory not writable
#		9	bad mount point parameter passed in
#		10	incompatible SunOS release
#		11	invalid top directory parameter
#		12	incompatible architecture
#
INSTALL_DIR=`pwd`
export INSTALL_DIR
PATH=/usr/ucb:/bin:/usr/bin:/etc:/usr/etc:$INSTALL_DIR
export PATH
PROG=`basename $0`
DEINSTALL_DIR=/var/tmp/unbundled/SNC
KARCH=`arch -k`
ROOT_DIR=""
PARAMS=""
TAR="tar"
EXPR="/usr/bin/expr"
ANS=""
MOUNT_PT=""
PRODUCT_DIR=""
PAGECMD="more"
PRODUCT_FULL="Sun Network CoProcessor"
PROD_ABBREV="SNC"
PROD_VERSION=1.2
PRODUCT="$PROD_ABBREV $PROD_VERSION"
export PRODUCT PROD_ABBREV PROD_VERSION
MANUAL="Sun Nework CoProcessor Installation Manual"
MANPART="800-6881-10"
USAGE="$0 -mMOUNT_PT -pPRODUCT_DIR -tROOT_DIR" 

#########################################################################
# Check for the correct login
#########################################################################
installer=`whoami`
if [ "$installer" != "root" ]
then
	echo
	echo "You must be root to install this product."
	echo "Terminating installation..."
	exit 6
fi

# Parse the arguments
for param in $*;
do
	case "$param" in
	-m*) MOUNT_PT=`expr $param : '-m\(.*\)' '|' "1"`
	     if [ $MOUNT_PT -eq 1 ]
	     then
		echo
		echo "Invalid mount point."
		echo "Usage: $USAGE"
		exit 9
	     fi
	     ;;
	-p*) PRODUCT_DIR=`expr $param : '-p\(.*\)' '|' "1"`
	     if [ $PRODUCT_DIR -eq 1 ]
	     then
		echo
		echo "Invalid product directory."
		echo "Usage: $USAGE"
		exit 5
	     fi
	     ;;
	-t*) ROOT_DIR=`expr $param : '-t\(.*\)' '|' "1"`
	     if [ $ROOT_DIR -eq 1 ]
	     then
		echo
		echo "Invalid root directory."
		echo "Usage: $USAGE"
		echo "Terminating installation..."
		exit 11
	     fi
	     ;;
	*)   echo USAGE : $USAGE
	     echo "Terminating installation..."
	     exit 1;;
	esac
done

#########################################################################
# Determine the current operating system version
#########################################################################
if [ -x /usr/etc/dmesg ]
then
        set -- `/usr/etc/dmesg | grep SunOS | tail -1 | sed -e 's/^.*SunOS/SunOS/'`
        OS_VER=$3
fi

if [ -z "$OS_VER" -a -x /usr/ucb/strings ]
then
        set -- `/usr/ucb/strings /vmunix | grep SunOS | sed -e 's/^.*SunOS/SunOS/'`
        OS_VER=$3
fi

if [ -z "$OS_VER" ]
then
	# If we cannot determine the OS, we assume 4.1.3 instead of
	# hosing the installation
	OS_VER="4.1.3"
fi

#########################################################################
# Determine which product directory to use
# NOTE: Reseting value of PRODUCT_DIR
#########################################################################
INSTALL_DIR=${MOUNT_PT}/${PRODUCT_DIR}/_install
PRODUCT_DIR=${PRODUCT_DIR}/${PROD_ABBREV}_${PROD_VERSION}_BIN.${KARCH}

#########################################################################
# Print copyright to the screen for further verification
#########################################################################
clear
echo "The following product will be installed:"
if [ -r ${INSTALL_DIR}/copyright.r ]
then 
	$PAGECMD ${INSTALL_DIR}/copyright.r
else
	echo
	echo "$0 : Problem accessing copyright file."
fi
while [ \( "$ANS" = "" \) ]
do
	echo
	echo -n "Do you want to continue [y|n]? "
	read ANS
	case $ANS in
	y*) continue;;
	n*) echo
	    echo Terminating installation...
	    exit 4;;
	*)  echo
	    echo "Enter \"y\" or \"n\""
	    ANS="";;
	esac
done


###########################################################################
#
# get_yes_no(): Function for getting 'y' or 'n' response from user
#
##########################################################################
get_yes_no() {
read reply
while [ "$reply" != "y" ] && [ "$reply" != "n" ]
do
	echo
	echo -n '   Please reply "y" or "n" '
	read reply
done
}

###########################################################################
#
# yes() & no(): More functions for getting 'y' or 'n' response from user
#
##########################################################################
yes() {
while true
do
	echo -n "
$* [yn] "
	read yn
	case "$yn" in
	[Yy]*)	unset yn ; return 0 ;;
	[Nn]*)	unset yn ; return 1 ;;
	*)	echo '
Please answer "Yes" or "No".' ;;
	esac
done
}

no() {
while true
do
	echo -n "
$* [yn] "
	read yn
	case "$yn" in
	[Yy]*)	unset yn ; return 1 ;;
	[Nn]*)	unset yn ; return 0 ;;
	*)	echo '
Please answer "Yes" or "No".' ;;
	esac
done
}

check_tar_status(){
TAR_STATUS=$1
if [ $TAR_STATUS != 0 ]
then
	echo "Tar failed."
	echo "Terminating installation..."
	exit 2
fi
}

UL () {
  echo "X $*" | sed -e 's/^X //' -e "s/./${ULSTR}&/g"
}

########################################
# Shell function to get a random response (into variable $ANS)
########################################
Response () {
  while true; do
    echo -n "
$* "
    read ANS
    case "$ANS" in
      !*)	SubShell $ANS ;;
      *)	return 0 ;;
    esac
  done
}

############################################################################
#
# install_usr(): Function to install binaries on specified usr file system
#
# although this is called with just "/", the installation is done to both
# / and /usr
#
############################################################################
install_usr () {
USRFS=$1
# 
# Check whether directory is writable (may be read-only FS)
#
if [ ! -w ${USRFS} ]
then
	echo "Directory ${USRFS} does not permit writes."
	echo "Terminating installation..."
	exit 8
fi	

if [ ! -w ${USRFS}/usr ]
then
	echo "Directory ${USRFS}usr does not permit writes."
	echo "Terminating installation..."
	exit 8
fi	

manpgdir="/usr/man"
while true
do
	if [ ! -w "$manpgdir" ]
	then
		echo
		echo "Directory $manpgdir does not permit writes or doesn't exist."
		echo "Please enter the name of a writable directory"
		echo -n "in which to place man pages: "
		read manpgdir
	else
		break
	fi
done

#
# Check whether there is enough space to install SNC on / and /usr
#
set USR_SIZE `du -s $MOUNT_PT/$PRODUCT_DIR/usr`
USR_SIZE=$2
set TOTAL_SIZE `du -s $MOUNT_PT/$PRODUCT_DIR`
TOTAL_SIZE=$2
ROOT_SIZE=`expr $TOTAL_SIZE - $USR_SIZE`

availspace=`/bin/df / | awk '/\// {print $4}'`

if [ $availspace -lt $ROOT_SIZE ]
then
	echo ""
	echo "$PRODUCT requires $ROOT_SIZE kbytes on $USRFS."
	echo "$USRFS has $availspace kbytes available."
	echo "Cannot install $PRODUCT on $USRFS."
	echo
	echo "Terminating installation..."
	exit 7
fi

#
# Check whether there is enough space to install SNC on /usr
#
availspace=`/bin/df /usr | awk '/\/usr/ {print $4}'`

if [ "$availspace" -lt "$USR_SIZE" ]
then
	echo ""
	echo "$PRODUCT requires $USR_SIZE kbytes on /usr."
	echo "/usr has $availspace kbytes available."
	echo "Cannot install $PRODUCT on /usr."
	echo
	echo "Terminating installation..."
	exit 7
fi

cd ${USRFS}

#
# Install files in / and /usr
#

echo 
echo "Installing $PRODUCT from cd-rom"
echo 

cd $MOUNT_PT/$PRODUCT_DIR

echo
echo "Installing files in / ..."
echo

tar cf - etc sbin sys dev | (cd ${USRFS}; tar xvfBp -)
status=$?
check_tar_status $status

echo
echo "Installing files in /usr ..."
echo

tar cfX - manpgs usr | (cd ${USRFS}; tar xvfBp -)
status=$?
check_tar_status $status

echo
cd usr/man
echo "Installing man pages in $manpgdir ..."
echo

tar cf - . | (cd $manpgdir; tar xvfBp -)
cd ../..

}

#########################################################################
# END OF UTILITY FUNCTIONS... BEGIN INSTALLATION SCRIPT...
#########################################################################

			
#########################################################################
# Welcome message
#########################################################################
clear
echo
echo "This is the $PRODUCT_FULL installation script."
echo
echo "Software Requirements for $PRODUCT:"
echo
echo "	SunOS 4.1.3 must already be installed on your system."
echo "	$PRODUCT is not supported with earlier SunOS versions."
echo
echo "	The Sys category of SunOS must be installed in order to"
echo "	build custom kernels."
echo

if [ "$KARCH" != "sun4m" -a "$KARCH" != "sun4" ]
then
	echo "$PRODUCT not compatible with hardware architecture, $KARCH"
	echo "Terminating installation..."
	exit 12
fi

OS_VALUE=`echo $OS_VER | tr -d "."`
if [ "$OS_VALUE" -lt "413" ]
then
	echo
	echo "$PRODUCT is not compatible with SunOS $OS_VER."
	echo "Terminating installation..."
	exit 10
fi

echo "Hardware Requirements for $PRODUCT on the $KARCH architecture:"
echo "						Max Supported NC boards"
if [ "$KARCH" = "sun4m" ]
then
	MAX_NCS=8
	echo "	SPARCserver 690MP (4/690)			8"
	echo "	SPARCserver 670MP (4/670)			4"
	echo "	SPARCserver 630MP (4/630)			1"
else
	MAX_NCS=6
	echo "	SPARCserver 490 (4/490)				6"
	echo "	SPARCserver 470 (4/470)				4"
fi
echo
echo "$PRODUCT consists of utilities, data files, and object files"
echo "needed to build custom kernels."
echo

if no "Continue $PRODUCT installation?"
then
	echo
	echo "$PRODUCT installation terminated by user"
	exit 4
fi

#########################################################################
# Post process newfile and overwritten file names
#########################################################################
OVER_SRC=snc.overwritten
OVER=/tmp/${OVER_SRC}
NEW_SRC=snc.newfiles
NEW=/tmp/${NEW_SRC}

sed -e "s/\${ARCH}/$KARCH/" < $OVER_SRC > $OVER
sed -e "s/\${ARCH}/$KARCH/" < $NEW_SRC > $NEW

#########################################################################
# Make copies of files to be overwritten in "file".presnc
#########################################################################

for FILE in `cat $OVER`
do
	if [ -f $ROOT_DIR/$FILE ] && [ ! -f $ROOT_DIR/$FILE.presnc ]
	then
		cp -p $ROOT_DIR/$DIR/$FILE $ROOT_DIR/$DIR/$FILE.presnc
	fi
done
if [ ! -d $DEINSTALL_DIR ]
then
	mkdir -p $DEINSTALL_DIR
fi
cp -p snc.overwritten $DEINSTALL_DIR
cp -p snc.newfiles  $DEINSTALL_DIR
cp -p deinstall_snc $DEINSTALL_DIR

#########################################################################
# Install binaries in the / and /usr file systems
#########################################################################
install_usr $ROOT_DIR/

#########################################################################
# Modify needed systems files to add device
#########################################################################

EDITED_OK=0
# need this for editing config file
NNCS=$MAX_NCS
export NNCS
echo
echo "Several system files require modification.  This can be done"
echo "automatically now, or you may manually perform the edits later."
echo
if yes "Would you like the edits to be performed automatically now?"
then
	WANT_EDIT=1
	echo
	if ${INSTALL_DIR}/edit.sh
	then
		echo
		echo "$PRODUCT edits were successful and complete."
		EDITED_OK=1
	else
		echo
		echo "One or more $PRODUCT edits were not performed"
		echo "due to unexpected differences between your system"
		echo "files and the standard ones.  You will need to go"
		echo "through the Installation Manual and check the files"
		echo "which need editing."
		exit 0
	fi
else
	WANT_EDIT=0
	echo
	echo "You now need to manually edit several Unix kernel source and"
	echo "configuration files, and then build a new kernel."
	echo "Please refer to the Installation Manual."
	exit 0
fi

#########################################################################
# Create devices
#########################################################################

cd $ROOT_DIR/dev
rm -f ne*	# remove old devices in case dev numbers have changed
NC=0
echo
while [ $NC -lt $NNCS ] ; do
	./MAKEDEV.ne ne$NC
	if [ -c ne$NC ] ; then
		echo Created special file /dev/ne$NC
	else
		echo "Failed to create special file /dev/ne$NC"
		echo "You will need to create it manually with /dev/MAKEDEV.ne."
	fi
	NC=`expr $NC + 1`
done
cd $INSTALL_DIR
#

#########################################################################
# Update hostnames if desired.
#########################################################################

set +f
if [ $EDITED_OK = 1 ] ; then
 if yes "Would you like to update the $ROOT_DIR/etc/hostname.* files now?"
  then
    more << EOF

You will now be asked for a host name to be associated with each network
interface on the server.

If you wish to `UL deactivate` a network interface, enter "NONE" for
its host name.  For example, if you intend to use the "ne0" interface
as a replacement for an old "ie0" interface, enter "NONE" as the host
name for the ie0 interface and enter the real host name for the ne0
interface.  (The word "NONE" should be entered in all upper-case and
without the quote marks).

EOF
    if [ "$ROOT_DIR/" ] ; then
      if [ -f $ROOT_DIR/etc/hostname.ie0 -a -r $ROOT_DIR/etc/hostname.ie0 ]
      then
        BASEHOST=`cat $ROOT_DIR/etc/hostname.ie0`
      elif [ -f $ROOT_DIR/etc/hostname.le0 -a -r $ROOT_DIR/etc/hostname.le0 ]
      then
        BASEHOST=`cat $ROOT_DIR/etc/hostname.le0`
      elif [ -f $ROOT_DIR/etc/hostname.ne0 -a -r $ROOT_DIR/etc/hostname.ne0 ]
      then
        BASEHOST=`cat $ROOT_DIR/etc/hostname.ne0`
      else
        BASEHOST=noname
      fi
    else
      BASEHOST=`hostname`
    fi
    NC=0
    while [ $NC -lt $NNCS ] ; do
      if [ ! -f $ROOT_DIR/etc/hostname.ne$NC ]
      then
	echo > $ROOT_DIR/etc/hostname.ne$NC
      fi
      NC=`expr $NC + 1`    
    done
    for hn in $ROOT_DIR/etc/hostname.??[0-9] ; do
      IF=`echo $hn | sed 's/^.*\.\(.*\)$/\1/'`
      CURHOST=`cat $hn`
      if [ "$CURHOST" ] ; then
	if yes "The $IF interface currently has a host name of $CURHOST.
Do you want to change (or deactivate) it?" ; then
          DEFHOST=$CURHOST
        else
          continue
        fi
      else
        DEFHOST=${BASEHOST}_$IF
      fi
      Response "Please enter a new host name (or \"NONE\") for
the $IF interface: [$DEFHOST] "
      if [ "$ANS" = "NONE" ] ; then
	rm -f $hn
	echo The $IF interface is now deactivated.
      elif [ ! "$ANS" ] ; then
        echo $DEFHOST > $hn
        echo Using the default host name \"$DEFHOST\".
      else
	echo $ANS > $hn
      fi
    done
    cat > /tmp/hnmsg.$$ << EOF

Your $ROOT_DIR/etc/hostname.* files are up to date.

Don't forget to update the "hosts" and "ethers" NIS maps
(or the $ROOT_DIR/etc/hosts and $ROOT_DIR/etc/ethers
files) if necessary.  Write down the Ethernet interfaces
and their corresponding host names now for future reference:

Interface	Host Name
 
EOF
    for hn in $ROOT_DIR/etc/hostname.??[0-9] ; do
      IF=`echo $hn | sed 's/^.*\.\(.*\)$/\1/'`
      HN=`cat $hn`
      [ "$HN" ] || HN='(deactivated)'
      echo "$IF		$HN" >> /tmp/hnmsg.$$
    done
    more < /tmp/hnmsg.$$
  else
    if [ $WANT_EDIT = 1 ]
    then
	echo "OK, but don't forget to do it later!"
    fi
  fi
fi
unset hn CURHOST IF NC HN

#
# Comment out the revarp line in /etc/rc.boot if the onboard ethernet
# port is not in use.  This allows the machine to boot all the way even
# if the onboard ethernet port is not used.
#
if [ ! -f $ROOT_DIR/etc/hostname.ie0 -a ! -f $ROOT_DIR/etc/hostname.le0 ]
then
	ed /etc/rc.boot << EOF > /dev/null 2>&1
/ifconfig -ad auto-revarp up/
s/^/#/
i 
# SNC - Comment out this line so that the server will still boot even 
#       if the onboard Ethernet port is not connected to a network.
.
w
q
EOF
fi

set -f

#########################################################################
# Make new kernel
#########################################################################

echo
if yes 'Would you like to make a new kernel for your system now?'
then
	${INSTALL_DIR}/make_kernel
	if [ "$?" -ne 0 ]
	then
		echo "Error in  making kernel."
		exit 3
	fi
fi

echo
echo "Installation of $PRODUCT files from cd-rom finished."
echo
echo "Now please continue with the installation instructions in the"
echo "Sun Network CoProcessor Installation Manual."
echo 

exit 0
