:
# Name:		install
# Usage:	/bin/sh ./install
# Purpose:	Installation script for TERM v6.2.5
#
# 21 Apr 95	Modify S4 install to support SunOS and Solaris 2.4 (wes)
# 23 Oct 91	Modify U3 install for general 6.2.2 install
# 16 Oct 91	Working U3 install
#
SCRIPT=" TERM Communications Software Install for SunOS "
SCRIPT="TERM Communications Software Install for Solaris"
VERSION="6.2"
LASTMOD="23 Oct 91"
BRANDIT="term"
BIN_FILES="$BRANDIT callin callout"
CMD_FILES="*.con *.cmd *.txt *pc remlpr remcp term.h* termcap.* autopoll autopoll.sh bbstart tprint README.DOC"
DIR_TERM=/usr/term

#
# If this is Solaris, customize the installation appropriately.
#
OSmajor=`uname -r | cut -d. -f1`
if [ $OSmajor = 5 ]
then
	LASTMOD="28 Apr 95"
	BIN_FILES="$BRANDIT"
	cd Solaris
else
	cd SunOS
fi

#
# Check to see if echo uses -n or \c
#
x=`echo -n`
if test "$x" = "-n"
then
	ES=\\c
else
	EP=-n
fi

#
# Check to see if clear executable is available
#
x=`clear`
if test "x$x" = "x"
then
	hasclear=N
else
	hasclear=Y
fi

#
# Clear the screen
#
cls()
{
	if test "$hasclear"="Y"
	then
		clear
	else
		cat << END_CLEAR
























END_CLEAR
	fi
}

#
# Display a header
#
header()
{
	cls
	echo "[  v$VERSION       $SCRIPT       $LASTMOD ]"
	echo
	echo
}

#
# Convert yes and no responses to uppercase
#
canyes()
{
	case $1 in
	Y|y|yes|Yes|YES)	xcanyes=YES;;
	*)			xcanyes=NO;;
	esac
}

#
# Echo arguments without a new line
#
echon()
{
	echo $EP "$*$ES"
}

#
# Query with default. Result return in 'xquery'
#
query()
{
	xquery=$2
	echon "$1  [DEFAULT = $2]:  "
	read x
	if test "x$x" = "x"
	then
		echon " "	
	else
		xquery=$x
	fi
}

#
# Error handling
#
error()
{
	errval=$1
	shift
	echo
	echo
	echo "$SCRIPT: $*" >&2
	echo
	exit $errval
}

#
# Check for root login
#
chkroot()
{
	who am i | grep root > /dev/null 2>&1
	case $? in
	0)	xchkroot=Y;;
	*)	xchkroot=N;;
	esac
}

#
# Main script - note that <Del> is not the default interrupt key
# on all systems!
#
header
cat << EOF_PAGE
      Welcome! This program will quickly install TERM on your system. This
      install procedure consists of the following steps:

	      1.  Move the distribution files to the proper location
	      2.  Serialize your copy of TERM

      You may abort this install program at any time by pressing your
      interrupt key (typically <Del> or ^C).

      Press <Enter> to continue with the install.
EOF_PAGE
read x
chkroot
if test "$xchkroot" = "N"
then
	header
	cat << EOF_PAGE
      This installation requires that you be logged in as the "root" user or
      have root privileges.

      You are not logged in as the "root" user! If the account you are using
      has root privileges, you can continue this installation; otherwise,
      you should NOT continue with this installation and re-run it when you
      are logged in as "root".

EOF_PAGE
	query "      Continue with this installation? " "NO"
	canyes $xquery
	if test "$xcanyes" = "NO"
	then
		error 3 "User aborted install script"
	fi
fi

#
# Select TERM directory
#
SELDIR=true
while $SELDIR
do
	echo
	echo
	query "      Directory to install TERM" $DIR_TERM
	echo
	if test -d $xquery
	then
		TMP=$xquery
		query "      $TMP already exists; use anyway?" "YES"
		canyes $xquery
		if test "$xcanyes" = "YES"
		then
			DIR_TERM=$TMP
			SELDIR=false
		fi
	else
		echo "      Directory '$xquery' does not exist; creating . . ."
		mkdir $xquery > /dev/null 2>&1
		if test "$?" = "0"	
		then
			echo "      Directory '$xquery' created. Press <Enter> to continue."
			read x
			chmod 777 $xquery
			DIR_TERM=$xquery
			SELDIR=false
		else
			echo "      Directory '$xquery' cannot be created."
			error 4 "Can't create install directory."
		fi
	fi
done

#
# Do the install -- extract the contents of the tarballs.
#
header
echo "      Extracting TERM executable files . . .  Please wait"
echo
tar xf terms4.tar > /dev/null 2>&1

echo "      Extracting TERM command and system files . . .  Please wait"
echo
for i in cmdfile*.tar
do
	tar xvf $i > /dev/null 2>&1
done
chown root *
chgrp bin *
#
# Brand the executable here and now while we're certain we can still
# modify it.
#
header
if test -f term
then
	./brand $BRANDIT 
	case $? in
	1)	error 5 "Error in serialization. Aborting install. You must reload your distribution files before trying to install again";;
	esac
	#
	# If somebody did this to my nice, functional Solaris system, I'd
	# be rather upset.  *Any* security monitor in the world would have
	# a fit over this change!  This won't affect SunOS systems, since
	# enable and disable are in /usr/5bin if present at all.
	#
	# SCO ownership incorrect on some releases
	# if test -f /bin/disable
	# then
	#	chown root /bin/disable
	#	chmod 4755 /bin/disable
	# fi
	# if test -f /bin/enable
	# then
	#	chown root /bin/enable
	#	chmod 4755 /bin/enable
	# fi
	#
	header
	echo
	echo "      Moving files to /usr/bin and $DIR_TERM . . .  Please wait"
	mv $BIN_FILES /usr/bin
	if test -r ntermrdr
	then
		mv ntermrdr /usr/bin
	fi

	#
	# The 6.2.5 distribution (Solaris) uses a termsys.tmpl
	# template file, rename this to .termsys for installation.
	#
	mv termsys.tmpl .termsys

	#
	# On SunOS set the ownership and permissions on the
	# callin and callout programs appropriately.
	#
	if [ $OSmajor != 5 ]
	then
		chown root /usr/bin/callout /usr/bin/callin
		chmod 4755 /usr/bin/callout /usr/bin/callin
	fi

	mv $CMD_FILES $DIR_TERM
	if test ! -d $DIR_TERM/queue
	then
		mkdir $DIR_TERM/queue
		chmod 777 $DIR_TERM/queue
	fi
	if test -r $DIR_TERM/.termrcb
	then
		echo "      Removing old $DIR_TERM/.termrcb"
		rm $DIR_TERM/.termrcb
	fi
	if test -r $DIR_TERM/.termrc
	then
		echo "      Moving old $DIR_TERM/.termrc to termrc.bak"
		mv $DIR_TERM/.termrc $DIR_TERM/termrc.bak
	fi
	if test -r $DIR_TERM/.termsys
	then
		echo "      Moving old $DIR_TERM/.termsys to termsys.bak"
		mv $DIR_TERM/.termsys $DIR_TERM/termsys.bak
	fi
	if test -r $DIR_TERM/service.sys
	then
		echo "      Moving old $DIR_TERM/service.sys to service.bak"
		mv $DIR_TERM/service.sys $DIR_TERM/service.bak
	fi	
	mv .termrc .termsys service.sys ../install $DIR_TERM
fi

#
# This is of questionable sensibility, but it won't hurt SunOS or
# Solaris installations, so I left it in.
#
if test ! -d /usr/spool/uucp
then
	mkdir /usr/spool/uucp
fi
chmod 777 /usr/spool/uucp
if test -d /usr/spool/locks
then
	chmod 777 /usr/spool/locks
fi

#
# Reorganized to use OSmajor variable, so as to not issue confusing
# questions to the installer.  Even though Solaris clearly states that
# it is indeed System V Release 4.0 at the login banner, most users
# fail to notice this, and asking them questions about it will just
# annoy them.
#
header
if [ $OSmajor = 5 ]
then
	echo
	echo "      You must run TERM's Setup Utility to define a port. If you have trouble"
	echo "      connecting to a specified port, please read the README.DOC file"
	echo
	echo "      Press <Enter> to continue."
	read x
else
	echo
	echo "      Enter tty port name to use for TERM (e.g. tty5, tty01, ttyh1):\c"
	read p
	if test "x$p" = "x"
	then
		echo
		echo
		echo "      You have not entered a port name. You must run the Setup Utility"
		echo "      to define a port. Press <Enter> to continue."
		read x
	else
		echo
		echo "      (if install sits here for more than half a minute, then you need"
		echo "      to interrupt it (break out) and make your cable first)"
		echo
		if test -c /dev/$p
		then
			if test -r /dev/term
			then
				rm -f /dev/term
			fi
			ln /dev/$p /dev/term
			chmod 0666 /dev/term
			if test -x /etc/unmodem
			then
				/etc/unmodem /dev/$p
			fi
		else
			if test -r /dev/term
			then
				echo "      TERM installed using previous TERM port..."
				echo
				echo "      Press <Enter> to continue."
				read x
			else
				echo "      There is no such device as /dev/$p...  Run TERM's Setup Utility to define a port "
				echo
				echo "      Press <Enter> to continue."
				read x
				exit
			fi
		fi
	fi
fi

#
# Remove the installation files.  No files are removed until this point
# mostly for debugging the installation program.
#
cd ..
rm -rf SunOS Solaris install

header
echo "      TERM installation completed successfully"
query "      Do you want to run the Setup Utility? " "YES"
canyes $xquery
if test "$xcanyes" = "NO"
then
	exit 0
else
	/usr/bin/term $DIR_TERM/setup
fi
