#!/bin/sh

# Name:		install
# Usage:	/bin/sh install
# Purpose:	Installation script for TERM v6.2.5
#
SCRIPT="TERM Communications Software Install"
VERSION="6.2"
LASTMOD="February 1999"
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

#
# Determine which executable to install.  If Solaris x86, we can
# determine this automagically and spare the user one prompt.
#
System=`uname -s`
Release=`uname -r`
Machine=`uname -m`
if [ $System = SunOS ]
then
    if [ $Machine = "i86pc" ]
    then
	EXETAR="sol"
	EXEDESC="Solaris x86 (SunOS Release $Release)"
	BIN_FILES="$BRANDIT"
    else
	echo
	echo
	echo "	This TERM distribution is for Solaris x86, and will not"
	echo "	execute correctly on Solaris $Machine."
	echo
	exit
    fi
else
    SELEXEC=N
    while test "$SELEXEC" = "N"
    do
	SELEXEC=Y
	header
	cat << EOF_PAGE
	Please select which system you are installing TERM on:

	1.  UNIX System V/386 (Including UnixWare, Altos, AT&T and Interactive)
	2.  SCO UNIX 386
	3.  SCO XENIX 386
	4.  Linux x86

EOF_PAGE
	echon "      SELECTION:  "
	read x
	case $x in
	  1)	EXETAR=i3;
		EXEDESC='UNIX System V/386'
		;;
	  2)	EXETAR=u3;
		EXEDESC='SCO UNIX 386'
		;;
	  3)	EXETAR=x3;
		EXEDESC='SCO XENIX 386'
		;;
	  4)	EXETAR=lx;
		EXEDESC='Linux x86'
		;;
	  *)	echo;
		echo;
		echo;
		echon "      Invalid selection. Press <Enter> to continue."
		read x;
		SELEXEC=N
		;;
	esac
    done
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
header
echo "      The following options have been selected:"
echo
echo "      Installing for    :		$EXEDESC"
echo "      TERM directory    :		$DIR_TERM"
echo
query "     Is this correct?" "YES"
canyes $xquery
if test "$xcanyes" = "NO"
then
	header
	echo "      You must re-run install to change your selections."
	echo
	exit 1
fi
#
# Do the install
#
header
echo "      Extracting TERM executable files . . .  Please wait"
echo
tar xvf term$EXETAR.tar > /dev/null 2>&1
rm term*.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 *

#
# Find out if we're installing on SVR4.  Answer is always yes for
# Solaris x86, maybe yes for UnixWare (i3).
#
header
NOTREL4=true
if test "$EXETAR" = "i3"
then
	query "Are you installing TERM under UNIX System V Release 4 (e.g. UnixWare)?" "NO"
	canyes $xquery
	if test "$xcanyes" = "NO"
	then
		NOTREL4=true
	else
		NOTREL4=false
	fi
else
	if [ "$EXETAR" = "sol" ]
	then
		NOTREL4=false
	fi
fi

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
	if test -f /usr/bin/callout
	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

	if $NOTREL4
	then
		if test "$System" = "Linux"; then 
		   sed -e '/^portlist/c\
portlist        "NUL:"' -e '/^lockfile/c\
lockfile        "/var/lock/LCK..%s"' termsys.tmpl > .termsys
		else
		sed -e '/^portlist/c\
portlist        "NUL:"' -e '/^lockfile/c\
lockfile        "/usr/spool/uucp/LCK..%s"' termsys.tmpl > .termsys
		fi
	else
		sed -e '/^portlist/c\
portlist        "NUL:"' -e '/^lockfile/c\
lockfile        "/var/spool/locks/LK.%m"' termsys.tmpl > .termsys
	fi
	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
if $NOTREL4
then
	echo
	echon "      Enter tty port name to use for TERM (e.g. tty5, tty01, ttyh1):"
	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

