:
#	@(#) serial.sh 22.2 90/04/18 
#
#	Copyright (C) The Santa Cruz Operation, 1985, 1989, 1990.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, Microsoft Corporation
#	and AT&T, and should be treated as Confidential.
#
#
# SERIAL	- UNIX 3.2 Serial Port Initialization
#

LANG=english_us.ascii
PATH=/etc:/bin:/usr/bin:/etc/conf/bin:/tcb/bin:

export LANG PATH

SERPERM="/usr/lib/mkdev/perms/SER"
INIT="/etc/inittab"
CF_INIT="/etc/conf/cf.d/inittab"
SIO_INIT="/etc/conf/init.d/sio"
TTY_TYPE="/etc/ttytype"
tmp="/tmp/ser$$"


# Define return values
: ${OK=0}  ${FAIL=1}  ${STOP=10}  ${HALT=11}

# FUNCTION DEFINITIONS
#########################

# Define traps for critical and non critical code.
set_trap()  {	
	trap 'echo "Interrupted! Exiting ..."; cleanup 1' 1 2 3 15
}
unset_trap()  {
	trap '' 1 2 3 15
}

cleanup()  {
	trap '' 1 2 3 15
	rm -f $tmp*
	exit $1
}

# clear the screen if it is supported
clearscr() {
	# check if the terminal environment is set up
	[ "$TERM" ] && clear 2> /dev/null
}

getyn() {
	while	echo "$* (y/n) \c" >&2
	do	read yn rest
		case $yn in
		[yY])	return $OK 				;;
		[nN])	return $FAIL				;;
		*)	echo "\nPlease answer y or n" >&2	;;
		esac
	done
}

# Prompt with mesg, return non-zero on q. Also allows setting -x and +x,
# shell escapes and the passing of default values
prompt() {
	mesg=$1
	while	echo "\n${mesg}or enter q to quit: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		-v)	# Print the values of requested variables
			read x
			for var in $x
			do
				eval echo \$$var
			done
			;;
		Q|q)	return $FAIL					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is a second argument use it
			# as the default, else loop until 'cmd' is set
			[ "$2" ] && { 
				cmd=$2
				return $OK
			}
			: continue
			;;
		*)	return $OK					;;
		esac
	done
}

# Print an error message
error() {
	echo "\nError: $*" >&2
	return $FAIL
}

# perms list needed if link kit must be installed
permschk () {
	if [ -f /etc/perms/inst ]; then
		PERM=/etc/perms/inst
	else
		error "Cannot locate /etc/perms/inst. Needed to verify
 linkkit installation"
		cleanup $FAIL
	fi
}

# test to see if link kit is installed
linkchk()  {
	cd /
	until	fixperm -i -d LINK $PERM
	do	case $? in
		4)  echo "\nThe Link Kit is not installed." >&2	;;
		5)  echo "\nThe Link Kit is only partially installed." >&2  ;;
		*)  echo "\nError testing for Link Kit.  Exiting." >&2; cleanup $FAIL  ;;
		esac

		# Not fully installed. Do so here
		getyn "\nDo you wish to install it now?" || {
			# answered no
			echo "
The link kit must be installed to run this program.  Exiting ..."
			cleanup $OK
		}

		# answered yes, so install link kit
		echo "\nInvoking /etc/custom\n"
		/etc/custom -o -i LINK || {
			# custom exited unsuccessfully
			error "custom failed to install Link Kit successfully.  Please try again."
			cleanup $FAIL
		}
	done
}

getbaud ()  {
	mspd=1200
	getyn "
Would you like the modem devices set at the default speed of 1200." || {
		while prompt "Please enter the new baud rate " 
		do 	case $cmd in
			300|1200|2400|4800|9600|19200)	mspd=$cmd; break      ;;
			[0-9]*)      	getyn "
$cmd is not a standard baud rate. Do you wish to change this. " && continue
					mspd=$cmd; break		      ;;
			*)	echo "\n$cmd is not valid. Try again." 
					continue 			      ;;
  			esac
 		done
	}
	return $OK
}

# main ()
#########################

set_trap
cd /
clearscr
echo "\n\n\nSerial Board Initialization \n"

permschk
linkchk

while	echo "You would like to install a:

	1. 1 port card
	2. 2 port card
	3. 4 port card
	4. 5 port card
	5. 8 port card

Select an option or enter 'q' to quit: \c"
	read x
do	case $x in
	1)	ports=1; break					;;
	2)	ports=2; break					;;
	3)	ports=4; break					;;
	4)	ports=5; break					;;
	5)	ports=8; break					;;
	q)	echo "\nInstallation aborted" >&2; cleanup $OK	;;
	*)	echo "\nEnter 1, 2, 3, 4, 5 or q\n" >&2		;;
	esac
done

while	echo "\nThe card is configured as:

	1. COM1
	2. COM2
	3. COM3
	4. COM4
	
Select an option or 'q' to quit: \c"
	read x
do	case $x in
	1)	com=1; break				;;
	2)	com=2; break				;;
	3)	com=3; break				;;
	4)	com=4; break				;;
	q|Q)	echo "\nInstallation aborted" >&2; cleanup $OK 	;;
	*)	echo "\nEnter 1, 2, 3, 4, or q\n" >&2	;;
	esac
done
	
# Open init.d/sio file if it doesn't exist. This contains information to 
# rebuild inittab.
[ -f $SIO_INIT ] || {
	umask 133
	> $SIO_INIT
	chgrp sys /$SIO_INIT
	umask 022
}

getbaud

fixperm -c -d SER$com $SERPERM 2> /dev/null || {
	echo "\nError: cannot create devices" >&2
	cleanup $FAIL 
}

# Fixperm makes 8 ports at a time, so remove unneeded ones
case $ports in
1)	rm -f /dev/tty${com}[b-hB-H]	;;
2)	rm -f /dev/tty${com}[c-hC-H]	;;
4)	rm -f /dev/tty${com}[e-hE-H]	;;
5)	rm -f /dev/tty${com}[f-hF-H]	;;
8)					;;
esac

cp $SIO_INIT $tmp.sio
cp $TTY_TYPE $tmp.type

echo "\nModifying system files ...\c"
cd /dev
for x in tty${com}?
do	
	case $x in
	*[A-Z])	spd=$mspd	type=dialup	modn="$modn $x"		
		proc="/usr/lib/uucp/uugetty -t60 $x $spd"		;;
	*[a-z])	spd=m
		[ $mspd = 19200 ] && spd=n 
		type=unknown	stdn="$stdn $x"		
		proc="/etc/getty $x $spd"				;;
	*[0-4]) continue						;;
	*)	echo "\nNon-standard device name in /dev" >&2; continue	;;
	esac
	
	# Remove any previous entry for tty and add entry to init.d directory 
	grep -v $x $tmp.sio  2> /dev/null 1> $tmp.sio1
	mv $tmp.sio1 $tmp.sio
	id=Se`expr "$x" : "tty\(..\)"`
	echo "$id:23:off:$proc" >> $tmp.sio

	# Add entry to ttytype
	grep  $x $tmp.type 2> /dev/null 1> /dev/null || {
		echo "$type\t$x" >> $tmp.type
	}
done
cd /

unset_trap
mv $tmp.sio $SIO_INIT
set_trap

# Create new inittab
idmkinit || {
	echo "\nError: Idmkinit failed." >&2
	cleanup $FAIL
}
chmod 660 $CF_INIT

unset_trap
mv $CF_INIT $INIT
mv $tmp.type $TTY_TYPE
set_trap

if ttys_update 
then
	echo "\nSystem files successfully updated."
else
	echo "\nError: ttys_update failed. 
You must update the security database /etc/auth/system/ttys 
with the following devices." >&2 
fi

telinit q
echo "
The following standard serial device(s) have been installed:
	$stdn

The following device(s) access the same physical port as
their lower-case counterpart, but have modem control properties:
	$modn
"
cleanup $OK
