:
# Name:		install
# Usage:	/bin/sh ./install
# Purpose:	Installation script for TERM v6.2.5
#
# 23 Oct 91	Modify U3 install for general 6.2.2 install
# 16 Oct 91	Working U3 install
SCRIPT="TERM Communications Software Install"
VERSION="6.2"
LASTMOD="7 Jul 94"
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
#
# 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 and display a header
#
header()
{
	if test "$hasclear"="Y"
	then
		clear
	else
		cat << END_CLEAR
























END_CLEAR
	fi
	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
#
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 <Del>.

      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
#
header
echo "      Extracting TERM executable files . . .  Please wait"
echo
EXETAR="`expr term??.tar : '^term\(..\)[.]tar$'`"
tar xvf term$EXETAR.tar > /dev/null 2>&1
rm term$EXETAR.tar
echo "      Extracting TERM command and system files . . .  Please wait"
echo
for i in *.tar
do
	tar xvf $i > /dev/null 2>&1
	rm $i
done
chown root *
chgrp bin *
header
unmodem=/etc/unmodem

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
	rm brand
# 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
	chown root /usr/bin/callout /usr/bin/callin
	chmod 4755 /usr/bin/callout /usr/bin/callin
	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	
	case "$EXETAR" in
	s4) # HDB lockfiles
	sed -e '/^portlist/c\
portlist        "/dev/term"' -e '/^lockfile/c\
lockfile        "/usr/spool/uucp/LCK..%b"'  termsys.tmpl > .termsys;;
	*) # BSD lockfiles
	sed -e '/^portlist/c\
portlist        "/dev/term"' -e '/^lockfile/c\
lockfile        "/usr/spool/uucp/LCK..%s"'  termsys.tmpl > .termsys;;
	esac
	mv .termrc .termsys service.sys install $DIR_TERM
fi
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

header
NOTREL4=true
if test "$EXETAR" = "i3"
then
	query "Are you installing TERM under UNIX System V Release 4 ?" "NO"
	canyes $xquery
	if test "$xcanyes" = "NO"
	then
		NOTREL4=true
	else
		NOTREL4=false
	fi
fi
header
if $NOTREL4
then
	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 $unmodem
			then
				$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
else
	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
fi
echo
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
