:
#	@(#) 21.1 89/09/26  setterm
#
#	Copyright (C) The Santa Cruz Operation, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated 
#	as Confidential.
#
#	setterm
#
#	set up terminal type based on display environment 
#	      

# set the standard exit values
: ${OK=0} ${FAIL=1} ${STOP=10}

# set the environment variables
PATH=/etc:/bin:/usr/bin:/mountram/etc:/mountram/bin:/mountram/usr/bin
export PATH

tmp=/tmp/$$

# intlecho()
# usage : intlecho key message_file
# Note  :  
#	  Absolute or relative pathname of the message file.
#         All the lines with the key will be displayed.

intlecho() {

echo "s!^$1\(.*\)\$!\1!p" > ${tmp}echo
sed -n -f ${tmp}echo $2
rm -f ${tmp}echo

}

# local_menu()
# Usage : local_menu key directory filename
# Dependencies : intlecho()
# Note :
#	The directory is the relative or absolute
#	pathname where the localised 
#	directories are kept.
#	The filename is the name of the message file
#	under the localised directories.
#	It will set up the global TRANSFILE on return
#	with the user chosen message file.

local_menu() {

	TRANSDIR=$2

	# if LANG variable defined use it.
	# LANG can be of "per language" as in LANG=english
	# The other fields will be taken from the defaults in
	# /etc/default/lang.
	# see - Intl OS System Guide, Administering International
	# Environment.

	[ "$LANG" ] && {
		# has LANG got a underscore
		echo $LANG | grep _ >/dev/null 2>&1 && {
			# yes
			lang=`echo $LANG | sed -n "s/^\(.*\)\..*$/\1/p"`
		} || {
			# no, fill in the field from /etc/default/lang
			lang=`sed -n "s/^LANG=\(${LANG}.*\)\..*$/\1/p" /etc/default/lang`
		}
		
		[ -f $TRANSDIR/$lang/$3 ] && {
			TRANSFILE=$TRANSDIR/$lang/$3
			return 0
		}
	}
	
	[ -f /etc/default/lang ] && {
		lang=`sed -n "s/^LANG=\(.*\)\..*$/\1/p" /etc/default/lang`
		for i in $lang
		do
			[ -f $TRANSDIR/$i/$3 ] && {
				TRANSFILE=$TRANSDIR/$i/$3
				return 0
			}
		done
	}

	count=0

	_curdir=`pwd`
	cd $TRANSDIR
	for i in *
	do
		[ -f $i/$3 ] || continue
		heading=`intlecho "$1" "$i/$3"`
		[ "$heading" ] || continue
		count=`expr $count + 1`
		eval language$count=\$heading
		eval option$count=$i
	done
	cd $_curdir

	# if count is equal to 0, then the message
	# files do not contain the key
	# or message file is missing
	[ $count -eq 0 ] && {
		# this message needs to be hard-coded because
		# message file is not there
		echo "\nError: Cannot find the right message files\n"
		return 1
	}

	num_lang=$count

	[ "$num_lang" -eq "1" ] && {
		TRANSFILE=$TRANSDIR/$option1/$3
		return 0
	}

	count=0

	while [ "$count" -lt "$num_lang" ]
	do
		count=`expr $count + 1`
		eval echo "$count.  \$language$count" >> /tmp/$$
	done

	while true
	do
		cat /tmp/$$
		echo -n "? "
		read answer

		case $answer in
		[1-9]|[1-9][0-9])
				  [ "$answer" -le "$num_lang" ] && {
					eval TRANSFILE=\$TRANSDIR/\$option$answer/\$3
					return 0 
				  }
					;;
		esac

	done

}


# check to see if termtype was already successfully determined
if [ -s /tmp/termtype ] 	# file is nonzero
then
	. /tmp/termtype
	[ "$TERM" ] && {	# TERM is set just fine
		exit 0
	}
fi

cd /
umask 0

local_menu "MENU=" "/inst4" "set.msg"
. $TRANSFILE

# set a trap so if they user dels out, a STOP exit value is passed
# to the calling program
trap 'exit $STOP' 1 2 3 15


#echo "$SETTERM1"

# now check to see if the ttytype is in termcap
#while	echo "$SETTERM2"
#do	read terminal
				
#	if grep -y $terminal /etc/termcap > /dev/null 2>&1 
#	then	break
#	else	eval echo "$SETTERM3"
#	fi
#done

# we found the terminal type, set TERM
#echo "TERM=$terminal\nexport TERM\nstty erase '^h' intr '^?' echoe" > /tmp/termtype 2> /dev/null
echo "TERM=ansi\nexport TERM\nstty erase '^h' intr '^?' echoe" > /tmp/termtype 2> /dev/null

exit $OK
